Memory API Reference

Low-level memory operations (memset, memcpy, memmove, memcmp)

Import

U std/memory

Functions

Fill Operations

FunctionSignatureDescription
mem_setF mem_set(dest: i64, value: i64, n: i64) -> i64Fill with byte value
mem_zeroF mem_zero(dest: i64, n: i64) -> i64Fill with zeros
mem_fill_i64F mem_fill_i64(dest: i64, value: i64, count: i64) -> i64Fill with i64 pattern

Copy Operations

FunctionSignatureDescription
mem_copyF mem_copy(dest: i64, src: i64, n: i64) -> i64Copy bytes (non-overlapping)
mem_moveF mem_move(dest: i64, src: i64, n: i64) -> i64Copy bytes (overlapping safe)
mem_copy_i64F mem_copy_i64(dest: i64, src: i64, count: i64) -> i64Copy i64 values

Comparison Operations

FunctionSignatureDescription
mem_cmpF mem_cmp(s1: i64, s2: i64, n: i64) -> i64Compare memory regions
mem_eqF mem_eq(s1: i64, s2: i64, n: i64) -> BoolCheck equality
mem_chrF mem_chr(ptr: i64, byte: i64, n: i64) -> i64Find first byte occurrence
mem_rchrF mem_rchr(ptr: i64, byte: i64, n: i64) -> i64Find last byte occurrence

Search Operations

FunctionSignatureDescription
mem_findF mem_find(haystack: i64, haystack_len: i64, needle: i64, needle_len: i64) -> i64Find pattern in memory

Allocation Helpers

FunctionSignatureDescription
mem_alloc_zeroedF mem_alloc_zeroed(size: i64) -> i64Allocate and zero-initialize
mem_reallocF mem_realloc(old_ptr: i64, old_size: i64, new_size: i64) -> i64Reallocate memory
mem_dupF mem_dup(src: i64, size: i64) -> i64Duplicate memory block

Pointer Arithmetic

FunctionSignatureDescription
ptr_align_upF ptr_align_up(ptr: i64, alignment: i64) -> i64Align pointer up
ptr_align_downF ptr_align_down(ptr: i64, alignment: i64) -> i64Align pointer down
ptr_is_alignedF ptr_is_aligned(ptr: i64, alignment: i64) -> BoolCheck if aligned
ptr_diffF ptr_diff(p1: i64, p2: i64) -> i64Calculate pointer distance
ptr_offsetF ptr_offset(ptr: i64, offset: i64) -> i64Offset pointer by bytes
ptr_offset_i64F ptr_offset_i64(ptr: i64, count: i64) -> i64Offset by i64 elements

Byte Swap (Endianness)

FunctionSignatureDescription
bswap16F bswap16(x: i64) -> i64Swap bytes of 16-bit value
bswap32F bswap32(x: i64) -> i64Swap bytes of 32-bit value
bswap64F bswap64(x: i64) -> i64Swap bytes of 64-bit value

Bit Manipulation

FunctionSignatureDescription
clz64F clz64(x: i64) -> i64Count leading zeros
ctz64F ctz64(x: i64) -> i64Count trailing zeros
popcount64F popcount64(x: i64) -> i64Count set bits
is_power_of_2F is_power_of_2(x: i64) -> BoolCheck if power of 2
next_power_of_2F next_power_of_2(x: i64) -> i64Round up to power of 2

Usage

U std/memory

F main() -> i64 {
    # Allocation
    buf := mem_alloc_zeroed(256)

    # Fill and copy
    mem_set(buf, 65, 10)  # Fill first 10 bytes with 'A'
    buf2 := mem_dup(buf, 256)

    # Search
    pos := mem_chr(buf, 65, 256)  # Find 'A'

    # Comparison
    I mem_eq(buf, buf2, 256) {
        puts("Equal")
    }

    # Endianness
    le_val := bswap32(0x12345678)  # Convert endianness

    # Bit operations
    zeros := clz64(0xFF)
    I is_power_of_2(256) {
        puts("Power of 2")
    }

    free(buf)
    free(buf2)
    0
}