Engine → Core → frame_allocator.h
memory::frame_allocator
| Type | Class |
| Namespace | conjure::core::memory |
| Include File Path | engine/include/conjure/core/memory/frame_allocator.h |
| Source File Path | engine/src/core/memory/frame_allocator.cpp |
| API ↗ | Memory |
Syntax
Constructors
| Access | Signature | Description |
|---|---|---|
| public | frame_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
| Access | Signature | Description |
|---|---|---|
| public | ~frame_allocator(); |
Variables
| Access | Name | Type | Default | Description |
|---|---|---|---|---|
| private | mArena | byte * | nullptr | |
| private | m | usize | 0 | |
| private | mEpoch | std::atomic<u64> | 0 | |
| private | m | std::atomic<usize> | 0 | |
| private | mOffset | std::atomic<usize> | 0 | |
| private | m | std::atomic<usize> | 0 | |
| private | m | std::atomic<u32> | 0 | |
| private | m | std:: | nullptr | |
| private | m | usize | 0 |
Functions
Public
allocate
allocateReturns 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
allocate_spantemplate <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
capacity_bytes[[nodiscard]] usize capacity_bytes() const noexcept;Usable arena bytes (after rounding to whole blocks); 0 when the allocator is heap-only.
construct
constructtemplate <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
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
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
overflow_bytes[[nodiscard]] usize overflow_bytes() const noexcept;Bytes served by the heap fallback in the current frame.
overflow_count
overflow_count[[nodiscard]] u32 overflow_count() const noexcept;Allocations served by the heap fallback in the current frame.
reset
resetvoid 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
thread_block_bytes[[nodiscard]] usize thread_block_bytes() const noexcept;Size of one thread block (after rounding).
used_bytes
used_bytes[[nodiscard]] usize used_bytes() const noexcept;Arena bytes claimed by thread blocks in the current frame.
Private
acquire_block
acquire_block[[nodiscard]] byte *acquire_block() noexcept;fits_in_fresh_block
fits_in_fresh_blockfree_overflow_blocks
free_overflow_blocksvoid free_overflow_blocks() noexcept;