mem -- Memory
The mem module provides safe wrappers around heap allocation and memory operations. All functions perform NULL checks and bounds validation internally.
#include "mem.h"Allocation
alloc
void* alloc(unsigned long size);Allocate size bytes of uninitialized heap memory. Returns a pointer to the allocated block, or NULL on failure.
alloc_zeroed
void* alloc_zeroed(unsigned long size);Allocate size bytes of zero-initialized heap memory. Returns a pointer to the allocated block, or NULL on failure.
dealloc
void dealloc(void* ptr);Free a previously allocated block. Passing NULL is a no-op.
realloc_buf
void* realloc_buf(void* ptr, unsigned long new_size);Resize a previously allocated block to new_size bytes. The contents are preserved up to the minimum of the old and new sizes. Returns the (possibly moved) pointer, or NULL on failure.
Memory Operations
safe_memcpy
void safe_memcpy(void* dst, const void* src, unsigned long n);Copy n bytes from src to dst. The source and destination regions must not overlap. Use safe_memmove for overlapping regions.
safe_memmove
void safe_memmove(void* dst, const void* src, unsigned long n);Copy n bytes from src to dst. Safe for overlapping regions.
safe_memset
void safe_memset(void* ptr, int val, unsigned long n);Set n bytes starting at ptr to the value val (interpreted as unsigned char).
safe_memcmp
int safe_memcmp(const void* a, const void* b, unsigned long n);Compare n bytes of memory at a and b. Returns:
< 0ifais less thanb0if equal> 0ifais greater thanb
Example
#include "mem.h"
#include "io.h"
int main() {
// Allocate a buffer for 10 ints
int* buf = alloc(10 * sizeof(int));
safe_memset(buf, 0, 10 * sizeof(int));
buf[0] = 42;
buf[1] = 99;
// Duplicate into a new buffer
int* copy = alloc(10 * sizeof(int));
safe_memcpy(copy, buf, 10 * sizeof(int));
print_int(copy[0]); // 42
println("");
dealloc(buf);
dealloc(copy);
return 0;
}