GCC Code Coverage Report


Directory: ../src/
File: heap.c
Date: 2026-09-06 17:54:43
Coverage Exec Excl Total
Lines: 94.0% 848 0 902
Functions: 95.6% 87 0 91
Branches: 84.4% 324 0 384

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
36 177136289 static inline lbm_value lbm_set_gc_mark(lbm_value x) {
37 177136289 return x | LBM_GC_MARKED;
38 }
39 176540528 static inline lbm_value lbm_clr_gc_mark(lbm_value x) {
40 176540528 return x & ~LBM_GC_MASK;
41 }
42
43 4007775913 static inline bool lbm_get_gc_mark(lbm_value x) {
44 4007775913 return x & LBM_GC_MASK;
45 }
46
47 26530 static inline void gc_mark(lbm_value c) {
48 //c must be a cons cell.
49 26530 lbm_cons_t *cell = lbm_ref_cell(c);
50 26530 cell->cdr = lbm_set_gc_mark(cell->cdr);
51 26530 }
52
53 27375 static inline bool gc_marked(lbm_value c) {
54 27375 lbm_cons_t *cell = lbm_ref_cell(c);
55 27375 return lbm_get_gc_mark(cell->cdr);
56 }
57
58 __attribute__((unused)) static inline void gc_clear_mark(lbm_value c) {
59 //c must be a cons cell.
60 lbm_cons_t *cell = lbm_ref_cell(c);
61 cell->cdr = lbm_clr_gc_mark(cell->cdr);
62 }
63
64 // flag is the same bit as mark, but in car
65 51266 static inline bool lbm_get_gc_flag(lbm_value x) {
66 51266 return x & LBM_GC_MARKED;
67 }
68
69 25633 static inline lbm_value lbm_set_gc_flag(lbm_value x) {
70 25633 return x | LBM_GC_MARKED;
71 }
72
73 25633 static inline lbm_value lbm_clr_gc_flag(lbm_value x) {
74 25633 return x & ~LBM_GC_MASK;
75 }
76
77
78 lbm_heap_state_t lbm_heap_state;
79
80 lbm_const_heap_t *lbm_const_heap_state;
81
82 lbm_cons_t *lbm_heaps[2] = {NULL, NULL};
83
84 static lbm_mutex_t lbm_const_heap_mutex;
85 static bool lbm_const_heap_mutex_initialized = false;
86
87 static lbm_mutex_t lbm_mark_mutex;
88 static bool lbm_mark_mutex_initialized = false;
89
90 // These mutexes are not currently used. There was a thought
91 // related to these and pointer-revesal.
92 // It is actually more important to lock out accesses to the heap
93 // when in the middle of ptr-rev gc as it temporarlity
94 // distorts the heap pointer structure.
95 2 void lbm_gc_lock(void) {
96 2 lbm_mutex_lock(&lbm_mark_mutex);
97 2 }
98 2 void lbm_gc_unlock(void) {
99 2 lbm_mutex_unlock(&lbm_mark_mutex);
100 2 }
101
102 // ////////////////////////////////////////////////////////////
103 // ENCODERS DECODERS
104 //
105
106 8510918 lbm_value lbm_enc_i32(int32_t x) {
107 #ifndef LBM64
108 5675896 lbm_value i = lbm_cons((lbm_uint)x, ENC_SYM_RAW_I_TYPE);
109
2/2
✓ Branch 1 taken 1236 times.
✓ Branch 2 taken 5674660 times.
5675896 if (lbm_type_of(i) == LBM_TYPE_SYMBOL) return i;
110 5674660 return lbm_set_ptr_type(i, LBM_TYPE_I32);
111 #else
112 2835022 return (((lbm_uint)x) << LBM_VAL_SHIFT) | LBM_TYPE_I32;
113 #endif
114 }
115
116 10211451 lbm_value lbm_enc_u32(uint32_t x) {
117 #ifndef LBM64
118 7373208 lbm_value u = lbm_cons(x, ENC_SYM_RAW_U_TYPE);
119
2/2
✓ Branch 1 taken 2176 times.
✓ Branch 2 taken 7371032 times.
7373208 if (lbm_type_of(u) == LBM_TYPE_SYMBOL) return u;
120 7371032 return lbm_set_ptr_type(u, LBM_TYPE_U32);
121 #else
122 2838243 return (((lbm_uint)x) << LBM_VAL_SHIFT) | LBM_TYPE_U32;
123 #endif
124 }
125
126 // ////////////////////////////////////////////////////////////
127 // Destructively update a value on 32bit systems.
128 // identiaval to enc_u32 on 64bit.
129 //
130 // The caller is responsible for making sure that the v input
131 // is of the correct type attempting set.
132 // using lbm_set_x on the wrong type WILL CRASH!
133 ✗ lbm_value lbm_set_u32(lbm_value v, uint32_t x) {
134 #ifndef LBM64
135 ✗ lbm_ref_cell(v)->car = x;
136 ✗ return v;
137 #else
138 (void) v;
139 ✗ return (((lbm_uint)x) << LBM_VAL_SHIFT) | LBM_TYPE_U32;
140 #endif
141 }
142
143 ✗ lbm_value lbm_set_i32(lbm_value v, int32_t x) {
144 #ifndef LBM64
145 ✗ lbm_ref_cell(v)->car = (uint32_t)x;
146 ✗ return v;
147 #else
148 (void) v;
149 ✗ return (((lbm_uint)x) << LBM_VAL_SHIFT) | LBM_TYPE_U32;
150 #endif
151 }
152
153 ✗ lbm_value lbm_set_float(lbm_value v, float x) {
154 #ifndef LBM64
155 lbm_uint t;
156 ✗ memcpy(&t, &x, sizeof(lbm_float));
157 ✗ lbm_ref_cell(v)->car = t;
158 ✗ return v;
159 #else
160 (void) v;
161 ✗ lbm_uint t = 0;
162 ✗ memcpy(&t, &x, sizeof(float));
163 ✗ return (((lbm_uint)t) << LBM_VAL_SHIFT) | LBM_TYPE_FLOAT;
164 #endif
165 }
166 // ////////////////////////////////////////////////////////////
167
168 689408343 lbm_value lbm_enc_float(float x) {
169 #ifndef LBM64
170 lbm_uint t;
171 459789175 memcpy(&t, &x, sizeof(lbm_float));
172 459789175 lbm_value f = lbm_cons(t, ENC_SYM_RAW_F_TYPE);
173
2/2
✓ Branch 1 taken 294488 times.
✓ Branch 2 taken 459494687 times.
459789175 if (lbm_type_of(f) == LBM_TYPE_SYMBOL) return f;
174 459494687 return lbm_set_ptr_type(f, LBM_TYPE_FLOAT);
175 #else
176 229619168 lbm_uint t = 0;
177 229619168 memcpy(&t, &x, sizeof(float));
178 229619168 return (((lbm_uint)t) << LBM_VAL_SHIFT) | LBM_TYPE_FLOAT;
179 #endif
180 }
181
182 #ifndef LBM64
183 16856785 static lbm_value enc_64_on_32(uint8_t *source, lbm_uint type_qual, lbm_uint type) {
184 16856785 lbm_value res = lbm_cons(ENC_SYM_NIL,ENC_SYM_NIL);
185
2/2
✓ Branch 1 taken 16852413 times.
✓ Branch 2 taken 4372 times.
16856785 if (lbm_type_of(res) != LBM_TYPE_SYMBOL) {
186 16852413 uint8_t* storage = lbm_malloc(sizeof(uint64_t));
187
2/2
✓ Branch 0 taken 16847157 times.
✓ Branch 1 taken 5256 times.
16852413 if (storage) {
188 16847157 memcpy(storage,source, sizeof(uint64_t));
189 16847157 lbm_set_car_and_cdr(res, (lbm_uint)storage, type_qual);
190 16847157 res = lbm_set_ptr_type(res, type);
191 } else {
192 5256 res = ENC_SYM_MERROR;
193 }
194 }
195 16856785 return res;
196 }
197 #endif
198
199 13472630 lbm_value lbm_enc_i64(int64_t x) {
200 #ifndef LBM64
201 8983246 return enc_64_on_32((uint8_t *)&x, ENC_SYM_IND_I_TYPE, LBM_TYPE_I64);
202 #else
203 4489384 lbm_value u = lbm_cons((uint64_t)x, ENC_SYM_RAW_I_TYPE);
204
2/2
✓ Branch 1 taken 1232 times.
✓ Branch 2 taken 4488152 times.
4489384 if (lbm_type_of(u) == LBM_TYPE_SYMBOL) return u;
205 4488152 return lbm_set_ptr_type(u, LBM_TYPE_I64);
206 #endif
207 }
208
209 10110284 lbm_value lbm_enc_u64(uint64_t x) {
210 #ifndef LBM64
211 6741536 return enc_64_on_32((uint8_t *)&x, ENC_SYM_IND_U_TYPE, LBM_TYPE_U64);
212 #else
213 3368748 lbm_value u = lbm_cons(x, ENC_SYM_RAW_U_TYPE);
214
2/2
✓ Branch 1 taken 876 times.
✓ Branch 2 taken 3367872 times.
3368748 if (lbm_type_of(u) == LBM_TYPE_SYMBOL) return u;
215 3367872 return lbm_set_ptr_type(u, LBM_TYPE_U64);
216 #endif
217 }
218
219 1697571 lbm_value lbm_enc_double(double x) {
220 #ifndef LBM64
221 1132003 return enc_64_on_32((uint8_t *)&x, ENC_SYM_IND_F_TYPE, LBM_TYPE_DOUBLE);
222 #else
223 lbm_uint t;
224 565568 memcpy(&t, &x, sizeof(double));
225 565568 lbm_value f = lbm_cons(t, ENC_SYM_RAW_F_TYPE);
226
2/2
✓ Branch 1 taken 332 times.
✓ Branch 2 taken 565236 times.
565568 if (lbm_type_of(f) == LBM_TYPE_SYMBOL) return f;
227 565236 return lbm_set_ptr_type(f, LBM_TYPE_DOUBLE);
228 #endif
229 }
230
231 // Type specific (as opposed to the dec_as_X) functions
232 // should only be run on values KNOWN to represent a value of the type
233 // that the decoder decodes.
234
235 1000361442 float lbm_dec_float(lbm_value x) {
236 #ifndef LBM64
237 float f_tmp;
238 667127478 lbm_uint tmp = lbm_car(x);
239 667127478 memcpy(&f_tmp, &tmp, sizeof(float));
240 667127478 return f_tmp;
241 #else
242 333233964 uint32_t tmp = (uint32_t)(x >> LBM_VAL_SHIFT);
243 float f_tmp;
244 333233964 memcpy(&f_tmp, &tmp, sizeof(float));
245 333233964 return f_tmp;
246 #endif
247 }
248
249 1694894 double lbm_dec_double(lbm_value x) {
250 #ifndef LBM64
251 1130250 double d = 0.0;
252
1/2
✓ Branch 1 taken 1130250 times.
✗ Branch 2 not taken.
1130250 if (lbm_is_ptr(x)) {
253 1130250 uint32_t *data = (uint32_t*)lbm_ref_cell(x)->car;
254 1130250 memcpy(&d, data, sizeof(double));
255 }
256 1130250 return d;
257 #else
258 double f_tmp;
259 564644 lbm_uint tmp = lbm_car(x);
260 564644 memcpy(&f_tmp, &tmp, sizeof(double));
261 564644 return f_tmp;
262 #endif
263 }
264
265 21035053 uint64_t lbm_dec_u64(lbm_value x) {
266 #ifndef LBM64
267 14025265 uint64_t u = 0;
268
1/2
✓ Branch 1 taken 14025265 times.
✗ Branch 2 not taken.
14025265 if (lbm_is_ptr(x)) {
269 14025265 uint32_t *data = (uint32_t*)lbm_ref_cell(x)->car;
270 14025265 memcpy(&u, data, 8);
271 }
272 14025265 return u;
273 #else
274 7009788 return (uint64_t)lbm_car(x);
275 #endif
276 }
277
278 27756148 int64_t lbm_dec_i64(lbm_value x) {
279 #ifndef LBM64
280 18506144 int64_t i = 0;
281
1/2
✓ Branch 1 taken 18506144 times.
✗ Branch 2 not taken.
18506144 if (lbm_is_ptr(x)) {
282 18506144 uint32_t *data = (uint32_t*)lbm_ref_cell(x)->car;
283 18506144 memcpy(&i, data, 8);
284 }
285 18506144 return i;
286 #else
287 9250004 return (int64_t)lbm_car(x);
288 #endif
289 }
290
291 1464512 char *lbm_dec_str(lbm_value val) {
292 1464512 char *res = 0;
293
2/2
✓ Branch 1 taken 1464071 times.
✓ Branch 2 taken 441 times.
1464512 if (lbm_is_array_r(val)) {
294 1464071 lbm_array_header_t *array = (lbm_array_header_t *)lbm_car(val);
295
1/2
✓ Branch 0 taken 1464071 times.
✗ Branch 1 not taken.
1464071 if (array) {
296 1464071 res = (char *)array->data;
297 }
298 }
299 1464512 return res;
300 }
301
302 579591 bool lbm_dec_str_size(lbm_value val, char **data, size_t *size) {
303 579591 lbm_array_header_t *array = lbm_dec_array_r(val);
304
2/2
✓ Branch 0 taken 579384 times.
✓ Branch 1 taken 207 times.
579591 if (array) {
305 579384 *data = (char*)array->data;
306 579384 *size = array->size;
307 579384 return true;
308 }
309 207 return false;
310 }
311
312 1809052 lbm_array_header_t *lbm_dec_array_r(lbm_value val) {
313 1809052 lbm_array_header_t *array = NULL;
314
2/2
✓ Branch 1 taken 1804930 times.
✓ Branch 2 taken 4122 times.
1809052 if (lbm_is_array_r(val)) {
315 1804930 array = (lbm_array_header_t *)lbm_car(val);
316 }
317 1809052 return array;
318 }
319
320 180853 lbm_array_header_t *lbm_dec_array_rw(lbm_value val) {
321 180853 lbm_array_header_t *array = NULL;
322
2/2
✓ Branch 1 taken 180595 times.
✓ Branch 2 taken 258 times.
180853 if (lbm_is_array_rw(val)) {
323 180595 array = (lbm_array_header_t *)lbm_car(val);
324 }
325 180853 return array;
326 }
327
328 39479974 lbm_char_channel_t *lbm_dec_channel(lbm_value val) {
329 39479974 lbm_char_channel_t *res = NULL;
330
331
1/2
✓ Branch 1 taken 39479974 times.
✗ Branch 2 not taken.
39479974 if (lbm_type_of(val) == LBM_TYPE_CHANNEL) {
332 39479974 res = (lbm_char_channel_t *)lbm_car(val);
333 }
334 39479974 return res;
335 }
336
337 14339 lbm_uint lbm_dec_custom(lbm_value val) {
338 14339 lbm_uint res = 0;
339
1/2
✓ Branch 1 taken 14339 times.
✗ Branch 2 not taken.
14339 if (lbm_type_of(val) == LBM_TYPE_CUSTOM) {
340 14339 res = (lbm_uint)lbm_car(val);
341 }
342 14339 return res;
343 }
344
345 182889 uint8_t lbm_dec_as_char(lbm_value a) {
346 182889 uint8_t r = 0;
347
10/10
✓ Branch 1 taken 182123 times.
✓ Branch 2 taken 163 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.
182889 switch (lbm_type_of_functional(a)) {
348 182123 case LBM_TYPE_CHAR:
349 182123 r = (uint8_t)lbm_dec_char(a); break;
350 163 case LBM_TYPE_I:
351 163 r = (uint8_t)lbm_dec_i(a); break;
352 86 case LBM_TYPE_U:
353 86 r = (uint8_t)lbm_dec_u(a); break;
354 85 case LBM_TYPE_I32:
355 85 r = (uint8_t)lbm_dec_i32(a); break;
356 85 case LBM_TYPE_U32:
357 85 r = (uint8_t)lbm_dec_u32(a); break;
358 85 case LBM_TYPE_FLOAT:
359 85 r = (uint8_t)lbm_dec_float(a); break;
360 85 case LBM_TYPE_I64:
361 85 r = (uint8_t)lbm_dec_i64(a); break;
362 85 case LBM_TYPE_U64:
363 85 r = (uint8_t)lbm_dec_u64(a); break;
364 85 case LBM_TYPE_DOUBLE:
365 85 r = (uint8_t) lbm_dec_double(a); break;
366 }
367 182889 return r;
368 }
369
370 19226459 uint32_t lbm_dec_as_u32(lbm_value a) {
371 19226459 uint32_t r = 0;
372
9/9
✓ Branch 1 taken 1405430 times.
✓ Branch 2 taken 4188156 times.
✓ Branch 3 taken 360529 times.
✓ Branch 4 taken 13271820 times.
✓ Branch 5 taken 94 times.
✓ Branch 6 taken 85 times.
✓ Branch 7 taken 253 times.
✓ Branch 8 taken 85 times.
✓ Branch 9 taken 7 times.
19226459 switch (lbm_type_of_functional(a)) {
373 1405430 case LBM_TYPE_CHAR:
374 1405430 r = (uint32_t)lbm_dec_char(a); break;
375 4188156 case LBM_TYPE_I:
376 4188156 r = (uint32_t)lbm_dec_i(a); break;
377 360529 case LBM_TYPE_U:
378 360529 r = (uint32_t)lbm_dec_u(a); break;
379 13271820 case LBM_TYPE_I32: /* fall through */
380 case LBM_TYPE_U32:
381 13271820 r = (uint32_t)lbm_dec_u32(a); break;
382 94 case LBM_TYPE_FLOAT:
383 94 r = (uint32_t)lbm_dec_float(a); break;
384 85 case LBM_TYPE_I64:
385 85 r = (uint32_t)lbm_dec_i64(a); break;
386 253 case LBM_TYPE_U64:
387 253 r = (uint32_t)lbm_dec_u64(a); break;
388 85 case LBM_TYPE_DOUBLE:
389 85 r = (uint32_t)lbm_dec_double(a); break;
390 }
391 19226459 return r;
392 }
393
394 513356425 int32_t lbm_dec_as_i32(lbm_value a) {
395 513356425 int32_t r = 0;
396
10/10
✓ Branch 1 taken 14156265 times.
✓ Branch 2 taken 488158525 times.
✓ Branch 3 taken 24422 times.
✓ Branch 4 taken 11014319 times.
✓ Branch 5 taken 91 times.
✓ Branch 6 taken 85 times.
✓ Branch 7 taken 169 times.
✓ Branch 8 taken 169 times.
✓ Branch 9 taken 85 times.
✓ Branch 10 taken 2295 times.
513356425 switch (lbm_type_of_functional(a)) {
397 14156265 case LBM_TYPE_CHAR:
398 14156265 r = (int32_t)lbm_dec_char(a); break;
399 488158525 case LBM_TYPE_I:
400 488158525 r = (int32_t)lbm_dec_i(a); break;
401 24422 case LBM_TYPE_U:
402 24422 r = (int32_t)lbm_dec_u(a); break;
403 11014319 case LBM_TYPE_I32:
404 11014319 r = (int32_t)lbm_dec_i32(a); break;
405 91 case LBM_TYPE_U32:
406 91 r = (int32_t)lbm_dec_u32(a); break;
407 85 case LBM_TYPE_FLOAT:
408 85 r = (int32_t)lbm_dec_float(a); break;
409 169 case LBM_TYPE_I64:
410 169 r = (int32_t)lbm_dec_i64(a); break;
411 169 case LBM_TYPE_U64:
412 169 r = (int32_t)lbm_dec_u64(a); break;
413 85 case LBM_TYPE_DOUBLE:
414 85 r = (int32_t) lbm_dec_double(a); break;
415 }
416 513356425 return r;
417 }
418
419 253669772 int64_t lbm_dec_as_i64(lbm_value a) {
420 253669772 int64_t r = 0;
421
10/10
✓ Branch 1 taken 7813529 times.
✓ Branch 2 taken 231561612 times.
✓ Branch 3 taken 506 times.
✓ Branch 4 taken 841 times.
✓ Branch 5 taken 1793 times.
✓ Branch 6 taken 205 times.
✓ Branch 7 taken 14290749 times.
✓ Branch 8 taken 337 times.
✓ Branch 9 taken 169 times.
✓ Branch 10 taken 31 times.
253669772 switch (lbm_type_of_functional(a)) {
422 7813529 case LBM_TYPE_CHAR:
423 7813529 r = (int64_t)lbm_dec_char(a); break;
424 231561612 case LBM_TYPE_I:
425 231561612 r = (int64_t)lbm_dec_i(a); break;
426 506 case LBM_TYPE_U:
427 506 r = (int64_t)lbm_dec_u(a); break;
428 841 case LBM_TYPE_I32:
429 841 r = (int64_t)lbm_dec_i32(a); break;
430 1793 case LBM_TYPE_U32:
431 1793 r = (int64_t)lbm_dec_u32(a); break;
432 205 case LBM_TYPE_FLOAT:
433 205 r = (int64_t)lbm_dec_float(a); break;
434 14290749 case LBM_TYPE_I64:
435 14290749 r = (int64_t)lbm_dec_i64(a); break;
436 337 case LBM_TYPE_U64:
437 337 r = (int64_t)lbm_dec_u64(a); break;
438 169 case LBM_TYPE_DOUBLE:
439 169 r = (int64_t) lbm_dec_double(a); break;
440 }
441 253669772 return r;
442 }
443
444 13559200 uint64_t lbm_dec_as_u64(lbm_value a) {
445 13559200 uint64_t r = 0;
446
10/10
✓ Branch 1 taken 1687425 times.
✓ Branch 2 taken 919353 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.
13559200 switch (lbm_type_of_functional(a)) {
447 1687425 case LBM_TYPE_CHAR:
448 1687425 r = (uint64_t)lbm_dec_char(a); break;
449 919353 case LBM_TYPE_I:
450 919353 r = (uint64_t)lbm_dec_i(a); break;
451 19410 case LBM_TYPE_U:
452 19410 r = (uint64_t)lbm_dec_u(a); break;
453 673 case LBM_TYPE_I32:
454 673 r = (uint64_t)lbm_dec_i32(a); break;
455 673 case LBM_TYPE_U32:
456 673 r = (uint64_t)lbm_dec_u32(a); break;
457 169 case LBM_TYPE_FLOAT:
458 169 r = (uint64_t)lbm_dec_float(a); break;
459 505 case LBM_TYPE_I64:
460 505 r = (uint64_t)lbm_dec_i64(a); break;
461 10930816 case LBM_TYPE_U64:
462 10930816 r = (uint64_t)lbm_dec_u64(a); break;
463 169 case LBM_TYPE_DOUBLE:
464 169 r = (uint64_t)lbm_dec_double(a); break;
465 }
466 13559200 return r;
467 }
468
469 1126213780 float lbm_dec_as_float(lbm_value a) {
470 1126213780 float r = 0;
471
10/10
✓ Branch 1 taken 311097810 times.
✓ Branch 2 taken 504152590 times.
✓ Branch 3 taken 478 times.
✓ Branch 4 taken 421 times.
✓ Branch 5 taken 536 times.
✓ Branch 6 taken 310960903 times.
✓ Branch 7 taken 421 times.
✓ Branch 8 taken 421 times.
✓ Branch 9 taken 85 times.
✓ Branch 10 taken 115 times.
1126213780 switch (lbm_type_of_functional(a)) {
472 311097810 case LBM_TYPE_CHAR:
473 311097810 r = (float)lbm_dec_char(a); break;
474 504152590 case LBM_TYPE_I:
475 504152590 r = (float)lbm_dec_i(a); break;
476 478 case LBM_TYPE_U:
477 478 r = (float)lbm_dec_u(a); break;
478 421 case LBM_TYPE_I32:
479 421 r = (float)lbm_dec_i32(a); break;
480 536 case LBM_TYPE_U32:
481 536 r = (float)lbm_dec_u32(a); break;
482 310960903 case LBM_TYPE_FLOAT:
483 310960903 r = (float)lbm_dec_float(a); break;
484 421 case LBM_TYPE_I64:
485 421 r = (float)lbm_dec_i64(a); break;
486 421 case LBM_TYPE_U64:
487 421 r = (float)lbm_dec_u64(a); break;
488 85 case LBM_TYPE_DOUBLE:
489 85 r = (float)lbm_dec_double(a); break;
490 }
491 1126213780 return r;
492 }
493
494 1692435 double lbm_dec_as_double(lbm_value a) {
495 1692435 double r = 0;
496
10/10
✓ Branch 1 taken 843316 times.
✓ Branch 2 taken 842451 times.
✓ Branch 3 taken 422 times.
✓ Branch 4 taken 421 times.
✓ Branch 5 taken 421 times.
✓ Branch 6 taken 1160 times.
✓ Branch 7 taken 421 times.
✓ Branch 8 taken 421 times.
✓ Branch 9 taken 3395 times.
✓ Branch 10 taken 7 times.
1692435 switch (lbm_type_of_functional(a)) {
497 843316 case LBM_TYPE_CHAR:
498 843316 r = (double)lbm_dec_char(a); break;
499 842451 case LBM_TYPE_I:
500 842451 r = (double)lbm_dec_i(a); break;
501 422 case LBM_TYPE_U:
502 422 r = (double)lbm_dec_u(a); break;
503 421 case LBM_TYPE_I32:
504 421 r = (double)lbm_dec_i32(a); break;
505 421 case LBM_TYPE_U32:
506 421 r = (double)lbm_dec_u32(a); break;
507 1160 case LBM_TYPE_FLOAT:
508 1160 r = (double)lbm_dec_float(a); break;
509 421 case LBM_TYPE_I64:
510 421 r = (double)lbm_dec_i64(a); break;
511 421 case LBM_TYPE_U64:
512 421 r = (double)lbm_dec_u64(a); break;
513 3395 case LBM_TYPE_DOUBLE:
514 3395 r = (double)lbm_dec_double(a); break;
515 }
516 1692435 return r;
517 }
518
519 // ////////////////////////////////////////////////////////////
520 // HEAP MANAGEMENT
521
522 67082 static bool generate_freelist(size_t num_cells) {
523 67082 size_t i = 0;
524
525
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67082 times.
67082 if (!lbm_heap_state.heap) return false;
526
527 67082 lbm_heap_state.freelist = lbm_enc_cons_ptr(0);
528
529 lbm_cons_t *t;
530
531 // Add all cells to free list
532
2/2
✓ Branch 0 taken 616835574 times.
✓ Branch 1 taken 67082 times.
616902656 for (i = 1; i < num_cells; i ++) {
533 616835574 t = lbm_ref_cell(lbm_enc_cons_ptr(i-1));
534 616835574 t->car = ENC_SYM_RECOVERED; // all cars in free list are "RECOVERED"
535 616835574 t->cdr = lbm_enc_cons_ptr(i);
536 }
537
538 // Replace the incorrect pointer at the last cell.
539 67082 t = lbm_ref_cell(lbm_enc_cons_ptr(num_cells-1));
540 67082 t->car = ENC_SYM_RECOVERED;
541 67082 t->cdr = ENC_SYM_NIL;
542
543 67082 return true;
544 }
545
546 1612275 void lbm_nil_freelist(void) {
547 1612275 lbm_heap_state.freelist = ENC_SYM_NIL;
548 1612275 lbm_heap_state.num_free = 0;
549 1612275 }
550
551 67082 static void heap_init_state(lbm_cons_t *addr, lbm_uint num_cells,
552 lbm_uint* gc_stack_storage, lbm_uint gc_stack_size) {
553 67082 lbm_heap_state.heap = addr;
554 67082 lbm_heap_state.heap_bytes = (unsigned int)(num_cells * sizeof(lbm_cons_t));
555 67082 lbm_heap_state.heap_size = num_cells;
556
557 67082 lbm_stack_create(&lbm_heap_state.gc_stack, gc_stack_storage, gc_stack_size);
558
559 67082 lbm_heap_state.num_free = num_cells;
560 67082 lbm_heap_state.num_alloc_arrays = 0;
561 67082 lbm_heap_state.gc_num = 0;
562 67082 lbm_heap_state.gc_marked = 0;
563 67082 lbm_heap_state.gc_recovered = 0;
564 67082 lbm_heap_state.gc_recovered_arrays = 0;
565 67082 lbm_heap_state.gc_least_free = num_cells;
566 67082 lbm_heap_state.gc_last_free = num_cells;
567 67082 }
568
569 1612275 void lbm_heap_new_freelist_length(void) {
570 1612275 lbm_heap_state.gc_last_free = lbm_heap_state.num_free;
571
2/2
✓ Branch 0 taken 12431 times.
✓ Branch 1 taken 1599844 times.
1612275 if (lbm_heap_state.num_free < lbm_heap_state.gc_least_free)
572 12431 lbm_heap_state.gc_least_free = lbm_heap_state.num_free;
573 1612275 }
574
575 67082 bool lbm_heap_init(lbm_cons_t *addr, lbm_uint num_cells,
576 lbm_uint gc_stack_size) {
577
578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67082 times.
67082 if (((uintptr_t)addr % 8) != 0) return false;
579
580 67082 memset(addr,0, sizeof(lbm_cons_t) * num_cells);
581
582 67082 lbm_uint *gc_stack_storage = (lbm_uint*)lbm_malloc(gc_stack_size * sizeof(lbm_uint));
583
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67082 times.
67082 if (gc_stack_storage == NULL) return 0;
584
585 67082 heap_init_state(addr, num_cells,
586 gc_stack_storage, gc_stack_size);
587
588 67082 lbm_heaps[LBM_RAM_HEAP] = addr;
589
590 67082 return generate_freelist(num_cells);
591 }
592
593
594 1415208332 lbm_value lbm_heap_allocate_cell(lbm_type ptr_type, lbm_value car, lbm_value cdr) {
595 lbm_value r;
596 1415208332 lbm_value cell = lbm_heap_state.freelist;
597
2/2
✓ Branch 0 taken 1414888751 times.
✓ Branch 1 taken 319581 times.
1415208332 if (cell) {
598 1414888751 lbm_uint heap_ix = lbm_dec_ptr(cell);
599 1414888751 lbm_heap_state.freelist = lbm_heap_state.heap[heap_ix].cdr;
600 1414888751 lbm_heap_state.num_free--;
601 1414888751 lbm_heap_state.heap[heap_ix].car = car;
602 1414888751 lbm_heap_state.heap[heap_ix].cdr = cdr;
603 1414888751 r = lbm_set_ptr_type(cell, ptr_type);
604 } else {
605 319581 r = ENC_SYM_MERROR;
606 }
607 1415208332 return r;
608 }
609
610 3772789 lbm_value lbm_heap_allocate_list(lbm_uint n) {
611
2/2
✓ Branch 0 taken 10112 times.
✓ Branch 1 taken 3762677 times.
3772789 if (n == 0) return ENC_SYM_NIL;
612
2/2
✓ Branch 1 taken 4675 times.
✓ Branch 2 taken 3758002 times.
3762677 if (lbm_heap_num_free() < n) return ENC_SYM_MERROR;
613 // Here the freelist is guaranteed to be a cons_cell.
614
615 3758002 lbm_value curr = lbm_heap_state.freelist;
616 3758002 lbm_value res = curr;
617
618 3758002 lbm_cons_t *c_cell = NULL;
619 3758002 lbm_uint count = 0;
620 do {
621 19405432 c_cell = lbm_ref_cell(curr);
622 19405432 c_cell->car = ENC_SYM_NIL;
623 19405432 curr = c_cell->cdr;
624 19405432 count ++;
625
2/2
✓ Branch 0 taken 15647430 times.
✓ Branch 1 taken 3758002 times.
19405432 } while (count < n);
626 3758002 lbm_heap_state.freelist = curr;
627 3758002 c_cell->cdr = ENC_SYM_NIL;
628 3758002 lbm_heap_state.num_free-=count;
629 3758002 return res;
630 }
631
632 65940909 lbm_value lbm_heap_allocate_list_init_va(unsigned int n, va_list valist) {
633
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65940909 times.
65940909 if (n == 0) return ENC_SYM_NIL;
634
2/2
✓ Branch 1 taken 99703 times.
✓ Branch 2 taken 65841206 times.
65940909 if (lbm_heap_num_free() < n) return ENC_SYM_MERROR;
635
636 65841206 lbm_value curr = lbm_heap_state.freelist;
637 65841206 lbm_value res = curr;
638
639 65841206 lbm_cons_t *c_cell = NULL;
640 65841206 unsigned int count = 0;
641 do {
642 132534146 c_cell = lbm_ref_cell(curr);
643 132534146 c_cell->car = va_arg(valist, lbm_value);
644 132534146 curr = c_cell->cdr;
645 132534146 count ++;
646
2/2
✓ Branch 0 taken 66692940 times.
✓ Branch 1 taken 65841206 times.
132534146 } while (count < n);
647 65841206 lbm_heap_state.freelist = curr;
648 65841206 c_cell->cdr = ENC_SYM_NIL;
649 65841206 lbm_heap_state.num_free-=count;
650 65841206 return res;
651 }
652
653 65940909 lbm_value lbm_heap_allocate_list_init(unsigned int n, ...) {
654 va_list valist;
655 65940909 va_start(valist, n);
656 65940909 lbm_value r = lbm_heap_allocate_list_init_va(n, valist);
657 65940909 va_end(valist);
658 65940909 return r;
659 }
660
661 5 lbm_uint lbm_heap_num_allocated(void) {
662 5 return lbm_heap_state.heap_size - lbm_heap_state.num_free;
663 }
664 5 lbm_uint lbm_heap_size(void) {
665 5 return lbm_heap_state.heap_size;
666 }
667
668 4 lbm_uint lbm_heap_size_bytes(void) {
669 4 return lbm_heap_state.heap_bytes;
670 }
671
672 783 void lbm_get_heap_state(lbm_heap_state_t *res) {
673 783 *res = lbm_heap_state;
674 783 }
675
676 7 lbm_uint lbm_get_gc_stack_max(void) {
677 7 return lbm_get_max_stack(&lbm_heap_state.gc_stack);
678 }
679
680 6 lbm_uint lbm_get_gc_stack_size(void) {
681 6 return lbm_heap_state.gc_stack.size;
682 }
683
684 310848 static inline void value_assign(lbm_value *a, lbm_value b) {
685 310848 lbm_value a_old = *a & LBM_GC_MASK;
686 310848 *a = a_old | (b & ~LBM_GC_MASK);
687 310848 }
688
689 // ////////////////////////////////////////////////////////////
690 // Deutch-Schorr-Waite (DSW) pointer reversal GC
691 //
692 /*
693 Deutch-Schorr-Waite (DSW) pointer reversal GC for 2-ptr cells
694 with a hack-solution for the lisp-array case (n-ptr cells).
695
696 DSW visits each branch node 3 times compared to 2 times for
697 the stack based recursive mark.
698 Where the stack based recursive mark performs a stack push/pop,
699 DSW rearranges the, current, prev, next and a ptr field on
700 the heap.
701
702 DSW changes the structure of the heap and it introduces an
703 invalid pointer (LBM_PTR_NULL) temporarily during marking.
704 Since the heap will be "messed up" while marking, a mutex
705 is introuded to keep other processes out of the heap while
706 marking.
707 */
708
709 5778 static int do_nothing(lbm_value v, bool shared, void *arg) {
710 (void) v;
711 (void) shared;
712 (void) arg;
713 5778 return TRAV_FUN_SUBTREE_CONTINUE;
714 }
715
716 2053 void lbm_gc_mark_phase_ptr_rev(lbm_value root) {
717 2053 lbm_mutex_lock(&lbm_const_heap_mutex);
718 2053 lbm_ptr_rev_trav(do_nothing, root, NULL);
719 2053 lbm_mutex_unlock(&lbm_const_heap_mutex);
720 2053 }
721
722 // ////////////////////////////////////////////////////////////
723 // Check if a value is currently on the stack
724
725 75373 bool active_ptr(lbm_value p) {
726 75373 lbm_stack_t *s = &lbm_heap_state.gc_stack;
727 75373 bool r = false;
728
2/2
✓ Branch 0 taken 196950 times.
✓ Branch 1 taken 75373 times.
272323 for (lbm_uint i = 0; i < s->sp; i ++) {
729
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 196950 times.
196950 if (p == s->data[i]) {
730 ✗ r = true;
731 ✗ break;
732 }
733 }
734 75373 return r;
735 }
736
737 // ////////////////////////////////////////////////////////////
738 // Explicit stack "recursive" mark phase
739
740 // Trees are marked in a left subtree before rigth subtree, car first then cdr,
741 // way to favor lisp lists. This means that stack will grow slowly when
742 // marking right-leaning (cdr-recursive) data-structures while left-leaning
743 // (car-recursive) structures uses a lot of stack.
744
745 // Lisp arrays contain an extra book-keeping field to keep track
746 // of how far into the array the marking process has gone.
747
748 // TODO: DSW should be used as a last-resort if the GC stack is exhausted.
749 // If we use DSW as last-resort can we get away with a way smaller
750 // GC stack and unchanged performance (on sensible programs)?
751
752 17931582 void lbm_gc_mark_phase(lbm_value root) {
753 lbm_value t_ptr;
754 17931582 lbm_stack_t *s = &lbm_heap_state.gc_stack;
755 17931582 s->data[s->sp++] = root;
756
757
2/2
✓ Branch 1 taken 116618501 times.
✓ Branch 2 taken 17931582 times.
134550083 while (!lbm_stack_is_empty(s)) {
758 lbm_value curr;
759 116618501 lbm_pop(s, &curr);
760
761 271299691 mark_shortcut:
762
763
2/2
✓ Branch 1 taken 169487581 times.
✓ Branch 2 taken 101812110 times.
271299691 if (!lbm_is_ptr(curr) ||
764
2/2
✓ Branch 0 taken 337 times.
✓ Branch 1 taken 169487244 times.
169487581 (curr & LBM_PTR_TO_CONSTANT_BIT)) {
765 112284052 continue;
766 }
767
768 169487244 lbm_cons_t *cell = &lbm_heap_state.heap[lbm_dec_ptr(curr)];
769
770
2/2
✓ Branch 1 taken 10399862 times.
✓ Branch 2 taken 159087382 times.
169487244 if (lbm_get_gc_mark(cell->cdr)) {
771 10399862 continue;
772 }
773
774 159087382 t_ptr = lbm_type_of(curr);
775
776 // An array is marked in O(N) time using an additional 32bit
777 // value per array that keeps track of how far into the array GC
778 // has progressed.
779
2/2
✓ Branch 0 taken 75373 times.
✓ Branch 1 taken 159012009 times.
159087382 if (t_ptr == LBM_TYPE_LISPARRAY) {
780 // O(stack_size) cost when seeing array. This protects
781 // against gc recursing into arrays via cycles.
782
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 75373 times.
75373 if (active_ptr(curr)) {
783 ✗ continue; // Already in process of marking this array, abort cycle!
784 }
785 75373 lbm_array_header_extended_t *arr = (lbm_array_header_extended_t*)cell->car;
786 75373 lbm_value *arrdata = (lbm_value *)arr->data;
787
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 75370 times.
75373 if (!lbm_push(s, curr)) { // put array back as bookkeeping.
788 // Switch to pointer reversal here.
789 // This is a new array that we have not yet started descending into.
790 3 lbm_gc_mark_phase_ptr_rev(curr);
791 3 continue;
792 }
793 // Example A: Array with 10 elements
794 // A: assume arr->index == 9
795 // Example B: Array with 0 elements
796 // B: assume arr->index == 0
797
2/2
✓ Branch 0 taken 73816 times.
✓ Branch 1 taken 1554 times.
75370 if (arr->index < ((arr->size/(sizeof(lbm_value))))) { // A: 9 < 10
798 // A: True path
799 // B: 0 < 0
800 // B: False path
801
3/4
✓ Branch 1 taken 4092 times.
✓ Branch 2 taken 69724 times.
✓ Branch 3 taken 4092 times.
✗ Branch 4 not taken.
73816 if (lbm_is_ptr(arrdata[arr->index]) && ((arrdata[arr->index] & LBM_PTR_TO_CONSTANT_BIT) == 0) &&
802
2/2
✓ Branch 0 taken 3630 times.
✓ Branch 1 taken 462 times.
4092 !((arrdata[arr->index] & LBM_CONTINUATION_INTERNAL) == LBM_CONTINUATION_INTERNAL)) {
803
804 3630 lbm_cons_t *elt = &lbm_heap_state.heap[lbm_dec_ptr(arrdata[arr->index])];
805
1/2
✓ Branch 1 taken 3630 times.
✗ Branch 2 not taken.
3630 if (!lbm_get_gc_mark(elt->cdr)) {
806 3630 curr = arrdata[arr->index];
807 3630 arr->index++;
808 3630 goto mark_shortcut;
809 }
810 }
811 // We need one more round of the loop if there is at least one element left in the array.
812 // If we skip this check things should work the same, but there will be one more recurse
813 // before we discover in the earlier conditional that we have reached the end of the array.
814
2/2
✓ Branch 0 taken 63520 times.
✓ Branch 1 taken 6666 times.
70186 if (arr->index < ((arr->size/(sizeof(lbm_value))) - 1)) { // A: 9 < 9
815 // A: False path
816 63520 arr->index++;
817 63520 continue;
818 }
819 }
820 8220 arr->index = 0;
821 8220 lbm_stack_drop(s,1); // Drop the bookkeeping element.
822 // elem_dropped == cell
823 // Cell is the array reference. so this marks the array reference itself
824 // after having marked all the elements.
825 8220 cell->cdr = lbm_set_gc_mark(cell->cdr);
826 8220 lbm_heap_state.gc_marked ++;
827 8220 continue;
828
2/2
✓ Branch 0 taken 822564 times.
✓ Branch 1 taken 158189445 times.
159012009 } else if (t_ptr == LBM_TYPE_CHANNEL) {
829 822564 cell->cdr = lbm_set_gc_mark(cell->cdr);
830 822564 lbm_heap_state.gc_marked ++;
831 // TODO: Can channels be explicitly freed ?
832
1/2
✓ Branch 0 taken 822564 times.
✗ Branch 1 not taken.
822564 if (cell->car != ENC_SYM_NIL) {
833 822564 lbm_char_channel_t *chan = (lbm_char_channel_t *)cell->car;
834 // A channel dependency is currently assumed to be a byte array (a string)
835 // or NIL.
836 822564 curr = chan->dependency;
837 822564 goto mark_shortcut;
838 }
839 ✗ continue;
840 }
841
842 158189445 cell->cdr = lbm_set_gc_mark(cell->cdr);
843 158189445 lbm_heap_state.gc_marked ++;
844
845
2/2
✓ Branch 0 taken 153854996 times.
✓ Branch 1 taken 4334449 times.
158189445 if (t_ptr == LBM_TYPE_CONS) {
846
2/2
✓ Branch 1 taken 98621819 times.
✓ Branch 2 taken 55233177 times.
153854996 if (lbm_is_ptr(cell->cdr)) {
847
2/2
✓ Branch 1 taken 2050 times.
✓ Branch 2 taken 98619769 times.
98621819 if (!lbm_push(s, cell->cdr)) {
848 // This is the only place where we grab a new stack position.
849 // If the stack is full switch to ptr-rev GC.
850 2050 lbm_gc_mark_phase_ptr_rev(cell->cdr);
851 }
852 }
853 153854996 curr = cell->car;
854 153854996 goto mark_shortcut; // Skip a push/pop
855 }
856 }
857 17931582 }
858
859 //Environments are proper lists with a 2 element list stored in each car.
860 53243061 void lbm_gc_mark_env(lbm_value env) {
861 53243061 lbm_value curr = env;
862 lbm_cons_t *c;
863
864
2/2
✓ Branch 1 taken 9044765 times.
✓ Branch 2 taken 53243061 times.
62287826 while (lbm_is_ptr(curr)) {
865 9044765 c = lbm_ref_cell(curr);
866 9044765 c->cdr = lbm_set_gc_mark(c->cdr); // mark the environent list structure.
867 9044765 lbm_cons_t *b = lbm_ref_cell(c->car);
868 9044765 b->cdr = lbm_set_gc_mark(b->cdr); // mark the binding list head cell.
869 9044765 lbm_heap_state.gc_marked +=2;
870
2/2
✓ Branch 1 taken 5809556 times.
✓ Branch 2 taken 3235209 times.
9044765 if (lbm_is_ptr(b->cdr)) {
871 5809556 lbm_gc_mark_phase(b->cdr); // mark the bound object.
872 }
873 9044765 curr = c->cdr;
874 }
875 53243061 }
876
877
878 // Marks a continuation stack which contains CONTINUATION markers.
879 // Continuation markers look like pointers to the GC and would be
880 // incorrectly followed unless they are filtered out.
881 1650261 void lbm_gc_mark_continuation_stack(lbm_uint *aux_data, lbm_uint aux_size) {
882
2/2
✓ Branch 0 taken 31692389 times.
✓ Branch 1 taken 1650261 times.
33342650 for (lbm_uint i = 0; i < aux_size; i ++) {
883
2/2
✓ Branch 1 taken 18923409 times.
✓ Branch 2 taken 12768980 times.
31692389 if (lbm_is_ptr(aux_data[i])) {
884 18923409 lbm_type pt_t = lbm_type_of(aux_data[i]);
885 18923409 lbm_uint pt_v = lbm_dec_ptr(aux_data[i]);
886
3/4
✓ Branch 0 taken 18923409 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8463065 times.
✓ Branch 3 taken 10460344 times.
18923409 if( pt_t >= LBM_POINTER_TYPE_FIRST &&
887 8463065 pt_t <= LBM_POINTER_TYPE_LAST &&
888
1/2
✓ Branch 0 taken 8463065 times.
✗ Branch 1 not taken.
8463065 pt_v < lbm_heap_state.heap_size) {
889 8463065 lbm_gc_mark_phase(aux_data[i]);
890 }
891 }
892 }
893 1650261 }
894
895 3311782 void lbm_gc_mark_roots(lbm_uint *roots, lbm_uint num_roots) {
896
2/2
✓ Branch 0 taken 6639665 times.
✓ Branch 1 taken 3311782 times.
9951447 for (lbm_uint i = 0; i < num_roots; i ++) {
897
2/2
✓ Branch 1 taken 2300361 times.
✓ Branch 2 taken 4339304 times.
6639665 if (lbm_is_ptr(roots[i])) {
898 2300361 lbm_gc_mark_phase(roots[i]);
899 }
900 }
901 3311782 }
902
903 // Sweep moves non-marked heap objects to the free list.
904 1612275 int lbm_gc_sweep_phase(void) {
905 1612275 unsigned int i = 0;
906 1612275 lbm_cons_t *heap = (lbm_cons_t *)lbm_heap_state.heap;
907
908
2/2
✓ Branch 0 taken 3838257664 times.
✓ Branch 1 taken 1612275 times.
3839869939 for (i = 0; i < lbm_heap_state.heap_size; i ++) {
909
2/2
✓ Branch 1 taken 176540528 times.
✓ Branch 2 taken 3661717136 times.
3838257664 if ( lbm_get_gc_mark(heap[i].cdr)) {
910 176540528 heap[i].cdr = lbm_clr_gc_mark(heap[i].cdr);
911 } else {
912 // Check if this cell is a pointer to an array
913 // and free it.
914
2/2
✓ Branch 1 taken 645993634 times.
✓ Branch 2 taken 3015723502 times.
3661717136 if (lbm_type_of(heap[i].cdr) == LBM_TYPE_SYMBOL) {
915
7/7
✓ Branch 0 taken 16793872 times.
✓ Branch 1 taken 86999 times.
✓ Branch 2 taken 1752236 times.
✓ Branch 3 taken 919114 times.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 84 times.
✓ Branch 6 taken 626441324 times.
645993634 switch(heap[i].cdr) {
916
917 16793872 case ENC_SYM_IND_I_TYPE: /* fall through */
918 case ENC_SYM_IND_U_TYPE:
919 case ENC_SYM_IND_F_TYPE:
920 16793872 lbm_memory_free((lbm_uint*)heap[i].car);
921 16793872 break;
922 86999 case ENC_SYM_DEFRAG_LISPARRAY_TYPE: /* fall through */
923 case ENC_SYM_DEFRAG_ARRAY_TYPE:
924 86999 lbm_defrag_mem_free((lbm_uint*)heap[i].car);
925 86999 break;
926 1752236 case ENC_SYM_LISPARRAY_TYPE: /* fall through */
927 case ENC_SYM_ARRAY_TYPE:{
928 1752236 lbm_array_header_t *arr = (lbm_array_header_t*)heap[i].car;
929 1752236 lbm_memory_free((lbm_uint *)arr->data);
930 1752236 lbm_heap_state.gc_recovered_arrays++;
931 1752236 lbm_memory_free((lbm_uint *)arr);
932 1752236 } break;
933 919114 case ENC_SYM_CHANNEL_TYPE:{
934 919114 lbm_char_channel_t *chan = (lbm_char_channel_t*)heap[i].car;
935 919114 lbm_memory_free((lbm_uint*)chan->state);
936 919114 lbm_memory_free((lbm_uint*)chan);
937 919114 } break;
938 5 case ENC_SYM_CUSTOM_TYPE: {
939 5 lbm_uint *t = (lbm_uint*)heap[i].car;
940 5 lbm_custom_type_destroy(t);
941 5 lbm_memory_free(t);
942 5 } break;
943 84 case ENC_SYM_DEFRAG_MEM_TYPE: {
944 84 lbm_uint *ptr = (lbm_uint *)heap[i].car;
945 84 lbm_defrag_mem_destroy(ptr);
946 84 } break;
947 626441324 default:
948 626441324 break;
949 }
950 }
951 // create pointer to use as new freelist
952 3661717136 lbm_uint addr = lbm_enc_cons_ptr(i);
953
954 // Clear the "freed" cell.
955 3661717136 heap[i].car = ENC_SYM_RECOVERED;
956 3661717136 heap[i].cdr = lbm_heap_state.freelist;
957 3661717136 lbm_heap_state.freelist = addr;
958 3661717136 lbm_heap_state.num_free++;
959 3661717136 lbm_heap_state.gc_recovered ++;
960 }
961 }
962 1612275 return 1;
963 }
964
965 1612275 void lbm_gc_state_inc(void) {
966 1612275 lbm_heap_state.gc_num ++;
967 1612275 lbm_heap_state.gc_recovered = 0;
968 1612275 lbm_heap_state.gc_marked = 0;
969 1612275 }
970
971 // construct, alter and break apart
972 1412164230 lbm_value lbm_cons(lbm_value car, lbm_value cdr) {
973 1412164230 return lbm_heap_allocate_cell(LBM_TYPE_CONS, car, cdr);
974 }
975
976 825574764 lbm_value lbm_car(lbm_value c){
977
2/2
✓ Branch 1 taken 825572178 times.
✓ Branch 2 taken 2586 times.
825574764 if (lbm_is_ptr(c) ){
978 825572178 lbm_cons_t *cell = lbm_ref_cell(c);
979 825572178 return cell->car;
980 }
981
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 2482 times.
2586 return c ? ENC_SYM_TERROR : c; //nil if c == nil
982 }
983
984 // TODO: Many comparisons "is this the nil symbol" can be
985 // streamlined a bit. NIL is 0 and cannot be confused with any other
986 // lbm_value.
987
988 138923 lbm_value lbm_caar(lbm_value c) {
989 138923 lbm_value tmp = ENC_SYM_NIL;
990
1/2
✓ Branch 1 taken 138923 times.
✗ Branch 2 not taken.
138923 if (lbm_is_ptr(c)) {
991 138923 tmp = lbm_ref_cell(c)->car;
992
1/2
✓ Branch 1 taken 138923 times.
✗ Branch 2 not taken.
138923 if (lbm_is_ptr(tmp)) {
993 138923 return lbm_ref_cell(tmp)->car;
994 }
995 }
996 ✗ return c || tmp ? ENC_SYM_TERROR : c; //nil if not something else
997 }
998
999
1000 35867 lbm_value lbm_cadr(lbm_value c) {
1001 35867 lbm_value tmp = ENC_SYM_NIL;
1002
1/2
✓ Branch 1 taken 35867 times.
✗ Branch 2 not taken.
35867 if (lbm_is_ptr(c)) {
1003 35867 tmp = lbm_ref_cell(c)->cdr;
1004
1/2
✓ Branch 1 taken 35867 times.
✗ Branch 2 not taken.
35867 if (lbm_is_ptr(tmp)) {
1005 35867 return lbm_ref_cell(tmp)->car;
1006 }
1007 }
1008 ✗ return c || tmp ? ENC_SYM_TERROR : c;
1009 }
1010
1011 92045596 lbm_value lbm_cdr(lbm_value c){
1012
2/2
✓ Branch 1 taken 90346048 times.
✓ Branch 2 taken 1699548 times.
92045596 if (lbm_is_ptr(c)) {
1013 90346048 lbm_cons_t *cell = lbm_ref_cell(c);
1014 90346048 return cell->cdr;
1015 }
1016
1/2
✓ Branch 0 taken 1699548 times.
✗ Branch 1 not taken.
1699548 return c ? ENC_SYM_TERROR: c;
1017 }
1018
1019 5 lbm_value lbm_cddr(lbm_value c) {
1020
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
5 if (lbm_is_ptr(c)) {
1021 3 lbm_value tmp = lbm_ref_cell(c)->cdr;
1022
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 time.
3 if (lbm_is_ptr(tmp)) {
1023 2 return lbm_ref_cell(tmp)->cdr;
1024 }
1025 }
1026
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 return c ? ENC_SYM_TERROR : c;
1027 }
1028
1029 /* calculate length of a proper list */
1030 3758564 lbm_uint lbm_list_length(lbm_value c) {
1031 3758564 lbm_uint len = 0;
1032
1033
2/2
✓ Branch 1 taken 17908541 times.
✓ Branch 2 taken 3758564 times.
21667105 while (lbm_is_cons(c)){
1034 17908541 len ++;
1035 17908541 c = lbm_ref_cell(c)->cdr;
1036 }
1037 3758564 return len;
1038 }
1039
1040 35402 lbm_value lbm_list_destructive_reverse(lbm_value list) {
1041
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 35398 times.
35402 if (!lbm_is_cons(list)) return list;
1042 35398 lbm_value curr = list;
1043 35398 lbm_value last_cell = ENC_SYM_NIL;
1044
1045
2/2
✓ Branch 1 taken 90585 times.
✓ Branch 2 taken 35398 times.
125983 while (lbm_is_cons_rw(curr)) {
1046 90585 lbm_cons_t *cell = lbm_ref_cell(curr);
1047 90585 lbm_value next = cell->cdr;
1048 90585 cell->cdr = last_cell;
1049 90585 last_cell = curr;
1050 90585 curr = next;
1051 }
1052 35398 return last_cell;
1053 }
1054
1055
1056 990868 lbm_value lbm_list_copy(int *m, lbm_value list) {
1057 990868 lbm_value curr = list;
1058 990868 lbm_uint n = lbm_list_length(list);
1059 990868 lbm_uint copy_n = n;
1060
4/4
✓ Branch 0 taken 18543 times.
✓ Branch 1 taken 972325 times.
✓ Branch 2 taken 16267 times.
✓ Branch 3 taken 2276 times.
990868 if (*m >= 0 && (lbm_uint)*m < n) {
1061 16267 copy_n = (lbm_uint)*m;
1062 } else { // *m == -1 or *m >= n
1063 974601 *m = (int)n;
1064 }
1065
2/2
✓ Branch 0 taken 693 times.
✓ Branch 1 taken 990175 times.
990868 if (copy_n == 0) return ENC_SYM_NIL;
1066 990175 lbm_uint new_list = lbm_heap_allocate_list(copy_n);
1067
2/2
✓ Branch 1 taken 1599 times.
✓ Branch 2 taken 988576 times.
990175 if (lbm_is_symbol(new_list)) return new_list;
1068 988576 lbm_value curr_targ = new_list;
1069
1070
4/4
✓ Branch 1 taken 11299872 times.
✓ Branch 2 taken 972555 times.
✓ Branch 3 taken 11283851 times.
✓ Branch 4 taken 16021 times.
12272427 while (lbm_is_cons(curr) && copy_n > 0) {
1071 11283851 lbm_cons_t *curr_cell = lbm_ref_cell(curr);
1072 11283851 lbm_cons_t *targ_cell = lbm_ref_cell(curr_targ);
1073 11283851 targ_cell->car = curr_cell->car;
1074 11283851 curr_targ = targ_cell->cdr;
1075 11283851 curr = curr_cell->cdr;
1076 11283851 copy_n --;
1077 }
1078 988576 return new_list;
1079 }
1080
1081 // Append for proper lists only
1082 // Destructive update of list1.
1083 72614 lbm_value lbm_list_append(lbm_value list1, lbm_value list2) {
1084
1085
2/4
✓ Branch 1 taken 72614 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 72614 times.
✗ Branch 4 not taken.
145228 if(lbm_is_list_rw(list1) &&
1086 72614 lbm_is_list(list2)) {
1087
1088 72614 lbm_value curr = list1;
1089 72614 lbm_cons_t *cell = lbm_ref_cell(curr);
1090
2/2
✓ Branch 1 taken 95069 times.
✓ Branch 2 taken 72614 times.
167683 while(lbm_is_cons_rw(cell->cdr)) {
1091 95069 curr = cell->cdr;
1092 95069 cell = lbm_ref_cell(cell->cdr);
1093 }
1094
2/2
✓ Branch 1 taken 90 times.
✓ Branch 2 taken 72524 times.
72614 if (lbm_is_symbol_nil(curr)) return list2;
1095 72524 lbm_set_cdr(curr, list2);
1096 72524 return list1;
1097 }
1098 ✗ return ENC_SYM_EERROR;
1099 }
1100
1101 252 lbm_value lbm_list_drop(unsigned int n, lbm_value ls) {
1102 252 lbm_value curr = ls;
1103
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) {
1104 2100 curr = lbm_ref_cell(curr)->cdr;
1105 2100 n --;
1106 }
1107 252 return curr;
1108 }
1109
1110 560280 lbm_value lbm_index_list(lbm_value l, int32_t n) {
1111 560280 lbm_value curr = l;
1112
1113
2/2
✓ Branch 0 taken 339 times.
✓ Branch 1 taken 559941 times.
560280 if (n < 0) {
1114 339 int32_t len = (int32_t)lbm_list_length(l);
1115 339 n = len + n;
1116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 339 times.
339 if (n < 0) return ENC_SYM_NIL;
1117 }
1118
1119
4/4
✓ Branch 1 taken 856364 times.
✓ Branch 2 taken 90 times.
✓ Branch 3 taken 296174 times.
✓ Branch 4 taken 560190 times.
856454 while (lbm_is_cons(curr) && n > 0) {
1120 296174 curr = lbm_ref_cell(curr)->cdr;
1121 296174 n --;
1122 }
1123
2/2
✓ Branch 1 taken 560190 times.
✓ Branch 2 taken 90 times.
560280 if (lbm_is_cons(curr)) {
1124 560190 return lbm_ref_cell(curr)->car;
1125 } else {
1126 90 return ENC_SYM_NIL;
1127 }
1128 }
1129
1130 // High-level arrays are just bytearrays but with a different tag and pointer type.
1131 // These arrays will be inspected by GC and the elements of the array will be marked.
1132
1133 // Arrays are part of the heap module because their lifespan is managed
1134 // by the garbage collector. The data in the array is not stored
1135 // in the "heap of cons cells".
1136 1771974 int lbm_heap_allocate_array_base(lbm_value *res, bool byte_array, lbm_uint size){
1137
1138 1771974 lbm_uint tag = ENC_SYM_ARRAY_TYPE;
1139 1771974 lbm_uint type = LBM_TYPE_ARRAY;
1140 1771974 lbm_array_header_t *array = NULL;
1141 1771974 lbm_array_header_extended_t *ext_array = NULL;
1142
1143
2/2
✓ Branch 0 taken 922904 times.
✓ Branch 1 taken 849070 times.
1771974 if (byte_array) {
1144 922904 array = (lbm_array_header_t*)lbm_malloc(sizeof(lbm_array_header_t));
1145 } else {
1146 849070 tag = ENC_SYM_LISPARRAY_TYPE;
1147 849070 type = LBM_TYPE_LISPARRAY;
1148 849070 size = sizeof(lbm_value) * size;
1149 849070 array = (lbm_array_header_t*)lbm_malloc(sizeof(lbm_array_header_extended_t));
1150 849070 ext_array = (lbm_array_header_extended_t*)array;
1151 }
1152
2/2
✓ Branch 0 taken 1768793 times.
✓ Branch 1 taken 3181 times.
1771974 if (array) {
1153
2/2
✓ Branch 0 taken 846490 times.
✓ Branch 1 taken 922303 times.
1768793 if (!byte_array) ext_array->index = 0;
1154
1155 1768793 array->data = NULL;
1156 1768793 array->size = size;
1157
2/2
✓ Branch 0 taken 1768341 times.
✓ Branch 1 taken 452 times.
1768793 if ( size > 0) {
1158 1768341 array->data = (lbm_uint*)lbm_malloc(size);
1159
2/2
✓ Branch 0 taken 14238 times.
✓ Branch 1 taken 1754103 times.
1768341 if (array->data == NULL) {
1160 14238 lbm_memory_free((lbm_uint*)array);
1161 14238 goto allocate_array_merror;
1162 }
1163 // It is more important to zero out high-level arrays.
1164 // 0 is symbol NIL which is perfectly safe for the GC to inspect.
1165 1754103 memset(array->data, 0, size);
1166 }
1167 // allocating a cell for array's heap-presence
1168 1754555 lbm_value cell = lbm_heap_allocate_cell(type, (lbm_uint) array, tag);
1169
2/2
✓ Branch 0 taken 1179 times.
✓ Branch 1 taken 1753376 times.
1754555 if (cell == ENC_SYM_MERROR) {
1170 1179 lbm_memory_free((lbm_uint*)array->data);
1171 1179 lbm_memory_free((lbm_uint*)array);
1172 1179 goto allocate_array_merror;
1173 }
1174 1753376 *res = cell;
1175 1753376 lbm_heap_state.num_alloc_arrays ++;
1176 1753376 return 1;
1177 }
1178 3181 allocate_array_merror:
1179 18598 *res = ENC_SYM_MERROR;
1180 18598 return 0;
1181 }
1182
1183 922904 int lbm_heap_allocate_array(lbm_value *res, lbm_uint size){
1184 922904 return lbm_heap_allocate_array_base(res, true, size);
1185 }
1186
1187 849070 int lbm_heap_allocate_lisp_array(lbm_value *res, lbm_uint size) {
1188 849070 return lbm_heap_allocate_array_base(res, false, size);
1189 }
1190
1191 3238 int lbm_lift_array(lbm_value *value, char *data, lbm_uint num_elt) {
1192
1193 3238 lbm_array_header_t *array = NULL;
1194 3238 lbm_value cell = lbm_heap_allocate_cell(LBM_TYPE_CONS, ENC_SYM_NIL, ENC_SYM_ARRAY_TYPE);
1195
1196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3238 times.
3238 if (cell == ENC_SYM_MERROR) {
1197 ✗ *value = cell;
1198 ✗ return 0;
1199 }
1200
1201 3238 array = (lbm_array_header_t*)lbm_malloc(sizeof(lbm_array_header_t));
1202
1203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3238 times.
3238 if (array == NULL) {
1204 ✗ lbm_set_car_and_cdr(cell, ENC_SYM_NIL, ENC_SYM_NIL);
1205 ✗ *value = ENC_SYM_MERROR;
1206 ✗ return 0;
1207 }
1208
1209 3238 array->data = (lbm_uint*)data;
1210 3238 array->size = num_elt;
1211
1212 3238 lbm_set_car(cell, (lbm_uint)array);
1213
1214 3238 cell = lbm_set_ptr_type(cell, LBM_TYPE_ARRAY);
1215 3238 *value = cell;
1216 3238 return 1;
1217 }
1218
1219 712749 lbm_int lbm_heap_array_get_size(lbm_value arr) {
1220
1221 712749 lbm_int r = -1;
1222 712749 lbm_array_header_t *header = lbm_dec_array_r(arr);
1223
1/2
✓ Branch 0 taken 712749 times.
✗ Branch 1 not taken.
712749 if (header) {
1224 712749 r = (lbm_int)header->size;
1225 }
1226 712749 return r;
1227 }
1228
1229 356555 const uint8_t *lbm_heap_array_get_data_ro(lbm_value arr) {
1230 356555 uint8_t *r = NULL;
1231 356555 lbm_array_header_t *header = lbm_dec_array_r(arr);
1232
1/2
✓ Branch 0 taken 356555 times.
✗ Branch 1 not taken.
356555 if (header) {
1233 356555 r = (uint8_t*)header->data;
1234 }
1235 356555 return r;
1236 }
1237
1238 /* Explicitly freeing an array.
1239
1240 This is a highly unsafe operation and can only be safely
1241 used if the heap cell that points to the array has not been made
1242 accessible to the program.
1243
1244 So This function can be used to free an array in case an array
1245 is being constructed and some error case appears while doing so
1246 If the array still have not become available it can safely be
1247 "explicitly" freed.
1248
1249 The problem is that if the "array" heap-cell is made available to
1250 the program, this cell can easily be duplicated and we would have
1251 to search the entire heap to find all cells pointing to the array
1252 memory in question and "null"-them out before freeing the memory
1253 */
1254
1255 336 int lbm_heap_explicit_free_array(lbm_value arr) {
1256
1257 336 int r = 0;
1258
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) {
1259 336 lbm_array_header_t *header = (lbm_array_header_t*)lbm_car(arr);
1260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
336 if (header == NULL) {
1261 ✗ return 0;
1262 }
1263 336 lbm_memory_free((lbm_uint*)header->data);
1264 336 lbm_memory_free((lbm_uint*)header);
1265
1266 336 arr = lbm_set_ptr_type(arr, LBM_TYPE_CONS);
1267 336 lbm_set_car(arr, ENC_SYM_NIL);
1268 336 lbm_set_cdr(arr, ENC_SYM_NIL);
1269 336 r = 1;
1270 }
1271
1272 336 return r;
1273 }
1274
1275 ✗ static bool dummy_flash_write(lbm_uint ix, lbm_uint val) {
1276 (void)ix;
1277 (void)val;
1278 ✗ return false;
1279 }
1280
1281 static const_heap_write_fun const_heap_write = dummy_flash_write;
1282
1283 67082 int lbm_const_heap_init(const_heap_write_fun w_fun,
1284 lbm_const_heap_t *heap,
1285 lbm_uint *addr) {
1286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67082 times.
67082 if (((uintptr_t)addr % 4) != 0) return 0;
1287
1288
2/2
✓ Branch 0 taken 66989 times.
✓ Branch 1 taken 93 times.
67082 if (!lbm_const_heap_mutex_initialized) {
1289 66989 lbm_mutex_init(&lbm_const_heap_mutex);
1290 66989 lbm_const_heap_mutex_initialized = true;
1291 }
1292
1293
2/2
✓ Branch 0 taken 66989 times.
✓ Branch 1 taken 93 times.
67082 if (!lbm_mark_mutex_initialized) {
1294 66989 lbm_mutex_init(&lbm_mark_mutex);
1295 66989 lbm_mark_mutex_initialized = true;
1296 }
1297
1298 67082 const_heap_write = w_fun;
1299
1300 67082 heap->heap = addr;
1301 67082 heap->size = 0;
1302 67082 heap->next = 0;
1303
1304 67082 lbm_const_heap_state = heap;
1305 // ref_cell views the lbm_uint array as an lbm_cons_t array
1306 67082 lbm_heaps[LBM_CONST_HEAP] = (lbm_cons_t*)addr;
1307 67082 return 1;
1308 }
1309
1310 40211 lbm_flash_status lbm_allocate_const_cell(lbm_value *res) {
1311 40211 lbm_flash_status r = LBM_FLASH_FULL;
1312
1313 40211 lbm_mutex_lock(&lbm_const_heap_mutex);
1314 // waste a cell if we have ended up unaligned after writing an array to flash.
1315
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 40118 times.
40211 if (lbm_const_heap_state->next % 2 == 1) {
1316 93 lbm_const_heap_state->next++;
1317 }
1318
1319
1/2
✓ Branch 0 taken 40211 times.
✗ Branch 1 not taken.
40211 if (lbm_const_heap_state &&
1320
2/2
✓ Branch 1 taken 40209 times.
✓ Branch 2 taken 2 times.
40211 (lbm_const_heap_state->next+1) < (uint32_t)lbm_image_get_write_index()) {
1321 // A cons cell uses two words.
1322 40209 lbm_value cell = lbm_const_heap_state->next;
1323 40209 lbm_const_heap_state->next += 2;
1324 40209 *res = ((cell >> 1) << LBM_ADDRESS_SHIFT) | LBM_PTR_BIT | LBM_TYPE_CONS | LBM_PTR_TO_CONSTANT_BIT;
1325 40209 r = LBM_FLASH_WRITE_OK;
1326 }
1327 40211 lbm_mutex_unlock(&lbm_const_heap_mutex);
1328 40211 return r;
1329 }
1330
1331 32804 lbm_flash_status lbm_allocate_const_raw(lbm_uint nwords, lbm_uint *res) {
1332 32804 lbm_flash_status r = LBM_FLASH_FULL;
1333
1334
1/2
✓ Branch 0 taken 32804 times.
✗ Branch 1 not taken.
32804 if (lbm_const_heap_state &&
1335
2/2
✓ Branch 1 taken 32803 times.
✓ Branch 2 taken 1 time.
32804 (lbm_const_heap_state->next + nwords) <= (uint32_t)lbm_image_get_write_index()) {
1336 32803 lbm_uint ix = lbm_const_heap_state->next;
1337 32803 *res = (lbm_uint)&lbm_const_heap_state->heap[ix];
1338 32803 lbm_const_heap_state->next += nwords;
1339 32803 r = LBM_FLASH_WRITE_OK;
1340 }
1341 32804 return r;
1342 }
1343
1344 763 lbm_flash_status lbm_write_const_raw(lbm_uint *data, lbm_uint n, lbm_uint *res) {
1345
1346 763 lbm_flash_status r = LBM_FLASH_FULL;
1347
1348
1/2
✓ Branch 0 taken 763 times.
✗ Branch 1 not taken.
763 if (lbm_const_heap_state &&
1349
1/2
✓ Branch 1 taken 763 times.
✗ Branch 2 not taken.
763 (lbm_const_heap_state->next + n) <= (uint32_t)lbm_image_get_write_index()) {
1350 763 lbm_uint ix = lbm_const_heap_state->next;
1351
1352
2/2
✓ Branch 0 taken 1545 times.
✓ Branch 1 taken 763 times.
2308 for (unsigned int i = 0; i < n; i ++) {
1353
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1545 times.
1545 if (!const_heap_write(((lbm_uint*)data)[i],ix + i))
1354 ✗ return LBM_FLASH_WRITE_ERROR;
1355 }
1356 763 lbm_const_heap_state->next += n;
1357 763 *res = (lbm_uint)&lbm_const_heap_state->heap[ix];
1358 763 r = LBM_FLASH_WRITE_OK;
1359 }
1360 763 return r;
1361 }
1362
1363 252 lbm_flash_status lbm_const_write(lbm_uint *tgt, lbm_uint val) {
1364
1365
1/2
✓ Branch 0 taken 252 times.
✗ Branch 1 not taken.
252 if (lbm_const_heap_state) {
1366 252 lbm_uint flash = (lbm_uint)lbm_const_heap_state->heap;
1367 252 lbm_uint ix = (((lbm_uint)tgt - flash) / sizeof(lbm_uint)); // byte address to ix
1368
1/2
✓ Branch 1 taken 252 times.
✗ Branch 2 not taken.
252 if (const_heap_write(val, ix)) {
1369 252 return LBM_FLASH_WRITE_OK;
1370 }
1371 ✗ return LBM_FLASH_WRITE_ERROR;
1372 }
1373 ✗ return LBM_FLASH_FULL;
1374 }
1375
1376 7491 lbm_flash_status write_const_cdr(lbm_value cell, lbm_value val) {
1377 7491 lbm_uint addr = lbm_dec_ptr(cell) << 1;
1378
1/2
✓ Branch 1 taken 7491 times.
✗ Branch 2 not taken.
7491 if (const_heap_write(val, addr+1))
1379 7491 return LBM_FLASH_WRITE_OK;
1380 ✗ return LBM_FLASH_WRITE_ERROR;
1381 }
1382
1383 7491 lbm_flash_status write_const_car(lbm_value cell, lbm_value val) {
1384 7491 lbm_uint addr = lbm_dec_ptr(cell) << 1;
1385
1/2
✓ Branch 1 taken 7491 times.
✗ Branch 2 not taken.
7491 if (const_heap_write(val, addr))
1386 7491 return LBM_FLASH_WRITE_OK;
1387 ✗ return LBM_FLASH_WRITE_ERROR;
1388 }
1389
1390 9 lbm_uint lbm_flash_memory_usage(void) {
1391 9 return lbm_const_heap_state->next;
1392 }
1393
1394
1395 // ////////////////////////////////////////////////////////////
1396 // pointer reversal traversal
1397 //
1398 // Caveats:
1399 // * Structures on the constant heap cannot be traversed using
1400 // pointer reversal. If a dynamic structure is pointing into the
1401 // constant heap, the 'f' will be applied to the constant cons cell on
1402 // the border and then traversal will retreat.
1403 //
1404 // * It may be impossible to detect all kinds of cycles
1405 // if attempting to use and restore the GC in a single pass
1406 // over the tree. To detect cycles all visited nodes must
1407 // remain detectable when traversing all branches!
1408 //
1409 // * Potential fix is to run GC after a complete traversal of the
1410 // entire value in order to restore the GC bits.
1411 //
1412 // * If we leave GC bits set when traversing values, we can use this
1413 // to detect cycles that happen in multiple steps accross values
1414 // in the environment.
1415 //
1416 // * lbm_ptr_rev_trav with the "do_nothing" travfun is the same thing
1417 // as a GC mark phase! Maybe utilize this for code-size
1418 // purposes. This also increases the amount of testing the
1419 // ptr_rev_trav function is subjected to.
1420
1421 3016 void lbm_ptr_rev_trav(trav_fun f, lbm_value v, void* arg) {
1422
1423 3016 lbm_value curr = v;
1424 3016 lbm_value prev = lbm_enc_cons_ptr(LBM_PTR_NULL);
1425 while (true) {
1426
1427 // Run leftwards and process conses until
1428 // hitting a leaf in the left direction.
1429
1430 // If curr is marked here there is a cycle in the graph.
1431 // In case of a cycle or leaf, this first loop is exited.
1432
2/2
✓ Branch 1 taken 28619 times.
✓ Branch 2 taken 210 times.
83735 while (((lbm_is_cons_rw(curr)) ||
1433
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28619 times.
57448 (lbm_is_lisp_array_rw(curr)) ||
1434
4/4
✓ Branch 0 taken 28829 times.
✓ Branch 1 taken 26077 times.
✓ Branch 4 taken 25910 times.
✓ Branch 5 taken 377 times.
83735 (lbm_is_channel(curr))) && !gc_marked(curr)) {
1435 25910 lbm_cons_t *cell = lbm_ref_cell(curr);
1436
2/2
✓ Branch 1 taken 25715 times.
✓ Branch 2 taken 195 times.
25910 if (lbm_is_cons(curr)) {
1437 // In-order traversal
1438
2/2
✓ Branch 1 taken 82 times.
✓ Branch 2 taken 25633 times.
25715 if (f(curr, false, arg) == TRAV_FUN_SUBTREE_DONE) {
1439 82 lbm_gc_mark_phase(curr);
1440 82 goto trav_backtrack;
1441 }
1442 25633 gc_mark(curr);
1443
1444 25633 lbm_value next = 0;
1445 25633 value_assign(&next, cell->car);
1446 25633 value_assign(&cell->car, prev);
1447 25633 value_assign(&prev, curr);
1448 25633 value_assign(&curr, next);
1449
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 195 times.
195 } else if (lbm_is_channel (curr)) {
1450 ✗ if (f(curr, false, arg) == TRAV_FUN_SUBTREE_DONE) {
1451 ✗ lbm_gc_mark_phase(curr);
1452 ✗ goto trav_backtrack;
1453 }
1454 ✗ gc_mark(curr);
1455
1456 ✗ lbm_char_channel_t *chan = (lbm_char_channel_t*)cell->car;
1457 ✗ lbm_value next = 0;
1458 ✗ value_assign(&next, chan->dependency);
1459 ✗ value_assign(&chan->dependency, prev);
1460 ✗ value_assign(&prev, curr);
1461 ✗ value_assign(&curr, next);
1462 } else { // it is an array
1463 195 lbm_array_header_extended_t *arr = (lbm_array_header_extended_t*)cell->car;
1464 195 lbm_value *arr_data = (lbm_value *)arr->data;
1465
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 189 times.
195 if (arr->size == 0) break;
1466
1467
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 189 times.
189 if (f(curr, false, arg) == TRAV_FUN_SUBTREE_DONE) {
1468 ✗ lbm_gc_mark_phase(curr);
1469 ✗ break;
1470 }
1471 // Cycles in arrays are not detected using the index
1472 // in this algorithm. If we enter into an array, we take
1473 // full ownership of it here, even if the stack based marker
1474 // has partially walked it already.
1475 189 arr->index = 1; // Explicitly sets array index to one.
1476 189 gc_mark(curr);
1477
1478 189 lbm_value next = 0;
1479 189 value_assign(&next, arr_data[0]);
1480 189 value_assign(&arr_data[0], prev);
1481 189 value_assign(&prev, curr);
1482 189 value_assign(&curr, next);
1483 }
1484 }
1485 // Currently there are a few different users of this traversal.
1486 // size, flatten and detect_sharing.
1487 // detect_sharing make use of the shared (true) argument in f(curr, true, arg)
1488 // while the other do not. detect_sharing also assumes it is run once per env item
1489 // while not resetting any GC-bits in between. This detects global sharing.
1490
1491
4/4
✓ Branch 1 taken 1088 times.
✓ Branch 2 taken 27914 times.
✓ Branch 4 taken 377 times.
✓ Branch 5 taken 711 times.
29002 if (lbm_is_ptr(curr) && gc_marked(curr)) {
1492 // gc bit set so this subtree is already traversed.
1493 // f is called with true to indicate visited node.
1494 // if this happens during a sharing discovery phase, curr will be added to sharing table.
1495 377 f(curr, true, arg);
1496 // In this case f should not be able to return subtree continue.
1497 // The only correct return value from f is PROCEED.
1498
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 28622 times.
28625 } else if (!lbm_is_cons(curr) || // Found a leaf
1499
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 (curr & LBM_PTR_TO_CONSTANT_BIT)) {
1500
4/4
✓ Branch 1 taken 711 times.
✓ Branch 2 taken 27914 times.
✓ Branch 3 taken 708 times.
✓ Branch 4 taken 3 times.
28625 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.
1501 28625 f(curr, false, arg);
1502 }
1503
1504 // Now either prev has the "flag" set or it doesnt.
1505 // If the flag is set that means that the prev node
1506 // have had both its car and cdr visited. So that node is done!
1507 //
1508 // If the flag is not set, jump down to SWAP
1509
1510 ✗ trav_backtrack:
1511
2/2
✓ Branch 1 taken 51266 times.
✓ Branch 2 taken 3016 times.
109188 while ((lbm_is_cons(prev) &&
1512
2/2
✓ Branch 1 taken 25633 times.
✓ Branch 2 taken 25633 times.
105548 (lbm_dec_ptr(prev) != LBM_PTR_NULL) && // is LBM_NULL a cons type?
1513
2/2
✓ Branch 2 taken 624 times.
✓ Branch 3 taken 28649 times.
80539 lbm_get_gc_flag(lbm_car(prev))) ||
1514
3/4
✓ Branch 0 taken 54282 times.
✓ Branch 1 taken 624 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28649 times.
112204 lbm_is_lisp_array_rw(prev) ||
1515 28649 lbm_is_channel(prev)) {
1516 26257 lbm_cons_t *cell = lbm_ref_cell(prev);
1517
2/2
✓ Branch 1 taken 25633 times.
✓ Branch 2 taken 624 times.
26257 if (lbm_is_cons(prev)) {
1518
1519 // clear the flag
1520 // This means that we are done with a "CDR" child.
1521 // prev = [ a , b ][flag = 1]
1522 // =>
1523 // prev = [ a , b ][flag = 0]
1524
1525 //gc_clear_mark(prev);
1526 25633 cell->car = lbm_clr_gc_flag(cell->car);
1527 // Move on downwards until
1528 // finding a cons cell without flag or NULL
1529
1530 // curr = c
1531 // prev = [ a , b ][flag = 0]
1532 // =>
1533 // prev = [ a , c ][flag = 0]
1534 // curr = prev
1535 // prev = b
1536
1537 25633 lbm_value next = 0;
1538 25633 value_assign(&next, cell->cdr);
1539 25633 value_assign(&cell->cdr, curr);
1540 25633 value_assign(&curr, prev);
1541 25633 value_assign(&prev, next);
1542
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 624 times.
624 } else if (lbm_is_channel(prev)) {
1543 ✗ lbm_char_channel_t *chan = (lbm_char_channel_t*)cell->car;
1544 ✗ lbm_value next = 0;
1545 ✗ value_assign(&next, chan->dependency);
1546 ✗ value_assign(&chan->dependency, curr);
1547 ✗ value_assign(&curr, prev);
1548 ✗ value_assign(&prev, next);
1549 } else { // is an array
1550 624 lbm_array_header_extended_t *arr = (lbm_array_header_extended_t*)cell->car;
1551 624 lbm_value *arr_data = (lbm_value *)arr->data;
1552 624 size_t arr_size = (size_t)arr->size / sizeof(lbm_value);
1553 624 lbm_value next = 0;
1554
2/2
✓ Branch 0 taken 189 times.
✓ Branch 1 taken 435 times.
624 if (arr->index == arr_size) {
1555 189 value_assign(&next, arr_data[arr->index-1]);
1556 189 value_assign(&arr_data[arr->index-1], curr);
1557 189 value_assign(&curr, prev);
1558 189 value_assign(&prev, next);
1559 189 arr->index = 0;
1560 } else {
1561 435 break;
1562 }
1563 }
1564 }
1565
1566 // SWAP
1567
1568 // if the prev node is NULL we have traced backwards all the
1569 // way back to where curr == v. Another alternative is that
1570 // the input v was an Atom. We are done!
1571
3/4
✓ Branch 1 taken 29084 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3016 times.
✓ Branch 4 taken 26068 times.
58168 if (lbm_is_ptr(prev) &&
1572 29084 lbm_dec_ptr(prev) == LBM_PTR_NULL) {
1573 3016 if (lbm_is_cons(curr)) {
1574 //gc_clear_mark(curr);
1575 }
1576 //done = true;
1577 3016 break;
1578 }
1579
1580 // if the prev node is not NULL then we should move
1581 // down to the prev node and start process its remaining child.
1582
2/2
✓ Branch 1 taken 25633 times.
✓ Branch 2 taken 435 times.
26068 else if (lbm_is_cons(prev)) {
1583
1584 25633 lbm_cons_t *cell = lbm_ref_cell(prev);
1585 25633 lbm_value next = 0;
1586
1587
1588 // prev = [ p , cdr ][flag = 0]
1589 // =>
1590 // prev = [ p , cdr ][flag = 1]
1591
1592 25633 cell->car = lbm_set_gc_flag(cell->car);
1593
1594 // switch to processing the cdr field and set the flag.
1595 // curr = c
1596 // prev = [ a, b ][flag = 1]
1597 // =>
1598 // prev = [ c, a ][flag = 1]
1599 // curr = b
1600
1601 25633 value_assign(&next, cell->car);
1602 25633 value_assign(&cell->car, curr);
1603 25633 value_assign(&curr, cell->cdr);
1604 25633 value_assign(&cell->cdr, next);
1605
1/2
✓ Branch 1 taken 435 times.
✗ Branch 2 not taken.
435 } else if (lbm_is_lisp_array_rw(prev)) {
1606 435 lbm_cons_t *cell = lbm_ref_cell(prev);
1607 435 lbm_array_header_extended_t *arr = (lbm_array_header_extended_t*)cell->car;
1608 435 lbm_value *arr_data = (lbm_value *)arr->data;
1609 435 lbm_value next = 0;
1610
1611 435 value_assign(&next, arr_data[arr->index-1]);
1612 435 value_assign(&arr_data[arr->index-1], curr);
1613 435 value_assign(&curr, arr_data[arr->index]);
1614 435 value_assign(&arr_data[arr->index], next);
1615 435 arr->index = arr->index + 1;
1616 }
1617 }
1618 3016 }
1619