std::memory::arena
Arena Allocator
Growable block-based arena allocator backed by the runtime.
Overview
ArenaAllocator is designed for workloads where many allocations share the
same lifetime. Instead of tracking every allocation separately, the arena
allocates from larger backing blocks and invalidates everything together on
reset() or destroy().
This makes it a good fit for:
- parser and compiler temporary objects
- short-lived request or frame allocations
- scratch buffers built and discarded in batches
- phase-local data where all values die together
Core Behavior
The arena has three important properties:
-
Fast allocation
- allocate from the current backing block
- if the block has no room, request or switch to another runtime block
-
No individual deallocation
- there is no per-allocation
free - memory stays owned by the arena until reset or destroy
- there is no per-allocation
-
Bulk lifetime management
reset()keeps the arena but rewinds allocation statedestroy()releases all arena-owned backing blocks
Mental Model
ArenaAllocator
┌─────────────────────────────────────────────────────────────────┐
│ handle -> runtime arena state │
│ blockSize -> preferred block size for future growth │
└─────────────────────────────────────────────────────────────────┘
Runtime-owned backing blocks
┌────────────────────────┐ ┌────────────────────────┐
│ [used][used][free...] │ │ [used][free.........] │
└────────────────────────┘ └────────────────────────┘
The exact block metadata lives in the runtime, not in this Ignis wrapper.
At the stdlib level, ArenaAllocator is a lightweight handle over that
runtime-managed arena state.
Allocation Semantics
allocate(layout)returns a properly aligned pointer forlayoutallocate<T>()allocates space for exactly oneTallocate<T>(count)allocates space forcountcontiguousTvalues- zero-sized requests return
null - if the arena handle has been destroyed, allocation returns
null
Reset vs Destroy
reset()
- keeps backing blocks
- invalidates all previously returned pointers
- makes arena space reusable
destroy()
- releases all backing blocks through the runtime
- sets handle to null
- allocator becomes unusable until reinitialized
Safety Notes
ArenaAllocator deliberately trades precision for speed.
Callers must treat all pointers returned from the arena as invalid after:
reset()destroy()drop()
Values allocated in the arena are not individually dropped by the allocator. If a stored type owns resources, the caller is responsible for ensuring the lifetime strategy is valid for arena allocation.
Example
import ArenaAllocator from "std::memory";
import Layout from "std::memory";
function main(): i32 {
let mut arena: ArenaAllocator = ArenaAllocator::new(4096);
let numbers: *mut i32 = arena.allocate<i32>(16);
if (numbers == null) {
return 1;
}
numbers[0] = 42;
arena.reset();
// `numbers` is now invalid and must not be used.
let raw: *mut u8 = arena.allocate(Layout::new<u64>());
if (raw == null) {
return 2;
}
arena.destroy();
return 0;
}
namespace __arenaFunctions
function ignis_arena_allocate(handle: *mut void, size: u64, alignment: u64): *mut u8Allocates size bytes from an arena handle with alignment.
function ignis_arena_create(blockSize: u64): *mut voidCreates a runtime arena handle with the requested block size.
function ignis_arena_destroy(handle: *mut void): voidDestroys an arena handle and releases all backing blocks.
function ignis_arena_reset(handle: *mut void): voidResets an arena, invalidating all allocations made from it.
Records
record ArenaAllocatorArena-style allocator backed by runtime-managed memory blocks.
This stdlib type is intentionally small: it stores only a runtime handle and the preferred block size used when the runtime grows the arena.
handle: *mut voidOpaque runtime handle for the arena state.
blockSize: u64Preferred backing block size used by the runtime arena implementation.
static init(blockSize: u64): ArenaAllocatorCompatibility alias for ArenaAllocator::new(blockSize).
static new(blockSize: u64): ArenaAllocatorCreates a new arena allocator with the given preferred block size.
Arguments
blockSize- preferred growth size for runtime backing blocks.
Example
import ArenaAllocator from "std::memory";
let arena: ArenaAllocator = ArenaAllocator::new(4096);
allocate(&mut self, layout: Layout): *mut u8Allocates raw memory for layout from the arena.
Returns null when the arena has been destroyed or when layout
represents a zero-sized allocation.
allocate(&mut self, layout: Layout): *mut u8Allocates raw memory for layout from the arena.
Returns null when the arena has been destroyed or when layout
represents a zero-sized allocation.
allocate(&mut self, layout: Layout): *mut u8Allocates raw memory for layout from the arena.
Returns null when the arena has been destroyed or when layout
represents a zero-sized allocation.
destroy(&mut self): voidReleases all runtime-managed backing blocks and invalidates the arena.
After destroy(), further allocations return null until the allocator
is recreated with init.
drop(&mut self): voidAutomatically destroys the arena when it goes out of scope.
reset(&mut self): voidRewinds allocation state so all arena space becomes reusable again.
All previously returned pointers become invalid after this call.