pool: remove typed base queries, tighten alignment tests
This commit is contained in:
parent
bdbcb0788d
commit
d4ba9fa061
37
pool.hpp
37
pool.hpp
@ -27,9 +27,9 @@ namespace cruft {
|
|||||||
protected:
|
protected:
|
||||||
union node;
|
union node;
|
||||||
|
|
||||||
union alignas(node*) node {
|
union node {
|
||||||
std::atomic<node*> next;
|
alignas(node*) std::atomic<node*> next;
|
||||||
std::byte data[sizeof(T)];
|
alignas(T) char data[sizeof(T)];
|
||||||
};
|
};
|
||||||
|
|
||||||
static_assert (std::atomic<node*>::is_always_lock_free);
|
static_assert (std::atomic<node*>::is_always_lock_free);
|
||||||
@ -148,7 +148,7 @@ namespace cruft {
|
|||||||
|
|
||||||
void destroy (size_t idx)
|
void destroy (size_t idx)
|
||||||
{
|
{
|
||||||
return destroy (base () + idx);
|
return destroy (&(*this)[idx]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -173,21 +173,32 @@ namespace cruft {
|
|||||||
// Indexing
|
// Indexing
|
||||||
size_t index (T const *ptr) const
|
size_t index (T const *ptr) const
|
||||||
{
|
{
|
||||||
CHECK_LIMIT (ptr, base (), base () + m_capacity);
|
CHECK_LIMIT (cruft::cast::alignment<node const*> (ptr), m_head, m_head + m_capacity);
|
||||||
return ptr - base ();
|
return cruft::cast::alignment<node const*> (ptr) - m_head;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// returns the base address of the allocation.
|
/// returns the base address of the allocation.
|
||||||
///
|
///
|
||||||
// guaranteed to point to the first _possible_ allocated value;
|
/// guaranteed to point to the first _possible_ allocated value;
|
||||||
// however it may not be _live_ at any given moment. provided to
|
/// however it may not be _live_ at any given moment.
|
||||||
// facilitate indexing.
|
///
|
||||||
T* base (void) { return reinterpret_cast<T*> (m_head); }
|
/// DO NOT use this pointer for indexing as you will be unable to
|
||||||
T const* base (void) const { return reinterpret_cast<T const*> (m_head); }
|
/// account for internal node sizes, alignment, or padding.
|
||||||
|
void * base (void) & { return m_head; }
|
||||||
|
void const* base (void) const& { return m_head; }
|
||||||
|
|
||||||
T& operator[] (size_t idx) &;
|
T& operator[] (size_t idx) &
|
||||||
|
{
|
||||||
|
CHECK_LIMIT (idx, 0u, capacity ());
|
||||||
|
return *cruft::cast::alignment<T*> (&m_head[idx].data[0]);
|
||||||
|
}
|
||||||
|
|
||||||
const T& operator[] (size_t idx) const&;
|
|
||||||
|
T const& operator[] (size_t idx) const&
|
||||||
|
{
|
||||||
|
CHECK_LIMIT (idx, 0u, capacity ());
|
||||||
|
return *cruft::cast::alignment<T const*> (&m_head[idx].data[0]);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user