pbr-cpp-memory-pool 1.1.2
Fixed-block-size O(1) memory pool — C++17 with an ANSI C public surface
Loading...
Searching...
No Matches
memory_pool.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2026 Daniel Polo
3
4#ifndef IT_D4NP_MEMORYPOOL_MEMORY_POOL_HPP_
5#define IT_D4NP_MEMORYPOOL_MEMORY_POOL_HPP_
6
22
23#include <cstddef>
24#include <optional>
25
26namespace it::d4np::memorypool {
27
51class Pool {
52public:
73 Pool(std::size_t block_size, std::size_t block_count);
74
85 [[nodiscard]] static std::optional<Pool> make(std::size_t block_size, std::size_t block_count);
86
100 [[nodiscard]] static std::optional<Pool> make_dynamic(std::size_t block_size, std::size_t block_count,
101 std::size_t growth_factor);
102
104 ~Pool() noexcept;
105
106 Pool(const Pool&) = delete;
107 Pool& operator=(const Pool&) = delete;
108
110 Pool(Pool&& other) noexcept;
111
113 Pool& operator=(Pool&& other) noexcept;
114
128 [[nodiscard]] void* allocate();
129
139 [[nodiscard]] void* try_allocate() noexcept;
140
146 void deallocate(void* block) noexcept;
147
153
165 [[nodiscard]] std::size_t metadata_bytes() const noexcept;
166
178 [[nodiscard]] std::size_t block_size() const noexcept;
179
180private:
187 explicit Pool(memory_pool_t* handle) noexcept;
188
189 memory_pool_t* handle_;
190};
191
220public:
222 PoolBuilder& with_block_size(std::size_t block_size) noexcept;
223
225 PoolBuilder& with_block_count(std::size_t block_count) noexcept;
226
233 PoolBuilder& with_growth_factor(std::size_t growth_factor) noexcept;
234
236 [[nodiscard]] std::optional<Pool> build() const;
237
238private:
239 std::size_t block_size_ = 0;
240 std::size_t block_count_ = 0;
241 std::size_t growth_factor_ = 0;
242};
243
244} // namespace it::d4np::memorypool
245
246#endif // IT_D4NP_MEMORYPOOL_MEMORY_POOL_HPP_
Fluent builder for configured Pool instances.
PoolBuilder & with_block_size(std::size_t block_size) noexcept
Set the per-block size in bytes; must satisfy ADR-0009 §2 at build time.
PoolBuilder & with_block_count(std::size_t block_count) noexcept
Set the block count; must satisfy ADR-0009 §3 at build time.
PoolBuilder & with_growth_factor(std::size_t growth_factor) noexcept
Opt into dynamic growth with the given geometric factor (ADR-0024 §3).
std::optional< Pool > build() const
Produce a std::optional<Pool> from the accumulated configuration.
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).
std::size_t block_size() const noexcept
Report the configured per-block size in bytes (ADR-0018 §3).
memory_pool_t * native_handle() noexcept
void * try_allocate() noexcept
Allocate one block in O(1) — non-throwing verb (ADR-0016 §2).
static std::optional< Pool > make_dynamic(std::size_t block_size, std::size_t block_count, std::size_t growth_factor)
Factory function for a dynamic-growth pool (spec §2.2, ADR-0022 / ADR-0024): on exhaustion it acquire...
std::size_t metadata_bytes() const noexcept
Report the per-pool metadata overhead in bytes (spec §3.2 / ADR-0015).
Pool(std::size_t block_size, std::size_t block_count)
Construct a pool with block_count blocks of block_size bytes each.
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 ...
~Pool() noexcept
Destroy the pool, releasing all backing storage to the OS.
Public C API for the pbr-cpp-memory-pool fixed-block-size allocator.
struct memory_pool memory_pool_t
Opaque handle to a memory pool instance.
Definition memory_pool.h:82