code wiki / _hdl_build / nx_alloc.nx
nx_alloc.nx
buildroot/runtime/_hdl_build/nx_alloc.nx
about
nx_alloc.nx -- SOVEREIGN MEMORY ALLOCATOR (OS gap: MEMORY-MGMT). A heap (malloc/free) over one mmap'd arena:
inline block headers [size(incl hdr) | free], first-fit allocate with split, free + coalesce adjacent free blocks.
No libc malloc -- our own, deterministic + auditable. NEVER-BRICK: operates on userspace RAM, writes 0 firmware.
Grounded: os_mem.raw (Memory_management).
T1 alloc 3 blocks, write distinct patterns, read back == no overlap.
T2 free the middle, re-malloc reuses that region (first-fit).
T3 free all + coalesce -> a single free block == whole arena (no fragmentation creep).
T4 teeth: an over-arena malloc fails (returns -1).
expect_exit: 0 Sovereign: nx_syscalls.
dependencies 3 imports · 0 importers
imports: nx_syscalls.nxnx_itoa_lib.nxnx_g_puts_lib.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 13 | const K_MAGIC_65536: i64 = 65536 |
| 14 | const K_MAGIC_100000: i64 = 100000 |
| 23 | const HDR: i64 = 16 // 8 bytes size + 8 bytes free-flag |
functions
| 20 | func g_pn(v: i64) -> i64 { nxi_out(v); return 0 } |
| 21 | func ck(name: *u8, c: i64) -> i64 { if c==1 { g_puts(" PASS " as *u8) } else { g_puts(" FAIL " as *u8) } g_puts(name); g_puts("\n" as *u8); return c } |
| 24 | func ar_rd(a: *u8, o: i64) -> i64 { var v: i64=0; var i: i64=0; while i<8 { v=v|((a[o+i] as i64)<<(i*8)); i=i+1 } return v } |
| 25 | func ar_wr(a: *u8, o: i64, v: i64) -> i64 { var i: i64=0; while i<8 { a[o+i]=((v>>(i*8))&255) as u8; i=i+1 } return 0 } |
| 27 | func na_init(a: *u8, total: i64) -> i64 { ar_wr(a,0,total); ar_wr(a,8,1); return 0 } // one big free block |
| 29 | func na_malloc(a: *u8, total: i64, req: i64) -> i64 |
| 43 | func na_free(a: *u8, dataoff: i64) -> i64 { ar_wr(a, dataoff-HDR+8, 1); return 0 } // mark block free |
| 45 | func na_coalesce(a: *u8, total: i64) -> i64 |
| 57 | func main() -> i64 |