allocators/core
sourceallocators/core.sx
pub type Arena = { buf: ref void, offset: usize, cap: usize }
An arena hands out memory from a single large block.
Individual allocations cannot be freed — the whole arena is freed at once. This is ideal for request-scoped or phase-scoped allocations.
Layout of an Arena struct (3 fields × 8 bytes each): offset 0: buf — ref void, pointer to the backing block offset 8: offset — usize, current bump position (bytes used) offset 16: cap — usize, total capacity in bytes
Fields
| Name | Type |
|---|
buf | ref void |
offset | usize |
cap | usize |
pub enum AllocError = { OutOfMemory }
An error that occurs when attempting to allocate memory.
Variants
pub spec Allocator = {
pub fn new(capacity: usize) result[Allocator, AllocError]
pub fn free(a: self) void
pub fn alloc(a: self, size: usize) option[ref void]
}
Interface for the classic bump allocator.
Functions
pub fn new(capacity: usize) result[Allocator, AllocError]pub fn free(a: self) voidpub fn alloc(a: self, size: usize) option[ref void]
pub fn (Arena) new(capacity: usize) result[Allocator, AllocError] = trust { ... }
Create a new arena backed by a block of capacity bytes. Capacity must be > 0.
pub fn (Arena) free(arena: Arena) void = trust { ... }
Free the entire arena and its backing block.
pub fn (Arena) alloc(arena: Arena, size: usize) option[ref void]
Allocate size bytes from the arena.
Requires size > 0. Returns none if there is not enough space remaining.
pub fn (Arena) alloc_or_none(arena: Arena, size: usize) ref void
Allocate size bytes from the arena, returning none when it is exhausted.
This is a convenience wrapper around alloc for call sites that prefer a nullable pointer over an option result.
pub fn (Arena) alloc_raw(arena: Arena, size: usize) ref void = trust { ... }
Allocate size bytes without wrapping the result in an option.
Returns none when the arena is exhausted.
pub fn (Arena) used(arena: Arena) usize
Return the number of bytes currently used.
pub fn (Arena) cap(arena: Arena) usize
Return the total capacity in bytes.
pub fn (Arena) remaining(arena: Arena) usize
Return the number of bytes remaining. Contracts: used <= cap, so the subtraction never underflows.
pub fn (Arena) reset(arena: Arena) void = trust { ... }
Reset the arena — all previously allocated pointers become invalid.
type Stack = { buf: ref void, offset: usize, cap: usize }
A Stack Allocator allows for LIFO (Last-In, First-Out) memory management.
You can "save" the current position and "restore" to it later.
Layout (identical to Arena for binary compatibility): offset 0: buf — ref void offset 8: offset — usize offset 16: cap — usize
Fields
| Name | Type |
|---|
buf | ref void |
offset | usize |
cap | usize |
pub fn (Stack) new(capacity: usize) result[Allocator, AllocError] = trust { ... }
Create a new stack allocator.
pub fn (Stack) alloc(stack: Stack, size: usize) option[ref void]
Allocate memory from the stack.
pub fn (Stack) save(stack: Stack) usize
Capture the current state of the stack. The returned usize is a "marker" or "checkpoint".
pub fn (Stack) restore(stack: Stack, checkpoint: usize) void
Restore the stack to a previous checkpoint. This effectively "frees" everything allocated after the 'save' call.
pub fn (Stack) free(stack: Stack) void = trust { ... }
Free the underlying memory.
pub spec GeneralAllocator = {
pub fn alloc(a: self, size: usize) option[ref void]
pub fn free(a: self, ptr: ref void) void
pub fn realloc(a: self, ptr: ref void, new_size: usize) option[ref void]
}
Functions
pub fn alloc(a: self, size: usize) option[ref void]pub fn free(a: self, ptr: ref void) voidpub fn realloc(a: self, ptr: ref void, new_size: usize) option[ref void]
pub type HeapAllocator = { }
A simple heap-backed general allocator.
pub fn (HeapAllocator) alloc(_: HeapAllocator, size: usize) option[ref void] = trust { ... }
pub fn (HeapAllocator) free(_: HeapAllocator, ptr: ref void) void = trust { ... }
Free a previously allocated block.
pub fn (HeapAllocator) realloc(_: HeapAllocator, ptr: ref void, new_size: usize) option[ref void] = trust { ... }
pub fn bump_alloc_unwrap[A: Allocator](ac: A, size: usize) ref void
Allocates memory, returns the pointer on success, and returns none on failure.
pub fn alloc_in[A: GeneralAllocator](ac: A, size: usize) option[ref void]
Allocates "size" bytes using the specified allocator.
Returns some containing a pointer to the allocated memory on success, or none if the allocation fails.
Requires size to be greater than zero.
pub fn free_in[A: GeneralAllocator](ac: A, ptr: ref void) void
Frees memory allocated by the specified allocator.
Does nothing if ptr is none.
pub fn realloc_in[A: GeneralAllocator](ac: A, ptr: ref void, new_size: usize) option[ref void]
Resizes a memory allocation using the specified allocator.
If ptr is none, allocates a new memory block of new_size bytes. Otherwise, resizes the existing allocation to new_size bytes. Returns some containing a pointer to the resized memory on success, or none if the allocation fails. Requires new_size to be greater than zero.
pub spec ScopedArenaAllocator = {}
Opaque handle type for ScopedArena.
pub type ScopedArena = { head: mut ref void, cur: mut ref void, block_size: mut usize }
A growable, resettable arena made of linked blocks.
reset reclaims all individual allocations at once while keeping the backing blocks for reuse. free releases the blocks and the arena handle.
Fields
| Name | Type |
|---|
head | mut ref void |
cur | mut ref void |
block_size | mut usize |
type ScopedArenaBlock = { buf: mut ref void, offset: mut usize, cap: mut usize, next: mut ref ScopedArenaBlock }
Fields
| Name | Type |
|---|
buf | mut ref void |
offset | mut usize |
cap | mut usize |
next | mut ref ScopedArenaBlock |
fn scoped_block_new(block_size: usize) result[ref ScopedArenaBlock, AllocError] = trust { ... }
pub fn (ScopedArena) new(block_size: usize) result[ScopedArenaAllocator, AllocError] = trust { ... }
Create a new scoped arena. block_size is the size of each backing block.
pub fn (ScopedArena) alloc(a: ScopedArenaAllocator, size: usize) option[ref void] = trust { ... }
Allocate size bytes from the arena, growing with fresh blocks as needed.
pub fn (ScopedArena) alloc_or_none(a: ScopedArenaAllocator, size: usize) ref void
Allocate size bytes, returning none on exhaustion.
pub fn (ScopedArena) alloc_raw(a: ScopedArenaAllocator, size: usize) ref void = trust { ... }
Allocate size bytes without wrapping the result in an option.
Returns none when allocation fails.
pub fn (ScopedArena) reset(a: ScopedArenaAllocator) void = trust { ... }
Reclaim all arena allocations while retaining the backing blocks.
pub fn (ScopedArena) free(a: ScopedArenaAllocator) void = trust { ... }
Release all backing blocks and the arena handle.
pub type ListArena = { arena: mut ScopedArenaAllocator }
A general allocator backed by a scoped arena.
This is useful for large groups of short-lived lists that all share one arena and are released together. Individual frees are no-ops, the backing arena is released separately.
Fields
| Name | Type |
|---|
arena | mut ScopedArenaAllocator |
pub fn (ListArena) alloc(a: ListArena, size: usize) option[ref void] = trust { ... }
Allocate a block from the list arena.
pub fn (ListArena) free(_: ListArena, _: ref void) void
Individual frees are no-ops, the whole arena is released at once.
pub fn (ListArena) realloc(a: ListArena, ptr: ref void, new_size: usize) option[ref void] = trust { ... }
Copy into a fresh arena block. The old block remains arena-owned.