compiler/tests/fixtures/runtime/hosted-heap-provider/main.ldn
skip to the library navigation
1import core/mem
2import core/vec
3import hosted/heap
4
5no_allocation: atom
6allocation: type = no_allocation | ptr mut u8
7same_block: (saved: allocation, block: ptr mut u8) -> (same: bool) =
8 match saved
9 no_allocation: same = false
10 ptr (present): same = usize(present) == usize(block)
11 end match
12end same_block
13
14is_absent: (value: allocation) -> (empty: bool) =
15 match value
16 no_allocation: empty = true
17 ptr: empty = false
18 end match
19end is_absent
20
21-- This fixture-local provider is an observer, not the reusable failure
22-- wrapper `core/fault` supplies. It delegates every operation
23-- to the real heap while retaining the two simultaneously live extents.
24audit_state: type = struct
25 inner: heap.heap
26 first_live: bool
27 first_block: allocation
28 first_size: usize
29 second_live: bool
30 second_block: allocation
31 second_size: usize
32 live: usize
33 peak_live: usize
34 allocations: usize
35 frees: usize
36 first_alloc_size: usize
37 second_alloc_size: usize
38 first_free_size: usize
39 second_free_size: usize
40 coexistence: bool
41 mismatch: bool
42end audit_state
43
44audit: type = struct
45 state: ptr mut audit_state
46end audit
47
48new_audit: () -> (state: audit_state) =
49 state = (inner: heap.new(),
50 first_live: false, first_block: no_allocation, first_size: 0,
51 second_live: false, second_block: no_allocation, second_size: 0,
52 live: 0, peak_live: 0, allocations: 0, frees: 0,
53 first_alloc_size: 0, second_alloc_size: 0,
54 first_free_size: 0, second_free_size: 0,
55 coexistence: false, mismatch: false)
56end new_audit
57
58audit_alloc: (self: audit, size: usize, alignment: usize)
59 -> (block: ptr mut u8) ! mem.out_of_memory =
60 state: ptr mut audit_state = self.state
61 block = try mem.allocate(state.val.inner, size, alignment)
62 if state.val.first_live and state.val.second_live then
63 mem.release(state.val.inner, block, size)
64 fail mem.out_of_memory
65 end if
66
67 if state.val.live > 0 then
68 state.val.coexistence = true
69 end if
70 inc state.val.live
71 if state.val.live > state.val.peak_live then
72 state.val.peak_live = state.val.live
73 end if
74 inc state.val.allocations
75 if state.val.allocations == 1 then
76 state.val.first_alloc_size = size
77 elsif state.val.allocations == 2 then
78 state.val.second_alloc_size = size
79 end if
80 if alignment > 1 and usize(block) % alignment <> 0 then
81 state.val.mismatch = true
82 end if
83
84 if not state.val.first_live then
85 state.val.first_live = true
86 state.val.first_block = block
87 state.val.first_size = size
88 else
89 state.val.second_live = true
90 state.val.second_block = block
91 state.val.second_size = size
92 end if
93end audit_alloc
94
95audit_free: (self: audit, block: ptr mut u8, size: usize) -> none =
96 state: ptr mut audit_state = self.state
97 mut matched: bool = false
98 if state.val.first_live and same_block(state.val.first_block, block) then
99 if size <> state.val.first_size then
100 state.val.mismatch = true
101 end if
102 state.val.first_live = false
103 state.val.first_block = no_allocation
104 matched = true
105 elsif state.val.second_live
106 and same_block(state.val.second_block, block) then
107 if size <> state.val.second_size then
108 state.val.mismatch = true
109 end if
110 state.val.second_live = false
111 state.val.second_block = no_allocation
112 matched = true
113 end if
114 if not matched then
115 state.val.mismatch = true
116 else
117 dec state.val.live
118 end if
119
120 inc state.val.frees
121 if state.val.frees == 1 then
122 state.val.first_free_size = size
123 elsif state.val.frees == 2 then
124 state.val.second_free_size = size
125 end if
126 mem.release(state.val.inner, block, size)
127end audit_free
128
129audit_grow: (self: audit, block: ptr mut u8,
130 old_size: usize, new_size: usize, alignment: usize)
131 -> (grown: bool) =
132 _ = self.state
133 _ = block
134 _ = old_size
135 _ = new_size
136 _ = alignment
137 grown = false
138end audit_grow
139
140-- Resizing moves the block, through this provider's own allocation
141-- and free.
142audit_resize: (self: audit, block: ptr mut u8,
143 old_size: usize, new_size: usize, alignment: usize)
144 -> (moved: ptr mut u8) ! mem.out_of_memory =
145 moved = try mem.relocate(self, block, old_size, new_size, alignment)
146end audit_resize
147
148audit is mem.allocator (alloc: audit_alloc, grow: audit_grow,
149 resize: audit_resize, free: audit_free)
150
151node: type = struct
152 value: i32
153end node
154
155node_1: node = (value: 1)
156node_2: node = (value: 2)
157node_3: node = (value: 3)
158node_4: node = (value: 4)
159node_5: node = (value: 5)
160node_6: node = (value: 6)
161node_7: node = (value: 7)
162node_8: node = (value: 8)
163node_9: node = (value: 9)
164
165public main: () -> (code: i32) =
166 code = 1
167 mut observed := new_audit()
168 observer: audit = (state: addr observed)
169 mut trace: i32 = 0
170
171 maximum: usize = 18446744073709551615
172 one: usize = 1
173 mut refused: bool = false
174 impossible: allocation = mem.allocate(observer, maximum, one)
175 else (problem)
176 _ = problem
177 refused = true
178 no_allocation
179 end
180 if refused and is_absent(impossible) and observed.allocations == 0 then
181 trace = trace + 1
182 end if
183
184 first_size: usize = 17
185 first_alignment: usize = 24
186 second_size: usize = 31
187 second_alignment: usize = 64
188 first: ptr mut u8 = mem.allocate
189 (observer, first_size, first_alignment) else (problem)
190 _ = problem
191 return
192 end
193 second: ptr mut u8 = mem.allocate
194 (observer, second_size, second_alignment) else (problem)
195 _ = problem
196 return
197 end
198
199 first_last: ptr mut u8 = ptr(usize(first) + first_size - 1)
200 second_last: ptr mut u8 = ptr(usize(second) + second_size - 1)
201 first.val = 11
202 first_last.val = 17
203 second.val = 23
204 second_last.val = 31
205 if usize(first) <> 0 and usize(second) <> 0
206 and usize(first) <> usize(second)
207 and usize(first) % first_alignment == 0
208 and usize(second) % second_alignment == 0
209 and first.val == 11 and first_last.val == 17
210 and second.val == 23 and second_last.val == 31
211 and observed.live == 2 and observed.peak_live == 2
212 and observed.coexistence and not observed.mismatch
213 then
214 trace = trace + 2
215 end if
216 mem.release(observer, first, first_size)
217 mem.release(observer, second, second_size)
218
219 empty_size: usize = 0
220 empty_alignment: usize = 0
221 empty: ptr mut u8 = mem.allocate
222 (observer, empty_size, empty_alignment) else (problem)
223 _ = problem
224 return
225 end
226 another_empty: ptr mut u8 = mem.allocate
227 (observer, empty_size, one) else (problem)
228 _ = problem
229 return
230 end
231 if usize(empty) <> 0 and usize(another_empty) <> 0
232 and usize(empty) <> usize(another_empty) and observed.live == 2
233 then
234 trace = trace + 4
235 end if
236 mem.release(observer, empty, empty_size)
237 mem.release(observer, another_empty, empty_size)
238 if observed.live == 0 and observed.allocations == 4
239 and observed.frees == 4 and not observed.first_live
240 and not observed.second_live and not observed.mismatch
241 and is_absent(observed.first_block)
242 and is_absent(observed.second_block)
243 then
244 trace = trace + 8
245 end if
246
247 mut growth := new_audit()
248 grower: audit = (state: addr growth)
249 mut values := vec.new(item: ptr node)
250 vec.push(values, grower, addr node_1) else none
251 vec.push(values, grower, addr node_2) else none
252 vec.push(values, grower, addr node_3) else none
253 vec.push(values, grower, addr node_4) else none
254 vec.push(values, grower, addr node_5) else none
255 vec.push(values, grower, addr node_6) else none
256 vec.push(values, grower, addr node_7) else none
257 vec.push(values, grower, addr node_8) else none
258 vec.push(values, grower, addr node_9) else none
259
260 at_first: usize = 0
261 at_eighth: usize = 7
262 at_ninth: usize = 8
263 kept_first: ptr node = vec.get(values, at_first) else addr node_9
264 kept_eighth: ptr node = vec.get(values, at_eighth) else addr node_9
265 kept_ninth: ptr node = vec.get(values, at_ninth) else addr node_1
266 if kept_first.val.value == 1 and kept_eighth.val.value == 8
267 and kept_ninth.val.value == 9 and vec.length(values) == 9
268 and vec.capacity(values) == 16 and growth.allocations == 2
269 and growth.frees == 1 and growth.live == 1
270 and growth.peak_live == 2 and growth.coexistence
271 and growth.first_alloc_size == 8 * sizeof ptr node
272 and growth.second_alloc_size == 16 * sizeof ptr node
273 and growth.first_free_size == 8 * sizeof ptr node
274 and not growth.mismatch
275 then
276 trace = trace + 16
277 end if
278 vec.release(values, grower)
279
280 if vec.length(values) == 0 and vec.capacity(values) == 0
281 and growth.allocations == 2 and growth.frees == 2
282 and growth.live == 0 and not growth.first_live
283 and not growth.second_live
284 and is_absent(growth.first_block) and is_absent(growth.second_block)
285 and growth.second_free_size == 16 * sizeof ptr node
286 and not growth.mismatch
287 then
288 trace = trace + 32
289 end if
290
291 if trace == 63 then
292 code = 42
293 else
294 code = trace
295 end if
296end main