interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / aienux / kernel / mm / heap.om

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