Skip to content

Commit

Permalink
[FIX] Fix self-assignments.
Browse files Browse the repository at this point in the history
  • Loading branch information
paintdream committed May 14, 2024
1 parent b4ebd5f commit 89684c0
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 33 deletions.
5 changes: 4 additions & 1 deletion src/iris_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ namespace iris {
}

iris_buffer_t& operator = (const iris_buffer_t& rhs) noexcept(noexcept(std::declval<iris_buffer_t>().copy(rhs))) {
copy(rhs);
if (this != &rhs) {
copy(rhs);
}

return *this;
}

Expand Down
25 changes: 15 additions & 10 deletions src/iris_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -1217,12 +1217,15 @@ namespace iris {

iris_queue_t& operator = (const iris_queue_t& rhs) = delete;
iris_queue_t& operator = (iris_queue_t&& rhs) noexcept {
static_cast<node_allocator_t&>(*this) = std::move(static_cast<node_allocator_t&>(rhs));
ring_buffer = rhs.ring_buffer;
push_count = rhs.push_count;
pop_count = rhs.pop_count;
if (this != &rhs) {
static_cast<node_allocator_t&>(*this) = std::move(static_cast<node_allocator_t&>(rhs));
ring_buffer = rhs.ring_buffer;
push_count = rhs.push_count;
pop_count = rhs.pop_count;

rhs.ring_buffer = nullptr;
}

rhs.ring_buffer = nullptr;
return *this;
}

Expand Down Expand Up @@ -1798,11 +1801,13 @@ namespace iris {
}

iris_queue_list_t& operator = (iris_queue_list_t&& rhs) noexcept {
IRIS_ASSERT(static_cast<node_allocator_t&>(*this) == static_cast<node_allocator_t&>(rhs));
// just swap pointers.
std::swap(push_head, rhs.push_head);
std::swap(pop_head, rhs.pop_head);
std::swap(iterator_counter, rhs.iterator_counter);
if (this != &rhs) {
IRIS_ASSERT(static_cast<node_allocator_t&>(*this) == static_cast<node_allocator_t&>(rhs));
// just swap pointers.
std::swap(push_head, rhs.push_head);
std::swap(pop_head, rhs.pop_head);
std::swap(iterator_counter, rhs.iterator_counter);
}

return *this;
}
Expand Down
28 changes: 18 additions & 10 deletions src/iris_coroutine.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ namespace iris {

iris_coroutine_t& operator = (const iris_coroutine_t& rhs) = delete;
iris_coroutine_t& operator = (iris_coroutine_t&& rhs) noexcept {
std::swap(handle, rhs.handle);
rhs.handle = std::coroutine_handle<promise_type>();
if (this != &rhs) {
std::swap(handle, rhs.handle);
rhs.handle = std::coroutine_handle<promise_type>();
}

return *this;
}

Expand Down Expand Up @@ -340,9 +343,12 @@ namespace iris {
// atomic variables are not movable.
iris_awaitable_multiple_t(iris_awaitable_multiple_t&& rhs) noexcept : caller(rhs.caller), async_worker(rhs.async_worker), awaitables(std::move(rhs.awaitables)), returns(rhs.returns) {}

iris_awaitable_multiple_t& operator = (iris_awaitable_multiple_t&& rhs) noexcept {
iris_awaitable_multiple_t t(std::move(rhs));
std::swap(*this, t);
iris_awaitable_multiple_t& operator = (iris_awaitable_multiple_t&& rhs) noexcept {\
if (this != &rhs) {
iris_awaitable_multiple_t t(std::move(rhs));
std::swap(*this, t);
}

return *this;
}

Expand Down Expand Up @@ -594,7 +600,7 @@ namespace iris {
// try fast preempt
for (iterator_t p = begin; p != end; ++p) {
warp_t* target = &(*p);
typename warp_t::template preempt_guard_t guard(*target, 0);
typename warp_t::preempt_guard_t guard(*target, 0);
if (guard) {
selected = target;
handle.resume();
Expand Down Expand Up @@ -1109,11 +1115,13 @@ namespace iris {

resource_t& operator = (const resource_t&) = delete;
resource_t& operator = (resource_t&& rhs) noexcept {
clear();
if (this != &rhs) {
clear();

host = rhs.host;
amount = rhs.amount;
rhs.host = nullptr;
host = rhs.host;
amount = rhs.amount;
rhs.host = nullptr;
}

return *this;
}
Expand Down
18 changes: 12 additions & 6 deletions src/iris_dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ namespace iris {
}

storage_t& operator = (storage_t&& rhs) noexcept {
queue_buffer = std::move(rhs.queue_buffer);
if (this != &rhs) {
queue_buffer = std::move(rhs.queue_buffer);
}

return *this;
}

Expand Down Expand Up @@ -876,7 +879,7 @@ namespace iris {
}
}

size_t get_pending_count() const {
size_t get_pending_count() const noexcept {
return pending_count.load(std::memory_order_acquire);
}

Expand Down Expand Up @@ -947,7 +950,7 @@ namespace iris {
complete(true);
}

void validate(routine_t* from, routine_t* to) {
void validate(routine_t* from, routine_t* to) const noexcept {
IRIS_ASSERT(from != to);

for (size_t i = 0; i < sizeof(to->next_tasks) / sizeof(to->next_tasks[0]); i++) {
Expand Down Expand Up @@ -981,9 +984,12 @@ namespace iris {
}

task_t& operator = (task_t&& rhs) noexcept {
task = std::move(rhs.task);
next = rhs.next;
rhs.next = nullptr;
if (this != &rhs) {
task = std::move(rhs.task);
next = rhs.next;
rhs.next = nullptr;
}

return *this;
}

Expand Down
35 changes: 29 additions & 6 deletions src/iris_lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@ namespace iris {
}

iris_lua_t& operator = (iris_lua_t&& rhs) noexcept {
state = rhs.state;
rhs.state = nullptr;
if (this != &rhs) {
state = rhs.state;
rhs.state = nullptr;
}

return *this;
}

Expand All @@ -99,7 +102,7 @@ namespace iris {
}

template <typename... args_t>
void log_error(const char* format, args_t&&... args) {
void log_error(const char* format, args_t&&... args) const {
iris_lua_t::log_error(state, format, std::forward<args_t>(args)...);
}

Expand Down Expand Up @@ -127,7 +130,14 @@ namespace iris {
ref_t(ref_t&& rhs) noexcept : value(rhs.value) { rhs.value = LUA_REFNIL; }
ref_t(const ref_t& rhs) = delete;
ref_t& operator = (const ref_t& rhs) = delete;
ref_t& operator = (ref_t&& rhs) noexcept { IRIS_ASSERT(value == LUA_REFNIL); std::swap(rhs.value, value); return *this; }
ref_t& operator = (ref_t&& rhs) noexcept {
if (this != &rhs) {
IRIS_ASSERT(value == LUA_REFNIL);
std::swap(rhs.value, value);
}

return *this;
}

using internal_type_t = void;

Expand Down Expand Up @@ -252,7 +262,13 @@ namespace iris {
reftype_t(reftype_t&& rhs) noexcept : ref_t(std::move(static_cast<ref_t&>(rhs))) {}
reftype_t(const reftype_t& rhs) = delete;
reftype_t& operator = (const reftype_t& rhs) = delete;
reftype_t& operator = (reftype_t&& rhs) noexcept { ref_t::operator = (std::move(static_cast<ref_t&>(rhs))); }
reftype_t& operator = (reftype_t&& rhs) noexcept {
if (this != &rhs) {
ref_t::operator = (std::move(static_cast<ref_t&>(rhs)));
}

return *this;
}

const void* get_type_hash() noexcept {
return reinterpret_cast<const void*>(get_hash<type_t>());
Expand Down Expand Up @@ -291,7 +307,14 @@ namespace iris {
refptr_t(refptr_t&& rhs) noexcept : ref_t(std::move(static_cast<ref_t&>(rhs))), ptr(rhs.ptr) {}
refptr_t(const refptr_t& rhs) = delete;
refptr_t& operator = (const refptr_t& rhs) = delete;
refptr_t& operator = (refptr_t&& rhs) noexcept { ref_t::operator = (std::move(static_cast<ref_t&>(rhs))); std::swap(ptr, rhs.ptr); return *this; }
refptr_t& operator = (refptr_t&& rhs) noexcept {
if (this != &rhs) {
ref_t::operator = (std::move(static_cast<ref_t&>(rhs)));
std::swap(ptr, rhs.ptr);
}

return *this;
}

using internal_type_t = type_t*;

Expand Down

0 comments on commit 89684c0

Please sign in to comment.