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 __mmanFunctions
function mmap(addr: *mut void, length: u64, prot: i32, flags: i32, fd: i32, offset: i64): *mut voidPOSIX mmap; maps memory or files into the process address space.
function mprotect(addr: *mut void, len: u64, prot: i32): i32POSIX mprotect; changes protection bits for a mapped range.
function munmap(addr: *mut void, length: u64): i32POSIX munmap; unmaps a mapped range.
Constants
const MAP_ANONYMOUS: i32const MAP_PRIVATE: i32const MAP_SHARED: i32const PROT_EXEC: i32const PROT_NONE: i32const PROT_READ: i32const PROT_WRITE: i32namespace __stdlib_allocFunctions
function calloc(count: u64, size: u64): *mut voidC calloc; allocates zero-initialized storage.
function free(ptr: *mut void): voidC free; releases an allocation.
function malloc(size: u64): *mut voidC malloc; allocates uninitialized storage.
function realloc(ptr: *mut void, size: u64): *mut voidC realloc; resizes an allocation.
namespace __string_memFunctions
function memcmp(s1: *void, s2: *void, n: u64): i32C memcmp; compares two byte ranges.
function memcpy(dest: *mut void, src: *void, n: u64): *mut voidC memcpy; copies non-overlapping memory.
function memmove(dest: *mut void, src: *void, n: u64): *mut voidC memmove; copies memory that may overlap.
function memset(dest: *mut void, c: i32, n: u64): *mut voidC memset; fills n bytes with byte value c.
namespace __unistd_memFunctions
function brk(addr: *mut void): i32POSIX brk; sets the program break to addr.
function sbrk(increment: i64): *mut voidPOSIX sbrk; adjusts the program break by increment.
namespace AllocatorMemory allocation (stdlib.h)
namespace MemoryLow-level memory: sbrk, brk, mmap (sys/mman.h, unistd.h) — POSIX only.
namespace MemoryOperationsMemory operations (string.h)
Functions
function brk(addr: *mut void): i32Sets the program break to addr. Low-level.
function calloc(count: u64, size: u64): *mut voidAllocates 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): voidFrees memory previously returned by malloc, calloc, or realloc.
function malloc(size: u64): *mut voidAllocates size bytes of uninitialized memory.
Returns a pointer to the allocated block, or null on failure.
function memcmp(s1: *void, s2: *void, n: u64): i32Compares n bytes of s1 and s2. Returns 0 if equal.
function memcpy(dest: *mut void, src: *void, n: u64): *mut voidCopies n bytes from src to dest. Regions must not overlap.
function memmove(dest: *mut void, src: *void, n: u64): *mut voidCopies n bytes from src to dest. Handles overlapping regions.
function memset(dest: *mut void, c: i32, n: u64): *mut voidFills 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 voidMaps 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): i32Changes the protection on len bytes starting at addr.
function munmap(addr: *mut void, length: u64): i32Unmaps length bytes starting at addr. Returns 0 on success.
function realloc(ptr: *mut void, size: u64): *mut voidChanges 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 voidAdjusts the program break by increment bytes. Low-level.
Constants
const MAP_ANONYMOUS: i32const MAP_PRIVATE: i32const MAP_SHARED: i32const PROT_EXEC: i32const PROT_NONE: i32const PROT_READ: i32const PROT_WRITE: i32