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).

DeletableAllocation

struct DeletableAllocation[T: AnyType]

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

A DeletableAllocation 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 DeletableAllocation 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_deletable() (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 destroy_n) before the DeletableAllocation is destroyed.

Example:

from std.memory.alloc import alloc, Layout

var deletable = alloc(Layout[Int32](count=4)).into_deletable()
var ptr = deletable.unsafe_ptr()
for i in range(4):
(ptr + i).unsafe_write(i)
# `deletable` 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 DeletableAllocation that owns allocation.

This is the constructor form of Allocation.into_deletable(). The new DeletableAllocation 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.

__del__

def __del__(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 destroy_n) beforehand if they need it.

into_allocation

def into_allocation(deinit self) -> Allocation[T]

Consumes the DeletableAllocation and returns its Allocation.

This converts the self-freeing handle back into the explicitly destroyed Allocation it wraps, undoing into_deletable(). The storage is no longer freed automatically: the returned Allocation is an @explicit_destroy type that must be destroyed manually on every path, either by passing it to dealloc or by calling unsafe_leak().

Returns:

Allocation[T]: The Allocation that owns the storage.

unsafe_ptr

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

Returns a pointer to the allocated storage without consuming self.

The returned pointer borrows from self, so the DeletableAllocation 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:

UnsafePointer[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 DeletableAllocation 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.