Engine → Core → pool_allocator.h
memory::pool_allocator
| Type | Class |
| Namespace | conjure::core::memory |
| Include File Path | engine/include/conjure/core/memory/pool_allocator.h |
| Source File Path | engine/src/core/memory/pool_allocator.cpp |
| API ↗ | Memory |
Syntax
Constructors
| Access | Signature | Description |
|---|---|---|
| public | pool_allocator(
usize blockSize,
usize alignment,
u32 blocksPerSlab,
u32 maxSlabs = 0); | blockSize is rounded up to a multiple of alignment and to at least one pointer so a free block can hold its link. alignment must be a power of two (asserts and falls back to DEFAULT_ALIGNMENT otherwise) and is raised to at least alignof(void *) because a free block stores its link in place. blocksPerSlab must be at least 1 (asserts and uses 1 otherwise). maxSlabs caps growth; 0 means unlimited. Nothing is allocated until the first allocate(). |
| public | pool_allocator(
pool_allocator &&other) noexcept; |
Important
Additional constructor overloads have been deleted.
Destructors
| Access | Signature | Description |
|---|---|---|
| public | ~pool_allocator(); |
Variables
| Access | Name | Type | Default | Description |
|---|---|---|---|---|
| private | mAlignment | usize | DEFAULT_ | |
| private | mBlockBytes | usize | 0 | |
| private | m | u32 | 1 | |
| private | mFreeList | free_block * | nullptr | |
| private | mLiveCount | usize | 0 | |
| private | mMaxSlabs | u32 | 0 | |
| private | mSlabBytes | usize | 0 | |
| private | m | usize | 0 | |
| private | mSlabCount | usize | 0 | |
| private | mSlabs | slab * | nullptr |
Functions
Public
alignment
alignment[[nodiscard]] usize alignment() const noexcept;Alignment every block is guaranteed: the requested alignment raised to at least alignof(void *).
allocate
allocate[[nodiscard]] void *allocate() noexcept;Returns one block, growing by a slab when none is free. Returns nullptr when the slab cap is reached, a slab would be larger than MAX_ALLOCATION_BYTES, or a new slab (or the room to record it) cannot be obtained from the heap.
allocate_as
allocate_astemplate <typename T> [[nodiscard]] T *allocate_as() noexcept;allocate() typed as a pointer to uninitialised storage for a T. Asserts (and returns nullptr) when a T does not fit a block or needs more alignment than the pool gives.
block_size
block_size[[nodiscard]] usize block_size() const noexcept;Effective block size: the requested size rounded up to the alignment and to a pointer.
blocks_per_slab
blocks_per_slab[[nodiscard]] u32 blocks_per_slab() const noexcept;Blocks each slab holds, as passed to the constructor (at least 1).
capacity
capacity[[nodiscard]] usize capacity() const noexcept;Blocks the held slabs can hold in total (live and free).
construct
constructtemplate <typename T, typename... Args> [[nodiscard]] T *construct(
Args &&...args);Allocates a block and constructs a T in it. Returns nullptr when allocation fails or a T does not fit a block. Pair with destroy().
deallocate
deallocatevoid deallocate(
void *block) noexcept;Returns a block to the pool. nullptr is ignored. A pointer the pool does not own, one that is not the start of a block, or a block that is already free asserts and is ignored.
destroy
destroytemplate <typename T> void destroy(
T *object) noexcept;Destroys an object made by construct() and returns its block. nullptr is ignored.
max_slabs
max_slabs[[nodiscard]] u32 max_slabs() const noexcept;Slab cap given to the constructor; 0 means unlimited.
owns
owns[[nodiscard]] bool owns(
const void *pointer) const noexcept;True when pointer is the start of a block in one of the pool's slabs (live or free).
shrink_to_fit
shrink_to_fitvoid shrink_to_fit() noexcept;Returns every slab that has no live block to the heap. Blocks in the remaining slabs are untouched; the slab table keeps its capacity.
Private
find_slab_index
find_slab_index[[nodiscard]] usize find_slab_index(
const void *pointer) const noexcept;grow
growbool grow() noexcept;is_block_start
is_block_start[[nodiscard]] bool is_block_start(
const slab &owner,
const void *pointer) const noexcept;release_slabs
release_slabsvoid release_slabs() noexcept;reserve_slab_table
reserve_slab_table[[nodiscard]] bool reserve_slab_table() noexcept;Operator Overloads
Public
operator=
operator=pool_allocator &operator=(
pool_allocator &&other) noexcept;