Module std::mem

Low-level memory primitives (compiler-internal floor).

std::mem is the memory-intrinsic floor: the general-purpose heap allocator and typed-pointer primitives that the generic containers (Vec/HashMap/… in later waves) are built on top of.

These functions are declared as bodyless #[intrinsic("mem.*")] stubs: the compiler supplies their semantics directly (the stdlib catalog row provides the lowering). alloc/realloc/dealloc lower to calls of the hew-runtime allocator (hew_alloc/hew_realloc/hew_dealloc, wrapping std::alloc with a Layout::from_size_align floor); ptr_offset lowers to a byte-level LLVM GEP and ptr_copy to @llvm.memcpy.

Safety boundary (A605)

Every function here is an unsafe primitive and is compiler-internal only. The #[intrinsic] surface is gated: only this floor module (std.mem, on the INTRINSIC_FLOOR_MODULES allowlist) may declare or call them. User code can never reach these — there is no surface unsafe/@unsafe, and the typecheck gate rejects any #[intrinsic] declaration outside an allowlisted floor module. They carry no bounds, lifetime, or aliasing checks; correctness is the caller's obligation, and the only callers are the compiler-authored containers.

Allocation contract (fail-closed)

Contents

Functions

Function alloc

pub fn alloc(size: u64, align: u64) -> *mut u8

Allocate size bytes aligned to align. Returns a null pointer if the (size, align) layout is invalid; aborts on genuine OOM. Pair with dealloc carrying the same (size, align).

Function realloc

pub fn realloc(ptr: *mut u8, old_size: u64, new_size: u64, align: u64) -> *mut u8

Resize the block at ptr (currently old_size bytes, align) to new_size bytes, preserving the leading min(old_size, new_size) bytes. Returns a null pointer if the new layout is invalid (the old block is left intact); aborts on genuine OOM. A null ptr is treated as a fresh alloc(new_size, align).

Function dealloc

pub fn dealloc(ptr: *mut u8, size: u64, align: u64)

Free the block at ptr, which must have been produced by alloc/realloc with the same (size, align). A mismatched layout aborts in the runtime.

Function ptr_offset

pub fn ptr_offset(ptr: *mut u8, byte_offset: u64) -> *mut u8

Return ptr.add(byte_offset) — byte-level pointer arithmetic with the same contract as Rust's <*mut T>::add (A612: byte-level monomorphic, no element-type scaling here; the caller has already multiplied by the element size). Lowers to an inbounds GEP, so the resulting pointer must stay within the bounds of the same allocation ptr points into — pointing one byte past the end of that allocation is permitted, but any other out-of-allocation or wrapping offset is undefined behaviour. Pure arithmetic; no load or store. Cross-allocation or wrapping offsets are a distinct future primitive and are intentionally not provided here (YAGNI).

Function ptr_copy

pub fn ptr_copy(dst: *mut u8, src: *mut u8, byte_count: u64)

Copy byte_count bytes from src to dst (raw memcpy; the regions must not overlap). Byte-level monomorphic (A612): the caller has already multiplied the element count by the element size. No bounds checking.