Skip to content
CryoCryo home
Stdliballoc

allocator

import std::alloc::allocator; · source

Types

The Allocator trait

type trait Allocator {
    allocate(mut &this, layout: Layout) -> Result<NonNull<u8>, AllocError>;
    deallocate(mut &this, ptr: NonNull<u8>, layout: Layout) -> void;
    reallocate(mut &this, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout) -> Result<NonNull<u8>, AllocError>;
}

allocate returns uninitialized memory — write before you read. deallocate requires that the pointer came from a previous allocate on this allocator with a matching layout.

reallocate has a default: allocate, copy, free. An allocator that can grow a block in place should override it.

Collections take A: Allocator as a type parameter, so the storage strategy stays a caller decision instead of being baked into the container.

Methods

allocate(mut &this, layout: Layout) -> Result<NonNull<u8>, AllocError>;

Return a pointer to at least layout.size bytes aligned to layout.alignment. The returned memory is uninitialized; the caller writes to it before any read.

deallocate(mut &this, ptr: NonNull<u8>, layout: Layout) -> void;

Return memory to the allocator. ptr must have come from a previous allocate on this allocator with a matching layout. Using the pointer after this call is undefined.

reallocate(mut &this, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout) -> Result<NonNull<u8>, AllocError>;

Default implementation: allocate fresh memory, copy, free old. Allocators that can grow a block in place (real realloc) should override for the win.

Implementors

implement trait Allocator for struct GlobalAlloc
implement trait Allocator for struct Arena
std::alloc::arena_alloc
implement trait Allocator for struct Pool
std::alloc::pool

AllocError

type struct AllocError {
    kind:   AllocErrorKind;
    layout: Layout;

    static new(kind: AllocErrorKind, layout: Layout) -> AllocError;
    describe(&this) -> Str;
}

Failures carry both a kind and the layout that was refused, and describe(&this) -> Str gives a short stable message — the same accessor every standard library error type exposes.

Properties

PropertyType
kindAllocErrorKind
layoutLayout

Constructors

static new(kind: AllocErrorKind, layout: Layout) -> AllocError;

Methods

describe(&this) -> Str;

A short, stable description of what went wrong, as a borrowed Str. The convergent accessor every stdlib error type exposes (describe(&this) -> Str); Display/Debug for AllocError delegate to it. The requested layout remains available as a field. Borrows a static string literal, so the view outlives any AllocError.

AllocErrorKind

type enum AllocErrorKind {
    OutOfMemory;
    ZeroSized;
    Exhausted;
    InvalidLayout;
}
Variants
VariantNotes
OutOfMemoryThe underlying allocator returned null; the system is out of memory or hit an address-space limit.
ZeroSizedThe allocator refuses zero-sized requests. Callers that want a non-null dangling pointer for a zero-sized type should handle the zero case themselves before calling allocate.
ExhaustedA bounded allocator (arena, pool) has no room for this request.
InvalidLayoutThe requested layout is incompatible with this allocator (e.g., the size or alignment doesn't match a Pool's fixed slot). The caller passed the wrong allocator for the requested layout.

Trait implementations

implement trait Display for struct AllocError
std::fmt::display
implement trait Debug for struct AllocError
std::fmt::display

GlobalAlloc

type struct GlobalAlloc {
    static new() -> GlobalAlloc;
}

A zero-sized handle to the process-wide allocator; every instance dispatches through the same backend. Construct it with GlobalAlloc::new(). It is the default for every allocator-generic type in the library, which is what keeps the bare Box<T> spelling meaningful everywhere.

The backend is libc malloc by default and the segment heap in alloc::heap under the native_alloc config. Either way, when the compiler driver has published a per-target Arena, GlobalAlloc routes through it for the duration of a build — that is the compiler's own business and is inactive in user programs.

Constructors

static new() -> GlobalAlloc;

Trait implementations

implement trait Allocator for struct GlobalAlloc

Free Functions

function next_capacity(current: u64, required: u64, min_capacity: u64) -> u64;

Geometric growth-size for a contiguous buffer that must hold at least required slots. Doubles current (seeded by min_capacity so empty-to-nonempty transitions don't start at a single slot) until it reaches required. Once doubling would overflow u64 the curve has hit its ceiling: return exactly required (already known to fit) rather than wrapping to 0 and looping forever. Shared by Array::reserve_grow and RawBuffer::reserve, whose storage layouts differ (ABI-load-bearing) but whose growth curve is identical.

This is the growth policy the containers share. It lives here rather than in collections so an allocator-aware type outside the stdlib can grow its buffers the same way.

public function alloc(size: u64, align: u64) -> void*;

Allocate size bytes aligned to align via GlobalAlloc. Null/zero guarded; returns null on OOM so the emitter's existing null-check on the old malloc result is unchanged.

public function free(ptr: void*, align: u64) -> void;

Free a block from alloc via GlobalAlloc::deallocate, which already no-ops arena-owned pointers (bulk-reclaimed at the target boundary) and frees libc-owned ones. align must match the new site; deallocate ignores the layout size, so a 1-byte placeholder is fine.

The untyped pair new and delete lower to. Both go through GlobalAlloc, so a new T() is placed exactly where a Box or an Array would be. alloc treats a zero size as one byte and a zero alignment as 16, and returns null when the allocation fails rather than aborting. free takes the alignment the block was allocated with.

Constants

const MAX_REALLOC_ALIGN: u64 = 16;