Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/aienux/kernel/mm/heap.om # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / aienux / kernel / mm / heap.om` 189 lines ยท 4448 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note aienux/kernel/mm/heap.om: Pure-Omega First-Fit Kernel Heap over a Contiguous Region. 3 note Converted from upstream aienux native/kernel/mm/heap.h and native/kernel/mm/heap.c. 4 note Blocks carry a 16-byte header (u64 size, u32 magic, u32 pad); payloads are 16-byte aligned 5 note and zeroed on allocation; free validates by walking from offset 0 and coalesces neighbors. 6 7 note Shared kernel runtime structs (declared before first fn per OM-2 rule) 8 struct ProbeState insn ulong fixup ulong esr ulong far ulong armed uint end 9 10 struct TimerState ticks ulong last ulong tmin ulong tmax ulong freq_hz ulong end 11 12 struct LcArg off ulong alen ulong end 13 14 struct PipeRet err uint val ulong val2 ulong end 15 16 const KHEAP_HDR ulong 16 17 const KHEAP_MIN_BLK ulong 32 18 const KHEAP_MAGIC_USED uint 0xa1e05ed0 19 const KHEAP_MAGIC_FREE uint 0xa1e0f4ee 20 const KHEAP_NONE ulong 0xffffffffffffffff 21 22 const KHEAP_OK int 0 23 const KHEAP_ERR int -1 24 25 fn kh_rd32 buf view uchar * off ulong to uint 26 b0 = uint get buf off 27 b1 = uint get buf add off 1 28 b2 = uint get buf add off 2 29 b3 = uint get buf add off 3 30 ret or or b0 shl b1 8 or shl b2 16 shl b3 24 31 end 32 33 fn kh_rd64 buf view uchar * off ulong to ulong 34 lo = ulong kh_rd32 buf off 35 hi = ulong kh_rd32 buf add off 4 36 ret or lo shl hi 32 37 end 38 39 fn kh_wr32 buf edit uchar * off ulong v uint to void 40 put buf off uchar and v 0xff 41 put buf add off 1 uchar and shr v 8 0xff 42 put buf add off 2 uchar and shr v 16 0xff 43 put buf add off 3 uchar and shr v 24 0xff 44 end 45 46 fn kh_wr64 buf edit uchar * off ulong v ulong to void 47 kh_wr32 buf off uint and v 0xffffffff 48 kh_wr32 buf add off 4 uint and shr v 32 0xffffffff 49 end 50 51 fn kheap_init buf edit uchar * size ulong to int 52 if or gt size len buf lt size KHEAP_MIN_BLK 53 ret KHEAP_ERR 54 end 55 aligned = and size xor ulong 0xffffffffffffffff 15 56 if lt aligned KHEAP_MIN_BLK 57 ret KHEAP_ERR 58 end 59 kh_wr64 buf 0 aligned 60 kh_wr32 buf 8 KHEAP_MAGIC_FREE 61 kh_wr32 buf 12 0 62 ret KHEAP_OK 63 end 64 65 note Allocates bytes from heap buf of total aligned size; returns payload offset (>0) or 0 on failure. 66 fn kheap_alloc buf edit uchar * hsz ulong bytes ulong to ulong 67 if or gt hsz len buf gt bytes hsz 68 ret 0 69 end 70 aligned_b = and add bytes 15 xor ulong 0xffffffffffffffff 15 71 need = add aligned_b KHEAP_HDR 72 if lt need KHEAP_MIN_BLK 73 need = KHEAP_MIN_BLK 74 end 75 off = ulong 0 76 while lt off hsz bound 4096 77 bsz = kh_rd64 buf off 78 bmag = kh_rd32 buf add off 8 79 if eq bsz 0 80 ret 0 81 end 82 if and eq bmag KHEAP_MAGIC_FREE ge bsz need 83 if ge sub bsz need KHEAP_MIN_BLK 84 roff = add off need 85 kh_wr64 buf roff sub bsz need 86 kh_wr32 buf add roff 8 KHEAP_MAGIC_FREE 87 kh_wr32 buf add roff 12 0 88 bsz = need 89 kh_wr64 buf off bsz 90 end 91 kh_wr32 buf add off 8 KHEAP_MAGIC_USED 92 poff = add off KHEAP_HDR 93 plen = sub bsz KHEAP_HDR 94 i = ulong 0 95 while lt i plen bound 65536 96 put buf add poff i 0 97 i = add i 1 98 end 99 ret poff 100 end 101 off = add off bsz 102 end 103 ret 0 104 end 105 106 note Frees payload offset poff by walking from offset 0 (forged/double-free/corrupt -> KHEAP_ERR). 107 fn kheap_free buf edit uchar * hsz ulong poff ulong to int 108 if or gt hsz len buf or lt poff KHEAP_HDR or ge poff hsz ne and poff 15 0 109 ret KHEAP_ERR 110 end 111 prev = KHEAP_NONE 112 off = ulong 0 113 while lt off hsz bound 4096 114 bsz = kh_rd64 buf off 115 if eq bsz 0 116 ret KHEAP_ERR 117 end 118 if eq add off KHEAP_HDR poff 119 bmag = kh_rd32 buf add off 8 120 if ne bmag KHEAP_MAGIC_USED 121 ret KHEAP_ERR 122 end 123 kh_wr32 buf add off 8 KHEAP_MAGIC_FREE 124 nxt = add off bsz 125 if lt nxt hsz 126 if eq kh_rd32 buf add nxt 8 KHEAP_MAGIC_FREE 127 bsz = add bsz kh_rd64 buf nxt 128 kh_wr64 buf off bsz 129 end 130 end 131 if ne prev KHEAP_NONE 132 if eq kh_rd32 buf add prev 8 KHEAP_MAGIC_FREE 133 psz = add kh_rd64 buf prev bsz 134 kh_wr64 buf prev psz 135 end 136 end 137 ret KHEAP_OK 138 end 139 prev = off 140 off = add off bsz 141 end 142 ret KHEAP_ERR 143 end 144 145 fn kheap_free_bytes buf view uchar * hsz ulong to ulong 146 if gt hsz len buf 147 ret 0 148 end 149 total = ulong 0 150 off = ulong 0 151 while lt off hsz bound 4096 152 bsz = kh_rd64 buf off 153 if eq bsz 0 154 break 155 end 156 if eq kh_rd32 buf add off 8 KHEAP_MAGIC_FREE 157 if gt bsz KHEAP_HDR 158 total = add total sub bsz KHEAP_HDR 159 end 160 end 161 off = add off bsz 162 end 163 ret total 164 end 165 166 fn kheap_check buf view uchar * hsz ulong to int 167 if gt hsz len buf 168 ret KHEAP_ERR 169 end 170 off = ulong 0 171 while lt off hsz bound 4096 172 bsz = kh_rd64 buf off 173 bmag = kh_rd32 buf add off 8 174 if or lt bsz KHEAP_MIN_BLK ne and bsz 15 0 175 ret KHEAP_ERR 176 end 177 if and ne bmag KHEAP_MAGIC_USED ne bmag KHEAP_MAGIC_FREE 178 ret KHEAP_ERR 179 end 180 if gt add off bsz hsz 181 ret KHEAP_ERR 182 end 183 off = add off bsz 184 end 185 if eq off hsz 186 ret KHEAP_OK 187 end 188 ret KHEAP_ERR 189 end