Module allocator

allocator

Memory allocation abstractions and global allocator interface.

Types

Allocator type-alias
Allocator

Type-erased dynamic allocator wrapping any CustomAllocator implementation.

Layout struct
Layout

Describes the size and alignment requirements for a memory allocation.

Fields

NameTypeDescription
sizeusize

Size in bytes.

alignmentusize

Alignment in bytes (must be a power of two).

AllocError enum
AllocError

Memory allocation error variants.

Variants

VariantFieldsDescription
OutOfMemory

Allocator ran out of memory.

InvalidSize

Requested size is invalid (e.g., zero or too large).

InvalidAlignment

Requested alignment is not a power of two.

InvalidPointer

Pointer passed to dealloc/realloc is invalid.

Traits / Modules

CustomAllocator

User-implementable memory allocator interface. Implement this trait to provide custom allocation strategies.

Methods

alloc : fn(self : *(Self), comptime(T) : Type, count : usize) -> Result(Option(*(T)), AllocError)

Allocate memory for count items of type T.

Parameters

NameTypeNotes
self*(Self)
TTypecomptime
countusize

Returns: Result(Option(*(T)), AllocError)

alloc_zeroed : fn(self : *(Self), comptime(T) : Type, count : usize) -> Result(Option(*(T)), AllocError)

Allocate zeroed memory for count items of type T.

Parameters

NameTypeNotes
self*(Self)
TTypecomptime
countusize

Returns: Result(Option(*(T)), AllocError)

alloc_aligned : fn(self : *(Self), comptime(T) : Type, count : usize, alignment : usize) -> Result(Option(*(T)), AllocError)

Allocate aligned memory for count items of type T with the given alignment.

Parameters

NameTypeNotesDescription
self*(Self)
TTypecomptime
countusize
alignmentusize

Alignment in bytes (must be a power of two).

Returns: Result(Option(*(T)), AllocError)

realloc : fn(forall(comptime(T) : Type), self : *(Self), old_ptr : Option(*(T)), new_count : usize) -> Result(Option(*(T)), AllocError)

Reallocate memory to new_count items, preserving existing data.

Parameters

NameTypeNotes
self*(Self)
old_ptrOption(*(T))
new_countusize

Returns: Result(Option(*(T)), AllocError)

dealloc : fn(forall(comptime(T) : Type), self : *(Self), ptr : *(T)) -> Result(unit, AllocError)

Deallocate memory pointed to by ptr.

Parameters

NameTypeNotes
self*(Self)
ptr*(T)

Returns: Result(unit, AllocError)

Functions

fn(comptime(T) : Type, count : usize) -> bool

Check if allocating count elements of type T would overflow usize.

Parameters

NameTypeNotes
TTypecomptime
countusize

Returns: bool

layout_of function
fn(comptime(T) : Type) -> (comptime(fn_return_yoa941ae71_id_60) : Layout)

Get the memory layout (size and alignment) of a type at compile time.

Parameters

NameTypeNotes
TTypecomptime

Returns: Layout

Constants

The process-wide global allocator (mimalloc by default, falls back to libc).

Value: GlobalAllocator(malloc: __yo_malloc, calloc: __yo_calloc, realloc: __yo_realloc, free: __yo_free, aligned_alloc: __yo_aligned_alloc)