@mem

The @mem module provides arena-based memory allocation for manual memory management.

EZ uses scope-based memory management by default — allocations are automatically cleaned up when they leave scope, so most programs never need to think about memory. However, for performance-critical code, long-lived data structures, or fine-grained control over allocation patterns, the @mem module gives you direct access to arena-based allocation. This is entirely opt-in; the language handles everything else for you.

Import

import @mem

Functions

arena()

(size int) -> Arena

Creates an arena with the given byte capacity.

import @mem

do main() {
    mut a = mem.arena(1024)  // 1KB arena
    ensure mem.destroy(a)

    // Allocate within the arena...
}

destroy()

(arena Arena)

Destroys an arena and frees all its memory.

mem.destroy(a)

reset()

(arena Arena)

Resets an arena, reclaiming all allocations without freeing the underlying memory. Useful for reusing the same arena across iterations.

mem.reset(a)  // All previous allocations are invalidated

usage()

(arena Arena) -> int

Returns the number of bytes currently used in the arena.

mut used = mem.usage(a)
println("Arena usage:", used, "bytes")

init()

(arena Arena, Type) -> ^Type

Allocates a zero-initialized value of the given type in the arena.

const Point struct {
    x int
    y int
}

mut p = mem.init(a, Point)  // Returns ^Point, zero-initialized
p^.x = 10
p^.y = 20

alloc()

(arena Arena, value T) -> ^T

Allocates a copy of the given value in the arena.

mut p = mem.alloc(a, Point{x: 5, y: 10})  // Returns ^Point
println(p^.x)  // 5

copy()

(dest, src, n int)

Copies n bytes from src to dest.


zero()

(ptr, n int)

Zeros out n bytes at the given pointer.


set()

(ptr, value int, n int)

Sets n bytes at the given pointer to the specified value.


Example Program

import @mem

const Node struct {
    value int
    next ^Node
}

do main() {
    mut a = mem.arena(4096)
    ensure mem.destroy(a)

    // Allocate nodes in the arena
    mut n1 = mem.alloc(a, Node{value: 1})
    mut n2 = mem.alloc(a, Node{value: 2})
    mut n3 = mem.alloc(a, Node{value: 3})

    // Link them
    n1^.next = n2
    n2^.next = n3

    // Traverse
    mut current = n1
    as_long_as current != nil {
        println(current^.value)
        current = current^.next
    }

    println("Arena usage:", mem.usage(a), "bytes")
}