code wiki / _hdl_build / nx_alloc.nx

nx_alloc.nx

buildroot/runtime/_hdl_build/nx_alloc.nx

5669 B100 linesdepth 3pulls 4 transitivereach 0 importersview sourcekind tool
docsdependenciesstructsconstsfunctions

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

nx_syscalls.nx nx_itoa_lib.nx nx_g_puts_lib.nx nx_alloc.nx

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

main g_puts sys_write sys_mmap na_init ar_wr na_malloc ar_rd ar_wr ↻ g_pn nxi_out nxi_fd sys_mmap ↻ ccz_cat_num sys_write ↻ sys_munmap ck g_puts ↻ na_free ar_wr ↻ na_coalesce ar_rd ↻ ar_wr ↻ ar_rd ↻ sys_openat_append sys_write ↻ sys_close sys_exit

structs

none

consts

13const K_MAGIC_65536: i64 = 65536
14const K_MAGIC_100000: i64 = 100000
23const HDR: i64 = 16 // 8 bytes size + 8 bytes free-flag

functions

20func g_pn(v: i64) -> i64 { nxi_out(v); return 0 }
called by 1: main calls 1: nxi_out
21func 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 }
called by 1: main calls 1: g_puts
24func 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 }
25func 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 }
27func na_init(a: *u8, total: i64) -> i64 { ar_wr(a,0,total); ar_wr(a,8,1); return 0 } // one big free block
called by 1: main calls 1: ar_wr
29func na_malloc(a: *u8, total: i64, req: i64) -> i64
called by 1: main calls 2: ar_rdar_wr
43func na_free(a: *u8, dataoff: i64) -> i64 { ar_wr(a, dataoff-HDR+8, 1); return 0 } // mark block free
called by 1: main calls 1: ar_wr
45func na_coalesce(a: *u8, total: i64) -> i64
called by 1: main calls 2: ar_rdar_wr
57func main() -> i64