IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: Nightly
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).

ManagedAllocation

struct ManagedAllocation[T: AnyType]

An owning handle to a heap allocation of T that frees itself.

A ManagedAllocation wraps an Allocation and deallocates the storage in its destructor. It is the self-freeing counterpart to Allocation: where Allocation is non-ImplicitlyDeletable and must be passed to dealloc on every path, a ManagedAllocation deallocates its storage automatically. Mojo runs the destructor after the value's last use, following its "as soon as possible" (ASAP) destruction policy — including on error paths, where the destructor runs as the stack unwinds.

This trades the compile-time leak-proofing of Allocation for ergonomics: there is no need to thread an explicit dealloc through every control-flow path. Create one from an Allocation with into_managed() (or by passing the Allocation to the constructor), and recover the underlying Allocation — taking back manual responsibility for deallocation — with into_allocation().

Like dealloc, the destructor frees the storage but does not run the destructors of any elements written into it. If the elements need their destructors run, destroy them yourself (for example with unsafe_destroy_n) before the ManagedAllocation is destroyed.

Example:

from std.memory.alloc import alloc, Layout

var managed = alloc(Layout[Int32](count=4)).into_managed()
var ptr = managed.unsafe_ptr()
for i in range(4):
ptr.unsafe_offset(i).unsafe_write(i)
# `managed` frees its storage when it is destroyed (after its last use).

Parameters

  • T (AnyType): The type of the elements stored in the allocation.

Implemented traits

AnyType, ImplicitlyDeletable, Movable, RegisterPassable, Writable

Methods

__init__

def __init__(var allocation: Allocation[T], /) -> Self

Initializes a ManagedAllocation that owns allocation.

This is the constructor form of Allocation.into_managed(). The new ManagedAllocation assumes responsibility for deallocating the storage and frees it automatically when it is destroyed, after its last use.

Args:

  • allocation (Allocation[T]): The Allocation to take ownership of. It is consumed by this call.

__deinit__

def __deinit__(deinit self)

Deallocates the owned storage.

Releases the storage owned by the wrapped Allocation by passing it to dealloc. Like dealloc, this frees the storage but does not run the destructors of any elements written into it; destroy them yourself (for example with unsafe_destroy_n) beforehand if they need it.

unsafe_ptr

def unsafe_ptr(ref self) -> Pointer[T, origin_of(self)]

Returns a pointer to the allocated storage without consuming self.

The returned pointer borrows from self, so the ManagedAllocation retains ownership of the storage and frees it automatically when it is destroyed.

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:

Pointer[T, origin_of(self)]: A pointer to the allocated storage.

unsafe_span

def unsafe_span(ref self) -> Span[T, origin_of(self._alloc._alloc)]

Returns a span over the allocated storage without consuming self.

The returned span borrows from self, so the ManagedAllocation 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._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.