Skip to content

Engine → Core → pool_allocator.h

memory::pool_allocator

Syntax

class CONJURE_API pool_allocator

Constructors

AccessSignatureDescription
publicpool_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().
publicpool_allocator( pool_allocator &&other) noexcept;

Important

Additional constructor overloads have been deleted.

Destructors

AccessSignatureDescription
public~pool_allocator();

Variables

AccessNameTypeDefaultDescription
privatemAlignmentusizeDEFAULT_ALIGNMENT
privatemBlockBytesusize0
privatemBlocksPerSlabu321
privatemFreeListfree_block *nullptr
privatemLiveCountusize0
privatemMaxSlabsu320
privatemSlabBytesusize0
privatemSlabCapacityusize0
privatemSlabCountusize0
privatemSlabsslab *nullptr

Functions

Public

alignment

[[nodiscard]] usize alignment() const noexcept;

Alignment every block is guaranteed: the requested alignment raised to at least alignof(void *).

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

template <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

[[nodiscard]] usize block_size() const noexcept;

Effective block size: the requested size rounded up to the alignment and to a pointer.

blocks_per_slab

[[nodiscard]] u32 blocks_per_slab() const noexcept;

Blocks each slab holds, as passed to the constructor (at least 1).

capacity

[[nodiscard]] usize capacity() const noexcept;

Blocks the held slabs can hold in total (live and free).

construct

template <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

void 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

template <typename T> void destroy(
    T *object) noexcept;

Destroys an object made by construct() and returns its block. nullptr is ignored.

live_count

[[nodiscard]] usize live_count() const noexcept;

Blocks currently handed out.

max_slabs

[[nodiscard]] u32 max_slabs() const noexcept;

Slab cap given to the constructor; 0 means unlimited.

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

void 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.

slab_count

[[nodiscard]] usize slab_count() const noexcept;

Slabs currently held.

Private

block_index

[[nodiscard]] u32 block_index(
    const slab &owner,
    const void *pointer) const noexcept;

find_slab_index

[[nodiscard]] usize find_slab_index(
    const void *pointer) const noexcept;

fits

[[nodiscard]] bool fits(
    usize size,
    usize alignment) const noexcept;

free_bits

[[nodiscard]] u64 *free_bits(
    const slab &owner) const noexcept;

grow

bool grow() noexcept;

is_block_start

[[nodiscard]] bool is_block_start(
    const slab &owner,
    const void *pointer) const noexcept;

is_free

[[nodiscard]] bool is_free(
    const slab &owner,
    u32 index) const noexcept;

mark_free

void mark_free(
    const slab &owner,
    u32 index,
    bool isFree) const noexcept;

release_slabs

void release_slabs() noexcept;

reserve_slab_table

[[nodiscard]] bool reserve_slab_table() noexcept;

Operator Overloads

Public

operator=

pool_allocator &operator=(
    pool_allocator &&other) noexcept;