GCC Code Coverage Report


Directory: ../src/
File: /home/joels/Current/lispbm/src/heap.c
Date: 2026-07-11 23:13:58
Exec Total Coverage
Lines: 836 875 95.5%
Functions: 85 89 95.5%
Branches: 319 376 84.8%

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 177052242 static inline lbm_value lbm_set_gc_mark(lbm_value x) {
40 177052242 return x | LBM_GC_MARKED;
41 }
42 176446913 static inline lbm_value lbm_clr_gc_mark(lbm_value x) {
43 176446913 return x & ~LBM_GC_MASK;
44 }
45
46 4005759474 static inline bool lbm_get_gc_mark(lbm_value x) {
47 4005759474 return x & LBM_GC_MASK;
48 }
49
50 24600 static inline void gc_mark(lbm_value c) {
51 //c must be a cons cell.
52 24600 lbm_cons_t *cell = lbm_ref_cell(c);
53 24600 cell->cdr = lbm_set_gc_mark(cell->cdr);
54 24600 }
55
56 24939 static inline bool gc_marked(lbm_value c) {
57 24939 lbm_cons_t *cell = lbm_ref_cell(c);
58 24939 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 47676 static inline bool lbm_get_gc_flag(lbm_value x) {
69 47676 return x & LBM_GC_MARKED;
70 }
71
72 23838 static inline lbm_value lbm_set_gc_flag(lbm_value x) {
73 23838 return x | LBM_GC_MARKED;
74 }
75
76 23838 static inline lbm_value lbm_clr_gc_flag(lbm_value x) {
77 23838 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 8510894 lbm_value lbm_enc_i32(int32_t x) {
112 #ifndef LBM64
113 5675896 lbm_value i = lbm_cons((lbm_uint)x, ENC_SYM_RAW_I_TYPE);
114
2/2
✓ Branch 0 taken 1236 times.
✓ Branch 1 taken 5674660 times.
5675896 if (lbm_type_of(i) == LBM_TYPE_SYMBOL) return i;
115 5674660 return lbm_set_ptr_type(i, LBM_TYPE_I32);
116 #else
117 2834998 return (((lbm_uint)x) << LBM_VAL_SHIFT) | LBM_TYPE_I32;
118 #endif
119 }
120
121 10211379 lbm_value lbm_enc_u32(uint32_t x) {
122 #ifndef LBM64
123 7373163 lbm_value u = lbm_cons(x, ENC_SYM_RAW_U_TYPE);
124
2/2
✓ Branch 0 taken 2176 times.
✓ Branch 1 taken 7370987 times.
7373163 if (lbm_type_of(u) == LBM_TYPE_SYMBOL) return u;
125 7370987 return lbm_set_ptr_type(u, LBM_TYPE_U32);
126 #else
127 2838216 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 689408204 lbm_value lbm_enc_float(float x) {
174 #ifndef LBM64
175 lbm_uint t;
176 459789080 memcpy(&t, &x, sizeof(lbm_float));
177 459789080 lbm_value f = lbm_cons(t, ENC_SYM_RAW_F_TYPE);
178
2/2
✓ Branch 0 taken 294488 times.
✓ Branch 1 taken 459494592 times.
459789080 if (lbm_type_of(f) == LBM_TYPE_SYMBOL) return f;
179 459494592 return lbm_set_ptr_type(f, LBM_TYPE_FLOAT);
180 #else
181 229619124 lbm_uint t = 0;
182 229619124 memcpy(&t, &x, sizeof(float));
183 229619124 return (((lbm_uint)t) << LBM_VAL_SHIFT) | LBM_TYPE_FLOAT;
184 #endif
185 }
186
187 #ifndef LBM64
188 16856785 static lbm_value enc_64_on_32(uint8_t *source, lbm_uint type_qual, lbm_uint type) {
189 16856785 lbm_value res = lbm_cons(ENC_SYM_NIL,ENC_SYM_NIL);
190
2/2
✓ Branch 0 taken 16852413 times.
✓ Branch 1 taken 4372 times.
16856785 if (lbm_type_of(res) != LBM_TYPE_SYMBOL) {
191 16852413 uint8_t* storage = lbm_malloc(sizeof(uint64_t));
192
2/2
✓ Branch 0 taken 16847157 times.
✓ Branch 1 taken 5256 times.
16852413 if (storage) {
193 16847157 memcpy(storage,source, sizeof(uint64_t));
194 16847157 lbm_set_car_and_cdr(res, (lbm_uint)storage, type_qual);
195 16847157 res = lbm_set_ptr_type(res, type);
196 } else {
197 5256 res = ENC_SYM_MERROR;
198 }
199 }
200 16856785 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 0 taken 1232 times.
✓ Branch 1 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 0 taken 876 times.
✓ Branch 1 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 1697571 lbm_value lbm_enc_double(double x) {
225 #ifndef LBM64
226 1132003 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 0 taken 332 times.
✓ Branch 1 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 1000359696 float lbm_dec_float(lbm_value x) {
241 #ifndef LBM64
242 float f_tmp;
243 667126510 lbm_uint tmp = lbm_car(x);
244 667126510 memcpy(&f_tmp, &tmp, sizeof(float));
245 667126510 return f_tmp;
246 #else
247 333233186 uint32_t tmp = (uint32_t)(x >> LBM_VAL_SHIFT);
248 float f_tmp;
249 333233186 memcpy(&f_tmp, &tmp, sizeof(float));
250 333233186 return f_tmp;
251 #endif
252 }
253
254 1694894 double lbm_dec_double(lbm_value x) {
255 #ifndef LBM64
256 1130250 double d = 0.0;
257
1/2
✓ Branch 0 taken 1130250 times.
✗ Branch 1 not taken.
1130250 if (lbm_is_ptr(x)) {
258 1130250 uint32_t *data = (uint32_t*)lbm_ref_cell(x)->car;
259 1130250 memcpy(&d, data, sizeof(double));
260 }
261 1130250 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 0 taken 14025265 times.
✗ Branch 1 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 0 taken 18506144 times.
✗ Branch 1 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 1464356 char *lbm_dec_str(lbm_value val) {
297 1464356 char *res = 0;
298
2/2
✓ Branch 0 taken 1463916 times.
✓ Branch 1 taken 440 times.
1464356 if (lbm_is_array_r(val)) {
299 1463916 lbm_array_header_t *array = (lbm_array_header_t *)lbm_car(val);
300
1/2
✓ Branch 0 taken 1463916 times.
✗ Branch 1 not taken.
1463916 if (array) {
301 1463916 res = (char *)array->data;
302 }
303 }
304 1464356 return res;
305 }
306
307 579591 bool lbm_dec_str_size(lbm_value val, char **data, size_t *size) {
308 579591 lbm_array_header_t *array = lbm_dec_array_r(val);
309
2/2
✓ Branch 0 taken 579384 times.
✓ Branch 1 taken 207 times.
579591 if (array) {
310 579384 *data = (char*)array->data;
311 579384 *size = array->size;
312 579384 return true;
313 }
314 207 return false;
315 }
316
317 1808086 lbm_array_header_t *lbm_dec_array_r(lbm_value val) {
318 1808086 lbm_array_header_t *array = NULL;
319
2/2
✓ Branch 0 taken 1803965 times.
✓ Branch 1 taken 4121 times.
1808086 if (lbm_is_array_r(val)) {
320 1803965 array = (lbm_array_header_t *)lbm_car(val);
321 }
322 1808086 return array;
323 }
324
325 180853 lbm_array_header_t *lbm_dec_array_rw(lbm_value val) {
326 180853 lbm_array_header_t *array = NULL;
327
2/2
✓ Branch 0 taken 180595 times.
✓ Branch 1 taken 258 times.
180853 if (lbm_is_array_rw(val)) {
328 180595 array = (lbm_array_header_t *)lbm_car(val);
329 }
330 180853 return array;
331 }
332
333 39456608 lbm_char_channel_t *lbm_dec_channel(lbm_value val) {
334 39456608 lbm_char_channel_t *res = NULL;
335
336
1/2
✓ Branch 0 taken 39456608 times.
✗ Branch 1 not taken.
39456608 if (lbm_type_of(val) == LBM_TYPE_CHANNEL) {
337 39456608 res = (lbm_char_channel_t *)lbm_car(val);
338 }
339 39456608 return res;
340 }
341
342 14270 lbm_uint lbm_dec_custom(lbm_value val) {
343 14270 lbm_uint res = 0;
344
1/2
✓ Branch 0 taken 14270 times.
✗ Branch 1 not taken.
14270 if (lbm_type_of(val) == LBM_TYPE_CUSTOM) {
345 14270 res = (lbm_uint)lbm_car(val);
346 }
347 14270 return res;
348 }
349
350 182831 uint8_t lbm_dec_as_char(lbm_value a) {
351 182831 uint8_t r = 0;
352
10/10
✓ Branch 0 taken 182065 times.
✓ Branch 1 taken 163 times.
✓ Branch 2 taken 86 times.
✓ Branch 3 taken 85 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 7 times.
182831 switch (lbm_type_of_functional(a)) {
353 182065 case LBM_TYPE_CHAR:
354 182065 r = (uint8_t)lbm_dec_char(a); break;
355 163 case LBM_TYPE_I:
356 163 r = (uint8_t)lbm_dec_i(a); break;
357 86 case LBM_TYPE_U:
358 86 r = (uint8_t)lbm_dec_u(a); break;
359 85 case LBM_TYPE_I32:
360 85 r = (uint8_t)lbm_dec_i32(a); break;
361 85 case LBM_TYPE_U32:
362 85 r = (uint8_t)lbm_dec_u32(a); break;
363 85 case LBM_TYPE_FLOAT:
364 85 r = (uint8_t)lbm_dec_float(a); break;
365 85 case LBM_TYPE_I64:
366 85 r = (uint8_t)lbm_dec_i64(a); break;
367 85 case LBM_TYPE_U64:
368 85 r = (uint8_t)lbm_dec_u64(a); break;
369 85 case LBM_TYPE_DOUBLE:
370 85 r = (uint8_t) lbm_dec_double(a); break;
371 }
372 182831 return r;
373 }
374
375 19224205 uint32_t lbm_dec_as_u32(lbm_value a) {
376 19224205 uint32_t r = 0;
377
9/9
✓ Branch 0 taken 1405430 times.
✓ Branch 1 taken 4186740 times.
✓ Branch 2 taken 359828 times.
✓ Branch 3 taken 13271683 times.
✓ Branch 4 taken 94 times.
✓ Branch 5 taken 85 times.
✓ Branch 6 taken 253 times.
✓ Branch 7 taken 85 times.
✓ Branch 8 taken 7 times.
19224205 switch (lbm_type_of_functional(a)) {
378 1405430 case LBM_TYPE_CHAR:
379 1405430 r = (uint32_t)lbm_dec_char(a); break;
380 4186740 case LBM_TYPE_I:
381 4186740 r = (uint32_t)lbm_dec_i(a); break;
382 359828 case LBM_TYPE_U:
383 359828 r = (uint32_t)lbm_dec_u(a); break;
384 13271683 case LBM_TYPE_I32: /* fall through */
385 case LBM_TYPE_U32:
386 13271683 r = (uint32_t)lbm_dec_u32(a); break;
387 94 case LBM_TYPE_FLOAT:
388 94 r = (uint32_t)lbm_dec_float(a); break;
389 85 case LBM_TYPE_I64:
390 85 r = (uint32_t)lbm_dec_i64(a); break;
391 253 case LBM_TYPE_U64:
392 253 r = (uint32_t)lbm_dec_u64(a); break;
393 85 case LBM_TYPE_DOUBLE:
394 85 r = (uint32_t)lbm_dec_double(a); break;
395 }
396 19224205 return r;
397 }
398
399 509921271 int32_t lbm_dec_as_i32(lbm_value a) {
400 509921271 int32_t r = 0;
401
10/10
✓ Branch 0 taken 13476711 times.
✓ Branch 1 taken 485403070 times.
✓ Branch 2 taken 24422 times.
✓ Branch 3 taken 11014290 times.
✓ Branch 4 taken 91 times.
✓ Branch 5 taken 85 times.
✓ Branch 6 taken 169 times.
✓ Branch 7 taken 169 times.
✓ Branch 8 taken 85 times.
✓ Branch 9 taken 2179 times.
509921271 switch (lbm_type_of_functional(a)) {
402 13476711 case LBM_TYPE_CHAR:
403 13476711 r = (int32_t)lbm_dec_char(a); break;
404 485403070 case LBM_TYPE_I:
405 485403070 r = (int32_t)lbm_dec_i(a); break;
406 24422 case LBM_TYPE_U:
407 24422 r = (int32_t)lbm_dec_u(a); break;
408 11014290 case LBM_TYPE_I32:
409 11014290 r = (int32_t)lbm_dec_i32(a); break;
410 91 case LBM_TYPE_U32:
411 91 r = (int32_t)lbm_dec_u32(a); break;
412 85 case LBM_TYPE_FLOAT:
413 85 r = (int32_t)lbm_dec_float(a); break;
414 169 case LBM_TYPE_I64:
415 169 r = (int32_t)lbm_dec_i64(a); break;
416 169 case LBM_TYPE_U64:
417 169 r = (int32_t)lbm_dec_u64(a); break;
418 85 case LBM_TYPE_DOUBLE:
419 85 r = (int32_t) lbm_dec_double(a); break;
420 }
421 509921276 return r;
422 }
423
424 253651082 int64_t lbm_dec_as_i64(lbm_value a) {
425 253651082 int64_t r = 0;
426
10/10
✓ Branch 0 taken 7813529 times.
✓ Branch 1 taken 231542922 times.
✓ Branch 2 taken 506 times.
✓ Branch 3 taken 841 times.
✓ Branch 4 taken 1793 times.
✓ Branch 5 taken 205 times.
✓ Branch 6 taken 14290749 times.
✓ Branch 7 taken 337 times.
✓ Branch 8 taken 169 times.
✓ Branch 9 taken 31 times.
253651082 switch (lbm_type_of_functional(a)) {
427 7813529 case LBM_TYPE_CHAR:
428 7813529 r = (int64_t)lbm_dec_char(a); break;
429 231542922 case LBM_TYPE_I:
430 231542922 r = (int64_t)lbm_dec_i(a); break;
431 506 case LBM_TYPE_U:
432 506 r = (int64_t)lbm_dec_u(a); break;
433 841 case LBM_TYPE_I32:
434 841 r = (int64_t)lbm_dec_i32(a); break;
435 1793 case LBM_TYPE_U32:
436 1793 r = (int64_t)lbm_dec_u32(a); break;
437 205 case LBM_TYPE_FLOAT:
438 205 r = (int64_t)lbm_dec_float(a); break;
439 14290749 case LBM_TYPE_I64:
440 14290749 r = (int64_t)lbm_dec_i64(a); break;
441 337 case LBM_TYPE_U64:
442 337 r = (int64_t)lbm_dec_u64(a); break;
443 169 case LBM_TYPE_DOUBLE:
444 169 r = (int64_t) lbm_dec_double(a); break;
445 }
446 253651082 return r;
447 }
448
449 13559144 uint64_t lbm_dec_as_u64(lbm_value a) {
450 13559144 uint64_t r = 0;
451
10/10
✓ Branch 0 taken 1687425 times.
✓ Branch 1 taken 919297 times.
✓ Branch 2 taken 19410 times.
✓ Branch 3 taken 673 times.
✓ Branch 4 taken 673 times.
✓ Branch 5 taken 169 times.
✓ Branch 6 taken 505 times.
✓ Branch 7 taken 10930816 times.
✓ Branch 8 taken 169 times.
✓ Branch 9 taken 7 times.
13559144 switch (lbm_type_of_functional(a)) {
452 1687425 case LBM_TYPE_CHAR:
453 1687425 r = (uint64_t)lbm_dec_char(a); break;
454 919297 case LBM_TYPE_I:
455 919297 r = (uint64_t)lbm_dec_i(a); break;
456 19410 case LBM_TYPE_U:
457 19410 r = (uint64_t)lbm_dec_u(a); break;
458 673 case LBM_TYPE_I32:
459 673 r = (uint64_t)lbm_dec_i32(a); break;
460 673 case LBM_TYPE_U32:
461 673 r = (uint64_t)lbm_dec_u32(a); break;
462 169 case LBM_TYPE_FLOAT:
463 169 r = (uint64_t)lbm_dec_float(a); break;
464 505 case LBM_TYPE_I64:
465 505 r = (uint64_t)lbm_dec_i64(a); break;
466 10930816 case LBM_TYPE_U64:
467 10930816 r = (uint64_t)lbm_dec_u64(a); break;
468 169 case LBM_TYPE_DOUBLE:
469 169 r = (uint64_t)lbm_dec_double(a); break;
470 }
471 13559144 return r;
472 }
473
474 1126212021 float lbm_dec_as_float(lbm_value a) {
475 1126212021 float r = 0;
476
10/10
✓ Branch 0 taken 311097810 times.
✓ Branch 1 taken 504152564 times.
✓ Branch 2 taken 478 times.
✓ Branch 3 taken 421 times.
✓ Branch 4 taken 536 times.
✓ Branch 5 taken 310959158 times.
✓ Branch 6 taken 421 times.
✓ Branch 7 taken 421 times.
✓ Branch 8 taken 85 times.
✓ Branch 9 taken 127 times.
1126212021 switch (lbm_type_of_functional(a)) {
477 311097810 case LBM_TYPE_CHAR:
478 311097810 r = (float)lbm_dec_char(a); break;
479 504152564 case LBM_TYPE_I:
480 504152564 r = (float)lbm_dec_i(a); break;
481 478 case LBM_TYPE_U:
482 478 r = (float)lbm_dec_u(a); break;
483 421 case LBM_TYPE_I32:
484 421 r = (float)lbm_dec_i32(a); break;
485 536 case LBM_TYPE_U32:
486 536 r = (float)lbm_dec_u32(a); break;
487 310959158 case LBM_TYPE_FLOAT:
488 310959158 r = (float)lbm_dec_float(a); break;
489 421 case LBM_TYPE_I64:
490 421 r = (float)lbm_dec_i64(a); break;
491 421 case LBM_TYPE_U64:
492 421 r = (float)lbm_dec_u64(a); break;
493 85 case LBM_TYPE_DOUBLE:
494 85 r = (float)lbm_dec_double(a); break;
495 }
496 1126212021 return r;
497 }
498
499 1692434 double lbm_dec_as_double(lbm_value a) {
500 1692434 double r = 0;
501
10/10
✓ Branch 0 taken 843316 times.
✓ Branch 1 taken 842451 times.
✓ Branch 2 taken 422 times.
✓ Branch 3 taken 421 times.
✓ Branch 4 taken 421 times.
✓ Branch 5 taken 1159 times.
✓ Branch 6 taken 421 times.
✓ Branch 7 taken 421 times.
✓ Branch 8 taken 3395 times.
✓ Branch 9 taken 7 times.
1692434 switch (lbm_type_of_functional(a)) {
502 843316 case LBM_TYPE_CHAR:
503 843316 r = (double)lbm_dec_char(a); break;
504 842451 case LBM_TYPE_I:
505 842451 r = (double)lbm_dec_i(a); break;
506 422 case LBM_TYPE_U:
507 422 r = (double)lbm_dec_u(a); break;
508 421 case LBM_TYPE_I32:
509 421 r = (double)lbm_dec_i32(a); break;
510 421 case LBM_TYPE_U32:
511 421 r = (double)lbm_dec_u32(a); break;
512 1159 case LBM_TYPE_FLOAT:
513 1159 r = (double)lbm_dec_float(a); break;
514 421 case LBM_TYPE_I64:
515 421 r = (double)lbm_dec_i64(a); break;
516 421 case LBM_TYPE_U64:
517 421 r = (double)lbm_dec_u64(a); break;
518 3395 case LBM_TYPE_DOUBLE:
519 3395 r = (double)lbm_dec_double(a); break;
520 }
521 1692434 return r;
522 }
523
524 // ////////////////////////////////////////////////////////////
525 // HEAP MANAGEMENT
526
527 67033 static bool generate_freelist(size_t num_cells) {
528 67033 size_t i = 0;
529
530
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67033 times.
67033 if (!lbm_heap_state.heap) return false;
531
532 67033 lbm_heap_state.freelist = lbm_enc_cons_ptr(0);
533
534 lbm_cons_t *t;
535
536 // Add all cells to free list
537
2/2
✓ Branch 0 taken 616702503 times.
✓ Branch 1 taken 67033 times.
616769536 for (i = 1; i < num_cells; i ++) {
538 616702503 t = lbm_ref_cell(lbm_enc_cons_ptr(i-1));
539 616702503 t->car = ENC_SYM_RECOVERED; // all cars in free list are "RECOVERED"
540 616702503 t->cdr = lbm_enc_cons_ptr(i);
541 }
542
543 // Replace the incorrect pointer at the last cell.
544 67033 t = lbm_ref_cell(lbm_enc_cons_ptr(num_cells-1));
545 67033 t->car = ENC_SYM_RECOVERED;
546 67033 t->cdr = ENC_SYM_NIL;
547
548 67033 return true;
549 }
550
551 1610805 void lbm_nil_freelist(void) {
552 1610805 lbm_heap_state.freelist = ENC_SYM_NIL;
553 1610805 lbm_heap_state.num_free = 0;
554 1610805 }
555
556 67033 static void heap_init_state(lbm_cons_t *addr, lbm_uint num_cells,
557 lbm_uint* gc_stack_storage, lbm_uint gc_stack_size) {
558 67033 lbm_heap_state.heap = addr;
559 67033 lbm_heap_state.heap_bytes = (unsigned int)(num_cells * sizeof(lbm_cons_t));
560 67033 lbm_heap_state.heap_size = num_cells;
561
562 67033 lbm_stack_create(&lbm_heap_state.gc_stack, gc_stack_storage, gc_stack_size);
563
564 67033 lbm_heap_state.num_free = num_cells;
565 67033 lbm_heap_state.num_alloc_arrays = 0;
566 67033 lbm_heap_state.gc_num = 0;
567 67033 lbm_heap_state.gc_marked = 0;
568 67033 lbm_heap_state.gc_recovered = 0;
569 67033 lbm_heap_state.gc_recovered_arrays = 0;
570 67033 lbm_heap_state.gc_least_free = num_cells;
571 67033 lbm_heap_state.gc_last_free = num_cells;
572 67033 }
573
574 1610804 void lbm_heap_new_freelist_length(void) {
575 1610804 lbm_heap_state.gc_last_free = lbm_heap_state.num_free;
576
2/2
✓ Branch 0 taken 12398 times.
✓ Branch 1 taken 1598406 times.
1610804 if (lbm_heap_state.num_free < lbm_heap_state.gc_least_free)
577 12398 lbm_heap_state.gc_least_free = lbm_heap_state.num_free;
578 1610804 }
579
580 67033 bool lbm_heap_init(lbm_cons_t *addr, lbm_uint num_cells,
581 lbm_uint gc_stack_size) {
582
583
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67033 times.
67033 if (((uintptr_t)addr % 8) != 0) return false;
584
585 67033 memset(addr,0, sizeof(lbm_cons_t) * num_cells);
586
587 67033 lbm_uint *gc_stack_storage = (lbm_uint*)lbm_malloc(gc_stack_size * sizeof(lbm_uint));
588
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67033 times.
67033 if (gc_stack_storage == NULL) return 0;
589
590 67033 heap_init_state(addr, num_cells,
591 gc_stack_storage, gc_stack_size);
592
593 67033 lbm_heaps[LBM_RAM_HEAP] = addr;
594
595 67033 return generate_freelist(num_cells);
596 }
597
598
599 1413923352 lbm_value lbm_heap_allocate_cell(lbm_type ptr_type, lbm_value car, lbm_value cdr) {
600 lbm_value r;
601 1413923352 lbm_value cell = lbm_heap_state.freelist;
602
2/2
✓ Branch 0 taken 1413603766 times.
✓ Branch 1 taken 319586 times.
1413923352 if (cell) {
603 1413603766 lbm_uint heap_ix = lbm_dec_ptr(cell);
604 1413603766 lbm_heap_state.freelist = lbm_heap_state.heap[heap_ix].cdr;
605 1413603766 lbm_heap_state.num_free--;
606 1413603766 lbm_heap_state.heap[heap_ix].car = car;
607 1413603766 lbm_heap_state.heap[heap_ix].cdr = cdr;
608 1413603766 r = lbm_set_ptr_type(cell, ptr_type);
609 } else {
610 319586 r = ENC_SYM_MERROR;
611 }
612 1413923352 return r;
613 }
614
615 3772695 lbm_value lbm_heap_allocate_list(lbm_uint n) {
616
2/2
✓ Branch 0 taken 10112 times.
✓ Branch 1 taken 3762583 times.
3772695 if (n == 0) return ENC_SYM_NIL;
617
2/2
✓ Branch 0 taken 4674 times.
✓ Branch 1 taken 3757909 times.
3762583 if (lbm_heap_num_free() < n) return ENC_SYM_MERROR;
618 // Here the freelist is guaranteed to be a cons_cell.
619
620 3757909 lbm_value curr = lbm_heap_state.freelist;
621 3757909 lbm_value res = curr;
622
623 3757909 lbm_cons_t *c_cell = NULL;
624 3757909 lbm_uint count = 0;
625 do {
626 19405032 c_cell = lbm_ref_cell(curr);
627 19405032 c_cell->car = ENC_SYM_NIL;
628 19405032 curr = c_cell->cdr;
629 19405032 count ++;
630
2/2
✓ Branch 0 taken 15647123 times.
✓ Branch 1 taken 3757909 times.
19405032 } while (count < n);
631 3757909 lbm_heap_state.freelist = curr;
632 3757909 c_cell->cdr = ENC_SYM_NIL;
633 3757909 lbm_heap_state.num_free-=count;
634 3757909 return res;
635 }
636
637 65936462 lbm_value lbm_heap_allocate_list_init_va(unsigned int n, va_list valist) {
638
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65936462 times.
65936462 if (n == 0) return ENC_SYM_NIL;
639
2/2
✓ Branch 0 taken 99698 times.
✓ Branch 1 taken 65836764 times.
65936462 if (lbm_heap_num_free() < n) return ENC_SYM_MERROR;
640
641 65836764 lbm_value curr = lbm_heap_state.freelist;
642 65836764 lbm_value res = curr;
643
644 65836764 lbm_cons_t *c_cell = NULL;
645 65836764 unsigned int count = 0;
646 do {
647 132525262 c_cell = lbm_ref_cell(curr);
648 132525262 c_cell->car = va_arg(valist, lbm_value);
649 132525262 curr = c_cell->cdr;
650 132525262 count ++;
651
2/2
✓ Branch 0 taken 66688498 times.
✓ Branch 1 taken 65836764 times.
132525262 } while (count < n);
652 65836764 lbm_heap_state.freelist = curr;
653 65836764 c_cell->cdr = ENC_SYM_NIL;
654 65836764 lbm_heap_state.num_free-=count;
655 65836764 return res;
656 }
657
658 65936462 lbm_value lbm_heap_allocate_list_init(unsigned int n, ...) {
659 va_list valist;
660 65936462 va_start(valist, n);
661 65936462 lbm_value r = lbm_heap_allocate_list_init_va(n, valist);
662 65936462 va_end(valist);
663 65936462 return r;
664 }
665
666 5 lbm_uint lbm_heap_num_allocated(void) {
667 5 return lbm_heap_state.heap_size - lbm_heap_state.num_free;
668 }
669 5 lbm_uint lbm_heap_size(void) {
670 5 return lbm_heap_state.heap_size;
671 }
672
673 4 lbm_uint lbm_heap_size_bytes(void) {
674 4 return lbm_heap_state.heap_bytes;
675 }
676
677 783 void lbm_get_heap_state(lbm_heap_state_t *res) {
678 783 *res = lbm_heap_state;
679 783 }
680
681 6 lbm_uint lbm_get_gc_stack_max(void) {
682 6 return lbm_get_max_stack(&lbm_heap_state.gc_stack);
683 }
684
685 5 lbm_uint lbm_get_gc_stack_size(void) {
686 5 return lbm_heap_state.gc_stack.size;
687 }
688
689 288228 static inline void value_assign(lbm_value *a, lbm_value b) {
690 288228 lbm_value a_old = *a & LBM_GC_MASK;
691 288228 *a = a_old | (b & ~LBM_GC_MASK);
692 288228 }
693
694 #ifdef LBM_USE_GC_PTR_REV
695 // ////////////////////////////////////////////////////////////
696 // Deutch-Schorr-Waite (DSW) pointer reversal GC
697 //
698 /*
699 Deutch-Schorr-Waite (DSW) pointer reversal GC for 2-ptr cells
700 with a hack-solution for the lisp-array case (n-ptr cells).
701
702 DSW visits each branch node 3 times compared to 2 times for
703 the stack based recursive mark.
704 Where the stack based recursive mark performs a stack push/pop,
705 DSW rearranges the, current, prev, next and a ptr field on
706 the heap.
707
708 DSW changes the structure of the heap and it introduces an
709 invalid pointer (LBM_PTR_NULL) temporarily during marking.
710 Since the heap will be "messed up" while marking, a mutex
711 is introuded to keep other processes out of the heap while
712 marking.
713 */
714
715 static int do_nothing(lbm_value v, bool shared, void *arg) {
716 (void) v;
717 (void) shared;
718 (void) arg;
719 return TRAV_FUN_SUBTREE_CONTINUE;
720 }
721
722 void lbm_gc_mark_phase(lbm_value root) {
723 lbm_mutex_lock(&lbm_const_heap_mutex);
724 lbm_ptr_rev_trav(do_nothing, root, NULL);
725 lbm_mutex_unlock(&lbm_const_heap_mutex);
726 }
727
728 #else
729 // ////////////////////////////////////////////////////////////
730 // Check if a value is currently on the stack
731
732 73834 bool active_ptr(lbm_value p) {
733 73834 lbm_stack_t *s = &lbm_heap_state.gc_stack;
734 73834 bool r = false;
735
2/2
✓ Branch 0 taken 342 times.
✓ Branch 1 taken 73834 times.
74176 for (lbm_uint i = 0; i < s->sp; i ++) {
736
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 342 times.
342 if (p == s->data[i]) {
737 ✗ r = true;
738 ✗ break;
739 }
740 }
741 73834 return r;
742 }
743
744 // ////////////////////////////////////////////////////////////
745 // Explicit stack "recursive" mark phase
746
747 // Trees are marked in a left subtree before rigth subtree, car first then cdr,
748 // way to favor lisp lists. This means that stack will grow slowly when
749 // marking right-leaning (cdr-recursive) data-structures while left-leaning
750 // (car-recursive) structures uses a lot of stack.
751
752 // Lisp arrays contain an extra book-keeping field to keep track
753 // of how far into the array the marking process has gone.
754
755 // TODO: DSW should be used as a last-resort if the GC stack is exhausted.
756 // If we use DSW as last-resort can we get away with a way smaller
757 // GC stack and unchanged performance (on sensible programs)?
758
759 17934977 void lbm_gc_mark_phase(lbm_value root) {
760 lbm_value t_ptr;
761 17934977 lbm_stack_t *s = &lbm_heap_state.gc_stack;
762 17934977 s->data[s->sp++] = root;
763
764
2/2
✓ Branch 0 taken 116579381 times.
✓ Branch 1 taken 17934976 times.
134514357 while (!lbm_stack_is_empty(s)) {
765 lbm_value curr;
766 116579381 lbm_pop(s, &curr);
767
768 154597370 mark_shortcut:
769
770
2/2
✓ Branch 0 taken 169431210 times.
✓ Branch 1 taken 101745541 times.
271176751 if (!lbm_is_ptr(curr) ||
771
2/2
✓ Branch 0 taken 337 times.
✓ Branch 1 taken 169430873 times.
169431210 (curr & LBM_PTR_TO_CONSTANT_BIT)) {
772 112244527 continue;
773 }
774
775 169430873 lbm_cons_t *cell = &lbm_heap_state.heap[lbm_dec_ptr(curr)];
776
777
2/2
✓ Branch 0 taken 10427677 times.
✓ Branch 1 taken 159003196 times.
169430873 if (lbm_get_gc_mark(cell->cdr)) {
778 10427677 continue;
779 }
780
781 159003196 t_ptr = lbm_type_of(curr);
782
783 // An array is marked in O(N) time using an additional 32bit
784 // value per array that keeps track of how far into the array GC
785 // has progressed.
786
2/2
✓ Branch 0 taken 73834 times.
✓ Branch 1 taken 158929362 times.
159003196 if (t_ptr == LBM_TYPE_LISPARRAY) {
787 // O(stack_size) cost when seeing array. This protects
788 // against gc recursing into arrays via cycles.
789
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73834 times.
73834 if (active_ptr(curr)) {
790 ✗ continue; // Already in process of marking this array, abort cycle!
791 }
792 73834 lbm_array_header_extended_t *arr = (lbm_array_header_extended_t*)cell->car;
793 73834 lbm_value *arrdata = (lbm_value *)arr->data;
794 73834 lbm_push(s, curr); // put array back as bookkeeping.
795 // Example A: Array with 10 elements
796 // A: assume arr->index == 9
797 // Example B: Array with 0 elements
798 // B: assume arr->index == 0
799
2/2
✓ Branch 0 taken 73048 times.
✓ Branch 1 taken 786 times.
73834 if (arr->index < ((arr->size/(sizeof(lbm_value))))) { // A: 9 < 10
800 // A: True path
801 // B: 0 < 0
802 // B: False path
803
3/4
✓ Branch 0 taken 3324 times.
✓ Branch 1 taken 69724 times.
✓ Branch 2 taken 3324 times.
✗ Branch 3 not taken.
73048 if (lbm_is_ptr(arrdata[arr->index]) && ((arrdata[arr->index] & LBM_PTR_TO_CONSTANT_BIT) == 0) &&
804
2/2
✓ Branch 0 taken 2862 times.
✓ Branch 1 taken 462 times.
3324 !((arrdata[arr->index] & LBM_CONTINUATION_INTERNAL) == LBM_CONTINUATION_INTERNAL)) {
805
806 2862 lbm_cons_t *elt = &lbm_heap_state.heap[lbm_dec_ptr(arrdata[arr->index])];
807
1/2
✓ Branch 0 taken 2862 times.
✗ Branch 1 not taken.
2862 if (!lbm_get_gc_mark(elt->cdr)) {
808 2862 curr = arrdata[arr->index];
809 2862 arr->index++;
810 2862 goto mark_shortcut;
811 }
812 }
813 // We need one more round of the loop if there is at least one element left in the array.
814 // If we skip this check things should work the same, but there will be one more recurse
815 // before we discover in the earlier conditional that we have reached the end of the array.
816
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
817 // A: False path
818 63520 arr->index++;
819 63520 continue;
820 }
821 }
822 7452 arr->index = 0;
823 7452 lbm_stack_drop(s,1); // Drop the bookkeeping element.
824 // elem_dropped == cell
825 // Cell is the array reference. so this marks the array reference itself
826 // after having marked all the elements.
827 7452 cell->cdr = lbm_set_gc_mark(cell->cdr);
828 7452 lbm_heap_state.gc_marked ++;
829 7452 continue;
830
2/2
✓ Branch 0 taken 822583 times.
✓ Branch 1 taken 158106779 times.
158929362 } else if (t_ptr == LBM_TYPE_CHANNEL) {
831 822583 cell->cdr = lbm_set_gc_mark(cell->cdr);
832 822583 lbm_heap_state.gc_marked ++;
833 // TODO: Can channels be explicitly freed ?
834
1/2
✓ Branch 0 taken 822583 times.
✗ Branch 1 not taken.
822583 if (cell->car != ENC_SYM_NIL) {
835 822583 lbm_char_channel_t *chan = (lbm_char_channel_t *)cell->car;
836 822583 curr = chan->dependency;
837 822583 goto mark_shortcut;
838 }
839 ✗ continue;
840 }
841
842 158106779 cell->cdr = lbm_set_gc_mark(cell->cdr);
843 158106779 lbm_heap_state.gc_marked ++;
844
845
2/2
✓ Branch 0 taken 153771926 times.
✓ Branch 1 taken 4334853 times.
158106779 if (t_ptr == LBM_TYPE_CONS) {
846
2/2
✓ Branch 0 taken 98578279 times.
✓ Branch 1 taken 55193647 times.
153771926 if (lbm_is_ptr(cell->cdr)) {
847
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 98578278 times.
98578279 if (!lbm_push(s, cell->cdr)) {
848 1 lbm_critical_error();
849 ✗ break;
850 }
851 }
852 153771925 curr = cell->car;
853 153771925 goto mark_shortcut; // Skip a push/pop
854 }
855 }
856 17934976 }
857 #endif
858
859 //Environments are proper lists with a 2 element list stored in each car.
860 53194572 void lbm_gc_mark_env(lbm_value env) {
861 53194572 lbm_value curr = env;
862 lbm_cons_t *c;
863
864
2/2
✓ Branch 0 taken 9045414 times.
✓ Branch 1 taken 53194571 times.
62239985 while (lbm_is_ptr(curr)) {
865 9045414 c = lbm_ref_cell(curr);
866 9045414 c->cdr = lbm_set_gc_mark(c->cdr); // mark the environent list structure.
867 9045414 lbm_cons_t *b = lbm_ref_cell(c->car);
868 9045414 b->cdr = lbm_set_gc_mark(b->cdr); // mark the binding list head cell.
869 9045414 lbm_heap_state.gc_marked +=2;
870
2/2
✓ Branch 0 taken 5806802 times.
✓ Branch 1 taken 3238612 times.
9045414 if (lbm_is_ptr(b->cdr)) {
871 5806802 lbm_gc_mark_phase(b->cdr); // mark the bound object.
872 }
873 9045413 curr = c->cdr;
874 }
875 53194571 }
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 1648835 void lbm_gc_mark_continuation_stack(lbm_uint *aux_data, lbm_uint aux_size) {
882
2/2
✓ Branch 0 taken 31688167 times.
✓ Branch 1 taken 1648835 times.
33337002 for (lbm_uint i = 0; i < aux_size; i ++) {
883
2/2
✓ Branch 0 taken 18929500 times.
✓ Branch 1 taken 12758667 times.
31688167 if (lbm_is_ptr(aux_data[i])) {
884 18929500 lbm_type pt_t = lbm_type_of(aux_data[i]);
885 18929500 lbm_uint pt_v = lbm_dec_ptr(aux_data[i]);
886
3/4
✓ Branch 0 taken 18929500 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8470064 times.
✓ Branch 3 taken 10459436 times.
18929500 if( pt_t >= LBM_POINTER_TYPE_FIRST &&
887 8470064 pt_t <= LBM_POINTER_TYPE_LAST &&
888
1/2
✓ Branch 0 taken 8470064 times.
✗ Branch 1 not taken.
8470064 pt_v < lbm_heap_state.heap_size) {
889 8470064 lbm_gc_mark_phase(aux_data[i]);
890 }
891 }
892 }
893 1648835 }
894
895 3309047 void lbm_gc_mark_roots(lbm_uint *roots, lbm_uint num_roots) {
896
2/2
✓ Branch 0 taken 6634212 times.
✓ Branch 1 taken 3309047 times.
9943259 for (lbm_uint i = 0; i < num_roots; i ++) {
897
2/2
✓ Branch 0 taken 2300102 times.
✓ Branch 1 taken 4334110 times.
6634212 if (lbm_is_ptr(roots[i])) {
898 2300102 lbm_gc_mark_phase(roots[i]);
899 }
900 }
901 3309047 }
902
903 // Sweep moves non-marked heap objects to the free list.
904 1610804 int lbm_gc_sweep_phase(void) {
905 1610804 unsigned int i = 0;
906 1610804 lbm_cons_t *heap = (lbm_cons_t *)lbm_heap_state.heap;
907
908
2/2
✓ Branch 0 taken 3836300800 times.
✓ Branch 1 taken 1610804 times.
3837911604 for (i = 0; i < lbm_heap_state.heap_size; i ++) {
909
2/2
✓ Branch 0 taken 176446913 times.
✓ Branch 1 taken 3659853887 times.
3836300800 if ( lbm_get_gc_mark(heap[i].cdr)) {
910 176446913 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 0 taken 645277798 times.
✓ Branch 1 taken 3014576089 times.
3659853887 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 1752667 times.
✓ Branch 3 taken 919074 times.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 84 times.
✓ Branch 6 taken 625725097 times.
645277798 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 1752667 case ENC_SYM_LISPARRAY_TYPE: /* fall through */
927 case ENC_SYM_ARRAY_TYPE:{
928 1752667 lbm_array_header_t *arr = (lbm_array_header_t*)heap[i].car;
929 1752667 lbm_memory_free((lbm_uint *)arr->data);
930 1752667 lbm_heap_state.gc_recovered_arrays++;
931 1752667 lbm_memory_free((lbm_uint *)arr);
932 1752667 } break;
933 919074 case ENC_SYM_CHANNEL_TYPE:{
934 919074 lbm_char_channel_t *chan = (lbm_char_channel_t*)heap[i].car;
935 919074 lbm_memory_free((lbm_uint*)chan->state);
936 919074 lbm_memory_free((lbm_uint*)chan);
937 919074 } 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 625725097 default:
948 625725097 break;
949 }
950 }
951 // create pointer to use as new freelist
952 3659853887 lbm_uint addr = lbm_enc_cons_ptr(i);
953
954 // Clear the "freed" cell.
955 3659853887 heap[i].car = ENC_SYM_RECOVERED;
956 3659853887 heap[i].cdr = lbm_heap_state.freelist;
957 3659853887 lbm_heap_state.freelist = addr;
958 3659853887 lbm_heap_state.num_free++;
959 3659853887 lbm_heap_state.gc_recovered ++;
960 }
961 }
962 1610804 return 1;
963 }
964
965 1610805 void lbm_gc_state_inc(void) {
966 1610805 lbm_heap_state.gc_num ++;
967 1610805 lbm_heap_state.gc_recovered = 0;
968 1610805 lbm_heap_state.gc_marked = 0;
969 1610805 }
970
971 // construct, alter and break apart
972 1410795031 lbm_value lbm_cons(lbm_value car, lbm_value cdr) {
973 1410795031 return lbm_heap_allocate_cell(LBM_TYPE_CONS, car, cdr);
974 }
975
976 825541265 lbm_value lbm_car(lbm_value c){
977
2/2
✓ Branch 0 taken 825538681 times.
✓ Branch 1 taken 2584 times.
825541265 if (lbm_is_ptr(c) ){
978 825538681 lbm_cons_t *cell = lbm_ref_cell(c);
979 825538681 return cell->car;
980 }
981
2/2
✓ Branch 0 taken 103 times.
✓ Branch 1 taken 2481 times.
2584 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 0 taken 138923 times.
✗ Branch 1 not taken.
138923 if (lbm_is_ptr(c)) {
991 138923 tmp = lbm_ref_cell(c)->car;
992
1/2
✓ Branch 0 taken 138923 times.
✗ Branch 1 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 0 taken 35867 times.
✗ Branch 1 not taken.
35867 if (lbm_is_ptr(c)) {
1003 35867 tmp = lbm_ref_cell(c)->cdr;
1004
1/2
✓ Branch 0 taken 35867 times.
✗ Branch 1 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 92038090 lbm_value lbm_cdr(lbm_value c){
1012
2/2
✓ Branch 0 taken 90338542 times.
✓ Branch 1 taken 1699548 times.
92038090 if (lbm_is_ptr(c)) {
1013 90338542 lbm_cons_t *cell = lbm_ref_cell(c);
1014 90338542 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 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 if (lbm_is_ptr(c)) {
1021 3 lbm_value tmp = lbm_ref_cell(c)->cdr;
1022
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (lbm_is_ptr(tmp)) {
1023 2 return lbm_ref_cell(tmp)->cdr;
1024 }
1025 }
1026
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 return c ? ENC_SYM_TERROR : c;
1027 }
1028
1029 /* calculate length of a proper list */
1030 3758502 lbm_uint lbm_list_length(lbm_value c) {
1031 3758502 lbm_uint len = 0;
1032
1033
2/2
✓ Branch 0 taken 17908212 times.
✓ Branch 1 taken 3758502 times.
21666714 while (lbm_is_cons(c)){
1034 17908212 len ++;
1035 17908212 c = lbm_ref_cell(c)->cdr;
1036 }
1037 3758502 return len;
1038 }
1039
1040 35316 lbm_value lbm_list_destructive_reverse(lbm_value list) {
1041
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 35312 times.
35316 if (!lbm_is_cons(list)) return list;
1042 35312 lbm_value curr = list;
1043 35312 lbm_value last_cell = ENC_SYM_NIL;
1044
1045
2/2
✓ Branch 0 taken 90327 times.
✓ Branch 1 taken 35312 times.
125639 while (lbm_is_cons_rw(curr)) {
1046 90327 lbm_cons_t *cell = lbm_ref_cell(curr);
1047 90327 lbm_value next = cell->cdr;
1048 90327 cell->cdr = last_cell;
1049 90327 last_cell = curr;
1050 90327 curr = next;
1051 }
1052 35312 return last_cell;
1053 }
1054
1055
1056 990839 lbm_value lbm_list_copy(int *m, lbm_value list) {
1057 990839 lbm_value curr = list;
1058 990839 lbm_uint n = lbm_list_length(list);
1059 990839 lbm_uint copy_n = n;
1060
4/4
✓ Branch 0 taken 18542 times.
✓ Branch 1 taken 972297 times.
✓ Branch 2 taken 16266 times.
✓ Branch 3 taken 2276 times.
990839 if (*m >= 0 && (lbm_uint)*m < n) {
1061 16266 copy_n = (lbm_uint)*m;
1062
2/2
✓ Branch 0 taken 887335 times.
✓ Branch 1 taken 87238 times.
974573 } else if (*m == -1) {
1063 887335 *m = (int)n; // TODO: smaller range in target variable.
1064 }
1065
2/2
✓ Branch 0 taken 693 times.
✓ Branch 1 taken 990146 times.
990839 if (copy_n == 0) return ENC_SYM_NIL;
1066 990146 lbm_uint new_list = lbm_heap_allocate_list(copy_n);
1067
2/2
✓ Branch 0 taken 1600 times.
✓ Branch 1 taken 988546 times.
990146 if (lbm_is_symbol(new_list)) return new_list;
1068 988546 lbm_value curr_targ = new_list;
1069
1070
4/4
✓ Branch 0 taken 11299566 times.
✓ Branch 1 taken 972526 times.
✓ Branch 2 taken 11283546 times.
✓ Branch 3 taken 16020 times.
12272092 while (lbm_is_cons(curr) && copy_n > 0) {
1071 11283546 lbm_cons_t *curr_cell = lbm_ref_cell(curr);
1072 11283546 lbm_cons_t *targ_cell = lbm_ref_cell(curr_targ);
1073 11283546 targ_cell->car = curr_cell->car;
1074 11283546 curr_targ = targ_cell->cdr;
1075 11283546 curr = curr_cell->cdr;
1076 11283546 copy_n --;
1077 }
1078 988546 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 0 taken 72614 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 72614 times.
✗ Branch 3 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 0 taken 95069 times.
✓ Branch 1 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 0 taken 90 times.
✓ Branch 1 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 0 taken 2184 times.
✓ Branch 1 taken 168 times.
✓ Branch 2 taken 2100 times.
✓ Branch 3 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 560273 lbm_value lbm_index_list(lbm_value l, int32_t n) {
1111 560273 lbm_value curr = l;
1112
1113
2/2
✓ Branch 0 taken 339 times.
✓ Branch 1 taken 559934 times.
560273 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 0 taken 856357 times.
✓ Branch 1 taken 90 times.
✓ Branch 2 taken 296174 times.
✓ Branch 3 taken 560183 times.
856447 while (lbm_is_cons(curr) && n > 0) {
1120 296174 curr = lbm_ref_cell(curr)->cdr;
1121 296174 n --;
1122 }
1123
2/2
✓ Branch 0 taken 560183 times.
✓ Branch 1 taken 90 times.
560273 if (lbm_is_cons(curr)) {
1124 560183 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 1771170 int lbm_heap_allocate_array_base(lbm_value *res, bool byte_array, lbm_uint size){
1137
1138 1771170 lbm_uint tag = ENC_SYM_ARRAY_TYPE;
1139 1771170 lbm_uint type = LBM_TYPE_ARRAY;
1140 1771170 lbm_array_header_t *array = NULL;
1141 1771170 lbm_array_header_extended_t *ext_array = NULL;
1142
1143
2/2
✓ Branch 0 taken 923003 times.
✓ Branch 1 taken 848167 times.
1771170 if (byte_array) {
1144 923003 array = (lbm_array_header_t*)lbm_malloc(sizeof(lbm_array_header_t));
1145 } else {
1146 848167 tag = ENC_SYM_LISPARRAY_TYPE;
1147 848167 type = LBM_TYPE_LISPARRAY;
1148 848167 size = sizeof(lbm_value) * size;
1149 848167 array = (lbm_array_header_t*)lbm_malloc(sizeof(lbm_array_header_extended_t));
1150 848167 ext_array = (lbm_array_header_extended_t*)array;
1151 }
1152
2/2
✓ Branch 0 taken 1767990 times.
✓ Branch 1 taken 3180 times.
1771170 if (array) {
1153
2/2
✓ Branch 0 taken 845587 times.
✓ Branch 1 taken 922403 times.
1767990 if (!byte_array) ext_array->index = 0;
1154
1155 1767990 array->data = NULL;
1156 1767990 array->size = size;
1157
2/2
✓ Branch 0 taken 1767538 times.
✓ Branch 1 taken 452 times.
1767990 if ( size > 0) {
1158 1767538 array->data = (lbm_uint*)lbm_malloc(size);
1159
2/2
✓ Branch 0 taken 14238 times.
✓ Branch 1 taken 1753300 times.
1767538 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 1753300 memset(array->data, 0, size);
1166 }
1167 // allocating a cell for array's heap-presence
1168 1753752 lbm_value cell = lbm_heap_allocate_cell(type, (lbm_uint) array, tag);
1169
2/2
✓ Branch 0 taken 1174 times.
✓ Branch 1 taken 1752578 times.
1753752 if (cell == ENC_SYM_MERROR) {
1170 1174 lbm_memory_free((lbm_uint*)array->data);
1171 1174 lbm_memory_free((lbm_uint*)array);
1172 1174 goto allocate_array_merror;
1173 }
1174 1752578 *res = cell;
1175 1752578 lbm_heap_state.num_alloc_arrays ++;
1176 1752578 return 1;
1177 }
1178 3180 allocate_array_merror:
1179 18592 *res = ENC_SYM_MERROR;
1180 18592 return 0;
1181 }
1182
1183 923003 int lbm_heap_allocate_array(lbm_value *res, lbm_uint size){
1184 923003 return lbm_heap_allocate_array_base(res, true, size);
1185 }
1186
1187 848167 int lbm_heap_allocate_lisp_array(lbm_value *res, lbm_uint size) {
1188 848167 return lbm_heap_allocate_array_base(res, false, size);
1189 }
1190
1191 3174 int lbm_lift_array(lbm_value *value, char *data, lbm_uint num_elt) {
1192
1193 3174 lbm_array_header_t *array = NULL;
1194 3174 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 3174 times.
3174 if (cell == ENC_SYM_MERROR) {
1197 ✗ *value = cell;
1198 ✗ return 0;
1199 }
1200
1201 3174 array = (lbm_array_header_t*)lbm_malloc(sizeof(lbm_array_header_t));
1202
1203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3174 times.
3174 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 3174 array->data = (lbm_uint*)data;
1210 3174 array->size = num_elt;
1211
1212 3174 lbm_set_car(cell, (lbm_uint)array);
1213
1214 3174 cell = lbm_set_ptr_type(cell, LBM_TYPE_ARRAY);
1215 3174 *value = cell;
1216 3174 return 1;
1217 }
1218
1219 712517 lbm_int lbm_heap_array_get_size(lbm_value arr) {
1220
1221 712517 lbm_int r = -1;
1222 712517 lbm_array_header_t *header = lbm_dec_array_r(arr);
1223
1/2
✓ Branch 0 taken 712517 times.
✗ Branch 1 not taken.
712517 if (header) {
1224 712517 r = (lbm_int)header->size;
1225 }
1226 712517 return r;
1227 }
1228
1229 356439 const uint8_t *lbm_heap_array_get_data_ro(lbm_value arr) {
1230 356439 uint8_t *r = NULL;
1231 356439 lbm_array_header_t *header = lbm_dec_array_r(arr);
1232
1/2
✓ Branch 0 taken 356439 times.
✗ Branch 1 not taken.
356439 if (header) {
1233 356439 r = (uint8_t*)header->data;
1234 }
1235 356439 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 0 taken 336 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 336 times.
✗ Branch 3 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 67033 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 67033 times.
67033 if (((uintptr_t)addr % 4) != 0) return 0;
1287
1288
2/2
✓ Branch 0 taken 66953 times.
✓ Branch 1 taken 80 times.
67033 if (!lbm_const_heap_mutex_initialized) {
1289 66953 lbm_mutex_init(&lbm_const_heap_mutex);
1290 66953 lbm_const_heap_mutex_initialized = true;
1291 }
1292
1293
2/2
✓ Branch 0 taken 66953 times.
✓ Branch 1 taken 80 times.
67033 if (!lbm_mark_mutex_initialized) {
1294 66953 lbm_mutex_init(&lbm_mark_mutex);
1295 66953 lbm_mark_mutex_initialized = true;
1296 }
1297
1298 67033 const_heap_write = w_fun;
1299
1300 67033 heap->heap = addr;
1301 67033 heap->size = 0;
1302 67033 heap->next = 0;
1303
1304 67033 lbm_const_heap_state = heap;
1305 // ref_cell views the lbm_uint array as an lbm_cons_t array
1306 67033 lbm_heaps[LBM_CONST_HEAP] = (lbm_cons_t*)addr;
1307 67033 return 1;
1308 }
1309
1310 7491 lbm_flash_status lbm_allocate_const_cell(lbm_value *res) {
1311 7491 lbm_flash_status r = LBM_FLASH_FULL;
1312
1313 7491 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 7398 times.
7491 if (lbm_const_heap_state->next % 2 == 1) {
1316 93 lbm_const_heap_state->next++;
1317 }
1318
1319
1/2
✓ Branch 0 taken 7491 times.
✗ Branch 1 not taken.
7491 if (lbm_const_heap_state &&
1320
1/2
✓ Branch 0 taken 7491 times.
✗ Branch 1 not taken.
7491 (lbm_const_heap_state->next+1) < (uint32_t)lbm_image_get_write_index()) {
1321 // A cons cell uses two words.
1322 7491 lbm_value cell = lbm_const_heap_state->next;
1323 7491 lbm_const_heap_state->next += 2;
1324 7491 *res = ((cell >> 1) << LBM_ADDRESS_SHIFT) | LBM_PTR_BIT | LBM_TYPE_CONS | LBM_PTR_TO_CONSTANT_BIT;
1325 7491 r = LBM_FLASH_WRITE_OK;
1326 }
1327 7491 lbm_mutex_unlock(&lbm_const_heap_mutex);
1328 7491 return r;
1329 }
1330
1331 84 lbm_flash_status lbm_allocate_const_raw(lbm_uint nwords, lbm_uint *res) {
1332 84 lbm_flash_status r = LBM_FLASH_FULL;
1333
1334
1/2
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
84 if (lbm_const_heap_state &&
1335
1/2
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
84 (lbm_const_heap_state->next + nwords) < (uint32_t)lbm_image_get_write_index()) {
1336 84 lbm_uint ix = lbm_const_heap_state->next;
1337 84 *res = (lbm_uint)&lbm_const_heap_state->heap[ix];
1338 84 lbm_const_heap_state->next += nwords;
1339 84 r = LBM_FLASH_WRITE_OK;
1340 }
1341 84 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 0 taken 763 times.
✗ Branch 1 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 0 not taken.
✓ Branch 1 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 0 taken 252 times.
✗ Branch 1 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 0 taken 7491 times.
✗ Branch 1 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 0 taken 7491 times.
✗ Branch 1 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 963 void lbm_ptr_rev_trav(trav_fun f, lbm_value v, void* arg) {
1422
1423 963 lbm_value curr = v;
1424 963 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 0 taken 75 times.
✓ Branch 1 taken 25026 times.
74229 while (((lbm_is_cons_rw(curr)) ||
1433
4/4
✓ Branch 0 taken 25101 times.
✓ Branch 1 taken 24027 times.
✓ Branch 2 taken 23976 times.
✓ Branch 3 taken 126 times.
49203 (lbm_is_lisp_array_rw(curr))) && !gc_marked(curr)) {
1434 23976 lbm_cons_t *cell = lbm_ref_cell(curr);
1435
2/2
✓ Branch 0 taken 23916 times.
✓ Branch 1 taken 60 times.
23976 if (lbm_is_cons(curr)) {
1436 // In-order traversal
1437
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 23838 times.
23916 if (f(curr, false, arg) == TRAV_FUN_SUBTREE_DONE) {
1438 78 lbm_gc_mark_phase(curr);
1439 78 goto trav_backtrack;
1440 }
1441 23838 gc_mark(curr);
1442
1443 23838 lbm_value next = 0;
1444 23838 value_assign(&next, cell->car);
1445 23838 value_assign(&cell->car, prev);
1446 23838 value_assign(&prev, curr);
1447 23838 value_assign(&curr, next);
1448 } else { // it is an array
1449
1450 60 lbm_array_header_extended_t *arr = (lbm_array_header_extended_t*)cell->car;
1451 60 lbm_value *arr_data = (lbm_value *)arr->data;
1452 60 uint32_t index = arr->index;
1453
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 54 times.
60 if (arr->size == 0) break;
1454
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
1455
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if (f(curr, false, arg) == TRAV_FUN_SUBTREE_DONE) {
1456 ✗ lbm_gc_mark_phase(curr);
1457 ✗ break;
1458 }
1459 54 arr->index = 1;
1460 54 gc_mark(curr);
1461
1462 54 lbm_value next = 0;
1463 54 value_assign(&next, arr_data[0]);
1464 54 value_assign(&arr_data[0], prev);
1465 54 value_assign(&prev, curr);
1466 54 value_assign(&curr, next);
1467 }
1468 }
1469 }
1470 // Currently there are a few different users of this traversal.
1471 // size, flatten and detect_sharing.
1472 // detect_sharing make use of the shared (true) argument in f(curr, true, arg)
1473 // while the other do not. detect_sharing also assumes it is run once per env item
1474 // while not resetting any GC-bits in between. This detects global sharing.
1475
1476
4/4
✓ Branch 0 taken 837 times.
✓ Branch 1 taken 24321 times.
✓ Branch 2 taken 126 times.
✓ Branch 3 taken 711 times.
25158 if (lbm_is_ptr(curr) && gc_marked(curr)) {
1477 // gc bit set so this subtree is already traversed.
1478 // f is called with true to indicate visited node.
1479 // if this happens during a sharing discovery phase, curr will be added to sharing table.
1480 126 f(curr, true, arg);
1481 // In this case f should not be able to return subtree continue.
1482 // The only correct return value from f is PROCEED.
1483
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 25029 times.
25032 } else if (!lbm_is_cons(curr) || // Found a leaf
1484
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 (curr & LBM_PTR_TO_CONSTANT_BIT)) {
1485
4/4
✓ Branch 0 taken 711 times.
✓ Branch 1 taken 24321 times.
✓ Branch 2 taken 708 times.
✓ Branch 3 taken 3 times.
25032 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.
1486 25032 f(curr, false, arg);
1487 }
1488
1489 // Now either prev has the "flag" set or it doesnt.
1490 // If the flag is set that means that the prev node
1491 // have had both its car and cdr visited. So that node is done!
1492 //
1493 // If the flag is not set, jump down to SWAP
1494
1495 ✗ trav_backtrack:
1496
2/2
✓ Branch 0 taken 47676 times.
✓ Branch 1 taken 963 times.
97767 while ((lbm_is_cons(prev) &&
1497
2/2
✓ Branch 0 taken 23838 times.
✓ Branch 1 taken 23838 times.
96315 (lbm_dec_ptr(prev) != LBM_PTR_NULL) && // is LBM_NULL a cons type?
1498
4/4
✓ Branch 0 taken 48639 times.
✓ Branch 1 taken 489 times.
✓ Branch 2 taken 489 times.
✓ Branch 3 taken 24801 times.
122094 lbm_get_gc_flag(lbm_car(prev))) ||
1499 25290 lbm_is_lisp_array_rw(prev)) {
1500 24327 lbm_cons_t *cell = lbm_ref_cell(prev);
1501
2/2
✓ Branch 0 taken 23838 times.
✓ Branch 1 taken 489 times.
24327 if (lbm_is_cons(prev)) {
1502
1503 // clear the flag
1504 // This means that we are done with a "CDR" child.
1505 // prev = [ a , b ][flag = 1]
1506 // =>
1507 // prev = [ a , b ][flag = 0]
1508
1509 //gc_clear_mark(prev);
1510 23838 cell->car = lbm_clr_gc_flag(cell->car);
1511 // Move on downwards until
1512 // finding a cons cell without flag or NULL
1513
1514 // curr = c
1515 // prev = [ a , b ][flag = 0]
1516 // =>
1517 // prev = [ a , c ][flag = 0]
1518 // curr = prev
1519 // prev = b
1520
1521 23838 lbm_value next = 0;
1522 23838 value_assign(&next, cell->cdr);
1523 23838 value_assign(&cell->cdr, curr);
1524 23838 value_assign(&curr, prev);
1525 23838 value_assign(&prev, next);
1526 } else { // is an array
1527 489 lbm_array_header_extended_t *arr = (lbm_array_header_extended_t*)cell->car;
1528 489 lbm_value *arr_data = (lbm_value *)arr->data;
1529 489 size_t arr_size = (size_t)arr->size / sizeof(lbm_value);
1530 489 lbm_value next = 0;
1531
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 435 times.
489 if (arr->index == arr_size) {
1532 54 value_assign(&next, arr_data[arr->index-1]);
1533 54 value_assign(&arr_data[arr->index-1], curr);
1534 54 value_assign(&curr, prev);
1535 54 value_assign(&prev, next);
1536 54 arr->index = 0;
1537 } else {
1538 435 break;
1539 }
1540 }
1541 }
1542
1543 // SWAP
1544
1545 // if the prev node is NULL we have traced backwards all the
1546 // way back to where curr == v. Another alternative is that
1547 // the input v was an Atom. We are done!
1548
3/4
✓ Branch 0 taken 25236 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 963 times.
✓ Branch 3 taken 24273 times.
50472 if (lbm_is_ptr(prev) &&
1549 25236 lbm_dec_ptr(prev) == LBM_PTR_NULL) {
1550 963 if (lbm_is_cons(curr)) {
1551 //gc_clear_mark(curr);
1552 }
1553 //done = true;
1554 963 break;
1555 }
1556
1557 // if the prev node is not NULL then we should move
1558 // down to the prev node and start process its remaining child.
1559
2/2
✓ Branch 0 taken 23838 times.
✓ Branch 1 taken 435 times.
24273 else if (lbm_is_cons(prev)) {
1560
1561 23838 lbm_cons_t *cell = lbm_ref_cell(prev);
1562 23838 lbm_value next = 0;
1563
1564
1565 // prev = [ p , cdr ][flag = 0]
1566 // =>
1567 // prev = [ p , cdr ][flag = 1]
1568
1569 23838 cell->car = lbm_set_gc_flag(cell->car);
1570
1571 // switch to processing the cdr field and set the flag.
1572 // curr = c
1573 // prev = [ a, b ][flag = 1]
1574 // =>
1575 // prev = [ c, a ][flag = 1]
1576 // curr = b
1577
1578 23838 value_assign(&next, cell->car);
1579 23838 value_assign(&cell->car, curr);
1580 23838 value_assign(&curr, cell->cdr);
1581 23838 value_assign(&cell->cdr, next);
1582
1/2
✓ Branch 0 taken 435 times.
✗ Branch 1 not taken.
435 } else if (lbm_is_lisp_array_rw(prev)) {
1583 435 lbm_cons_t *cell = lbm_ref_cell(prev);
1584 435 lbm_array_header_extended_t *arr = (lbm_array_header_extended_t*)cell->car;
1585 435 lbm_value *arr_data = (lbm_value *)arr->data;
1586 435 lbm_value next = 0;
1587
1588 435 value_assign(&next, arr_data[arr->index-1]);
1589 435 value_assign(&arr_data[arr->index-1], curr);
1590 435 value_assign(&curr, arr_data[arr->index]);
1591 435 value_assign(&arr_data[arr->index], next);
1592 435 arr->index = arr->index + 1;
1593 }
1594 }
1595 963 }
1596