Commit a8f11418 authored by Marc Celani's avatar Marc Celani Committed by Facebook Github Bot

Make hazptr_obj copyable and moveable

Summary: hazptr_obj is copyable and moveable, but doing so would cause a bug, because the next ptr would point to the object that was copied/moved from, which can oftentimes be garbage or another object that has not yet retired. This diff changes the copy/move constructors and assignment operators to simply check that the rhs has not been retired.

Reviewed By: magedm

Differential Revision: D7539444

fbshipit-source-id: f9325c7b886fcf4b553344ee37f2355ca41268b6
parent cb28abd6
......@@ -127,9 +127,20 @@ class hazptr_obj {
hazptr_obj* next_;
public:
hazptr_obj() {
// Only for catching misuse bugs like double retire
next_ = this;
// All constructors set next_ to this in order to catch misuse bugs like
// double retire.
hazptr_obj() noexcept : next_(this) {}
hazptr_obj(const hazptr_obj&) noexcept : next_(this) {}
hazptr_obj(hazptr_obj&&) noexcept : next_(this) {}
hazptr_obj& operator=(const hazptr_obj&) {
return *this;
}
hazptr_obj& operator=(hazptr_obj&&) {
return *this;
}
private:
......
......@@ -733,3 +733,31 @@ TEST_F(HazptrTest, ForkTest) {
_exit(0); // Do not print gtest results
}
}
TEST_F(HazptrTest, CopyAndMoveTest) {
struct Obj : hazptr_obj_base<Obj> {
int a;
};
auto p1 = new Obj();
auto p2 = new Obj(*p1);
p1->retire();
p2->retire();
p1 = new Obj();
p2 = new Obj(std::move(*p1));
p1->retire();
p2->retire();
p1 = new Obj();
p2 = new Obj();
*p2 = *p1;
p1->retire();
p2->retire();
p1 = new Obj();
p2 = new Obj();
*p2 = std::move(*p1);
p1->retire();
p2->retire();
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment