std::mem (v0.2.1)
Allocation, reallocation, memory copy/move/set helpers, and memory mapping constants.
Overview
The std::mem module contains raw memory operations. It covers allocation, reallocation, freeing, byte copying, byte moving, byte filling, comparison, and memory mapping constants.
The functions are direct by design. Sizes are byte counts, pointers are raw pointers, and ownership is managed by the caller.
Higher-level code, including vector, builds on this module. Use it when you need precise control over storage and are prepared to manage lifetimes explicitly.
Source: thrustc/std/v0.2.1/mem.thrust
Public Signatures
Exported declarations for this module snapshot.
const PROT_READ: s32 @public = c::c_PROT_READ;
const PROT_WRITE: s32 @public = c::c_PROT_WRITE;
const PROT_EXEC: s32 @public = c::c_PROT_EXEC;
const PROT_NONE: s32 @public = c::c_PROT_NONE;
const PROT_GROWSDOWN: s32 @public = c::c_PROT_GROWSDOWN;
const PROT_GROWSUP: s32 @public = c::c_PROT_GROWSUP;
const MAP_SHARED: s32 @public = c::c_MAP_SHARED;
const MAP_PRIVATE: s32 @public = c::c_MAP_PRIVATE;
const MAP_SHARED_VALIDATE: s32 @public = c::c_MAP_SHARED_VALIDATE;
const MAP_TYPE: s32 @public = c::c_MAP_TYPE;
const MAP_FIXED: s32 @public = c::c_MAP_FIXED;
const MAP_ANONYMOUS: s32 @public = c::c_MAP_ANONYMOUS;
const MAP_ANON: s32 @public = c::c_MAP_ANON;
const MAP_GROWSDOWN: s32 @public = c::c_MAP_GROWSDOWN;
const MAP_DENYWRITE: s32 @public = c::c_MAP_DENYWRITE;
const MAP_EXECUTABLE: s32 @public = c::c_MAP_EXECUTABLE;
const MAP_LOCKED: s32 @public = c::c_MAP_LOCKED;
const MAP_NORESERVE: s32 @public = c::c_MAP_NORESERVE;
const MAP_POPULATE: s32 @public = c::c_MAP_POPULATE;
const MAP_NONBLOCK: s32 @public = c::c_MAP_NONBLOCK;
const MAP_STACK: s32 @public = c::c_MAP_STACK;
const MAP_HUGETLB: s32 @public = c::c_MAP_HUGETLB;
const MAP_SYNC: s32 @public = c::c_MAP_SYNC;
const MAP_FIXED_NOREPLACE: s32 @public = c::c_MAP_FIXED_NOREPLACE;
const MS_ASYNC: s32 @public = c::c_MS_ASYNC;
const MS_INVALIDATE: s32 @public = c::c_MS_INVALIDATE;
const MS_SYNC: s32 @public = c::c_MS_SYNC;
const MADV_NORMAL: s32 @public = c::c_MADV_NORMAL;
const MADV_RANDOM: s32 @public = c::c_MADV_RANDOM;
const MADV_SEQUENTIAL: s32 @public = c::c_MADV_SEQUENTIAL;
const MADV_WILLNEED: s32 @public = c::c_MADV_WILLNEED;
const MADV_DONTNEED: s32 @public = c::c_MADV_DONTNEED;
const MADV_FREE: s32 @public = c::c_MADV_FREE;
const MADV_REMOVE: s32 @public = c::c_MADV_REMOVE;
const MADV_DONTFORK: s32 @public = c::c_MADV_DONTFORK;
const MADV_DOFORK: s32 @public = c::c_MADV_DOFORK;
const MADV_MERGEABLE: s32 @public = c::c_MADV_MERGEABLE;
const MADV_UNMERGEABLE: s32 @public = c::c_MADV_UNMERGEABLE;
const MADV_HUGEPAGE: s32 @public = c::c_MADV_HUGEPAGE;
const MADV_NOHUGEPAGE: s32 @public = c::c_MADV_NOHUGEPAGE;
const MADV_DONTDUMP: s32 @public = c::c_MADV_DONTDUMP;
const MADV_DODUMP: s32 @public = c::c_MADV_DODUMP;
const MADV_WIPEONFORK: s32 @public = c::c_MADV_WIPEONFORK;
const MADV_KEEPONFORK: s32 @public = c::c_MADV_KEEPONFORK;
const MADV_COLD: s32 @public = c::c_MADV_COLD;
const MADV_PAGEOUT: s32 @public = c::c_MADV_PAGEOUT;
const MADV_POPULATE_READ: s32 @public = c::c_MADV_POPULATE_READ;
const MADV_POPULATE_WRITE: s32 @public = c::c_MADV_POPULATE_WRITE;
const MADV_DONTNEED_LOCKED: s32 @public = c::c_MADV_DONTNEED_LOCKED;
const MADV_HWPOISON: s32 @public = c::c_MADV_HWPOISON;
const MCL_CURRENT: s32 @public = c::c_MCL_CURRENT;
const MCL_FUTURE: s32 @public = c::c_MCL_FUTURE;
const MCL_ONFAULT: s32 @public = c::c_MCL_ONFAULT;
const M_MXFAST: s32 @public = c::c_M_MXFAST;
const M_TRIM_THRESHOLD: s32 @public = c::c_M_TRIM_THRESHOLD;
const M_TOP_PAD: s32 @public = c::c_M_TOP_PAD;
const M_MMAP_THRESHOLD: s32 @public = c::c_M_MMAP_THRESHOLD;
const M_MMAP_MAX: s32 @public = c::c_M_MMAP_MAX;
const M_CHECK_ACTION: s32 @public = c::c_M_CHECK_ACTION;
const M_PERTURB: s32 @public = c::c_M_PERTURB;
const M_ARENA_TEST: s32 @public = c::c_M_ARENA_TEST;
const M_ARENA_MAX: s32 @public = c::c_M_ARENA_MAX;
fn allocateMemory(size: usize) ptr @public;
fn allocateZeroedMemory(count: usize, size: usize) ptr @public;
fn reallocateMemory(block: ptr, size: usize) ptr @public;
fn freeMemory(block: ptr) void @public;
fn allocateAlignedMemory(alignment: usize, size: usize) ptr @public;
fn copyMemory(dest: ptr, src: ptr, size: usize) ptr @public;
fn moveMemory(dest: ptr, src: ptr, size: usize) ptr @public;
fn setMemory(dest: ptr, value: s32, size: usize) ptr @public;
fn compareMemory(left: ptr, right: ptr, size: usize) s32 @public;
fn findByteInMemory(buffer: ptr, value: s32, size: usize) ptr @public;
fn getUsableSize(block: ptr) usize @public;
fn mapMemory(base_addr: ptr, length: usize, prot: s32, flags: s32, fd: s32, offset: s64) ptr @public;
fn unmapMemory(base_addr: ptr, length: usize) s32 @public;
fn protectMemory(base_addr: ptr, length: usize, prot: s32) s32 @public;
fn adviseMemory(base_addr: ptr, length: usize, advice: s32) s32 @public;
fn syncMemory(base_addr: ptr, length: usize, flags: s32) s32 @public;
fn lockMemory(base_addr: ptr, length: usize) s32 @public;
fn unlockMemory(base_addr: ptr, length: usize) s32 @public;
fn getPageSize() s32 @public;
Behavior and Use
Allocation functions can return nullptr. Always check the result before writing through the pointer.
Use moveMemory when the source and destination can overlap. Use copyMemory only when they are separate ranges.
Examples
import std::mem;
fn main() s32 @public {
var block: ptr = mem::allocateMemory(128);
if block == nullptr {
return 1;
}
mem::setMemory(block, 0, 128);
mem::freeMemory(block);
return 0;
}
import std::mem;
fn main() s32 @public {
var src: array[u8; 6] = fixed[10, 20, 30, 40, 50, 60];
var dst: array[u8; 6] = fixed[0, 0, 0, 0, 0, 0];
mem::copyMemory(
(ref dst) as ptr,
(ref src) as ptr,
sizeOf(array[u8; 6])
);
if (deref dst[2]) == 30 {
return 0;
}
return 1;
}
Notes
- Lifetime ownership is manual. There is no automatic memory release.
- All transfer helpers receive sizes in bytes.
- Mapping constants are exposed to keep flag composition readable.