Module

std::libc::memory

LibC Memory

Memory allocation, byte-level operations, and virtual memory management.

Sub-namespaces

Namespace C Header Description
LibC::Allocator stdlib.h malloc, calloc, realloc, free
LibC::MemoryOperations string.h memcpy, memmove, memset, memcmp
LibC::Memory sys/mman.h, unistd.h mmap, munmap, mprotect, sbrk, brk

Constants (LibC::Memory)

Constant Description
PROT_NONE No access permitted
PROT_READ Pages may be read
PROT_WRITE Pages may be written
PROT_EXEC Pages may be executed
MAP_SHARED Share mapping across processes
MAP_PRIVATE Private copy-on-write mapping
MAP_ANONYMOUS Mapping not backed by a file

Example

import LibC, CType from "std::libc";

function main(): i32 {
  // Heap allocation
  let buf: CType::CVoidPtr = LibC::Allocator::malloc(256);
  LibC::MemoryOperations::memset(buf, 0, 256);
  LibC::Allocator::free(buf);

  // Anonymous mmap
  let page: CType::CVoidPtr = LibC::Memory::mmap(
    null, 4096,
    LibC::Memory::PROT_READ | LibC::Memory::PROT_WRITE,
    LibC::Memory::MAP_PRIVATE | LibC::Memory::MAP_ANONYMOUS,
    -1, 0,
  );
  LibC::Memory::munmap(page, 4096);

  return 0;
}
namespace __mman

Functions

function mmap(addr: *mut void, length: u64, prot: i32, flags: i32, fd: i32, offset: i64): *mut void

POSIX mmap; maps memory or files into the process address space.

function mprotect(addr: *mut void, len: u64, prot: i32): i32

POSIX mprotect; changes protection bits for a mapped range.

function munmap(addr: *mut void, length: u64): i32

POSIX munmap; unmaps a mapped range.

Constants

const MAP_ANONYMOUS: i32
const MAP_PRIVATE: i32
const MAP_SHARED: i32
const PROT_EXEC: i32
const PROT_NONE: i32
const PROT_READ: i32
const PROT_WRITE: i32
namespace __stdlib_alloc

Functions

function calloc(count: u64, size: u64): *mut void

C calloc; allocates zero-initialized storage.

function free(ptr: *mut void): void

C free; releases an allocation.

function malloc(size: u64): *mut void

C malloc; allocates uninitialized storage.

function realloc(ptr: *mut void, size: u64): *mut void

C realloc; resizes an allocation.

namespace __string_mem

Functions

function memcmp(s1: *void, s2: *void, n: u64): i32

C memcmp; compares two byte ranges.

function memcpy(dest: *mut void, src: *void, n: u64): *mut void

C memcpy; copies non-overlapping memory.

function memmove(dest: *mut void, src: *void, n: u64): *mut void

C memmove; copies memory that may overlap.

function memset(dest: *mut void, c: i32, n: u64): *mut void

C memset; fills n bytes with byte value c.

namespace __unistd_mem

Functions

function brk(addr: *mut void): i32

POSIX brk; sets the program break to addr.

function sbrk(increment: i64): *mut void

POSIX sbrk; adjusts the program break by increment.

namespace Allocator

Memory allocation (stdlib.h)

namespace Memory

Low-level memory: sbrk, brk, mmap (sys/mman.h, unistd.h) — POSIX only.

namespace MemoryOperations

Memory operations (string.h)

Functions

function brk(addr: *mut void): i32

Sets the program break to addr. Low-level.

function calloc(count: u64, size: u64): *mut void

Allocates zero-initialized memory for count objects of size bytes each.

Returns a pointer to the allocated block, or null on failure.

function free(ptr: *mut void): void

Frees memory previously returned by malloc, calloc, or realloc.

function malloc(size: u64): *mut void

Allocates size bytes of uninitialized memory.

Returns a pointer to the allocated block, or null on failure.

function memcmp(s1: *void, s2: *void, n: u64): i32

Compares n bytes of s1 and s2. Returns 0 if equal.

function memcpy(dest: *mut void, src: *void, n: u64): *mut void

Copies n bytes from src to dest. Regions must not overlap.

function memmove(dest: *mut void, src: *void, n: u64): *mut void

Copies n bytes from src to dest. Handles overlapping regions.

function memset(dest: *mut void, c: i32, n: u64): *mut void

Fills n bytes of dest with the byte value c.

function mmap(addr: *mut void, length: u64, prot: i32, flags: i32, fd: i32, offset: i64): *mut void

Maps length bytes into the process address space.

Use PROT_* for protection and MAP_* for flags. Pass fd = -1 with MAP_ANONYMOUS for anonymous mappings.

function mprotect(addr: *mut void, len: u64, prot: i32): i32

Changes the protection on len bytes starting at addr.

function munmap(addr: *mut void, length: u64): i32

Unmaps length bytes starting at addr. Returns 0 on success.

function realloc(ptr: *mut void, size: u64): *mut void

Changes the size of the allocation at ptr to size bytes.

The contents are preserved up to the minimum of old and new sizes. Returns a (possibly moved) pointer, or null on failure.

function sbrk(increment: i64): *mut void

Adjusts the program break by increment bytes. Low-level.

Constants

const MAP_ANONYMOUS: i32
const MAP_PRIVATE: i32
const MAP_SHARED: i32
const PROT_EXEC: i32
const PROT_NONE: i32
const PROT_READ: i32
const PROT_WRITE: i32