4#ifndef IT_D4NP_MEMORYPOOL_POOL_ALLOCATOR_HPP_
5#define IT_D4NP_MEMORYPOOL_POOL_ALLOCATOR_HPP_
34namespace it::d4np::memorypool {
77 using propagate_on_container_copy_assignment = std::false_type;
78 using propagate_on_container_move_assignment = std::false_type;
79 using propagate_on_container_swap = std::false_type;
81 using is_always_equal = std::false_type;
109 if (routes_to_pool(n)) {
111 if (block ==
nullptr) {
112 throw std::bad_alloc{};
114 return static_cast<T*
>(block);
116 return allocate_fallback(n);
125 if (routes_to_pool(n)) {
129 deallocate_fallback(ptr);
133 template <
typename U>
135 return pool_ == rhs.pool_;
139 template <
typename U>
141 return !(*
this == rhs);
147 template <
typename U>
151 [[nodiscard]]
bool routes_to_pool(std::size_t n)
const noexcept {
152 return n == 1U &&
sizeof(T) <= pool_->
block_size() &&
alignof(T) <=
alignof(std::max_align_t);
156 [[nodiscard]]
static T* allocate_fallback(std::size_t n) {
157 if (n > (std::numeric_limits<std::size_t>::max() /
sizeof(T))) {
160 throw std::bad_alloc{};
162 const std::size_t bytes = n *
sizeof(T);
168 return static_cast<T*
>(::operator
new(bytes, std::align_val_t{
alignof(T)}));
172 static void deallocate_fallback(T* ptr)
noexcept {
178 ::operator
delete(ptr, std::align_val_t{
alignof(T)});
STL-compatible allocator vending storage from a Pool.
void deallocate(T *ptr, std::size_t n) noexcept
Return storage obtained from allocate.
T * allocate(std::size_t n)
Allocate storage for n objects of T (ADR-0018 §2).
bool operator==(const PoolAllocator< U > &rhs) const noexcept
Two adapters are equal iff they reference the same pool.
bool operator!=(const PoolAllocator< U > &rhs) const noexcept
Negation of operator==.
PoolAllocator(Pool &pool) noexcept
Bind the adapter to pool.
PoolAllocator(const PoolAllocator< U > &other) noexcept
Rebinding converting constructor — required by the Cpp17Allocator requirements so a container can bui...
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).
std::size_t block_size() const noexcept
Report the configured per-block size in bytes (ADR-0018 §3).
void * try_allocate() noexcept
Allocate one block in O(1) — non-throwing verb (ADR-0016 §2).
C++17 RAII wrapper around the C memory pool.