4#ifndef IT_D4NP_MEMORYPOOL_TYPED_POOL_HPP_
5#define IT_D4NP_MEMORYPOOL_TYPED_POOL_HPP_
30namespace it::d4np::memorypool {
57 static_assert(!std::is_void_v<T>,
"TypedPool<T>: T must be an object type; use Pool for raw void* blocks");
58 static_assert(!std::is_reference_v<T>,
"TypedPool<T>: T must be an object type, not a reference");
59 static_assert(
alignof(T) <=
alignof(std::max_align_t),
60 "TypedPool<T>: over-aligned types are not supported — the pool guarantees "
61 "alignof(std::max_align_t) only (ADR-0009 §5, ADR-0017 §2)");
73 constexpr std::size_t FLOOR =
sizeof(T) <
sizeof(
void*) ?
sizeof(
void*) :
sizeof(T);
74 constexpr std::size_t ALIGN =
alignof(std::max_align_t);
75 return ((FLOOR + ALIGN - 1U) / ALIGN) * ALIGN;
100 [[nodiscard]]
static std::optional<TypedPool>
make(std::size_t block_count) {
102 if (!pool.has_value()) {
120 return static_cast<T*
>(pool_.
allocate());
160 template <
typename... Args>
170 return ::new (raw) T(std::forward<Args>(args)...);
187 if (obj ==
nullptr) {
213 explicit TypedPool(
Pool&& pool) noexcept : pool_(std::move(pool)) {}
Owning, non-copyable, move-only wrapper around a memory_pool_t*.
void deallocate(void *block) noexcept
Return a previously allocated block to the pool in O(1).
void * allocate()
Allocate one block in O(1) — throwing verb (ADR-0016 §2).
memory_pool_t * native_handle() noexcept
void * try_allocate() noexcept
Allocate one block in O(1) — non-throwing verb (ADR-0016 §2).
std::size_t metadata_bytes() const noexcept
Report the per-pool metadata overhead in bytes (spec §3.2 / ADR-0015).
static std::optional< Pool > make(std::size_t block_size, std::size_t block_count)
Factory function returning an engaged std::optional<Pool> on successful construction or std::nullopt ...
Move-only, RAII, type-safe pool of fixed T-sized slots.
static constexpr std::size_t block_size() noexcept
The per-slot footprint in bytes — max(sizeof(T), sizeof(void*)) rounded up to the next multiple of al...
memory_pool_t * native_handle() noexcept
T * allocate()
Allocate one slot of uninitialized storage in O(1) — throwing verb (ADR-0016 §2).
T * try_allocate() noexcept
Allocate one slot of uninitialized storage in O(1) — non-throwing verb (ADR-0016 §2).
std::size_t metadata_bytes() const noexcept
Per-pool metadata overhead in bytes — forwards through Pool to memory_pool_metadata_bytes (spec §3....
TypedPool(std::size_t block_count)
Construct a typed pool with capacity for block_count slots.
static std::optional< TypedPool > make(std::size_t block_count)
Factory mirroring Pool::make (ADR-0011, restructured per ADR-0016 §3): failure as a value instead of ...
void destroy(T *obj) noexcept
Destroy a T obtained from construct and return its slot to the pool in O(1).
T * construct(Args &&... args)
Allocate a slot and placement-new a T into it — the object-lifetime verb (ADR-0017 §3).
void deallocate(T *block) noexcept
Return a storage slot obtained from allocate / try_allocate to the pool in O(1).
struct memory_pool memory_pool_t
Opaque handle to a memory pool instance.
C++17 RAII wrapper around the C memory pool.