Vixen
02b45cc4
csharp
public sealed class ArenaAllocator

A bump allocator: hand out memory by advancing a pointer, and reclaim all of it at once by moving the pointer back.

No guide page documents this yet — the page shows what the code says about itself.

Remarks

Allocation is an add and a compare. There is no free list, no size classes, no fragmentation, and — the point — no per-allocation release. That is exactly right for memory whose lifetime is a frame or a scope: render command payloads, culling results, layout scratch, the intermediate arrays of a single pass.

It is exactly wrong for anything that outlives the reset. A pointer handed out before a Reset points into memory that is about to be handed to somebody else, and nothing here will tell you. Scope the arena tightly and the property becomes a guarantee rather than a hazard.

Memory comes in blocks that are kept and reused across resets, so a steady workload stops calling the system allocator entirely after the first few frames. Not thread-safe: each thread gets its own — see FrameArena.

Fields and properties (5)

  • public const int DefaultBlockSize

    The block size used when none is given: 1 MiB.

  • public long BytesAllocated

    How many bytes have been handed out since the last reset.

  • public long BytesReserved

    How many bytes the arena holds across all its blocks.

  • public int BlockCount

    How many blocks the arena has taken from the system allocator.

  • public long PeakBytesAllocated

    The high-water mark of BytesAllocated across every reset, which is the number to size the arena from.

Methods (7)

  • public ArenaAllocator(int blockSize = 1048576, string? name = null)

    Creates an arena that takes memory in blocks of a given size.

  • public void* Allocate(nuint byteCount, int alignment = 16)

    Hands out bytes of uninitialised memory.

  • public Span<T> Allocate<T>(int count) where T : unmanaged

    Hands out room for elements, as a span.

  • public Span<T> AllocateZeroed<T>(int count) where T : unmanaged

    Hands out room for elements, zeroed.

  • public void Reset()

    Reclaims everything at once. The blocks are kept, so the next frame allocates out of memory that is already warm.

  • public ArenaAllocator.Scope Push()

    Opens a nested scope that rewinds to where it started when disposed.

  • public void Dispose()

    Frees every block. The arena is unusable afterwards.

Used by (3)

  • FrameArenaVixen.Core.Memory
  • MemoryTestsVixen.Core.Memory.Tests
  • ScopeVixen.Core.Memory