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

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 (not Deinitable) 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 Allocation with its Layout and passing that to dealloc (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, UnsafeNicheable, Writable, _FieldNicheable

Methods​

__init__​

def __init__(*, unsafe_owned_ptr: Pointer[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_owned_ptr (Pointer[T, MutUntrackedOrigin]): The raw pointer to take ownership of. The new ThinAllocation assumes responsibility for deallocating this storage.

unsafe_with_layout​

def unsafe_with_layout[alignment: Alignment, //](var self, layout: Layout[T, alignment=alignment]) -> Allocation[T, alignment=alignment]

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 its element count and its alignment. A ThinAllocation records neither, so nothing checks this for you: dealloc releases the storage according to the Layout you supply here, and a mismatch frees the wrong number of bytes and can corrupt the allocator.

Parameters:

  • ​alignment (Alignment): Byte alignment of the storage, taken from layout.

Args:

Returns:

Allocation[T, alignment=alignment]: An Allocation owning this storage together with layout.

unsafe_leak​

def unsafe_leak(deinit self) -> Pointer[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:

Pointer[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) -> Pointer[T, origin_of(origin["element"]), address_space=address_space]

Returns a pointer to the allocated storage without consuming self.

The returned pointer borrows from self and is in the same address space as self, so the ThinAllocation retains ownership of the storage.

Parameters:

  • ​origin (Origin[mut=mut]): The origin of self. The returned pointer carries an interior origin derived from it.
  • ​address_space (AddressSpace): The address space of self, propagated to the returned pointer.

Returns:

Pointer[T, origin_of(origin["element"]), address_space=address_space]: A pointer to the allocated storage in the given 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.