For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).
Allocation
struct Allocation[T: AnyType]
An owning handle to a heap allocation of T together with its Layout.
An Allocation pairs a ThinAllocation (the raw owning pointer) with the
Layout that produced it, so the element count and alignment needed to
deallocate the storage travel with the pointer. It is the value returned by
alloc.
Allocation is an explicitly destroyed (@explicit_destroy) type: it is
never deallocated automatically, and the compiler requires every value to be
destroyed manually on all paths. This prevents accidental leaks and
double-frees. Destroy one by either:
- passing it to
deallocto deallocate the storage, - calling
unsafe_leak()to take ownership of the raw pointer (the caller then becomes responsible for manually deallocating it), or - calling
into_thin()to drop theLayoutand keep the owningThinAllocation.
Example:
from std.memory.alloc import alloc, dealloc, Layout
var allocation = alloc(Layout[Int32](count=4))
# use the allocation...
dealloc(allocation^)
Parameters
- T (
AnyType): The type of the elements stored in the allocation.
Implemented traits
AnyType,
Movable,
RegisterPassable,
Writable
Methods
unsafe_leak
def unsafe_leak(deinit self) -> UnsafePointer[T, MutUntrackedOrigin]
Consumes the Allocation and returns its raw owning pointer.
Allocation is an explicitly destroyed type: it is never deallocated
automatically, and the compiler requires every value to be destroyed
manually. This method is one such destructor — it consumes the handle
and returns the raw pointer, which the compiler no longer tracks. The
caller then owns the storage and must deallocate it manually by wrapping
the pointer in a ThinAllocation, pairing it with its Layout, and
passing the result to dealloc.
Safety:
Leaking hands manual responsibility for the storage to the caller. Because the compiler no longer tracks the returned pointer, forgetting to deallocate it leaks the memory.
Returns:
UnsafePointer[T, MutUntrackedOrigin]: The raw pointer to the allocated storage.
unsafe_ptr
def unsafe_ptr(ref self) -> UnsafePointer[T, origin_of(self._alloc)]
Returns a pointer to the allocated storage without consuming self.
The returned pointer borrows from self, so the Allocation retains
ownership of the storage and remains responsible for deallocating it.
Safety:
alloc returns uninitialized storage, so the returned pointer may point
to uninitialized memory. Initialize an element (for example with
init_pointee_move) before reading it.
Returns:
UnsafePointer[T, origin_of(self._alloc)]: A pointer to the allocated storage.
unsafe_span
def unsafe_span(ref self) -> Span[T, origin_of(self._alloc)]
Returns a span over the allocated storage without consuming self.
The returned span borrows from self, so the Allocation retains
ownership of the storage. The span covers layout.count() elements.
Safety:
alloc returns uninitialized storage, so the returned span may cover
uninitialized memory. Initialize the elements before reading them.
Returns:
Span[T, origin_of(self._alloc)]: A span over the allocated storage.
layout
def layout(self) -> Layout[T]
Returns the Layout the storage was allocated with.
The returned Layout carries the element count and alignment used to
allocate the storage — the same information needed to deallocate it.
Returns:
Layout[T]: The Layout this allocation was created with.
into_thin
def into_thin(deinit self) -> ThinAllocation[T]
Consumes the Allocation and returns its associated ThinAllocation.
This drops the Layout and keeps only the owning handle. The returned
ThinAllocation must be consumed in turn.
Returns:
ThinAllocation[T]: The ThinAllocation that owns the storage.
write_to
def write_to(self, mut writer: T)
Writes a human-readable representation of this allocation to writer.
Args:
- writer (
T): The writer to write to.
write_repr_to
def write_repr_to(self, mut writer: T)
Writes a debug representation of this allocation to writer.
Args:
- writer (
T): The writer to write to.