Pool
struct Pool[T: Deinitable & Movable]Typed heap allocator over Pointer[T].alloc(1).
Stateless — every method is @staticmethod. The struct
exists only to pin the type parameter; instantiating it is
not required.
alloc_move(value) returns the heap address as an Int
so callers can store it in a Mojo-managed structure
(Dict[Int, Int], List[Int], struct field) without a
typed pointer escaping into user code. free(addr) runs
T's destructor and releases the memory.
ZST handling: when size_of[T]() == 0, alloc[T](1) is
rejected by Mojo's stdlib ("size must be greater than zero").
Pool transparently allocates a single placeholder byte via
alloc[UInt8](1) and bitcasts to Pointer[T] for
the move-in / dereference pattern (which is a no-op for a
zero-byte type). Callers see the same API regardless of
whether T is ZST.
Threading: each Pool[T] operation is independent (no
shared state between calls). Multiple threads can call
alloc_move and free concurrently as long as each
address is owned by exactly one thread at a time.
Implements Movable
alloc_move
def alloc_move(var value: T) -> Int Heap-allocate one T, move value into it, return the address.
- Args
value: OwnedTto move into the heap cell.
Raises Error: When the underlying allocation returns null.
free
def free(addr: Int) Run T's destructor at addr and release the memory.
Idempotent on addr == 0 (no-op). Calling free
twice on the same non-zero addr is undefined
behaviour — the caller must own the address exactly
once, just as with raw Pointer.
- Args
addr: Address returned byPool[T].alloc_move, or 0 (no-op).
get_ptr
def get_ptr(addr: Int) -> Pointer[T, MutUntrackedOrigin] Re-materialise a typed pointer from an address.
For callers that need to dereference the cell (e.g. read /
write a field) without allocating their own
Pointer arithmetic. Returned pointer carries
MutUntrackedOrigin so the Mojo optimiser cannot hoist
loads through it.
Caller is responsible for ensuring addr is a valid
live pool allocation.