pool: align nodes to pointer boundaries

This commit is contained in:
Danny Robson 2018-07-04 14:02:29 +10:00
parent d466ed75ac
commit 0f5f660fa3

View File

@ -19,6 +19,7 @@
#include "nocopy.hpp" #include "nocopy.hpp"
#include "debug.hpp" #include "debug.hpp"
#include "cast.hpp"
#include <atomic> #include <atomic>
#include <cstdlib> #include <cstdlib>
@ -34,7 +35,7 @@ namespace util {
protected: protected:
union node; union node;
union alignas (T) node { union alignas(node*) node {
std::atomic<node*> next; std::atomic<node*> next;
char data[sizeof (T)]; char data[sizeof (T)];
}; };
@ -97,7 +98,7 @@ namespace util {
if (m_next.compare_exchange_weak (curr, soon)) { if (m_next.compare_exchange_weak (curr, soon)) {
++m_size; ++m_size;
return std::launder (reinterpret_cast<T*> (curr)); return std::launder (util::cast::alignment<T*> (curr));
} }
} while (1); } while (1);
} }
@ -105,7 +106,7 @@ namespace util {
void void
deallocate (T *base) deallocate (T *base)
{ {
auto soon = reinterpret_cast<node*> (base); auto soon = util::cast::alignment<node*> (base);
do { do {
node *curr = m_next; node *curr = m_next;