GCC Code Coverage Report


Directory: ../src/
File: heap.c
Date: 2026-07-02 10:10:49
Coverage Exec Excl Total
Lines: 95.5% 827 0 866
Functions: 95.5% 84 0 88
Branches: 84.6% 313 0 370

Line Branch Exec Source
1 /*
2 Copyright 2018, 2020, 2022 - 2026 Joel Svensson svenssonjoel@yahoo.se
3 2022 Benjamin Vedder
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdint.h>
22 #include <stdarg.h>
23 #include <inttypes.h>
24 #include <lbm_memory.h>
25 #include <lbm_custom_type.h>
26 #include <lbm_defrag_mem.h>
27 #include <lbm_image.h>
28
29 #include "heap.h"
30 #include "symrepr.h"
31 #include "stack.h"
32 #include "lbm_channel.h"
33 #include "platform_mutex.h"
34 #include "eval_cps.h"
35 #ifdef VISUALIZE_HEAP
36 #include "heap_vis.h"
37 #endif
38
39 176046999 static inline lbm_value lbm_set_gc_mark(lbm_value x) {
40 176046999 return x | LBM_GC_MARKED;
41 }
42 175450949 static inline lbm_value lbm_clr_gc_mark(lbm_value x) {
43 175450949 return x & ~LBM_GC_MASK;
44 }
45
46 3994659067 static inline bool lbm_get_gc_mark(lbm_value x) {
47 3994659067 return x & LBM_GC_MASK;
48 }
49
50 24090 static inline void gc_mark(lbm_value c) {
51 //c must be a cons cell.
52 24090 lbm_cons_t *cell = lbm_ref_cell(c);
53 24090 cell->cdr = lbm_set_gc_mark(cell->cdr);
54 24090 }
55
56 24429 static inline bool gc_marked(lbm_value c) {
57 24429 lbm_cons_t *cell = lbm_ref_cell(c);
58 24429 return lbm_get_gc_mark(cell->cdr);
59 }
60
61 __attribute__((unused)) static inline void gc_clear_mark(lbm_value c) {
62 //c must be a cons cell.
63 lbm_cons_t *cell = lbm_ref_cell(c);
64 cell->cdr = lbm_clr_gc_mark(cell->cdr);
65 }
66
67 // flag is the same bit as mark, but in car
68 46692 static inline bool lbm_get_gc_flag(lbm_value x) {
69 46692 return x & LBM_GC_MARKED;
70 }
71
72 23346 static inline lbm_value lbm_set_gc_flag(lbm_value x) {
73 23346 return x | LBM_GC_MARKED;
74 }
75
76 23346 static inline lbm_value lbm_clr_gc_flag(lbm_value x) {
77 23346 return x & ~LBM_GC_MASK;
78 }
79
80
81 lbm_heap_state_t lbm_heap_state;
82
83 lbm_const_heap_t *lbm_const_heap_state;
84
85 lbm_cons_t *lbm_heaps[2] = {NULL, NULL};
86
87 static lbm_mutex_t lbm_const_heap_mutex;
88 static bool lbm_const_heap_mutex_initialized = false;
89
90 static lbm_mutex_t lbm_mark_mutex;
91 static bool lbm_mark_mutex_initialized = false;
92
93 #ifdef USE_GC_PTR_REV
94 void lbm_gc_lock(void) {
95 lbm_mutex_lock(&lbm_mark_mutex);
96 }
97 void lbm_gc_unlock(void) {
98 lbm_mutex_unlock(&lbm_mark_mutex);
99 }
100 #else
101 4 void lbm_gc_lock(void) {
102 4 }
103 4 void lbm_gc_unlock(void) {
104 4 }
105 #endif
106
107 // ////////////////////////////////////////////////////////////
108 // ENCODERS DECODERS
109 //
110
111 8510993 lbm_value lbm_enc_i32(int32_t x) {
112 #ifndef LBM64
113 5675892 lbm_value i = lbm_cons((lbm_uint)x, ENC_SYM_RAW_I_TYPE);
114
2/2
✓ Branch 1 taken 1236 times.
✓ Branch 2 taken 5674656 times.
5675892 if (lbm_type_of(i) == LBM_TYPE_SYMBOL) return i;
115 5674656 return lbm_set_ptr_type(i, LBM_TYPE_I32);
116 #else
117 2835101 return (((lbm_uint)x) << LBM_VAL_SHIFT) | LBM_TYPE_I32;
118 #endif
119 }
120
121 10205174 lbm_value lbm_enc_u32(uint32_t x) {
122 #ifndef LBM64
123 7366853 lbm_value u = lbm_cons(x, ENC_SYM_RAW_U_TYPE);
124
2/2
✓ Branch 1 taken 2176 times.
✓ Branch 2 taken 7364677 times.
7366853 if (lbm_type_of(u) == LBM_TYPE_SYMBOL) return u;
125 7364677 return lbm_set_ptr_type(u, LBM_TYPE_U32);
126 #else
127 2838321 return (((lbm_uint)x) << LBM_VAL_SHIFT) | LBM_TYPE_U32;
128 #endif
129 }
130
131 // ////////////////////////////////////////////////////////////
132 // Destructively update a value on 32bit systems.
133 // identiaval to enc_u32 on 64bit.
134 //
135 // The caller is responsible for making sure that the v input
136 // is of the correct type attempting set.
137 // using lbm_set_x on the wrong type WILL CRASH!
138 ✗ lbm_value lbm_set_u32(lbm_value v, uint32_t x) {
139 #ifndef LBM64
140 ✗ lbm_ref_cell(v)->car = x;
141 ✗ return v;
142 #else
143 (void) v;
144 ✗ return (((lbm_uint)x) << LBM_VAL_SHIFT) | LBM_TYPE_U32;
145 #endif
146 }
147
148 ✗ lbm_value lbm_set_i32(lbm_value v, int32_t x) {
149 #ifndef LBM64
150 ✗ lbm_ref_cell(v)->car = (uint32_t)x;
151 ✗ return v;
152 #else
153 (void) v;
154 ✗ return (((lbm_uint)x) << LBM_VAL_SHIFT) | LBM_TYPE_U32;
155 #endif
156 }
157
158 ✗ lbm_value lbm_set_float(lbm_value v, float x) {
159 #ifndef LBM64
160 lbm_uint t;
161 ✗ memcpy(&t, &x, sizeof(lbm_float));
162 ✗ lbm_ref_cell(v)->car = t;
163 ✗ return v;
164 #else
165 (void) v;
166 ✗ lbm_uint t = 0;
167 ✗ memcpy(&t, &x, sizeof(float));
168 ✗ return (((lbm_uint)t) << LBM_VAL_SHIFT) | LBM_TYPE_FLOAT;
169 #endif
170 }
171 // ////////////////////////////////////////////////////////////
172
173 689407270 lbm_value lbm_enc_float(float x) {
174 #ifndef LBM64
175 lbm_uint t;
176 459788099 memcpy(&t, &x, sizeof(lbm_float));
177 459788099 lbm_value f = lbm_cons(t, ENC_SYM_RAW_F_TYPE);
178
2/2
✓ Branch 1 taken 294488 times.
✓ Branch 2 taken 459493611 times.
459788099 if (lbm_type_of(f) == LBM_TYPE_SYMBOL) return f;
179 459493611 return lbm_set_ptr_type(f, LBM_TYPE_FLOAT);
180 #else
181 229619171 lbm_uint t = 0;
182 229619171 memcpy(&t, &x, sizeof(float));
183 229619171 return (((lbm_uint)t) << LBM_VAL_SHIFT) | LBM_TYPE_FLOAT;
184 #endif
185 }
186
187 #ifndef LBM64
188 16856753 static lbm_value enc_64_on_32(uint8_t *source, lbm_uint type_qual, lbm_uint type) {
189 16856753 lbm_value res = lbm_cons(ENC_SYM_NIL,ENC_SYM_NIL);
190
2/2
✓ Branch 1 taken 16852381 times.
✓ Branch 2 taken 4372 times.
16856753 if (lbm_type_of(res) != LBM_TYPE_SYMBOL) {
191 16852381 uint8_t* storage = lbm_malloc(sizeof(uint64_t));
192
2/2
✓ Branch 0 taken 16847125 times.
✓ Branch 1 taken 5256 times.
16852381 if (storage) {
193 16847125 memcpy(storage,source, sizeof(uint64_t));
194 16847125 lbm_set_car_and_cdr(res, (lbm_uint)storage, type_qual);
195 16847125 res = lbm_set_ptr_type(res, type);
196 } else {
197 5256 res = ENC_SYM_MERROR;
198 }
199 }
200 16856753 return res;
201 }
202 #endif
203
204 13472630 lbm_value lbm_enc_i64(int64_t x) {
205 #ifndef LBM64
206 8983246 return enc_64_on_32((uint8_t *)&x, ENC_SYM_IND_I_TYPE, LBM_TYPE_I64);
207 #else
208 4489384 lbm_value u = lbm_cons((uint64_t)x, ENC_SYM_RAW_I_TYPE);
209
2/2
✓ Branch 1 taken 1232 times.
✓ Branch 2 taken 4488152 times.
4489384 if (lbm_type_of(u) == LBM_TYPE_SYMBOL) return u;
210 4488152 return lbm_set_ptr_type(u, LBM_TYPE_I64);
211 #endif
212 }
213
214 10110284 lbm_value lbm_enc_u64(uint64_t x) {
215 #ifndef LBM64
216 6741536 return enc_64_on_32((uint8_t *)&x, ENC_SYM_IND_U_TYPE, LBM_TYPE_U64);
217 #else
218 3368748 lbm_value u = lbm_cons(x, ENC_SYM_RAW_U_TYPE);
219
2/2
✓ Branch 1 taken 876 times.
✓ Branch 2 taken 3367872 times.
3368748 if (lbm_type_of(u) == LBM_TYPE_SYMBOL) return u;
220 3367872 return lbm_set_ptr_type(u, LBM_TYPE_U64);
221 #endif
222 }
223
224 1697539 lbm_value lbm_enc_double(double x) {
225 #ifndef LBM64
226 1131971 return enc_64_on_32((uint8_t *)&x, ENC_SYM_IND_F_TYPE, LBM_TYPE_DOUBLE);
227 #else
228 lbm_uint t;
229 565568 memcpy(&t, &x, sizeof(double));
230 565568 lbm_value f = lbm_cons(t, ENC_SYM_RAW_F_TYPE);
231
2/2
✓ Branch 1 taken 332 times.
✓ Branch 2 taken 565236 times.
565568 if (lbm_type_of(f) == LBM_TYPE_SYMBOL) return f;
232 565236 return lbm_set_ptr_type(f, LBM_TYPE_DOUBLE);
233 #endif
234 }
235
236 // Type specific (as opposed to the dec_as_X) functions
237 // should only be run on values KNOWN to represent a value of the type
238 // that the decoder decodes.
239
240 1000359483 float lbm_dec_float(lbm_value x) {
241 #ifndef LBM64
242 float f_tmp;
243 667125689 lbm_uint tmp = lbm_car(x);
244 667125689 memcpy(&f_tmp, &tmp, sizeof(float));
245 667125689 return f_tmp;
246 #else
247 333233794 uint32_t tmp = (uint32_t)(x >> LBM_VAL_SHIFT);
248 float f_tmp;
249 333233794 memcpy(&f_tmp, &tmp, sizeof(float));
250 333233794 return f_tmp;
251 #endif
252 }
253
254 1694806 double lbm_dec_double(lbm_value x) {
255 #ifndef LBM64
256 1130162 double d = 0.0;
257
1/2
✓ Branch 1 taken 1130162 times.
✗ Branch 2 not taken.
1130162 if (lbm_is_ptr(x)) {
258 1130162 uint32_t *data = (uint32_t*)lbm_ref_cell(x)->car;
259 1130162 memcpy(&d, data, sizeof(double));
260 }
261 1130162 return d;
262 #else
263 double f_tmp;
264 564644 lbm_uint tmp = lbm_car(x);
265 564644 memcpy(&f_tmp, &tmp, sizeof(double));
266 564644 return f_tmp;
267 #endif
268 }
269
270 21035053 uint64_t lbm_dec_u64(lbm_value x) {
271 #ifndef LBM64
272 14025265 uint64_t u = 0;
273
1/2
✓ Branch 1 taken 14025265 times.
✗ Branch 2 not taken.
14025265 if (lbm_is_ptr(x)) {
274 14025265 uint32_t *data = (uint32_t*)lbm_ref_cell(x)->car;
275 14025265 memcpy(&u, data, 8);
276 }
277 14025265 return u;
278 #else
279 7009788 return (uint64_t)lbm_car(x);
280 #endif
281 }
282
283 27756148 int64_t lbm_dec_i64(lbm_value x) {
284 #ifndef LBM64
285 18506144 int64_t i = 0;
286
1/2
✓ Branch 1 taken 18506144 times.
✗ Branch 2 not taken.
18506144 if (lbm_is_ptr(x)) {
287 18506144 uint32_t *data = (uint32_t*)lbm_ref_cell(x)->car;
288 18506144 memcpy(&i, data, 8);
289 }
290 18506144 return i;
291 #else
292 9250004 return (int64_t)lbm_car(x);
293 #endif
294 }
295
296 1459716 char *lbm_dec_str(lbm_value val) {
297 1459716 char *res = 0;
298
2/2
✓ Branch 1 taken 1459288 times.
✓ Branch 2 taken 428 times.
1459716 if (lbm_is_array_r(val)) {
299 1459288 lbm_array_header_t *array = (lbm_array_header_t *)lbm_car(val);
300
1/2
✓ Branch 0 taken 1459288 times.
✗ Branch 1 not taken.
1459288 if (array) {
301 1459288 res = (char *)array->data;
302 }
303 }
304 1459716 return res;
305 }
306
307 1804863 lbm_array_header_t *lbm_dec_array_r(lbm_value val) {
308 1804863 lbm_array_header_t *array = NULL;
309
2/2
✓ Branch 1 taken 1800788 times.
✓ Branch 2 taken 4075 times.
1804863 if (lbm_is_array_r(val)) {
310 1800788 array = (lbm_array_header_t *)lbm_car(val);
311 }
312 1804863 return array;
313 }
314
315 179356 lbm_array_header_t *lbm_dec_array_rw(lbm_value val) {
316 179356 lbm_array_header_t *array = NULL;
317
2/2
✓ Branch 1 taken 179102 times.
✓ Branch 2 taken 254 times.
179356 if (lbm_is_array_rw(val)) {
318 179102 array = (lbm_array_header_t *)lbm_car(val);
319 }
320 179356 return array;
321 }
322
323 36448577 lbm_char_channel_t *lbm_dec_channel(lbm_value val) {
324 36448577 lbm_char_channel_t *res = NULL;
325
326
1/2
✓ Branch 1 taken 36448577 times.
✗ Branch 2 not taken.
36448577 if (lbm_type_of(val) == LBM_TYPE_CHANNEL) {
327 36448577 res = (lbm_char_channel_t *)lbm_car(val);
328 }
329 36448577 return res;
330 }
331
332 5007 lbm_uint lbm_dec_custom(lbm_value val) {
333 5007 lbm_uint res = 0;
334
1/2
✓ Branch 1 taken 5007 times.
✗ Branch 2 not taken.
5007 if (lbm_type_of(val) == LBM_TYPE_CUSTOM) {
335 5007 res = (lbm_uint)lbm_car(val);
336 }
337 5007 return res;
338 }
339
340 182988 uint8_t lbm_dec_as_char(lbm_value a) {
341 182988 uint8_t r = 0;
342
10/10
✓ Branch 1 taken 182277 times.
✓ Branch 2 taken 108 times.
✓ Branch 3 taken 86 times.
✓ Branch 4 taken 85 times.
✓ Branch 5 taken 85 times.
✓ Branch 6 taken 85 times.
✓ Branch 7 taken 85 times.
✓ Branch 8 taken 85 times.
✓ Branch 9 taken 85 times.
✓ Branch 10 taken 7 times.
182988 switch (lbm_type_of_functional(a)) {
343 182277 case LBM_TYPE_CHAR:
344 182277 r = (uint8_t)lbm_dec_char(a); break;
345 108 case LBM_TYPE_I:
346 108 r = (uint8_t)lbm_dec_i(a); break;
347 86 case LBM_TYPE_U:
348 86 r = (uint8_t)lbm_dec_u(a); break;
349 85 case LBM_TYPE_I32:
350 85 r = (uint8_t)lbm_dec_i32(a); break;
351 85 case LBM_TYPE_U32:
352 85 r = (uint8_t)lbm_dec_u32(a); break;
353 85 case LBM_TYPE_FLOAT:
354 85 r = (uint8_t)lbm_dec_float(a); break;
355 85 case LBM_TYPE_I64:
356 85 r = (uint8_t)lbm_dec_i64(a); break;
357 85 case LBM_TYPE_U64:
358 85 r = (uint8_t)lbm_dec_u64(a); break;
359 85 case LBM_TYPE_DOUBLE:
360 85 r = (uint8_t) lbm_dec_double(a); break;
361 }
362 182988 return r;
363 }
364
365 19150110 uint32_t lbm_dec_as_u32(lbm_value a) {
366 19150110 uint32_t r = 0;
367
9/9
✓ Branch 1 taken 1405424 times.
✓ Branch 2 taken 4179067 times.
✓ Branch 3 taken 297567 times.
✓ Branch 4 taken 13267534 times.
✓ Branch 5 taken 88 times.
✓ Branch 6 taken 85 times.
✓ Branch 7 taken 253 times.
✓ Branch 8 taken 85 times.
✓ Branch 9 taken 7 times.
19150110 switch (lbm_type_of_functional(a)) {
368 1405424 case LBM_TYPE_CHAR:
369 1405424 r = (uint32_t)lbm_dec_char(a); break;
370 4179067 case LBM_TYPE_I:
371 4179067 r = (uint32_t)lbm_dec_i(a); break;
372 297567 case LBM_TYPE_U:
373 297567 r = (uint32_t)lbm_dec_u(a); break;
374 13267534 case LBM_TYPE_I32: /* fall through */
375 case LBM_TYPE_U32:
376 13267534 r = (uint32_t)lbm_dec_u32(a); break;
377 88 case LBM_TYPE_FLOAT:
378 88 r = (uint32_t)lbm_dec_float(a); break;
379 85 case LBM_TYPE_I64:
380 85 r = (uint32_t)lbm_dec_i64(a); break;
381 253 case LBM_TYPE_U64:
382 253 r = (uint32_t)lbm_dec_u64(a); break;
383 85 case LBM_TYPE_DOUBLE:
384 85 r = (uint32_t)lbm_dec_double(a); break;
385 }
386 19150110 return r;
387 }
388
389 505519239 int32_t lbm_dec_as_i32(lbm_value a) {
390 505519239 int32_t r = 0;
391
10/10
✓ Branch 1 taken 13846531 times.
✓ Branch 2 taken 480631117 times.
✓ Branch 3 taken 24422 times.
✓ Branch 4 taken 11014395 times.
✓ Branch 5 taken 87 times.
✓ Branch 6 taken 85 times.
✓ Branch 7 taken 169 times.
✓ Branch 8 taken 169 times.
✓ Branch 9 taken 85 times.
✓ Branch 10 taken 2179 times.
505519239 switch (lbm_type_of_functional(a)) {
392 13846531 case LBM_TYPE_CHAR:
393 13846531 r = (int32_t)lbm_dec_char(a); break;
394 480631117 case LBM_TYPE_I:
395 480631117 r = (int32_t)lbm_dec_i(a); break;
396 24422 case LBM_TYPE_U:
397 24422 r = (int32_t)lbm_dec_u(a); break;
398 11014395 case LBM_TYPE_I32:
399 11014395 r = (int32_t)lbm_dec_i32(a); break;
400 87 case LBM_TYPE_U32:
401 87 r = (int32_t)lbm_dec_u32(a); break;
402 85 case LBM_TYPE_FLOAT:
403 85 r = (int32_t)lbm_dec_float(a); break;
404 169 case LBM_TYPE_I64:
405 169 r = (int32_t)lbm_dec_i64(a); break;
406 169 case LBM_TYPE_U64:
407 169 r = (int32_t)lbm_dec_u64(a); break;
408 85 case LBM_TYPE_DOUBLE:
409 85 r = (int32_t) lbm_dec_double(a); break;
410 }
411 505519239 return r;
412 }
413
414 253667332 int64_t lbm_dec_as_i64(lbm_value a) {
415 253667332 int64_t r = 0;
416
10/10
✓ Branch 1 taken 7813529 times.
✓ Branch 2 taken 231559212 times.
✓ Branch 3 taken 506 times.
✓ Branch 4 taken 841 times.
✓ Branch 5 taken 1793 times.
✓ Branch 6 taken 181 times.
✓ Branch 7 taken 14290749 times.
✓ Branch 8 taken 337 times.
✓ Branch 9 taken 169 times.
✓ Branch 10 taken 15 times.
253667332 switch (lbm_type_of_functional(a)) {
417 7813529 case LBM_TYPE_CHAR:
418 7813529 r = (int64_t)lbm_dec_char(a); break;
419 231559212 case LBM_TYPE_I:
420 231559212 r = (int64_t)lbm_dec_i(a); break;
421 506 case LBM_TYPE_U:
422 506 r = (int64_t)lbm_dec_u(a); break;
423 841 case LBM_TYPE_I32:
424 841 r = (int64_t)lbm_dec_i32(a); break;
425 1793 case LBM_TYPE_U32:
426 1793 r = (int64_t)lbm_dec_u32(a); break;
427 181 case LBM_TYPE_FLOAT:
428 181 r = (int64_t)lbm_dec_float(a); break;
429 14290749 case LBM_TYPE_I64:
430 14290749 r = (int64_t)lbm_dec_i64(a); break;
431 337 case LBM_TYPE_U64:
432 337 r = (int64_t)lbm_dec_u64(a); break;
433 169 case LBM_TYPE_DOUBLE:
434 169 r = (int64_t) lbm_dec_double(a); break;
435 }
436 253667332 return r;
437 }
438
439 13559144 uint64_t lbm_dec_as_u64(lbm_value a) {
440 13559144 uint64_t r = 0;
441
10/10
✓ Branch 1 taken 1687425 times.
✓ Branch 2 taken 919297 times.
✓ Branch 3 taken 19410 times.
✓ Branch 4 taken 673 times.
✓ Branch 5 taken 673 times.
✓ Branch 6 taken 169 times.
✓ Branch 7 taken 505 times.
✓ Branch 8 taken 10930816 times.
✓ Branch 9 taken 169 times.
✓ Branch 10 taken 7 times.
13559144 switch (lbm_type_of_functional(a)) {
442 1687425 case LBM_TYPE_CHAR:
443 1687425 r = (uint64_t)lbm_dec_char(a); break;
444 919297 case LBM_TYPE_I:
445 919297 r = (uint64_t)lbm_dec_i(a); break;
446 19410 case LBM_TYPE_U:
447 19410 r = (uint64_t)lbm_dec_u(a); break;
448 673 case LBM_TYPE_I32:
449 673 r = (uint64_t)lbm_dec_i32(a); break;
450 673 case LBM_TYPE_U32:
451 673 r = (uint64_t)lbm_dec_u32(a); break;
452 169 case LBM_TYPE_FLOAT:
453 169 r = (uint64_t)lbm_dec_float(a); break;
454 505 case LBM_TYPE_I64:
455 505 r = (uint64_t)lbm_dec_i64(a); break;
456 10930816 case LBM_TYPE_U64:
457 10930816 r = (uint64_t)lbm_dec_u64(a); break;
458 169 case LBM_TYPE_DOUBLE:
459 169 r = (uint64_t)lbm_dec_double(a); break;
460 }
461 13559144 return r;
462 }
463
464 1126211660 float lbm_dec_as_float(lbm_value a) {
465 1126211660 float r = 0;
466
10/10
✓ Branch 1 taken 311097780 times.
✓ Branch 2 taken 504152199 times.
✓ Branch 3 taken 478 times.
✓ Branch 4 taken 421 times.
✓ Branch 5 taken 534 times.
✓ Branch 6 taken 310959210 times.
✓ Branch 7 taken 421 times.
✓ Branch 8 taken 421 times.
✓ Branch 9 taken 85 times.
✓ Branch 10 taken 111 times.
1126211660 switch (lbm_type_of_functional(a)) {
467 311097780 case LBM_TYPE_CHAR:
468 311097780 r = (float)lbm_dec_char(a); break;
469 504152199 case LBM_TYPE_I:
470 504152199 r = (float)lbm_dec_i(a); break;
471 478 case LBM_TYPE_U:
472 478 r = (float)lbm_dec_u(a); break;
473 421 case LBM_TYPE_I32:
474 421 r = (float)lbm_dec_i32(a); break;
475 534 case LBM_TYPE_U32:
476 534 r = (float)lbm_dec_u32(a); break;
477 310959210 case LBM_TYPE_FLOAT:
478 310959210 r = (float)lbm_dec_float(a); break;
479 421 case LBM_TYPE_I64:
480 421 r = (float)lbm_dec_i64(a); break;
481 421 case LBM_TYPE_U64:
482 421 r = (float)lbm_dec_u64(a); break;
483 85 case LBM_TYPE_DOUBLE:
484 85 r = (float)lbm_dec_double(a); break;
485 }
486 1126211660 return r;
487 }
488
489 1692305 double lbm_dec_as_double(lbm_value a) {
490 1692305 double r = 0;
491
10/10
✓ Branch 1 taken 843314 times.
✓ Branch 2 taken 842439 times.
✓ Branch 3 taken 422 times.
✓ Branch 4 taken 421 times.
✓ Branch 5 taken 421 times.
✓ Branch 6 taken 1116 times.
✓ Branch 7 taken 421 times.
✓ Branch 8 taken 421 times.
✓ Branch 9 taken 3323 times.
✓ Branch 10 taken 7 times.
1692305 switch (lbm_type_of_functional(a)) {
492 843314 case LBM_TYPE_CHAR:
493 843314 r = (double)lbm_dec_char(a); break;
494 842439 case LBM_TYPE_I:
495 842439 r = (double)lbm_dec_i(a); break;
496 422 case LBM_TYPE_U:
497 422 r = (double)lbm_dec_u(a); break;
498 421 case LBM_TYPE_I32:
499 421 r = (double)lbm_dec_i32(a); break;
500 421 case LBM_TYPE_U32:
501 421 r = (double)lbm_dec_u32(a); break;
502 1116 case LBM_TYPE_FLOAT:
503 1116 r = (double)lbm_dec_float(a); break;
504 421 case LBM_TYPE_I64:
505 421 r = (double)lbm_dec_i64(a); break;
506 421 case LBM_TYPE_U64:
507 421 r = (double)lbm_dec_u64(a); break;
508 3323 case LBM_TYPE_DOUBLE:
509 3323 r = (double)lbm_dec_double(a); break;
510 }
511 1692305 return r;
512 }
513
514 // ////////////////////////////////////////////////////////////
515 // HEAP MANAGEMENT
516
517 66578 static bool generate_freelist(size_t num_cells) {
518 66578 size_t i = 0;
519
520
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66578 times.
66578 if (!lbm_heap_state.heap) return false;
521
522 66578 lbm_heap_state.freelist = lbm_enc_cons_ptr(0);
523
524 lbm_cons_t *t;
525
526 // Add all cells to free list
527
2/2
✓ Branch 0 taken 615162862 times.
✓ Branch 1 taken 66578 times.
615229440 for (i = 1; i < num_cells; i ++) {
528 615162862 t = lbm_ref_cell(lbm_enc_cons_ptr(i-1));
529 615162862 t->car = ENC_SYM_RECOVERED; // all cars in free list are "RECOVERED"
530 615162862 t->cdr = lbm_enc_cons_ptr(i);
531 }
532
533 // Replace the incorrect pointer at the last cell.
534 66578 t = lbm_ref_cell(lbm_enc_cons_ptr(num_cells-1));
535 66578 t->car = ENC_SYM_RECOVERED;
536 66578 t->cdr = ENC_SYM_NIL;
537
538 66578 return true;
539 }
540
541 1606407 void lbm_nil_freelist(void) {
542 1606407 lbm_heap_state.freelist = ENC_SYM_NIL;
543 1606407 lbm_heap_state.num_free = 0;
544 1606407 }
545
546 66578 static void heap_init_state(lbm_cons_t *addr, lbm_uint num_cells,
547 lbm_uint* gc_stack_storage, lbm_uint gc_stack_size) {
548 66578 lbm_heap_state.heap = addr;
549 66578 lbm_heap_state.heap_bytes = (unsigned int)(num_cells * sizeof(lbm_cons_t));
550 66578 lbm_heap_state.heap_size = num_cells;
551
552 66578 lbm_stack_create(&lbm_heap_state.gc_stack, gc_stack_storage, gc_stack_size);
553
554 66578 lbm_heap_state.num_free = num_cells;
555 66578 lbm_heap_state.num_alloc_arrays = 0;
556 66578 lbm_heap_state.gc_num = 0;
557 66578 lbm_heap_state.gc_marked = 0;
558 66578 lbm_heap_state.gc_recovered = 0;
559 66578 lbm_heap_state.gc_recovered_arrays = 0;
560 66578 lbm_heap_state.gc_least_free = num_cells;
561 66578 lbm_heap_state.gc_last_free = num_cells;
562 66578 }
563
564 1606406 void lbm_heap_new_freelist_length(void) {
565 1606406 lbm_heap_state.gc_last_free = lbm_heap_state.num_free;
566
2/2
✓ Branch 0 taken 12270 times.
✓ Branch 1 taken 1594136 times.
1606406 if (lbm_heap_state.num_free < lbm_heap_state.gc_least_free)
567 12270 lbm_heap_state.gc_least_free = lbm_heap_state.num_free;
568 1606406 }
569
570 66578 bool lbm_heap_init(lbm_cons_t *addr, lbm_uint num_cells,
571 lbm_uint gc_stack_size) {
572
573
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66578 times.
66578 if (((uintptr_t)addr % 8) != 0) return false;
574
575 66578 memset(addr,0, sizeof(lbm_cons_t) * num_cells);
576
577 66578 lbm_uint *gc_stack_storage = (lbm_uint*)lbm_malloc(gc_stack_size * sizeof(lbm_uint));
578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66578 times.
66578 if (gc_stack_storage == NULL) return 0;
579
580 66578 heap_init_state(addr, num_cells,
581 gc_stack_storage, gc_stack_size);
582
583 66578 lbm_heaps[LBM_RAM_HEAP] = addr;
584
585 66578 return generate_freelist(num_cells);
586 }
587
588
589 1414435771 lbm_value lbm_heap_allocate_cell(lbm_type ptr_type, lbm_value car, lbm_value cdr) {
590 lbm_value r;
591 1414435771 lbm_value cell = lbm_heap_state.freelist;
592
2/2
✓ Branch 0 taken 1414116629 times.
✓ Branch 1 taken 319142 times.
1414435771 if (cell) {
593 1414116629 lbm_uint heap_ix = lbm_dec_ptr(cell);
594 1414116629 lbm_heap_state.freelist = lbm_heap_state.heap[heap_ix].cdr;
595 1414116629 lbm_heap_state.num_free--;
596 1414116629 lbm_heap_state.heap[heap_ix].car = car;
597 1414116629 lbm_heap_state.heap[heap_ix].cdr = cdr;
598 1414116629 r = lbm_set_ptr_type(cell, ptr_type);
599 } else {
600 319142 r = ENC_SYM_MERROR;
601 }
602 1414435771 return r;
603 }
604
605 3768995 lbm_value lbm_heap_allocate_list(lbm_uint n) {
606
2/2
✓ Branch 0 taken 10036 times.
✓ Branch 1 taken 3758959 times.
3768995 if (n == 0) return ENC_SYM_NIL;
607
2/2
✓ Branch 1 taken 4656 times.
✓ Branch 2 taken 3754303 times.
3758959 if (lbm_heap_num_free() < n) return ENC_SYM_MERROR;
608 // Here the freelist is guaranteed to be a cons_cell.
609
610 3754303 lbm_value curr = lbm_heap_state.freelist;
611 3754303 lbm_value res = curr;
612
613 3754303 lbm_cons_t *c_cell = NULL;
614 3754303 lbm_uint count = 0;
615 do {
616 19398531 c_cell = lbm_ref_cell(curr);
617 19398531 c_cell->car = ENC_SYM_NIL;
618 19398531 curr = c_cell->cdr;
619 19398531 count ++;
620
2/2
✓ Branch 0 taken 15644228 times.
✓ Branch 1 taken 3754303 times.
19398531 } while (count < n);
621 3754303 lbm_heap_state.freelist = curr;
622 3754303 c_cell->cdr = ENC_SYM_NIL;
623 3754303 lbm_heap_state.num_free-=count;
624 3754303 return res;
625 }
626
627 65905073 lbm_value lbm_heap_allocate_list_init_va(unsigned int n, va_list valist) {
628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65905073 times.
65905073 if (n == 0) return ENC_SYM_NIL;
629
2/2
✓ Branch 1 taken 99567 times.
✓ Branch 2 taken 65805506 times.
65905073 if (lbm_heap_num_free() < n) return ENC_SYM_MERROR;
630
631 65805506 lbm_value curr = lbm_heap_state.freelist;
632 65805506 lbm_value res = curr;
633
634 65805506 lbm_cons_t *c_cell = NULL;
635 65805506 unsigned int count = 0;
636 do {
637 132462491 c_cell = lbm_ref_cell(curr);
638 132462491 c_cell->car = va_arg(valist, lbm_value);
639 132462491 curr = c_cell->cdr;
640 132462491 count ++;
641
2/2
✓ Branch 0 taken 66656985 times.
✓ Branch 1 taken 65805506 times.
132462491 } while (count < n);
642 65805506 lbm_heap_state.freelist = curr;
643 65805506 c_cell->cdr = ENC_SYM_NIL;
644 65805506 lbm_heap_state.num_free-=count;
645 65805506 return res;
646 }
647
648 65905073 lbm_value lbm_heap_allocate_list_init(unsigned int n, ...) {
649 va_list valist;
650 65905073 va_start(valist, n);
651 65905073 lbm_value r = lbm_heap_allocate_list_init_va(n, valist);
652 65905073 va_end(valist);
653 65905073 return r;
654 }
655
656 5 lbm_uint lbm_heap_num_allocated(void) {
657 5 return lbm_heap_state.heap_size - lbm_heap_state.num_free;
658 }
659 5 lbm_uint lbm_heap_size(void) {
660 5 return lbm_heap_state.heap_size;
661 }
662
663 4 lbm_uint lbm_heap_size_bytes(void) {
664 4 return lbm_heap_state.heap_bytes;
665 }
666
667 765 void lbm_get_heap_state(lbm_heap_state_t *res) {
668 765 *res = lbm_heap_state;
669 765 }
670
671 6 lbm_uint lbm_get_gc_stack_max(void) {
672 6 return lbm_get_max_stack(&lbm_heap_state.gc_stack);
673 }
674
675 5 lbm_uint lbm_get_gc_stack_size(void) {
676 5 return lbm_heap_state.gc_stack.size;
677 }
678
679 282324 static inline void value_assign(lbm_value *a, lbm_value b) {
680 282324 lbm_value a_old = *a & LBM_GC_MASK;
681 282324 *a = a_old | (b & ~LBM_GC_MASK);
682 282324 }
683
684 #ifdef LBM_USE_GC_PTR_REV
685 // ////////////////////////////////////////////////////////////
686 // Deutch-Schorr-Waite (DSW) pointer reversal GC
687 //
688 /*
689 Deutch-Schorr-Waite (DSW) pointer reversal GC for 2-ptr cells
690 with a hack-solution for the lisp-array case (n-ptr cells).
691
692 DSW visits each branch node 3 times compared to 2 times for
693 the stack based recursive mark.
694 Where the stack based recursive mark performs a stack push/pop,
695 DSW rearranges the, current, prev, next and a ptr field on
696 the heap.
697
698 DSW changes the structure of the heap and it introduces an
699 invalid pointer (LBM_PTR_NULL) temporarily during marking.
700 Since the heap will be "messed up" while marking, a mutex
701 is introuded to keep other processes out of the heap while
702 marking.
703 */
704
705 static int do_nothing(lbm_value v, bool shared, void *arg) {
706 (void) v;
707 (void) shared;
708 (void) arg;
709 return TRAV_FUN_SUBTREE_CONTINUE;
710 }
711
712 void lbm_gc_mark_phase(lbm_value root) {
713 lbm_mutex_lock(&lbm_const_heap_mutex);
714 lbm_ptr_rev_trav(do_nothing, root, NULL);
715 lbm_mutex_unlock(&lbm_const_heap_mutex);
716 }
717
718 #else
719 // ////////////////////////////////////////////////////////////
720 // Check if a value is currently on the stack
721
722 66822 bool active_ptr(lbm_value p) {
723 66822 lbm_stack_t *s = &lbm_heap_state.gc_stack;
724 66822 bool r = false;
725
2/2
✓ Branch 0 taken 330 times.
✓ Branch 1 taken 66822 times.
67152 for (lbm_uint i = 0; i < s->sp; i ++) {
726
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 330 times.
330 if (p == s->data[i]) {
727 ✗ r = true;
728 ✗ break;
729 }
730 }
731 66822 return r;
732 }
733
734 // ////////////////////////////////////////////////////////////
735 // Explicit stack "recursive" mark phase
736
737 // Trees are marked in a left subtree before rigth subtree, car first then cdr,
738 // way to favor lisp lists. This means that stack will grow slowly when
739 // marking right-leaning (cdr-recursive) data-structures while left-leaning
740 // (car-recursive) structures uses a lot of stack.
741
742 // Lisp arrays contain an extra book-keeping field to keep track
743 // of how far into the array the marking process has gone.
744
745 // TODO: DSW should be used as a last-resort if the GC stack is exhausted.
746 // If we use DSW as last-resort can we get away with a way smaller
747 // GC stack and unchanged performance (on sensible programs)?
748
749 25381260 void lbm_gc_mark_phase(lbm_value root) {
750 lbm_value t_ptr;
751 25381260 lbm_stack_t *s = &lbm_heap_state.gc_stack;
752 25381260 s->data[s->sp++] = root;
753
754
2/2
✓ Branch 1 taken 123452152 times.
✓ Branch 2 taken 25381259 times.
148833411 while (!lbm_stack_is_empty(s)) {
755 lbm_value curr;
756 123452152 lbm_pop(s, &curr);
757
758 277204443 mark_shortcut:
759
760
2/2
✓ Branch 1 taken 168477735 times.
✓ Branch 2 taken 108726708 times.
277204443 if (!lbm_is_ptr(curr) ||
761
2/2
✓ Branch 0 taken 319 times.
✓ Branch 1 taken 168477416 times.
168477735 (curr & LBM_PTR_TO_CONSTANT_BIT)) {
762 119148226 continue;
763 }
764
765 168477416 lbm_cons_t *cell = &lbm_heap_state.heap[lbm_dec_ptr(curr)];
766
767
2/2
✓ Branch 1 taken 10357103 times.
✓ Branch 2 taken 158120313 times.
168477416 if (lbm_get_gc_mark(cell->cdr)) {
768 10357103 continue;
769 }
770
771 158120313 t_ptr = lbm_type_of(curr);
772
773 // An array is marked in O(N) time using an additional 32bit
774 // value per array that keeps track of how far into the array GC
775 // has progressed.
776
2/2
✓ Branch 0 taken 66822 times.
✓ Branch 1 taken 158053491 times.
158120313 if (t_ptr == LBM_TYPE_LISPARRAY) {
777 // O(stack_size) cost when seeing array. This protects
778 // against gc recursing into arrays via cycles.
779
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 66822 times.
66822 if (active_ptr(curr)) {
780 ✗ continue; // Already in process of marking this array, abort cycle!
781 }
782 66822 lbm_array_header_extended_t *arr = (lbm_array_header_extended_t*)cell->car;
783 66822 lbm_value *arrdata = (lbm_value *)arr->data;
784 66822 lbm_push(s, curr); // put array back as bookkeeping.
785 // Example A: Array with 10 elements
786 // A: assume arr->index == 9
787 // Example B: Array with 0 elements
788 // B: assume arr->index == 0
789
2/2
✓ Branch 0 taken 66220 times.
✓ Branch 1 taken 602 times.
66822 if (arr->index < ((arr->size/(sizeof(lbm_value))))) { // A: 9 < 10
790 // A: True path
791 // B: 0 < 0
792 // B: False path
793
3/4
✓ Branch 1 taken 3188 times.
✓ Branch 2 taken 63032 times.
✓ Branch 3 taken 3188 times.
✗ Branch 4 not taken.
66220 if (lbm_is_ptr(arrdata[arr->index]) && ((arrdata[arr->index] & LBM_PTR_TO_CONSTANT_BIT) == 0) &&
794
2/2
✓ Branch 0 taken 2726 times.
✓ Branch 1 taken 462 times.
3188 !((arrdata[arr->index] & LBM_CONTINUATION_INTERNAL) == LBM_CONTINUATION_INTERNAL)) {
795
796 2726 lbm_cons_t *elt = &lbm_heap_state.heap[lbm_dec_ptr(arrdata[arr->index])];
797
1/2
✓ Branch 1 taken 2726 times.
✗ Branch 2 not taken.
2726 if (!lbm_get_gc_mark(elt->cdr)) {
798 2726 curr = arrdata[arr->index];
799 2726 arr->index++;
800 2726 goto mark_shortcut;
801 }
802 }
803 // We need one more round of the loop if there is at least one element left in the array.
804 // If we skip this check things should work the same, but there will be one more recurse
805 // before we discover in the earlier conditional that we have reached the end of the array.
806
2/2
✓ Branch 0 taken 57064 times.
✓ Branch 1 taken 6430 times.
63494 if (arr->index < ((arr->size/(sizeof(lbm_value))) - 1)) { // A: 9 < 9
807 // A: False path
808 57064 arr->index++;
809 57064 continue;
810 }
811 }
812 7032 arr->index = 0;
813 7032 lbm_stack_drop(s,1); // Drop the bookkeeping element.
814 // elem_dropped == cell
815 // Cell is the array reference. so this marks the array reference itself
816 // after having marked all the elements.
817 7032 cell->cdr = lbm_set_gc_mark(cell->cdr);
818 7032 lbm_heap_state.gc_marked ++;
819 7032 continue;
820
2/2
✓ Branch 0 taken 815421 times.
✓ Branch 1 taken 157238070 times.
158053491 } else if (t_ptr == LBM_TYPE_CHANNEL) {
821 815421 cell->cdr = lbm_set_gc_mark(cell->cdr);
822 815421 lbm_heap_state.gc_marked ++;
823 // TODO: Can channels be explicitly freed ?
824
1/2
✓ Branch 0 taken 815421 times.
✗ Branch 1 not taken.
815421 if (cell->car != ENC_SYM_NIL) {
825 815421 lbm_char_channel_t *chan = (lbm_char_channel_t *)cell->car;
826 815421 curr = chan->dependency;
827 815421 goto mark_shortcut;
828 }
829 ✗ continue;
830 }
831
832 157238070 cell->cdr = lbm_set_gc_mark(cell->cdr);
833 157238070 lbm_heap_state.gc_marked ++;
834
835
2/2
✓ Branch 0 taken 152934145 times.
✓ Branch 1 taken 4303925 times.
157238070 if (t_ptr == LBM_TYPE_CONS) {
836
2/2
✓ Branch 1 taken 98011359 times.
✓ Branch 2 taken 54922786 times.
152934145 if (lbm_is_ptr(cell->cdr)) {
837
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 98011358 times.
98011359 if (!lbm_push(s, cell->cdr)) {
838 1 lbm_critical_error();
839 ✗ break;
840 }
841 }
842 152934144 curr = cell->car;
843 152934144 goto mark_shortcut; // Skip a push/pop
844 }
845 }
846 25381259 }
847 #endif
848
849 //Environments are proper lists with a 2 element list stored in each car.
850 53049293 void lbm_gc_mark_env(lbm_value env) {
851 53049293 lbm_value curr = env;
852 lbm_cons_t *c;
853
854
2/2
✓ Branch 1 taken 8981193 times.
✓ Branch 2 taken 53049292 times.
62030485 while (lbm_is_ptr(curr)) {
855 8981193 c = lbm_ref_cell(curr);
856 8981193 c->cdr = lbm_set_gc_mark(c->cdr); // mark the environent list structure.
857 8981193 lbm_cons_t *b = lbm_ref_cell(c->car);
858 8981193 b->cdr = lbm_set_gc_mark(b->cdr); // mark the binding list head cell.
859 8981193 lbm_gc_mark_phase(b->cdr); // mark the bound object.
860 8981192 lbm_heap_state.gc_marked +=2;
861 8981192 curr = c->cdr;
862 }
863 53049292 }
864
865
866 1644292 void lbm_gc_mark_aux(lbm_uint *aux_data, lbm_uint aux_size) {
867
2/2
✓ Branch 0 taken 31553497 times.
✓ Branch 1 taken 1644292 times.
33197789 for (lbm_uint i = 0; i < aux_size; i ++) {
868
2/2
✓ Branch 1 taken 18839514 times.
✓ Branch 2 taken 12713983 times.
31553497 if (lbm_is_ptr(aux_data[i])) {
869 18839514 lbm_type pt_t = lbm_type_of(aux_data[i]);
870 18839514 lbm_uint pt_v = lbm_dec_ptr(aux_data[i]);
871
3/4
✓ Branch 0 taken 18839514 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8430767 times.
✓ Branch 3 taken 10408747 times.
18839514 if( pt_t >= LBM_POINTER_TYPE_FIRST &&
872 8430767 pt_t <= LBM_POINTER_TYPE_LAST &&
873
1/2
✓ Branch 0 taken 8430767 times.
✗ Branch 1 not taken.
8430767 pt_v < lbm_heap_state.heap_size) {
874 8430767 lbm_gc_mark_phase(aux_data[i]);
875 }
876 }
877 }
878 1644292 }
879
880 3299191 void lbm_gc_mark_roots(lbm_uint *roots, lbm_uint num_roots) {
881
2/2
✓ Branch 0 taken 6613544 times.
✓ Branch 1 taken 3299191 times.
9912735 for (lbm_uint i = 0; i < num_roots; i ++) {
882 6613544 lbm_gc_mark_phase(roots[i]);
883 }
884 3299191 }
885
886 // Sweep moves non-marked heap objects to the free list.
887 1606406 int lbm_gc_sweep_phase(void) {
888 1606406 unsigned int i = 0;
889 1606406 lbm_cons_t *heap = (lbm_cons_t *)lbm_heap_state.heap;
890
891
2/2
✓ Branch 0 taken 3826154496 times.
✓ Branch 1 taken 1606406 times.
3827760902 for (i = 0; i < lbm_heap_state.heap_size; i ++) {
892
2/2
✓ Branch 1 taken 175450949 times.
✓ Branch 2 taken 3650703547 times.
3826154496 if ( lbm_get_gc_mark(heap[i].cdr)) {
893 175450949 heap[i].cdr = lbm_clr_gc_mark(heap[i].cdr);
894 } else {
895 // Check if this cell is a pointer to an array
896 // and free it.
897
2/2
✓ Branch 1 taken 642938153 times.
✓ Branch 2 taken 3007765394 times.
3650703547 if (lbm_type_of(heap[i].cdr) == LBM_TYPE_SYMBOL) {
898
7/7
✓ Branch 0 taken 16793870 times.
✓ Branch 1 taken 86997 times.
✓ Branch 2 taken 1729128 times.
✓ Branch 3 taken 914917 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 84 times.
✓ Branch 6 taken 623413154 times.
642938153 switch(heap[i].cdr) {
899
900 16793870 case ENC_SYM_IND_I_TYPE: /* fall through */
901 case ENC_SYM_IND_U_TYPE:
902 case ENC_SYM_IND_F_TYPE:
903 16793870 lbm_memory_free((lbm_uint*)heap[i].car);
904 16793870 break;
905 86997 case ENC_SYM_DEFRAG_LISPARRAY_TYPE: /* fall through */
906 case ENC_SYM_DEFRAG_ARRAY_TYPE:
907 86997 lbm_defrag_mem_free((lbm_uint*)heap[i].car);
908 86997 break;
909 1729128 case ENC_SYM_LISPARRAY_TYPE: /* fall through */
910 case ENC_SYM_ARRAY_TYPE:{
911 1729128 lbm_array_header_t *arr = (lbm_array_header_t*)heap[i].car;
912 1729128 lbm_memory_free((lbm_uint *)arr->data);
913 1729128 lbm_heap_state.gc_recovered_arrays++;
914 1729128 lbm_memory_free((lbm_uint *)arr);
915 1729128 } break;
916 914917 case ENC_SYM_CHANNEL_TYPE:{
917 914917 lbm_char_channel_t *chan = (lbm_char_channel_t*)heap[i].car;
918 914917 lbm_memory_free((lbm_uint*)chan->state);
919 914917 lbm_memory_free((lbm_uint*)chan);
920 914917 } break;
921 3 case ENC_SYM_CUSTOM_TYPE: {
922 3 lbm_uint *t = (lbm_uint*)heap[i].car;
923 3 lbm_custom_type_destroy(t);
924 3 lbm_memory_free(t);
925 3 } break;
926 84 case ENC_SYM_DEFRAG_MEM_TYPE: {
927 84 lbm_uint *ptr = (lbm_uint *)heap[i].car;
928 84 lbm_defrag_mem_destroy(ptr);
929 84 } break;
930 623413154 default:
931 623413154 break;
932 }
933 }
934 // create pointer to use as new freelist
935 3650703547 lbm_uint addr = lbm_enc_cons_ptr(i);
936
937 // Clear the "freed" cell.
938 3650703547 heap[i].car = ENC_SYM_RECOVERED;
939 3650703547 heap[i].cdr = lbm_heap_state.freelist;
940 3650703547 lbm_heap_state.freelist = addr;
941 3650703547 lbm_heap_state.num_free++;
942 3650703547 lbm_heap_state.gc_recovered ++;
943 }
944 }
945 1606406 return 1;
946 }
947
948 1606407 void lbm_gc_state_inc(void) {
949 1606407 lbm_heap_state.gc_num ++;
950 1606407 lbm_heap_state.gc_recovered = 0;
951 1606407 lbm_heap_state.gc_marked = 0;
952 1606407 }
953
954 // construct, alter and break apart
955 1411339950 lbm_value lbm_cons(lbm_value car, lbm_value cdr) {
956 1411339950 return lbm_heap_allocate_cell(LBM_TYPE_CONS, car, cdr);
957 }
958
959 821805576 lbm_value lbm_car(lbm_value c){
960
2/2
✓ Branch 1 taken 821803076 times.
✓ Branch 2 taken 2500 times.
821805576 if (lbm_is_ptr(c) ){
961 821803076 lbm_cons_t *cell = lbm_ref_cell(c);
962 821803076 return cell->car;
963 }
964
2/2
✓ Branch 0 taken 103 times.
✓ Branch 1 taken 2397 times.
2500 return c ? ENC_SYM_TERROR : c; //nil if c == nil
965 }
966
967 // TODO: Many comparisons "is this the nil symbol" can be
968 // streamlined a bit. NIL is 0 and cannot be confused with any other
969 // lbm_value.
970
971 138913 lbm_value lbm_caar(lbm_value c) {
972 138913 lbm_value tmp = ENC_SYM_NIL;
973
1/2
✓ Branch 1 taken 138913 times.
✗ Branch 2 not taken.
138913 if (lbm_is_ptr(c)) {
974 138913 tmp = lbm_ref_cell(c)->car;
975
1/2
✓ Branch 1 taken 138913 times.
✗ Branch 2 not taken.
138913 if (lbm_is_ptr(tmp)) {
976 138913 return lbm_ref_cell(tmp)->car;
977 }
978 }
979 ✗ return c || tmp ? ENC_SYM_TERROR : c; //nil if not something else
980 }
981
982
983 35173 lbm_value lbm_cadr(lbm_value c) {
984 35173 lbm_value tmp = ENC_SYM_NIL;
985
1/2
✓ Branch 1 taken 35173 times.
✗ Branch 2 not taken.
35173 if (lbm_is_ptr(c)) {
986 35173 tmp = lbm_ref_cell(c)->cdr;
987
1/2
✓ Branch 1 taken 35173 times.
✗ Branch 2 not taken.
35173 if (lbm_is_ptr(tmp)) {
988 35173 return lbm_ref_cell(tmp)->car;
989 }
990 }
991 ✗ return c || tmp ? ENC_SYM_TERROR : c;
992 }
993
994 91639409 lbm_value lbm_cdr(lbm_value c){
995
2/2
✓ Branch 1 taken 89939901 times.
✓ Branch 2 taken 1699508 times.
91639409 if (lbm_is_ptr(c)) {
996 89939901 lbm_cons_t *cell = lbm_ref_cell(c);
997 89939901 return cell->cdr;
998 }
999
1/2
✓ Branch 0 taken 1699508 times.
✗ Branch 1 not taken.
1699508 return c ? ENC_SYM_TERROR: c;
1000 }
1001
1002 5 lbm_value lbm_cddr(lbm_value c) {
1003
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
5 if (lbm_is_ptr(c)) {
1004 3 lbm_value tmp = lbm_ref_cell(c)->cdr;
1005
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 time.
3 if (lbm_is_ptr(tmp)) {
1006 2 return lbm_ref_cell(tmp)->cdr;
1007 }
1008 }
1009
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 return c ? ENC_SYM_TERROR : c;
1010 }
1011
1012 /* calculate length of a proper list */
1013 3751575 lbm_uint lbm_list_length(lbm_value c) {
1014 3751575 lbm_uint len = 0;
1015
1016
2/2
✓ Branch 1 taken 17894950 times.
✓ Branch 2 taken 3751575 times.
21646525 while (lbm_is_cons(c)){
1017 17894950 len ++;
1018 17894950 c = lbm_ref_cell(c)->cdr;
1019 }
1020 3751575 return len;
1021 }
1022
1023 32731 lbm_value lbm_list_destructive_reverse(lbm_value list) {
1024
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 32727 times.
32731 if (!lbm_is_cons(list)) return list;
1025 32727 lbm_value curr = list;
1026 32727 lbm_value last_cell = ENC_SYM_NIL;
1027
1028
2/2
✓ Branch 1 taken 84042 times.
✓ Branch 2 taken 32727 times.
116769 while (lbm_is_cons_rw(curr)) {
1029 84042 lbm_cons_t *cell = lbm_ref_cell(curr);
1030 84042 lbm_value next = cell->cdr;
1031 84042 cell->cdr = last_cell;
1032 84042 last_cell = curr;
1033 84042 curr = next;
1034 }
1035 32727 return last_cell;
1036 }
1037
1038
1039 990805 lbm_value lbm_list_copy(int *m, lbm_value list) {
1040 990805 lbm_value curr = list;
1041 990805 lbm_uint n = lbm_list_length(list);
1042 990805 lbm_uint copy_n = n;
1043
4/4
✓ Branch 0 taken 18542 times.
✓ Branch 1 taken 972263 times.
✓ Branch 2 taken 16266 times.
✓ Branch 3 taken 2276 times.
990805 if (*m >= 0 && (lbm_uint)*m < n) {
1044 16266 copy_n = (lbm_uint)*m;
1045
2/2
✓ Branch 0 taken 887213 times.
✓ Branch 1 taken 87326 times.
974539 } else if (*m == -1) {
1046 887213 *m = (int)n; // TODO: smaller range in target variable.
1047 }
1048
2/2
✓ Branch 0 taken 679 times.
✓ Branch 1 taken 990126 times.
990805 if (copy_n == 0) return ENC_SYM_NIL;
1049 990126 lbm_uint new_list = lbm_heap_allocate_list(copy_n);
1050
2/2
✓ Branch 1 taken 1579 times.
✓ Branch 2 taken 988547 times.
990126 if (lbm_is_symbol(new_list)) return new_list;
1051 988547 lbm_value curr_targ = new_list;
1052
1053
4/4
✓ Branch 1 taken 11300278 times.
✓ Branch 2 taken 972527 times.
✓ Branch 3 taken 11284258 times.
✓ Branch 4 taken 16020 times.
12272805 while (lbm_is_cons(curr) && copy_n > 0) {
1054 11284258 lbm_cons_t *curr_cell = lbm_ref_cell(curr);
1055 11284258 lbm_cons_t *targ_cell = lbm_ref_cell(curr_targ);
1056 11284258 targ_cell->car = curr_cell->car;
1057 11284258 curr_targ = targ_cell->cdr;
1058 11284258 curr = curr_cell->cdr;
1059 11284258 copy_n --;
1060 }
1061 988547 return new_list;
1062 }
1063
1064 // Append for proper lists only
1065 // Destructive update of list1.
1066 72400 lbm_value lbm_list_append(lbm_value list1, lbm_value list2) {
1067
1068
2/4
✓ Branch 1 taken 72400 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 72400 times.
✗ Branch 4 not taken.
144800 if(lbm_is_list_rw(list1) &&
1069 72400 lbm_is_list(list2)) {
1070
1071 72400 lbm_value curr = list1;
1072 72400 lbm_cons_t *cell = lbm_ref_cell(curr);
1073
2/2
✓ Branch 1 taken 94941 times.
✓ Branch 2 taken 72400 times.
167341 while(lbm_is_cons_rw(cell->cdr)) {
1074 94941 curr = cell->cdr;
1075 94941 cell = lbm_ref_cell(cell->cdr);
1076 }
1077
2/2
✓ Branch 1 taken 86 times.
✓ Branch 2 taken 72314 times.
72400 if (lbm_is_symbol_nil(curr)) return list2;
1078 72314 lbm_set_cdr(curr, list2);
1079 72314 return list1;
1080 }
1081 ✗ return ENC_SYM_EERROR;
1082 }
1083
1084 252 lbm_value lbm_list_drop(unsigned int n, lbm_value ls) {
1085 252 lbm_value curr = ls;
1086
4/4
✓ Branch 1 taken 2184 times.
✓ Branch 2 taken 168 times.
✓ Branch 3 taken 2100 times.
✓ Branch 4 taken 84 times.
2352 while (lbm_is_cons(curr) && n > 0) {
1087 2100 curr = lbm_ref_cell(curr)->cdr;
1088 2100 n --;
1089 }
1090 252 return curr;
1091 }
1092
1093 486464 lbm_value lbm_index_list(lbm_value l, int32_t n) {
1094 486464 lbm_value curr = l;
1095
1096
2/2
✓ Branch 0 taken 337 times.
✓ Branch 1 taken 486127 times.
486464 if (n < 0) {
1097 337 int32_t len = (int32_t)lbm_list_length(l);
1098 337 n = len + n;
1099
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 337 times.
337 if (n < 0) return ENC_SYM_NIL;
1100 }
1101
1102
4/4
✓ Branch 1 taken 735122 times.
✓ Branch 2 taken 86 times.
✓ Branch 3 taken 248744 times.
✓ Branch 4 taken 486378 times.
735208 while (lbm_is_cons(curr) && n > 0) {
1103 248744 curr = lbm_ref_cell(curr)->cdr;
1104 248744 n --;
1105 }
1106
2/2
✓ Branch 1 taken 486378 times.
✓ Branch 2 taken 86 times.
486464 if (lbm_is_cons(curr)) {
1107 486378 return lbm_ref_cell(curr)->car;
1108 } else {
1109 86 return ENC_SYM_NIL;
1110 }
1111 }
1112
1113 // High-level arrays are just bytearrays but with a different tag and pointer type.
1114 // These arrays will be inspected by GC and the elements of the array will be marked.
1115
1116 // Arrays are part of the heap module because their lifespan is managed
1117 // by the garbage collector. The data in the array is not stored
1118 // in the "heap of cons cells".
1119 1745510 int lbm_heap_allocate_array_base(lbm_value *res, bool byte_array, lbm_uint size){
1120
1121 1745510 lbm_uint tag = ENC_SYM_ARRAY_TYPE;
1122 1745510 lbm_uint type = LBM_TYPE_ARRAY;
1123 1745510 lbm_array_header_t *array = NULL;
1124 1745510 lbm_array_header_extended_t *ext_array = NULL;
1125
1126
2/2
✓ Branch 0 taken 897500 times.
✓ Branch 1 taken 848010 times.
1745510 if (byte_array) {
1127 897500 array = (lbm_array_header_t*)lbm_malloc(sizeof(lbm_array_header_t));
1128 } else {
1129 848010 tag = ENC_SYM_LISPARRAY_TYPE;
1130 848010 type = LBM_TYPE_LISPARRAY;
1131 848010 size = sizeof(lbm_value) * size;
1132 848010 array = (lbm_array_header_t*)lbm_malloc(sizeof(lbm_array_header_extended_t));
1133 848010 ext_array = (lbm_array_header_extended_t*)array;
1134 }
1135
2/2
✓ Branch 0 taken 1742328 times.
✓ Branch 1 taken 3182 times.
1745510 if (array) {
1136
2/2
✓ Branch 0 taken 845430 times.
✓ Branch 1 taken 896898 times.
1742328 if (!byte_array) ext_array->index = 0;
1137
1138 1742328 array->data = NULL;
1139 1742328 array->size = size;
1140
2/2
✓ Branch 0 taken 1741894 times.
✓ Branch 1 taken 434 times.
1742328 if ( size > 0) {
1141 1741894 array->data = (lbm_uint*)lbm_malloc(size);
1142
2/2
✓ Branch 0 taken 14234 times.
✓ Branch 1 taken 1727660 times.
1741894 if (array->data == NULL) {
1143 14234 lbm_memory_free((lbm_uint*)array);
1144 14234 goto allocate_array_merror;
1145 }
1146 // It is more important to zero out high-level arrays.
1147 // 0 is symbol NIL which is perfectly safe for the GC to inspect.
1148 1727660 memset(array->data, 0, size);
1149 }
1150 // allocating a cell for array's heap-presence
1151 1728094 lbm_value cell = lbm_heap_allocate_cell(type, (lbm_uint) array, tag);
1152
2/2
✓ Branch 0 taken 1120 times.
✓ Branch 1 taken 1726974 times.
1728094 if (cell == ENC_SYM_MERROR) {
1153 1120 lbm_memory_free((lbm_uint*)array->data);
1154 1120 lbm_memory_free((lbm_uint*)array);
1155 1120 goto allocate_array_merror;
1156 }
1157 1726974 *res = cell;
1158 1726974 lbm_heap_state.num_alloc_arrays ++;
1159 1726974 return 1;
1160 }
1161 3182 allocate_array_merror:
1162 18536 *res = ENC_SYM_MERROR;
1163 18536 return 0;
1164 }
1165
1166 897500 int lbm_heap_allocate_array(lbm_value *res, lbm_uint size){
1167 897500 return lbm_heap_allocate_array_base(res, true, size);
1168 }
1169
1170 848010 int lbm_heap_allocate_lisp_array(lbm_value *res, lbm_uint size) {
1171 848010 return lbm_heap_allocate_array_base(res, false, size);
1172 }
1173
1174 1133 int lbm_lift_array(lbm_value *value, char *data, lbm_uint num_elt) {
1175
1176 1133 lbm_array_header_t *array = NULL;
1177 1133 lbm_value cell = lbm_heap_allocate_cell(LBM_TYPE_CONS, ENC_SYM_NIL, ENC_SYM_ARRAY_TYPE);
1178
1179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1133 times.
1133 if (cell == ENC_SYM_MERROR) {
1180 ✗ *value = cell;
1181 ✗ return 0;
1182 }
1183
1184 1133 array = (lbm_array_header_t*)lbm_malloc(sizeof(lbm_array_header_t));
1185
1186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1133 times.
1133 if (array == NULL) {
1187 ✗ lbm_set_car_and_cdr(cell, ENC_SYM_NIL, ENC_SYM_NIL);
1188 ✗ *value = ENC_SYM_MERROR;
1189 ✗ return 0;
1190 }
1191
1192 1133 array->data = (lbm_uint*)data;
1193 1133 array->size = num_elt;
1194
1195 1133 lbm_set_car(cell, (lbm_uint)array);
1196
1197 1133 cell = lbm_set_ptr_type(cell, LBM_TYPE_ARRAY);
1198 1133 *value = cell;
1199 1133 return 1;
1200 }
1201
1202 713377 lbm_int lbm_heap_array_get_size(lbm_value arr) {
1203
1204 713377 lbm_int r = -1;
1205 713377 lbm_array_header_t *header = lbm_dec_array_r(arr);
1206
1/2
✓ Branch 0 taken 713377 times.
✗ Branch 1 not taken.
713377 if (header) {
1207 713377 r = (lbm_int)header->size;
1208 }
1209 713377 return r;
1210 }
1211
1212 356863 const uint8_t *lbm_heap_array_get_data_ro(lbm_value arr) {
1213 356863 uint8_t *r = NULL;
1214 356863 lbm_array_header_t *header = lbm_dec_array_r(arr);
1215
1/2
✓ Branch 0 taken 356863 times.
✗ Branch 1 not taken.
356863 if (header) {
1216 356863 r = (uint8_t*)header->data;
1217 }
1218 356863 return r;
1219 }
1220
1221 /* Explicitly freeing an array.
1222
1223 This is a highly unsafe operation and can only be safely
1224 used if the heap cell that points to the array has not been made
1225 accessible to the program.
1226
1227 So This function can be used to free an array in case an array
1228 is being constructed and some error case appears while doing so
1229 If the array still have not become available it can safely be
1230 "explicitly" freed.
1231
1232 The problem is that if the "array" heap-cell is made available to
1233 the program, this cell can easily be duplicated and we would have
1234 to search the entire heap to find all cells pointing to the array
1235 memory in question and "null"-them out before freeing the memory
1236 */
1237
1238 336 int lbm_heap_explicit_free_array(lbm_value arr) {
1239
1240 336 int r = 0;
1241
2/4
✓ Branch 1 taken 336 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 336 times.
✗ Branch 5 not taken.
336 if (lbm_is_array_rw(arr) && lbm_cdr(arr) == ENC_SYM_ARRAY_TYPE) {
1242 336 lbm_array_header_t *header = (lbm_array_header_t*)lbm_car(arr);
1243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
336 if (header == NULL) {
1244 ✗ return 0;
1245 }
1246 336 lbm_memory_free((lbm_uint*)header->data);
1247 336 lbm_memory_free((lbm_uint*)header);
1248
1249 336 arr = lbm_set_ptr_type(arr, LBM_TYPE_CONS);
1250 336 lbm_set_car(arr, ENC_SYM_NIL);
1251 336 lbm_set_cdr(arr, ENC_SYM_NIL);
1252 336 r = 1;
1253 }
1254
1255 336 return r;
1256 }
1257
1258 ✗ static bool dummy_flash_write(lbm_uint ix, lbm_uint val) {
1259 (void)ix;
1260 (void)val;
1261 ✗ return false;
1262 }
1263
1264 static const_heap_write_fun const_heap_write = dummy_flash_write;
1265
1266 66578 int lbm_const_heap_init(const_heap_write_fun w_fun,
1267 lbm_const_heap_t *heap,
1268 lbm_uint *addr) {
1269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66578 times.
66578 if (((uintptr_t)addr % 4) != 0) return 0;
1270
1271
2/2
✓ Branch 0 taken 66498 times.
✓ Branch 1 taken 80 times.
66578 if (!lbm_const_heap_mutex_initialized) {
1272 66498 lbm_mutex_init(&lbm_const_heap_mutex);
1273 66498 lbm_const_heap_mutex_initialized = true;
1274 }
1275
1276
2/2
✓ Branch 0 taken 66498 times.
✓ Branch 1 taken 80 times.
66578 if (!lbm_mark_mutex_initialized) {
1277 66498 lbm_mutex_init(&lbm_mark_mutex);
1278 66498 lbm_mark_mutex_initialized = true;
1279 }
1280
1281 66578 const_heap_write = w_fun;
1282
1283 66578 heap->heap = addr;
1284 66578 heap->size = 0;
1285 66578 heap->next = 0;
1286
1287 66578 lbm_const_heap_state = heap;
1288 // ref_cell views the lbm_uint array as an lbm_cons_t array
1289 66578 lbm_heaps[LBM_CONST_HEAP] = (lbm_cons_t*)addr;
1290 66578 return 1;
1291 }
1292
1293 7393 lbm_flash_status lbm_allocate_const_cell(lbm_value *res) {
1294 7393 lbm_flash_status r = LBM_FLASH_FULL;
1295
1296 7393 lbm_mutex_lock(&lbm_const_heap_mutex);
1297 // waste a cell if we have ended up unaligned after writing an array to flash.
1298
2/2
✓ Branch 0 taken 89 times.
✓ Branch 1 taken 7304 times.
7393 if (lbm_const_heap_state->next % 2 == 1) {
1299 89 lbm_const_heap_state->next++;
1300 }
1301
1302
1/2
✓ Branch 0 taken 7393 times.
✗ Branch 1 not taken.
7393 if (lbm_const_heap_state &&
1303
1/2
✓ Branch 1 taken 7393 times.
✗ Branch 2 not taken.
7393 (lbm_const_heap_state->next+1) < (uint32_t)lbm_image_get_write_index()) {
1304 // A cons cell uses two words.
1305 7393 lbm_value cell = lbm_const_heap_state->next;
1306 7393 lbm_const_heap_state->next += 2;
1307 7393 *res = ((cell >> 1) << LBM_ADDRESS_SHIFT) | LBM_PTR_BIT | LBM_TYPE_CONS | LBM_PTR_TO_CONSTANT_BIT;
1308 7393 r = LBM_FLASH_WRITE_OK;
1309 }
1310 7393 lbm_mutex_unlock(&lbm_const_heap_mutex);
1311 7393 return r;
1312 }
1313
1314 84 lbm_flash_status lbm_allocate_const_raw(lbm_uint nwords, lbm_uint *res) {
1315 84 lbm_flash_status r = LBM_FLASH_FULL;
1316
1317
1/2
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
84 if (lbm_const_heap_state &&
1318
1/2
✓ Branch 1 taken 84 times.
✗ Branch 2 not taken.
84 (lbm_const_heap_state->next + nwords) < (uint32_t)lbm_image_get_write_index()) {
1319 84 lbm_uint ix = lbm_const_heap_state->next;
1320 84 *res = (lbm_uint)&lbm_const_heap_state->heap[ix];
1321 84 lbm_const_heap_state->next += nwords;
1322 84 r = LBM_FLASH_WRITE_OK;
1323 }
1324 84 return r;
1325 }
1326
1327 733 lbm_flash_status lbm_write_const_raw(lbm_uint *data, lbm_uint n, lbm_uint *res) {
1328
1329 733 lbm_flash_status r = LBM_FLASH_FULL;
1330
1331
1/2
✓ Branch 0 taken 733 times.
✗ Branch 1 not taken.
733 if (lbm_const_heap_state &&
1332
1/2
✓ Branch 1 taken 733 times.
✗ Branch 2 not taken.
733 (lbm_const_heap_state->next + n) < (uint32_t)lbm_image_get_write_index()) {
1333 733 lbm_uint ix = lbm_const_heap_state->next;
1334
1335
2/2
✓ Branch 0 taken 1497 times.
✓ Branch 1 taken 733 times.
2230 for (unsigned int i = 0; i < n; i ++) {
1336
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1497 times.
1497 if (!const_heap_write(((lbm_uint*)data)[i],ix + i))
1337 ✗ return LBM_FLASH_WRITE_ERROR;
1338 }
1339 733 lbm_const_heap_state->next += n;
1340 733 *res = (lbm_uint)&lbm_const_heap_state->heap[ix];
1341 733 r = LBM_FLASH_WRITE_OK;
1342 }
1343 733 return r;
1344 }
1345
1346 252 lbm_flash_status lbm_const_write(lbm_uint *tgt, lbm_uint val) {
1347
1348
1/2
✓ Branch 0 taken 252 times.
✗ Branch 1 not taken.
252 if (lbm_const_heap_state) {
1349 252 lbm_uint flash = (lbm_uint)lbm_const_heap_state->heap;
1350 252 lbm_uint ix = (((lbm_uint)tgt - flash) / sizeof(lbm_uint)); // byte address to ix
1351
1/2
✓ Branch 1 taken 252 times.
✗ Branch 2 not taken.
252 if (const_heap_write(val, ix)) {
1352 252 return LBM_FLASH_WRITE_OK;
1353 }
1354 ✗ return LBM_FLASH_WRITE_ERROR;
1355 }
1356 ✗ return LBM_FLASH_FULL;
1357 }
1358
1359 7393 lbm_flash_status write_const_cdr(lbm_value cell, lbm_value val) {
1360 7393 lbm_uint addr = lbm_dec_ptr(cell) << 1;
1361
1/2
✓ Branch 1 taken 7393 times.
✗ Branch 2 not taken.
7393 if (const_heap_write(val, addr+1))
1362 7393 return LBM_FLASH_WRITE_OK;
1363 ✗ return LBM_FLASH_WRITE_ERROR;
1364 }
1365
1366 7393 lbm_flash_status write_const_car(lbm_value cell, lbm_value val) {
1367 7393 lbm_uint addr = lbm_dec_ptr(cell) << 1;
1368
1/2
✓ Branch 1 taken 7393 times.
✗ Branch 2 not taken.
7393 if (const_heap_write(val, addr))
1369 7393 return LBM_FLASH_WRITE_OK;
1370 ✗ return LBM_FLASH_WRITE_ERROR;
1371 }
1372
1373 9 lbm_uint lbm_flash_memory_usage(void) {
1374 9 return lbm_const_heap_state->next;
1375 }
1376
1377
1378 // ////////////////////////////////////////////////////////////
1379 // pointer reversal traversal
1380 //
1381 // Caveats:
1382 // * Structures on the constant heap cannot be traversed using
1383 // pointer reversal. If a dynamic structure is pointing into the
1384 // constant heap, the 'f' will be applied to the constant cons cell on
1385 // the border and then traversal will retreat.
1386 //
1387 // * It may be impossible to detect all kinds of cycles
1388 // if attempting to use and restore the GC in a single pass
1389 // over the tree. To detect cycles all visited nodes must
1390 // remain detectable when traversing all branches!
1391 //
1392 // * Potential fix is to run GC after a complete traversal of the
1393 // entire value in order to restore the GC bits.
1394 //
1395 // * If we leave GC bits set when traversing values, we can use this
1396 // to detect cycles that happen in multiple steps accross values
1397 // in the environment.
1398 //
1399 // * lbm_ptr_rev_trav with the "do_nothing" travfun is the same thing
1400 // as a GC mark phase! Maybe utilize this for code-size
1401 // purposes. This also increases the amount of testing the
1402 // ptr_rev_trav function is subjected to.
1403
1404 939 void lbm_ptr_rev_trav(trav_fun f, lbm_value v, void* arg) {
1405
1406 939 lbm_value curr = v;
1407 939 lbm_value prev = lbm_enc_cons_ptr(LBM_PTR_NULL);
1408 while (true) {
1409
1410 // Run leftwards and process conses until
1411 // hitting a leaf in the left direction.
1412
1413 // If curr is marked here there is a cycle in the graph.
1414 // In case of a cycle or leaf, this first loop is exited.
1415
2/2
✓ Branch 1 taken 75 times.
✓ Branch 2 taken 24510 times.
72705 while (((lbm_is_cons_rw(curr)) ||
1416
4/4
✓ Branch 0 taken 24585 times.
✓ Branch 1 taken 23535 times.
✓ Branch 4 taken 23484 times.
✓ Branch 5 taken 126 times.
48195 (lbm_is_lisp_array_rw(curr))) && !gc_marked(curr)) {
1417 23484 lbm_cons_t *cell = lbm_ref_cell(curr);
1418
2/2
✓ Branch 1 taken 23424 times.
✓ Branch 2 taken 60 times.
23484 if (lbm_is_cons(curr)) {
1419 // In-order traversal
1420
2/2
✓ Branch 1 taken 78 times.
✓ Branch 2 taken 23346 times.
23424 if (f(curr, false, arg) == TRAV_FUN_SUBTREE_DONE) {
1421 78 lbm_gc_mark_phase(curr);
1422 78 goto trav_backtrack;
1423 }
1424 23346 gc_mark(curr);
1425
1426 23346 lbm_value next = 0;
1427 23346 value_assign(&next, cell->car);
1428 23346 value_assign(&cell->car, prev);
1429 23346 value_assign(&prev, curr);
1430 23346 value_assign(&curr, next);
1431 } else { // it is an array
1432
1433 60 lbm_array_header_extended_t *arr = (lbm_array_header_extended_t*)cell->car;
1434 60 lbm_value *arr_data = (lbm_value *)arr->data;
1435 60 uint32_t index = arr->index;
1436
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 54 times.
60 if (arr->size == 0) break;
1437
1/2
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
54 if (index == 0) { // index should only be 0 or there is a potential cycle
1438
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 54 times.
54 if (f(curr, false, arg) == TRAV_FUN_SUBTREE_DONE) {
1439 ✗ lbm_gc_mark_phase(curr);
1440 ✗ break;
1441 }
1442 54 arr->index = 1;
1443 54 gc_mark(curr);
1444
1445 54 lbm_value next = 0;
1446 54 value_assign(&next, arr_data[0]);
1447 54 value_assign(&arr_data[0], prev);
1448 54 value_assign(&prev, curr);
1449 54 value_assign(&curr, next);
1450 }
1451 }
1452 }
1453 // Currently there are a few different users of this traversal.
1454 // size, flatten and detect_sharing.
1455 // detect_sharing make use of the shared (true) argument in f(curr, true, arg)
1456 // while the other do not. detect_sharing also assumes it is run once per env item
1457 // while not resetting any GC-bits in between. This detects global sharing.
1458
1459
4/4
✓ Branch 1 taken 819 times.
✓ Branch 2 taken 23823 times.
✓ Branch 4 taken 126 times.
✓ Branch 5 taken 693 times.
24642 if (lbm_is_ptr(curr) && gc_marked(curr)) {
1460 // gc bit set so this subtree is already traversed.
1461 // f is called with true to indicate visited node.
1462 // if this happens during a sharing discovery phase, curr will be added to sharing table.
1463 126 f(curr, true, arg);
1464 // In this case f should not be able to return subtree continue.
1465 // The only correct return value from f is PROCEED.
1466
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 24513 times.
24516 } else if (!lbm_is_cons(curr) || // Found a leaf
1467
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 (curr & LBM_PTR_TO_CONSTANT_BIT)) {
1468
4/4
✓ Branch 1 taken 693 times.
✓ Branch 2 taken 23823 times.
✓ Branch 3 taken 690 times.
✓ Branch 4 taken 3 times.
24516 if (lbm_is_ptr(curr) && !(curr & LBM_PTR_TO_CONSTANT_BIT)) gc_mark(curr); // Mark it so that the mandatory GC does not swipe it.
1469 24516 f(curr, false, arg);
1470 }
1471
1472 // Now either prev has the "flag" set or it doesnt.
1473 // If the flag is set that means that the prev node
1474 // have had both its car and cdr visited. So that node is done!
1475 //
1476 // If the flag is not set, jump down to SWAP
1477
1478 ✗ trav_backtrack:
1479
2/2
✓ Branch 1 taken 46692 times.
✓ Branch 2 taken 939 times.
95751 while ((lbm_is_cons(prev) &&
1480
2/2
✓ Branch 1 taken 23346 times.
✓ Branch 2 taken 23346 times.
94323 (lbm_dec_ptr(prev) != LBM_PTR_NULL) && // is LBM_NULL a cons type?
1481
4/4
✓ Branch 0 taken 47631 times.
✓ Branch 1 taken 489 times.
✓ Branch 4 taken 489 times.
✓ Branch 5 taken 24285 times.
119586 lbm_get_gc_flag(lbm_car(prev))) ||
1482 24774 lbm_is_lisp_array_rw(prev)) {
1483 23835 lbm_cons_t *cell = lbm_ref_cell(prev);
1484
2/2
✓ Branch 1 taken 23346 times.
✓ Branch 2 taken 489 times.
23835 if (lbm_is_cons(prev)) {
1485
1486 // clear the flag
1487 // This means that we are done with a "CDR" child.
1488 // prev = [ a , b ][flag = 1]
1489 // =>
1490 // prev = [ a , b ][flag = 0]
1491
1492 //gc_clear_mark(prev);
1493 23346 cell->car = lbm_clr_gc_flag(cell->car);
1494 // Move on downwards until
1495 // finding a cons cell without flag or NULL
1496
1497 // curr = c
1498 // prev = [ a , b ][flag = 0]
1499 // =>
1500 // prev = [ a , c ][flag = 0]
1501 // curr = prev
1502 // prev = b
1503
1504 23346 lbm_value next = 0;
1505 23346 value_assign(&next, cell->cdr);
1506 23346 value_assign(&cell->cdr, curr);
1507 23346 value_assign(&curr, prev);
1508 23346 value_assign(&prev, next);
1509 } else { // is an array
1510 489 lbm_array_header_extended_t *arr = (lbm_array_header_extended_t*)cell->car;
1511 489 lbm_value *arr_data = (lbm_value *)arr->data;
1512 489 size_t arr_size = (size_t)arr->size / sizeof(lbm_value);
1513 489 lbm_value next = 0;
1514
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 435 times.
489 if (arr->index == arr_size) {
1515 54 value_assign(&next, arr_data[arr->index-1]);
1516 54 value_assign(&arr_data[arr->index-1], curr);
1517 54 value_assign(&curr, prev);
1518 54 value_assign(&prev, next);
1519 54 arr->index = 0;
1520 } else {
1521 435 break;
1522 }
1523 }
1524 }
1525
1526 // SWAP
1527
1528 // if the prev node is NULL we have traced backwards all the
1529 // way back to where curr == v. Another alternative is that
1530 // the input v was an Atom. We are done!
1531
3/4
✓ Branch 1 taken 24720 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 939 times.
✓ Branch 4 taken 23781 times.
49440 if (lbm_is_ptr(prev) &&
1532 24720 lbm_dec_ptr(prev) == LBM_PTR_NULL) {
1533 939 if (lbm_is_cons(curr)) {
1534 //gc_clear_mark(curr);
1535 }
1536 //done = true;
1537 939 break;
1538 }
1539
1540 // if the prev node is not NULL then we should move
1541 // down to the prev node and start process its remaining child.
1542
2/2
✓ Branch 1 taken 23346 times.
✓ Branch 2 taken 435 times.
23781 else if (lbm_is_cons(prev)) {
1543
1544 23346 lbm_cons_t *cell = lbm_ref_cell(prev);
1545 23346 lbm_value next = 0;
1546
1547
1548 // prev = [ p , cdr ][flag = 0]
1549 // =>
1550 // prev = [ p , cdr ][flag = 1]
1551
1552 23346 cell->car = lbm_set_gc_flag(cell->car);
1553
1554 // switch to processing the cdr field and set the flag.
1555 // curr = c
1556 // prev = [ a, b ][flag = 1]
1557 // =>
1558 // prev = [ c, a ][flag = 1]
1559 // curr = b
1560
1561 23346 value_assign(&next, cell->car);
1562 23346 value_assign(&cell->car, curr);
1563 23346 value_assign(&curr, cell->cdr);
1564 23346 value_assign(&cell->cdr, next);
1565
1/2
✓ Branch 1 taken 435 times.
✗ Branch 2 not taken.
435 } else if (lbm_is_lisp_array_rw(prev)) {
1566 435 lbm_cons_t *cell = lbm_ref_cell(prev);
1567 435 lbm_array_header_extended_t *arr = (lbm_array_header_extended_t*)cell->car;
1568 435 lbm_value *arr_data = (lbm_value *)arr->data;
1569 435 lbm_value next = 0;
1570
1571 435 value_assign(&next, arr_data[arr->index-1]);
1572 435 value_assign(&arr_data[arr->index-1], curr);
1573 435 value_assign(&curr, arr_data[arr->index]);
1574 435 value_assign(&arr_data[arr->index], next);
1575 435 arr->index = arr->index + 1;
1576 }
1577 }
1578 939 }
1579