mem
heap allocator
import std.mem.HeapAlloc;
let allocator: HeapAlloc = HeapAlloc.new();
let buffer: Option[*int32] = allocator.alloc(1024); // size in bytes
let buffer = match buffer {
Some(ptr) => ptr,
None => panic("allocation failed!"),
}
// do something
allocator.free(buffer);
methods
| method | returns | description |
|---|---|---|
HeapAlloc.new() | HeapAlloc | initializes a new allocator |
.alloc[T](size: usize) | Option[*T] | allocates size bytes, returns typed pointer |
.free[T](buffer: *T) | — | frees all memory allocated for this buffer |
arena allocator
arena allocator allocates memory in a single contiguous buffer. all memory is freed at once when the arena is dropped — no individual frees needed.
let arena: ArenaAlloc = ArenaAlloc.new();
let ptr = match arena.alloc[int32](1024) {
Some(p) => p,
None => panic("arena allocation failed!"),
};
// ...
arena.drop(); // frees everything at once
methods
| method | returns | description |
|---|---|---|
ArenaAlloc.new() | ArenaAlloc | initializes a new arena |
.alloc[T](size: usize) | Option[*T] | allocates size bytes, returns typed pointer |
.drop() | — | frees all memory allocated in this arena |
note
unlike HeapAlloc, arena does not have a .free() for individual pointers —
everything is freed together via .drop().