vera:memory

Heap allocation and memory operations.

import "vera:memory"

Overview

Provides heap allocation (allocate/free) and bulk memory operations (copy, zero, set). All functions are built on compiler primitives. The allocator implementation lives in the compiler/runtime, not in VERA code. __vera_sys_alloc/__vera_sys_free use platform-appropriate strategies (mmap+freelist on Linux/macOS, VirtualAlloc+HeapAlloc on Windows).

Dependencies: None (uses primitives directly)

Functions

Allocation


allocate

function allocate(size: i64) -> ptr

Allocate size bytes of heap memory with default alignment (8 bytes).

Parameters:

  • size (i64) — number of bytes to allocate

Returns: pointer to the allocated block, or null (0) on failure


allocate_aligned

function allocate_aligned(size: i64, align: i64) -> ptr

Allocate size bytes with a specific alignment. Alignment must be a power of 2.

Parameters:

  • size (i64) — number of bytes to allocate
  • align (i64) — alignment in bytes (must be a power of 2)

Returns: pointer to the allocated block, or null (0) on failure


free

function free(addr: ptr, size: i64)

Free a previously allocated block. The caller must pass the same size that was used for the original allocate call.

Parameters:

  • addr (ptr) — pointer to the block to free
  • size (i64) — size of the block (must match the allocation size)

free_aligned

function free_aligned(addr: ptr, size: i64, align: i64)

Free a block that was allocated with allocate_aligned. The caller must pass the same size and alignment used for allocation.

Parameters:

  • addr (ptr) — pointer to the block to free
  • size (i64) — size of the block (must match the allocation size)
  • align (i64) — alignment of the block (must match the allocation alignment)

Bulk Memory Operations


copy_memory

function copy_memory(dest: ptr, src: ptr, count: i64)

Copy count bytes from src to dest. The source and destination regions must not overlap.

Parameters:

  • dest (ptr) — destination pointer
  • src (ptr) — source pointer
  • count (i64) — number of bytes to copy

set_memory

function set_memory(dest: ptr, value: i8, count: i64)

Set count bytes at dest to value.

Parameters:

  • dest (ptr) — destination pointer
  • value (i8) — byte value to fill with
  • count (i64) — number of bytes to set

zero_memory

function zero_memory(dest: ptr, count: i64)

Zero count bytes at dest. Equivalent to set_memory(dest, 0, count).

Parameters:

  • dest (ptr) — destination pointer
  • count (i64) — number of bytes to zero