Skip to content

Engine → Core → arena.h

memory::arena

Syntax

class CONJURE_API arena

Constructors

AccessSignatureDescription
publicexplicit arena( usize blockBytes = DEFAULT_ARENA_BLOCK_BYTES);
publicarena( arena &&other) noexcept;

Important

Additional constructor overloads have been deleted.

Destructors

AccessSignatureDescription
public~arena();

Variables

AccessNameTypeDefaultDescription
privatemBlockBytesusizeDEFAULT_ARENA_BLOCK_BYTES
privatemBlockCountusize0
privatemBytesAllocatedusize0
privatemBytesReservedusize0
privatemCurrentblock *nullptr
privatemFirstblock *nullptr

Functions

Public

allocate

[[nodiscard]] void *allocate(
    usize bytes,
    usize alignment = DEFAULT_ALIGNMENT) noexcept;

Returns bytes bytes aligned to alignment (a power of two, up to and beyond 256). Bumps within the current block; a request that does not fit in a block of the configured size gets its own block. Returns nullptr for a zero-byte request, for an alignment that is not a power of two (asserts) and when the heap is exhausted.

block_bytes

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

Capacity of a regular block, as passed to the constructor.

block_count

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

Number of blocks currently held.

bytes_allocated

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

Sum of the bytes handed out since the last reset(), excluding alignment padding.

bytes_reserved

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

Sum of the capacities of the blocks currently held (the usable bytes, excluding the small per-block header).

construct

template <typename T, typename... Args> [[nodiscard]] T *construct(
    Args &&...args);

Allocates and constructs a T in the arena. T must be trivially destructible because reset() and release() never run destructors. Returns nullptr when allocation fails.

owns

[[nodiscard]] bool owns(
    const void *pointer) const noexcept;

True when pointer lies inside the used part of one of the arena's blocks. Linear in the block count; meant for assertions and tests.

release

void release() noexcept;

Invalidates every allocation and returns every block to the heap. The arena stays usable; the next allocate() obtains a fresh block.

reset

void reset() noexcept;

Invalidates every allocation. The first block whose capacity is the regular block size is kept for reuse, if there is one; every other block is returned to the heap. A dedicated oversized block therefore never survives a reset, wherever it sits in the chain: an arena whose first request of every cycle is oversized re-allocates that block every cycle, which a larger block size avoids.

Private

acquire_block

block *acquire_block(
    usize capacity) noexcept;

free_block

void free_block(
    block *node) noexcept;

Operator Overloads

Public

operator=

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