std1.0.0-beta-7 SafeC docs

Module mem

#include <std/mem.h> source

Memory declarations

Functions

allocHeap allocation. T comes from the variable the result is assigned to ('?&heap char buf = std::alloc(64UL);'); 'unsigned char' for untyped bytes. Sizes are in bytes.
alloc_zeroed
dealloc
realloc_buf
checked_mul_sizeMultiply two allocation-size factors (typically an element count and an element size) and abort with a diagnostic instead of silently wrapping if the product overflows unsigned long — the classic "count * elem_size" integer-overflow-to-undersized-allocation bug. Use this instead of a bare '*' anywhere the multiplicands aren't both compile-time constants small enough to prove can't overflow.
checked_add_sizeSame idea as checked_mul_size but for addition (e.g. summing several fields' sizes into one combined allocation) -- abort instead of silently wrapping if the sum overflows unsigned long.
safe_memcpyElement-wise copies between slices: min(dst.len, src.len) elements, the count returned. safe_memmove allows dst and src to overlap.
safe_memmove
safe_memsetSets every byte of s's elements to val (memset).
safe_memset_objThe same, for one object: safe_memset_obj(&ev, 0) clears a struct.
safe_memcmpByte-wise comparison (memcmp) of a and b over their common length; when that prefix is equal, the shorter slice compares less.
mem_align_upRound addr up to the next multiple of align (must be power of two).
mem_align_downRound addr down to the previous multiple of align.
mem_is_alignedReturn 1 if addr is aligned to align bytes.
mem_prefetchSoftware prefetch hint (maps to __builtin_prefetch on GCC/Clang, no-op elsewhere). locality: 0=no temporal, 1=low, 2=moderate, 3=high
mem_zero_secureZero s (all its elements' bytes) with volatile stores the optimizer can't remove (for secrets); mem_zero_secure_obj zeroes one object the same way.
mem_zero_secure_obj
mem_clflushWrite back and invalidate the cache line containing addr (x86 CLFLUSH, AArch64 DC CIVAC; a no-op on other targets).
mem_align_ptrRound a reference up to the next align-byte boundary (power of two).
mem_fits_pageCheck if size bytes fit within a page (4096 bytes) from addr.

Macros

MEM_CACHE_LINE_SIZEbytes (typical x86/ARM/RISC-V L1)