Skip to content

Commit

Permalink
Use std::destroy_at instead of manually calling the destructor
Browse files Browse the repository at this point in the history
Ideally the construction should also use std::construct_at instead of a
placement new but this would require C++20
  • Loading branch information
sjoubert committed Dec 13, 2024
1 parent 9903bf0 commit 9a55a74
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
4 changes: 2 additions & 2 deletions include/pistache/async.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ namespace Pistache::Async

if (allocated)
{
reinterpret_cast<T*>(mem)->~T();
std::destroy_at(reinterpret_cast<T*>(mem));
allocated = false;
}

Expand All @@ -274,7 +274,7 @@ namespace Pistache::Async
{
if (allocated)
{
reinterpret_cast<T*>(&storage)->~T();
std::destroy_at(reinterpret_cast<T*>(storage));
allocated = false;
}
}
Expand Down
9 changes: 4 additions & 5 deletions include/pistache/mailbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,7 @@ namespace Pistache
T& data() { return *reinterpret_cast<T*>(&storage); }

private:
typedef typename std::aligned_storage<sizeof(T), alignof(T)>::type Storage;
Storage storage;
alignas(T) std::byte storage[sizeof(T)];
std::atomic<Entry*> next;
};

Expand All @@ -233,7 +232,7 @@ namespace Pistache
while (!empty())
{
Entry* e = pop();
e->data().~T();
std::destroy_at(&e->data());
delete e;
}
delete tail;
Expand All @@ -258,7 +257,7 @@ namespace Pistache
// Since it's Single-Consumer, the store does not need to be atomic
tail = next;
new (&res->storage) T(std::move(next->data()));
next->data().~T();
std::destroy_at(&next->data());
return res;
}
return nullptr;
Expand All @@ -275,7 +274,7 @@ namespace Pistache
if (entry)
{
object.reset(new T(std::move(entry->data())));
entry->data().~T();
std::destroy_at(&entry->data());
}

return object;
Expand Down

0 comments on commit 9a55a74

Please sign in to comment.