Allocator
Defines the structural raw-storage protocol used by owning containers.
Module std.memory
import std.memory;Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.
Allocator
pub concept Allocator<Self>Defines the structural raw-storage protocol used by owning containers.
An allocator starts and ends allocation storage only; it never begins or ends a T object lifetime. Every operation is unsafe because callers own the exact identity, layout, lifetime, alias, and compatibility proofs.
Example
module example.allocator;
from std.memory import DefaultAllocator, Layout;
fn Main() -> i32 {
var Heap = default(DefaultAllocator);
let StorageLayout = Layout.Of<u64>();
unsafe {
switch Heap.Allocate(StorageLayout) {
case .Ok { Value = Storage } => {
Heap.Free(Storage, StorageLayout);
return 0;
}
case .Err { Error = _ } => return 1;
}
}
}Allocator.Allocate
requires unsafe fn Allocate(StorageLayout: Layout)
-> Result<mut byte*, AllocationError>;Acquires one distinct nonnull allocation identity. StorageLayout: Exact valid requested layout. Return: Writable raw bytes or a recoverable allocation error.
Allocator.Free
requires unsafe fn Free(
Storage: mut byte*,
StorageLayout: Layout
) -> void;Releases one compatible allocation without destroying objects. Storage: Exact base pointer, or null only with default(Layout). StorageLayout: Exact layout used to acquire nonnull storage. Return: No value after the allocation lifetime ends.