Skip to content

Engine → Core → frame_allocator.h

memory::frame_allocator

Syntax

class CONJURE_API frame_allocator

Constructors

AccessSignatureDescription
publicframe_allocator( usize arenaBytes, usize threadBlockBytes);threadBlockBytes is rounded up to a multiple of CACHE_LINE_SIZE (at least one line) so blocks of different threads never share a cache line; arenaBytes is rounded up to a whole number of blocks. An arena of 0 bytes is allowed: every allocation then overflows, which keeps the allocator usable when no frame budget is configured. An arena the heap cannot provide (too large for the address space or refused by the system allocator) degrades the same way: capacity_bytes() reports 0 and every allocation overflows.

Important

Additional constructor overloads have been deleted.

Destructors

AccessSignatureDescription
public~frame_allocator();

Variables

AccessNameTypeDefaultDescription
privatemArenabyte *nullptr
privatemCapacityBytesusize0
privatemEpochstd::atomic<u64>0
privatemHighWaterBytesstd::atomic<usize>0
privatemOffsetstd::atomic<usize>0
privatemOverflowBytesstd::atomic<usize>0
privatemOverflowCountstd::atomic<u32>0
privatemOverflowHeadstd::atomic<overflow_block *>nullptr
privatemThreadBlockBytesusize0

Functions

Public

allocate

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

Returns bytes bytes aligned to alignment (a power of two) that live until the next reset(). Thread-safe and lock-free. Returns nullptr for a zero-byte request, for an alignment that is not a power of two (asserts) and when the heap fallback fails.

allocate_span

template <typename T> [[nodiscard]] std::span<T> allocate_span(
    usize count) noexcept;

Allocates uninitialised storage for count elements of a trivially destructible T: nothing is constructed or destroyed, the caller initialises every element before reading it. Returns an empty span for a count of 0 or when allocation fails.

capacity_bytes

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

Usable arena bytes (after rounding to whole blocks); 0 when the allocator is heap-only.

construct

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

Allocates and constructs a T that lives until the next reset(). T must be trivially destructible because reset() never runs destructors. Returns nullptr when allocation fails.

epoch

[[nodiscard]] u64 epoch() const noexcept;

Identifies the current frame. Changes on every reset() and is unique across every frame_allocator in the process.

high_water_bytes

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

The largest frame seen so far, including the current one: arena bytes claimed by thread blocks plus bytes served by the heap fallback. Size the arena from this.

overflow_bytes

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

Bytes served by the heap fallback in the current frame.

overflow_count

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

Allocations served by the heap fallback in the current frame.

reset

void reset() noexcept;

Starts a new frame: frees the overflow blocks, returns every thread block to the arena and bumps the epoch so cached thread blocks are abandoned. Not thread-safe: no allocate() may run concurrently.

thread_block_bytes

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

Size of one thread block (after rounding).

used_bytes

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

Arena bytes claimed by thread blocks in the current frame.

Private

acquire_block

[[nodiscard]] byte *acquire_block() noexcept;

allocate_overflow

[[nodiscard]] void *allocate_overflow(
    usize bytes,
    usize alignment) noexcept;

fits_in_fresh_block

[[nodiscard]] bool fits_in_fresh_block(
    usize bytes,
    usize alignment) const noexcept;

free_overflow_blocks

void free_overflow_blocks() noexcept;