std/mem

March 14, 2026 ยท View on GitHub

The std/mem module provides core memory management utilities, including manual allocation functions, standard lifecycle traits, and smart pointer implementations.

Overview

  • Manual Allocation: Wrappers around malloc, calloc, and free with type-safe signatures.
  • Traits: Defines the primary lifecycle traits: Drop (destructors), Clone (deep copies), and Copy (implicit copies).
  • Smart Pointers: Includes Box<T> for heap-allocated data with automatic cleanup (RAII).
  • Buffer Utilities: High-level functions for swapping, zeroing, and copying memory.

Usage

import "std/mem.zc"

fn main() {
    // Manual allocation
    let ptr = alloc<int>();
    *ptr = 42;
    free(ptr);
    
    // Automatic cleanup with Box (RAII)
    {
        let b = Box<int>::new();
        *b.get() = 100;
        // memory is freed automatically here
    }
}

Methods

Allocation

MethodSignatureDescription
alloc<T>alloc<T>() -> T*Allocates memory for a single instance of T.
zalloc<T>zalloc<T>() -> T*Allocates zero-initialized memory for a single instance of T.
alloc_n<T>alloc_n<T>(n: usize) -> T*Allocates memory for an array of n instances of T.

Operations

MethodSignatureDescription
mem_zero<T>mem_zero<T>(ptr: T*, count: usize)Sets memory for count instances of T to zero.
mem_copy<T>mem_copy<T>(dst: T*, src: T*, count: usize)Copies count instances of T from src to dst.
swap<T>swap<T>(a: T*, b: T*)Swaps the values between two memory locations.

Traits

TraitMethodSignatureDescription
Dropdropdrop(self)Destructor called when object goes out of scope.
Clonecloneclone(self) -> SelfCreates a deep copy of the object.
Copy(Marker)N/AIndicates the type should use implicit copies instead of moves.

Struct Definition: Box<T>

A simple RAII smart pointer for managing heap memory.

struct Box<T> {
    ptr: T*;
}

Box Methods

MethodSignatureDescription
newBox::new() -> Box<T>Allocates a new heap-managed instance.
from_ptrBox::from_ptr(p: T*) -> Box<T>Creates a Box that takes ownership of an existing pointer.
getget(self) -> T*Returns the raw internal pointer.
freefree(self)Manually frees the underlying memory.
Traitimpl Drop for Box<T>Automatically calls free() when the box exists scope.