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).
ThinAllocation
struct ThinAllocation[T: AnyType]
An owning handle to a heap allocation of T, without its Layout.
A ThinAllocation is the minimal owning handle to allocated storage: just
the pointer, with no record of the element count or alignment. Because it
does not carry a Layout, the layout must be supplied again to deallocate
the storage. This makes ThinAllocation a good storage field for a
container that already tracks its own capacity, such as List.
ThinAllocation 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. Destroy one by either:
- wrapping it in an
Allocationwith itsLayoutand passing that todealloc(dealloc(allocation^.unsafe_with_layout(layout))), or - calling
unsafe_leak()to take ownership of the raw pointer (the caller then becomes responsible for deallocating it).
Parameters
- T (
AnyType): The type of the elements stored in the allocation.
Implemented traits
AnyType,
Movable,
RegisterPassable,
Writable
Methods
__init__
def __init__(*, unsafe_assume_ownership: UnsafePointer[T, MutUntrackedOrigin]) -> Self
Initializes a ThinAllocation that takes ownership of a raw pointer.
Safety:
The pointer must own storage that can be released through dealloc
(typically obtained from alloc), and no other value may own it.
Otherwise, destroying the ThinAllocation causes a double-free.
Args:
- unsafe_assume_ownership (
UnsafePointer[T, MutUntrackedOrigin]): The raw pointer to take ownership of. The newThinAllocationassumes responsibility for deallocating this storage.
unsafe_with_layout
def unsafe_with_layout(var self, layout: Layout[T]) -> Allocation[T]
Pairs this ThinAllocation with a Layout to form an Allocation.
Consumes self and bundles it with layout, producing an Allocation
that can be passed to dealloc.
Safety:
The layout must exactly match the Layout that was passed to alloc
when this storage was originally allocated — both the element count and
the alignment. dealloc releases the storage according to this
Layout, so a mismatch frees the wrong number of bytes (or assumes the
wrong alignment) and can corrupt the allocator.
Args:
- layout (
Layout[T]): TheLayoutthe storage was allocated with.
Returns:
Allocation[T]: An Allocation owning this storage together with layout.
unsafe_leak
def unsafe_leak(deinit self) -> UnsafePointer[T, MutUntrackedOrigin]
Consumes the ThinAllocation and returns its raw owning pointer.
ThinAllocation 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 with its Layout and passing
that to dealloc. Nothing enforces this once the pointer is leaked,
so failing to do so leaks memory.
Returns:
UnsafePointer[T, MutUntrackedOrigin]: The raw pointer to the allocated storage.
unsafe_ptr
def unsafe_ptr[origin: Origin[mut=origin.mut], address_space: AddressSpace, //](ref[origin.mut, origin] self) -> UnsafePointer[T, origin, address_space=address_space]
Returns a pointer to the allocated storage without consuming self.
The returned pointer borrows from self with the same origin and
address space, so the ThinAllocation retains ownership of the storage.
Parameters:
- origin (
Origin[mut=origin.mut]): The origin ofself, propagated to the returned pointer. - address_space (
AddressSpace): The address space ofself, propagated to the returned pointer.
Returns:
UnsafePointer[T, origin, address_space=address_space]: A pointer to the allocated storage in the given origin and address
space.
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.