GCC Code Coverage Report


Directory: ../src/
File: /home/joels/Current/lispbm/src/heap.c
Date: 2026-09-05 08:38:23
Exec Total Coverage
Lines: 849 904 93.9%
Functions: 87 91 95.6%
Branches: 325 386 84.2%

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 177032821 static inline lbm_value lbm_set_gc_mark(lbm_value x) {
37 177032821 return x | LBM_GC_MARKED;
38 }
39 176437300 static inline lbm_value lbm_clr_gc_mark(lbm_value x) {
40 176437300 return x & ~LBM_GC_MASK;
41 }
42
43 4005761843 static inline bool lbm_get_gc_mark(lbm_value x) {
44 4005761843 return x & LBM_GC_MASK;
45 }
46
47 26395 static inline void gc_mark(lbm_value c) {
48 //c must be a cons cell.
49 26395 lbm_cons_t *cell = lbm_ref_cell(c);
50 26395 cell->cdr = lbm_set_gc_mark(cell->cdr);
51 26395 }
52
53 27240 static inline bool gc_marked(lbm_value c) {
54 27240 lbm_cons_t *cell = lbm_ref_cell(c);
55 27240 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 8510917 lbm_value lbm_enc_i32(int32_t x) {
107 #ifndef LBM64
108 5675924 lbm_value i = lbm_cons((lbm_uint)x, ENC_SYM_RAW_I_TYPE);
109
2/2
✓ Branch 0 taken 1236 times.
✓ Branch 1 taken 5674688 times.
5675924 if (lbm_type_of(i) == LBM_TYPE_SYMBOL) return i;
110 5674688 return lbm_set_ptr_type(i, LBM_TYPE_I32);
111 #else
112 2834993 return (((lbm_uint)x) << LBM_VAL_SHIFT) | LBM_TYPE_I32;
113 #endif
114 }
115
116 10211456 lbm_value lbm_enc_u32(uint32_t x) {
117 #ifndef LBM64
118 7373243 lbm_value u = lbm_cons(x, ENC_SYM_RAW_U_TYPE);
119
2/2
✓ Branch 0 taken 2176 times.
✓ Branch 1 taken 7371067 times.
7373243 if (lbm_type_of(u) == LBM_TYPE_SYMBOL) return u;
120 7371067 return lbm_set_ptr_type(u, LBM_TYPE_U32);
121 #else
122 2838213 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 689408315 lbm_value lbm_enc_float(float x) {
169 #ifndef LBM64
170 lbm_uint t;
171 459789093 memcpy(&t, &x, sizeof(lbm_float));
172 459789093 lbm_value f = lbm_cons(t, ENC_SYM_RAW_F_TYPE);
173
2/2
✓ Branch 0 taken 294488 times.
✓ Branch 1 taken 459494605 times.
459789093 if (lbm_type_of(f) == LBM_TYPE_SYMBOL) return f;
174 459494605 return lbm_set_ptr_type(f, LBM_TYPE_FLOAT);
175 #else
176 229619222 lbm_uint t = 0;
177 229619222 memcpy(&t, &x, sizeof(float));
178 229619222 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 0 taken 16852413 times.
✓ Branch 1 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 0 taken 1232 times.
✓ Branch 1 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 0 taken 876 times.
✓ Branch 1 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 0 taken 332 times.
✓ Branch 1 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 1000359143 float lbm_dec_float(lbm_value x) {
236 #ifndef LBM64
237 float f_tmp;
238 667126265 lbm_uint tmp = lbm_car(x);
239 667126265 memcpy(&f_tmp, &tmp, sizeof(float));
240 667126265 return f_tmp;
241 #else
242 333232878 uint32_t tmp = (uint32_t)(x >> LBM_VAL_SHIFT);
243 float f_tmp;
244 333232878 memcpy(&f_tmp, &tmp, sizeof(float));
245 333232878 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 0 taken 1130250 times.
✗ Branch 1 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 0 taken 14025265 times.
✗ Branch 1 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 0 taken 18506144 times.
✗ Branch 1 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 1464521 char *lbm_dec_str(lbm_value val) {
292 1464521 char *res = 0;
293
2/2
✓ Branch 0 taken 1464080 times.
✓ Branch 1 taken 441 times.
1464521 if (lbm_is_array_r(val)) {
294 1464080 lbm_array_header_t *array = (lbm_array_header_t *)lbm_car(val);
295
1/2
✓ Branch 0 taken 1464080 times.
✗ Branch 1 not taken.
1464080 if (array) {
296 1464080 res = (char *)array->data;
297 }
298 }
299 1464521 return res;
300 }
301
302 579597 bool lbm_dec_str_size(lbm_value val, char **data, size_t *size) {
303 579597 lbm_array_header_t *array = lbm_dec_array_r(val);
304
2/2
✓ Branch 0 taken 579390 times.
✓ Branch 1 taken 207 times.
579597 if (array) {
305 579390 *data = (char*)array->data;
306 579390 *size = array->size;
307 579390 return true;
308 }
309 207 return false;
310 }
311
312 1809083 lbm_array_header_t *lbm_dec_array_r(lbm_value val) {
313 1809083 lbm_array_header_t *array = NULL;
314
2/2
✓ Branch 0 taken 1804961 times.
✓ Branch 1 taken 4122 times.
1809083 if (lbm_is_array_r(val)) {
315 1804961 array = (lbm_array_header_t *)lbm_car(val);
316 }
317 1809083 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 0 taken 180595 times.
✓ Branch 1 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 39479979 lbm_char_channel_t *lbm_dec_channel(lbm_value val) {
329 39479979 lbm_char_channel_t *res = NULL;
330
331
1/2
✓ Branch 0 taken 39479979 times.
✗ Branch 1 not taken.
39479979 if (lbm_type_of(val) == LBM_TYPE_CHANNEL) {
332 39479979 res = (lbm_char_channel_t *)lbm_car(val);
333 }
334 39479979 return res;
335 }
336
337 14339 lbm_uint lbm_dec_custom(lbm_value val) {
338 14339 lbm_uint res = 0;
339
1/2
✓ Branch 0 taken 14339 times.
✗ Branch 1 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 182891 uint8_t lbm_dec_as_char(lbm_value a) {
346 182891 uint8_t r = 0;
347
10/10
✓ Branch 0 taken 182125 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.
182891 switch (lbm_type_of_functional(a)) {
348 182125 case LBM_TYPE_CHAR:
349 182125 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 182891 return r;
368 }
369
370 19225939 uint32_t lbm_dec_as_u32(lbm_value a) {
371 19225939 uint32_t r = 0;
372
9/9
✓ Branch 0 taken 1405430 times.
✓ Branch 1 taken 4187583 times.
✓ Branch 2 taken 360581 times.
✓ Branch 3 taken 13271821 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.
19225939 switch (lbm_type_of_functional(a)) {
373 1405430 case LBM_TYPE_CHAR:
374 1405430 r = (uint32_t)lbm_dec_char(a); break;
375 4187583 case LBM_TYPE_I:
376 4187583 r = (uint32_t)lbm_dec_i(a); break;
377 360581 case LBM_TYPE_U:
378 360581 r = (uint32_t)lbm_dec_u(a); break;
379 13271821 case LBM_TYPE_I32: /* fall through */
380 case LBM_TYPE_U32:
381 13271821 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 19225939 return r;
392 }
393
394 511195676 int32_t lbm_dec_as_i32(lbm_value a) {
395 511195676 int32_t r = 0;
396
10/10
✓ Branch 0 taken 13732386 times.
✓ Branch 1 taken 486421654 times.
✓ Branch 2 taken 24422 times.
✓ Branch 3 taken 11014320 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 2295 times.
511195676 switch (lbm_type_of_functional(a)) {
397 13732386 case LBM_TYPE_CHAR:
398 13732386 r = (int32_t)lbm_dec_char(a); break;
399 486421654 case LBM_TYPE_I:
400 486421654 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 11014320 case LBM_TYPE_I32:
404 11014320 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 511195681 return r;
417 }
418
419 253648716 int64_t lbm_dec_as_i64(lbm_value a) {
420 253648716 int64_t r = 0;
421
10/10
✓ Branch 0 taken 7813529 times.
✓ Branch 1 taken 231540556 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.
253648716 switch (lbm_type_of_functional(a)) {
422 7813529 case LBM_TYPE_CHAR:
423 7813529 r = (int64_t)lbm_dec_char(a); break;
424 231540556 case LBM_TYPE_I:
425 231540556 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 253648716 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 0 taken 1687425 times.
✓ Branch 1 taken 919353 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.
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 1126211481 float lbm_dec_as_float(lbm_value a) {
470 1126211481 float r = 0;
471
10/10
✓ Branch 0 taken 311097810 times.
✓ Branch 1 taken 504152590 times.
✓ Branch 2 taken 478 times.
✓ Branch 3 taken 421 times.
✓ Branch 4 taken 536 times.
✓ Branch 5 taken 310958604 times.
✓ Branch 6 taken 421 times.
✓ Branch 7 taken 421 times.
✓ Branch 8 taken 85 times.
✓ Branch 9 taken 115 times.
1126211481 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 310958604 case LBM_TYPE_FLOAT:
483 310958604 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 1126211481 return r;
492 }
493
494 1692435 double lbm_dec_as_double(lbm_value a) {
495 1692435 double r = 0;
496
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 1160 times.
✓ Branch 6 taken 421 times.
✓ Branch 7 taken 421 times.
✓ Branch 8 taken 3395 times.
✓ Branch 9 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 67085 static bool generate_freelist(size_t num_cells) {
523 67085 size_t i = 0;
524
525
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67085 times.
67085 if (!lbm_heap_state.heap) return false;
526
527 67085 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 616841715 times.
✓ Branch 1 taken 67085 times.
616908800 for (i = 1; i < num_cells; i ++) {
533 616841715 t = lbm_ref_cell(lbm_enc_cons_ptr(i-1));
534 616841715 t->car = ENC_SYM_RECOVERED; // all cars in free list are "RECOVERED"
535 616841715 t->cdr = lbm_enc_cons_ptr(i);
536 }
537
538 // Replace the incorrect pointer at the last cell.
539 67085 t = lbm_ref_cell(lbm_enc_cons_ptr(num_cells-1));
540 67085 t->car = ENC_SYM_RECOVERED;
541 67085 t->cdr = ENC_SYM_NIL;
542
543 67085 return true;
544 }
545
546 1610733 void lbm_nil_freelist(void) {
547 1610733 lbm_heap_state.freelist = ENC_SYM_NIL;
548 1610733 lbm_heap_state.num_free = 0;
549 1610733 }
550
551 67085 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 67085 lbm_heap_state.heap = addr;
554 67085 lbm_heap_state.heap_bytes = (unsigned int)(num_cells * sizeof(lbm_cons_t));
555 67085 lbm_heap_state.heap_size = num_cells;
556
557 67085 lbm_stack_create(&lbm_heap_state.gc_stack, gc_stack_storage, gc_stack_size);
558
559 67085 lbm_heap_state.num_free = num_cells;
560 67085 lbm_heap_state.num_alloc_arrays = 0;
561 67085 lbm_heap_state.gc_num = 0;
562 67085 lbm_heap_state.gc_marked = 0;
563 67085 lbm_heap_state.gc_recovered = 0;
564 67085 lbm_heap_state.gc_recovered_arrays = 0;
565 67085 lbm_heap_state.gc_least_free = num_cells;
566 67085 lbm_heap_state.gc_last_free = num_cells;
567 67085 }
568
569 1610733 void lbm_heap_new_freelist_length(void) {
570 1610733 lbm_heap_state.gc_last_free = lbm_heap_state.num_free;
571
2/2
✓ Branch 0 taken 12392 times.
✓ Branch 1 taken 1598341 times.
1610733 if (lbm_heap_state.num_free < lbm_heap_state.gc_least_free)
572 12392 lbm_heap_state.gc_least_free = lbm_heap_state.num_free;
573 1610733 }
574
575 67085 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 67085 times.
67085 if (((uintptr_t)addr % 8) != 0) return false;
579
580 67085 memset(addr,0, sizeof(lbm_cons_t) * num_cells);
581
582 67085 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 67085 times.
67085 if (gc_stack_storage == NULL) return 0;
584
585 67085 heap_init_state(addr, num_cells,
586 gc_stack_storage, gc_stack_size);
587
588 67085 lbm_heaps[LBM_RAM_HEAP] = addr;
589
590 67085 return generate_freelist(num_cells);
591 }
592
593
594 1413387793 lbm_value lbm_heap_allocate_cell(lbm_type ptr_type, lbm_value car, lbm_value cdr) {
595 lbm_value r;
596 1413387793 lbm_value cell = lbm_heap_state.freelist;
597
2/2
✓ Branch 0 taken 1413068221 times.
✓ Branch 1 taken 319572 times.
1413387793 if (cell) {
598 1413068221 lbm_uint heap_ix = lbm_dec_ptr(cell);
599 1413068221 lbm_heap_state.freelist = lbm_heap_state.heap[heap_ix].cdr;
600 1413068221 lbm_heap_state.num_free--;
601 1413068221 lbm_heap_state.heap[heap_ix].car = car;
602 1413068221 lbm_heap_state.heap[heap_ix].cdr = cdr;
603 1413068221 r = lbm_set_ptr_type(cell, ptr_type);
604 } else {
605 319572 r = ENC_SYM_MERROR;
606 }
607 1413387793 return r;
608 }
609
610 3772791 lbm_value lbm_heap_allocate_list(lbm_uint n) {
611
2/2
✓ Branch 0 taken 10112 times.
✓ Branch 1 taken 3762679 times.
3772791 if (n == 0) return ENC_SYM_NIL;
612
2/2
✓ Branch 0 taken 4668 times.
✓ Branch 1 taken 3758011 times.
3762679 if (lbm_heap_num_free() < n) return ENC_SYM_MERROR;
613 // Here the freelist is guaranteed to be a cons_cell.
614
615 3758011 lbm_value curr = lbm_heap_state.freelist;
616 3758011 lbm_value res = curr;
617
618 3758011 lbm_cons_t *c_cell = NULL;
619 3758011 lbm_uint count = 0;
620 do {
621 19405459 c_cell = lbm_ref_cell(curr);
622 19405459 c_cell->car = ENC_SYM_NIL;
623 19405459 curr = c_cell->cdr;
624 19405459 count ++;
625
2/2
✓ Branch 0 taken 15647448 times.
✓ Branch 1 taken 3758011 times.
19405459 } while (count < n);
626 3758011 lbm_heap_state.freelist = curr;
627 3758011 c_cell->cdr = ENC_SYM_NIL;
628 3758011 lbm_heap_state.num_free-=count;
629 3758011 return res;
630 }
631
632 65941664 lbm_value lbm_heap_allocate_list_init_va(unsigned int n, va_list valist) {
633
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65941664 times.
65941664 if (n == 0) return ENC_SYM_NIL;
634
2/2
✓ Branch 0 taken 99738 times.
✓ Branch 1 taken 65841926 times.
65941664 if (lbm_heap_num_free() < n) return ENC_SYM_MERROR;
635
636 65841926 lbm_value curr = lbm_heap_state.freelist;
637 65841926 lbm_value res = curr;
638
639 65841926 lbm_cons_t *c_cell = NULL;
640 65841926 unsigned int count = 0;
641 do {
642 132535596 c_cell = lbm_ref_cell(curr);
643 132535596 c_cell->car = va_arg(valist, lbm_value);
644 132535596 curr = c_cell->cdr;
645 132535596 count ++;
646
2/2
✓ Branch 0 taken 66693670 times.
✓ Branch 1 taken 65841926 times.
132535596 } while (count < n);
647 65841926 lbm_heap_state.freelist = curr;
648 65841926 c_cell->cdr = ENC_SYM_NIL;
649 65841926 lbm_heap_state.num_free-=count;
650 65841926 return res;
651 }
652
653 65941664 lbm_value lbm_heap_allocate_list_init(unsigned int n, ...) {
654 va_list valist;
655 65941664 va_start(valist, n);
656 65941664 lbm_value r = lbm_heap_allocate_list_init_va(n, valist);
657 65941664 va_end(valist);
658 65941664 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 309768 static inline void value_assign(lbm_value *a, lbm_value b) {
685 309768 lbm_value a_old = *a & LBM_GC_MASK;
686 309768 *a = a_old | (b & ~LBM_GC_MASK);
687 309768 }
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 5640 static int do_nothing(lbm_value v, bool shared, void *arg) {
710 (void) v;
711 (void) shared;
712 (void) arg;
713 5640 return TRAV_FUN_SUBTREE_CONTINUE;
714 }
715
716 2050 void lbm_gc_mark_phase_ptr_rev(lbm_value root) {
717 2050 lbm_mutex_lock(&lbm_const_heap_mutex);
718 2050 lbm_ptr_rev_trav(do_nothing, root, NULL);
719 2050 lbm_mutex_unlock(&lbm_const_heap_mutex);
720 2050 }
721
722 // ////////////////////////////////////////////////////////////
723 // Check if a value is currently on the stack
724
725 73834 bool active_ptr(lbm_value p) {
726 73834 lbm_stack_t *s = &lbm_heap_state.gc_stack;
727 73834 bool r = false;
728
2/2
✓ Branch 0 taken 342 times.
✓ Branch 1 taken 73834 times.
74176 for (lbm_uint i = 0; i < s->sp; i ++) {
729
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 342 times.
342 if (p == s->data[i]) {
730 ✗ r = true;
731 ✗ break;
732 }
733 }
734 73834 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 17923744 void lbm_gc_mark_phase(lbm_value root) {
753 lbm_value t_ptr;
754 17923744 lbm_stack_t *s = &lbm_heap_state.gc_stack;
755 17923744 s->data[s->sp++] = root;
756
757
2/2
✓ Branch 0 taken 116557180 times.
✓ Branch 1 taken 17923744 times.
134480924 while (!lbm_stack_is_empty(s)) {
758 lbm_value curr;
759 116557180 lbm_pop(s, &curr);
760
761 154590281 mark_shortcut:
762
763
2/2
✓ Branch 0 taken 169393902 times.
✓ Branch 1 taken 101753559 times.
271147461 if (!lbm_is_ptr(curr) ||
764
2/2
✓ Branch 0 taken 337 times.
✓ Branch 1 taken 169393565 times.
169393902 (curr & LBM_PTR_TO_CONSTANT_BIT)) {
765 112223157 continue;
766 }
767
768 169393565 lbm_cons_t *cell = &lbm_heap_state.heap[lbm_dec_ptr(curr)];
769
770
2/2
✓ Branch 0 taken 10398289 times.
✓ Branch 1 taken 158995276 times.
169393565 if (lbm_get_gc_mark(cell->cdr)) {
771 10398289 continue;
772 }
773
774 158995276 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 73834 times.
✓ Branch 1 taken 158921442 times.
158995276 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 0 not taken.
✓ Branch 1 taken 73834 times.
73834 if (active_ptr(curr)) {
783 ✗ continue; // Already in process of marking this array, abort cycle!
784 }
785 73834 lbm_array_header_extended_t *arr = (lbm_array_header_extended_t*)cell->car;
786 73834 lbm_value *arrdata = (lbm_value *)arr->data;
787
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73834 times.
73834 if (!lbm_push(s, curr)) { // put array back as bookkeeping.
788 ✗ lbm_critical_error();
789 ✗ break;
790 }
791 // Example A: Array with 10 elements
792 // A: assume arr->index == 9
793 // Example B: Array with 0 elements
794 // B: assume arr->index == 0
795
2/2
✓ Branch 0 taken 73048 times.
✓ Branch 1 taken 786 times.
73834 if (arr->index < ((arr->size/(sizeof(lbm_value))))) { // A: 9 < 10
796 // A: True path
797 // B: 0 < 0
798 // B: False path
799
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) &&
800
2/2
✓ Branch 0 taken 2862 times.
✓ Branch 1 taken 462 times.
3324 !((arrdata[arr->index] & LBM_CONTINUATION_INTERNAL) == LBM_CONTINUATION_INTERNAL)) {
801
802 2862 lbm_cons_t *elt = &lbm_heap_state.heap[lbm_dec_ptr(arrdata[arr->index])];
803
1/2
✓ Branch 0 taken 2862 times.
✗ Branch 1 not taken.
2862 if (!lbm_get_gc_mark(elt->cdr)) {
804 2862 curr = arrdata[arr->index];
805 2862 arr->index++;
806 2862 goto mark_shortcut;
807 }
808 }
809 // We need one more round of the loop if there is at least one element left in the array.
810 // If we skip this check things should work the same, but there will be one more recurse
811 // before we discover in the earlier conditional that we have reached the end of the array.
812
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
813 // A: False path
814 63520 arr->index++;
815 63520 continue;
816 }
817 }
818 7452 arr->index = 0;
819 7452 lbm_stack_drop(s,1); // Drop the bookkeeping element.
820 // elem_dropped == cell
821 // Cell is the array reference. so this marks the array reference itself
822 // after having marked all the elements.
823 7452 cell->cdr = lbm_set_gc_mark(cell->cdr);
824 7452 lbm_heap_state.gc_marked ++;
825 7452 continue;
826
2/2
✓ Branch 0 taken 822562 times.
✓ Branch 1 taken 158098880 times.
158921442 } else if (t_ptr == LBM_TYPE_CHANNEL) {
827 822562 cell->cdr = lbm_set_gc_mark(cell->cdr);
828 822562 lbm_heap_state.gc_marked ++;
829 // TODO: Can channels be explicitly freed ?
830
1/2
✓ Branch 0 taken 822562 times.
✗ Branch 1 not taken.
822562 if (cell->car != ENC_SYM_NIL) {
831 822562 lbm_char_channel_t *chan = (lbm_char_channel_t *)cell->car;
832 822562 curr = chan->dependency;
833 822562 goto mark_shortcut;
834 }
835 ✗ continue;
836 }
837
838 158098880 cell->cdr = lbm_set_gc_mark(cell->cdr);
839 158098880 lbm_heap_state.gc_marked ++;
840
841
2/2
✓ Branch 0 taken 153764857 times.
✓ Branch 1 taken 4334023 times.
158098880 if (t_ptr == LBM_TYPE_CONS) {
842
2/2
✓ Branch 0 taken 98569104 times.
✓ Branch 1 taken 55195753 times.
153764857 if (lbm_is_ptr(cell->cdr)) {
843
2/2
✓ Branch 0 taken 2050 times.
✓ Branch 1 taken 98567054 times.
98569104 if (!lbm_push(s, cell->cdr)) {
844 // This is the only place where we grab a new stack position.
845 // If the stack is full switch to ptr-rev GC.
846 2050 lbm_gc_mark_phase_ptr_rev(cell->cdr);
847 }
848 }
849 153764857 curr = cell->car;
850 153764857 goto mark_shortcut; // Skip a push/pop
851 }
852 }
853 17923744 }
854
855 //Environments are proper lists with a 2 element list stored in each car.
856 53192179 void lbm_gc_mark_env(lbm_value env) {
857 53192179 lbm_value curr = env;
858 lbm_cons_t *c;
859
860
2/2
✓ Branch 0 taken 9038766 times.
✓ Branch 1 taken 53192179 times.
62230945 while (lbm_is_ptr(curr)) {
861 9038766 c = lbm_ref_cell(curr);
862 9038766 c->cdr = lbm_set_gc_mark(c->cdr); // mark the environent list structure.
863 9038766 lbm_cons_t *b = lbm_ref_cell(c->car);
864 9038766 b->cdr = lbm_set_gc_mark(b->cdr); // mark the binding list head cell.
865 9038766 lbm_heap_state.gc_marked +=2;
866
2/2
✓ Branch 0 taken 5805175 times.
✓ Branch 1 taken 3233591 times.
9038766 if (lbm_is_ptr(b->cdr)) {
867 5805175 lbm_gc_mark_phase(b->cdr); // mark the bound object.
868 }
869 9038766 curr = c->cdr;
870 }
871 53192179 }
872
873
874 // Marks a continuation stack which contains CONTINUATION markers.
875 // Continuation markers look like pointers to the GC and would be
876 // incorrectly followed unless they are filtered out.
877 1648723 void lbm_gc_mark_continuation_stack(lbm_uint *aux_data, lbm_uint aux_size) {
878
2/2
✓ Branch 0 taken 31681181 times.
✓ Branch 1 taken 1648723 times.
33329904 for (lbm_uint i = 0; i < aux_size; i ++) {
879
2/2
✓ Branch 0 taken 18917829 times.
✓ Branch 1 taken 12763352 times.
31681181 if (lbm_is_ptr(aux_data[i])) {
880 18917829 lbm_type pt_t = lbm_type_of(aux_data[i]);
881 18917829 lbm_uint pt_v = lbm_dec_ptr(aux_data[i]);
882
3/4
✓ Branch 0 taken 18917829 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8460218 times.
✓ Branch 3 taken 10457611 times.
18917829 if( pt_t >= LBM_POINTER_TYPE_FIRST &&
883 8460218 pt_t <= LBM_POINTER_TYPE_LAST &&
884
1/2
✓ Branch 0 taken 8460218 times.
✗ Branch 1 not taken.
8460218 pt_v < lbm_heap_state.heap_size) {
885 8460218 lbm_gc_mark_phase(aux_data[i]);
886 }
887 }
888 }
889 1648723 }
890
891 3308711 void lbm_gc_mark_roots(lbm_uint *roots, lbm_uint num_roots) {
892
2/2
✓ Branch 0 taken 6633329 times.
✓ Branch 1 taken 3308711 times.
9942040 for (lbm_uint i = 0; i < num_roots; i ++) {
893
2/2
✓ Branch 0 taken 2299992 times.
✓ Branch 1 taken 4333337 times.
6633329 if (lbm_is_ptr(roots[i])) {
894 2299992 lbm_gc_mark_phase(roots[i]);
895 }
896 }
897 3308711 }
898
899 // Sweep moves non-marked heap objects to the free list.
900 1610733 int lbm_gc_sweep_phase(void) {
901 1610733 unsigned int i = 0;
902 1610733 lbm_cons_t *heap = (lbm_cons_t *)lbm_heap_state.heap;
903
904
2/2
✓ Branch 0 taken 3836338176 times.
✓ Branch 1 taken 1610733 times.
3837948909 for (i = 0; i < lbm_heap_state.heap_size; i ++) {
905
2/2
✓ Branch 0 taken 176437300 times.
✓ Branch 1 taken 3659900876 times.
3836338176 if ( lbm_get_gc_mark(heap[i].cdr)) {
906 176437300 heap[i].cdr = lbm_clr_gc_mark(heap[i].cdr);
907 } else {
908 // Check if this cell is a pointer to an array
909 // and free it.
910
2/2
✓ Branch 0 taken 645936607 times.
✓ Branch 1 taken 3013964269 times.
3659900876 if (lbm_type_of(heap[i].cdr) == LBM_TYPE_SYMBOL) {
911
7/7
✓ Branch 0 taken 16793872 times.
✓ Branch 1 taken 86999 times.
✓ Branch 2 taken 1752734 times.
✓ Branch 3 taken 919024 times.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 84 times.
✓ Branch 6 taken 626383889 times.
645936607 switch(heap[i].cdr) {
912
913 16793872 case ENC_SYM_IND_I_TYPE: /* fall through */
914 case ENC_SYM_IND_U_TYPE:
915 case ENC_SYM_IND_F_TYPE:
916 16793872 lbm_memory_free((lbm_uint*)heap[i].car);
917 16793872 break;
918 86999 case ENC_SYM_DEFRAG_LISPARRAY_TYPE: /* fall through */
919 case ENC_SYM_DEFRAG_ARRAY_TYPE:
920 86999 lbm_defrag_mem_free((lbm_uint*)heap[i].car);
921 86999 break;
922 1752734 case ENC_SYM_LISPARRAY_TYPE: /* fall through */
923 case ENC_SYM_ARRAY_TYPE:{
924 1752734 lbm_array_header_t *arr = (lbm_array_header_t*)heap[i].car;
925 1752734 lbm_memory_free((lbm_uint *)arr->data);
926 1752734 lbm_heap_state.gc_recovered_arrays++;
927 1752734 lbm_memory_free((lbm_uint *)arr);
928 1752734 } break;
929 919024 case ENC_SYM_CHANNEL_TYPE:{
930 919024 lbm_char_channel_t *chan = (lbm_char_channel_t*)heap[i].car;
931 919024 lbm_memory_free((lbm_uint*)chan->state);
932 919024 lbm_memory_free((lbm_uint*)chan);
933 919024 } break;
934 5 case ENC_SYM_CUSTOM_TYPE: {
935 5 lbm_uint *t = (lbm_uint*)heap[i].car;
936 5 lbm_custom_type_destroy(t);
937 5 lbm_memory_free(t);
938 5 } break;
939 84 case ENC_SYM_DEFRAG_MEM_TYPE: {
940 84 lbm_uint *ptr = (lbm_uint *)heap[i].car;
941 84 lbm_defrag_mem_destroy(ptr);
942 84 } break;
943 626383889 default:
944 626383889 break;
945 }
946 }
947 // create pointer to use as new freelist
948 3659900876 lbm_uint addr = lbm_enc_cons_ptr(i);
949
950 // Clear the "freed" cell.
951 3659900876 heap[i].car = ENC_SYM_RECOVERED;
952 3659900876 heap[i].cdr = lbm_heap_state.freelist;
953 3659900876 lbm_heap_state.freelist = addr;
954 3659900876 lbm_heap_state.num_free++;
955 3659900876 lbm_heap_state.gc_recovered ++;
956 }
957 }
958 1610733 return 1;
959 }
960
961 1610733 void lbm_gc_state_inc(void) {
962 1610733 lbm_heap_state.gc_num ++;
963 1610733 lbm_heap_state.gc_recovered = 0;
964 1610733 lbm_heap_state.gc_marked = 0;
965 1610733 }
966
967 // construct, alter and break apart
968 1410344265 lbm_value lbm_cons(lbm_value car, lbm_value cdr) {
969 1410344265 return lbm_heap_allocate_cell(LBM_TYPE_CONS, car, cdr);
970 }
971
972 825573071 lbm_value lbm_car(lbm_value c){
973
2/2
✓ Branch 0 taken 825570485 times.
✓ Branch 1 taken 2586 times.
825573071 if (lbm_is_ptr(c) ){
974 825570485 lbm_cons_t *cell = lbm_ref_cell(c);
975 825570485 return cell->car;
976 }
977
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 2482 times.
2586 return c ? ENC_SYM_TERROR : c; //nil if c == nil
978 }
979
980 // TODO: Many comparisons "is this the nil symbol" can be
981 // streamlined a bit. NIL is 0 and cannot be confused with any other
982 // lbm_value.
983
984 138923 lbm_value lbm_caar(lbm_value c) {
985 138923 lbm_value tmp = ENC_SYM_NIL;
986
1/2
✓ Branch 0 taken 138923 times.
✗ Branch 1 not taken.
138923 if (lbm_is_ptr(c)) {
987 138923 tmp = lbm_ref_cell(c)->car;
988
1/2
✓ Branch 0 taken 138923 times.
✗ Branch 1 not taken.
138923 if (lbm_is_ptr(tmp)) {
989 138923 return lbm_ref_cell(tmp)->car;
990 }
991 }
992 ✗ return c || tmp ? ENC_SYM_TERROR : c; //nil if not something else
993 }
994
995
996 35897 lbm_value lbm_cadr(lbm_value c) {
997 35897 lbm_value tmp = ENC_SYM_NIL;
998
1/2
✓ Branch 0 taken 35897 times.
✗ Branch 1 not taken.
35897 if (lbm_is_ptr(c)) {
999 35897 tmp = lbm_ref_cell(c)->cdr;
1000
1/2
✓ Branch 0 taken 35897 times.
✗ Branch 1 not taken.
35897 if (lbm_is_ptr(tmp)) {
1001 35897 return lbm_ref_cell(tmp)->car;
1002 }
1003 }
1004 ✗ return c || tmp ? ENC_SYM_TERROR : c;
1005 }
1006
1007 92045867 lbm_value lbm_cdr(lbm_value c){
1008
2/2
✓ Branch 0 taken 90346319 times.
✓ Branch 1 taken 1699548 times.
92045867 if (lbm_is_ptr(c)) {
1009 90346319 lbm_cons_t *cell = lbm_ref_cell(c);
1010 90346319 return cell->cdr;
1011 }
1012
1/2
✓ Branch 0 taken 1699548 times.
✗ Branch 1 not taken.
1699548 return c ? ENC_SYM_TERROR: c;
1013 }
1014
1015 5 lbm_value lbm_cddr(lbm_value c) {
1016
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 if (lbm_is_ptr(c)) {
1017 3 lbm_value tmp = lbm_ref_cell(c)->cdr;
1018
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (lbm_is_ptr(tmp)) {
1019 2 return lbm_ref_cell(tmp)->cdr;
1020 }
1021 }
1022
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 return c ? ENC_SYM_TERROR : c;
1023 }
1024
1025 /* calculate length of a proper list */
1026 3758557 lbm_uint lbm_list_length(lbm_value c) {
1027 3758557 lbm_uint len = 0;
1028
1029
2/2
✓ Branch 0 taken 17908498 times.
✓ Branch 1 taken 3758557 times.
21667055 while (lbm_is_cons(c)){
1030 17908498 len ++;
1031 17908498 c = lbm_ref_cell(c)->cdr;
1032 }
1033 3758557 return len;
1034 }
1035
1036 35402 lbm_value lbm_list_destructive_reverse(lbm_value list) {
1037
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 35398 times.
35402 if (!lbm_is_cons(list)) return list;
1038 35398 lbm_value curr = list;
1039 35398 lbm_value last_cell = ENC_SYM_NIL;
1040
1041
2/2
✓ Branch 0 taken 90585 times.
✓ Branch 1 taken 35398 times.
125983 while (lbm_is_cons_rw(curr)) {
1042 90585 lbm_cons_t *cell = lbm_ref_cell(curr);
1043 90585 lbm_value next = cell->cdr;
1044 90585 cell->cdr = last_cell;
1045 90585 last_cell = curr;
1046 90585 curr = next;
1047 }
1048 35398 return last_cell;
1049 }
1050
1051
1052 990864 lbm_value lbm_list_copy(int *m, lbm_value list) {
1053 990864 lbm_value curr = list;
1054 990864 lbm_uint n = lbm_list_length(list);
1055 990864 lbm_uint copy_n = n;
1056
4/4
✓ Branch 0 taken 18543 times.
✓ Branch 1 taken 972321 times.
✓ Branch 2 taken 16267 times.
✓ Branch 3 taken 2276 times.
990864 if (*m >= 0 && (lbm_uint)*m < n) {
1057 16267 copy_n = (lbm_uint)*m;
1058 } else { // *m == -1 or *m >= n
1059 974597 *m = (int)n;
1060 }
1061
2/2
✓ Branch 0 taken 693 times.
✓ Branch 1 taken 990171 times.
990864 if (copy_n == 0) return ENC_SYM_NIL;
1062 990171 lbm_uint new_list = lbm_heap_allocate_list(copy_n);
1063
2/2
✓ Branch 0 taken 1594 times.
✓ Branch 1 taken 988577 times.
990171 if (lbm_is_symbol(new_list)) return new_list;
1064 988577 lbm_value curr_targ = new_list;
1065
1066
4/4
✓ Branch 0 taken 11299882 times.
✓ Branch 1 taken 972556 times.
✓ Branch 2 taken 11283861 times.
✓ Branch 3 taken 16021 times.
12272438 while (lbm_is_cons(curr) && copy_n > 0) {
1067 11283861 lbm_cons_t *curr_cell = lbm_ref_cell(curr);
1068 11283861 lbm_cons_t *targ_cell = lbm_ref_cell(curr_targ);
1069 11283861 targ_cell->car = curr_cell->car;
1070 11283861 curr_targ = targ_cell->cdr;
1071 11283861 curr = curr_cell->cdr;
1072 11283861 copy_n --;
1073 }
1074 988577 return new_list;
1075 }
1076
1077 // Append for proper lists only
1078 // Destructive update of list1.
1079 72620 lbm_value lbm_list_append(lbm_value list1, lbm_value list2) {
1080
1081
2/4
✓ Branch 0 taken 72620 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 72620 times.
✗ Branch 3 not taken.
145240 if(lbm_is_list_rw(list1) &&
1082 72620 lbm_is_list(list2)) {
1083
1084 72620 lbm_value curr = list1;
1085 72620 lbm_cons_t *cell = lbm_ref_cell(curr);
1086
2/2
✓ Branch 0 taken 95069 times.
✓ Branch 1 taken 72620 times.
167689 while(lbm_is_cons_rw(cell->cdr)) {
1087 95069 curr = cell->cdr;
1088 95069 cell = lbm_ref_cell(cell->cdr);
1089 }
1090
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 72530 times.
72620 if (lbm_is_symbol_nil(curr)) return list2;
1091 72530 lbm_set_cdr(curr, list2);
1092 72530 return list1;
1093 }
1094 ✗ return ENC_SYM_EERROR;
1095 }
1096
1097 252 lbm_value lbm_list_drop(unsigned int n, lbm_value ls) {
1098 252 lbm_value curr = ls;
1099
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) {
1100 2100 curr = lbm_ref_cell(curr)->cdr;
1101 2100 n --;
1102 }
1103 252 return curr;
1104 }
1105
1106 560295 lbm_value lbm_index_list(lbm_value l, int32_t n) {
1107 560295 lbm_value curr = l;
1108
1109
2/2
✓ Branch 0 taken 339 times.
✓ Branch 1 taken 559956 times.
560295 if (n < 0) {
1110 339 int32_t len = (int32_t)lbm_list_length(l);
1111 339 n = len + n;
1112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 339 times.
339 if (n < 0) return ENC_SYM_NIL;
1113 }
1114
1115
4/4
✓ Branch 0 taken 856379 times.
✓ Branch 1 taken 90 times.
✓ Branch 2 taken 296174 times.
✓ Branch 3 taken 560205 times.
856469 while (lbm_is_cons(curr) && n > 0) {
1116 296174 curr = lbm_ref_cell(curr)->cdr;
1117 296174 n --;
1118 }
1119
2/2
✓ Branch 0 taken 560205 times.
✓ Branch 1 taken 90 times.
560295 if (lbm_is_cons(curr)) {
1120 560205 return lbm_ref_cell(curr)->car;
1121 } else {
1122 90 return ENC_SYM_NIL;
1123 }
1124 }
1125
1126 // High-level arrays are just bytearrays but with a different tag and pointer type.
1127 // These arrays will be inspected by GC and the elements of the array will be marked.
1128
1129 // Arrays are part of the heap module because their lifespan is managed
1130 // by the garbage collector. The data in the array is not stored
1131 // in the "heap of cons cells".
1132 1771396 int lbm_heap_allocate_array_base(lbm_value *res, bool byte_array, lbm_uint size){
1133
1134 1771396 lbm_uint tag = ENC_SYM_ARRAY_TYPE;
1135 1771396 lbm_uint type = LBM_TYPE_ARRAY;
1136 1771396 lbm_array_header_t *array = NULL;
1137 1771396 lbm_array_header_extended_t *ext_array = NULL;
1138
1139
2/2
✓ Branch 0 taken 923229 times.
✓ Branch 1 taken 848167 times.
1771396 if (byte_array) {
1140 923229 array = (lbm_array_header_t*)lbm_malloc(sizeof(lbm_array_header_t));
1141 } else {
1142 848167 tag = ENC_SYM_LISPARRAY_TYPE;
1143 848167 type = LBM_TYPE_LISPARRAY;
1144 848167 size = sizeof(lbm_value) * size;
1145 848167 array = (lbm_array_header_t*)lbm_malloc(sizeof(lbm_array_header_extended_t));
1146 848167 ext_array = (lbm_array_header_extended_t*)array;
1147 }
1148
2/2
✓ Branch 0 taken 1768212 times.
✓ Branch 1 taken 3184 times.
1771396 if (array) {
1149
2/2
✓ Branch 0 taken 845587 times.
✓ Branch 1 taken 922625 times.
1768212 if (!byte_array) ext_array->index = 0;
1150
1151 1768212 array->data = NULL;
1152 1768212 array->size = size;
1153
2/2
✓ Branch 0 taken 1767760 times.
✓ Branch 1 taken 452 times.
1768212 if ( size > 0) {
1154 1767760 array->data = (lbm_uint*)lbm_malloc(size);
1155
2/2
✓ Branch 0 taken 14238 times.
✓ Branch 1 taken 1753522 times.
1767760 if (array->data == NULL) {
1156 14238 lbm_memory_free((lbm_uint*)array);
1157 14238 goto allocate_array_merror;
1158 }
1159 // It is more important to zero out high-level arrays.
1160 // 0 is symbol NIL which is perfectly safe for the GC to inspect.
1161 1753522 memset(array->data, 0, size);
1162 }
1163 // allocating a cell for array's heap-presence
1164 1753974 lbm_value cell = lbm_heap_allocate_cell(type, (lbm_uint) array, tag);
1165
2/2
✓ Branch 0 taken 1172 times.
✓ Branch 1 taken 1752802 times.
1753974 if (cell == ENC_SYM_MERROR) {
1166 1172 lbm_memory_free((lbm_uint*)array->data);
1167 1172 lbm_memory_free((lbm_uint*)array);
1168 1172 goto allocate_array_merror;
1169 }
1170 1752802 *res = cell;
1171 1752802 lbm_heap_state.num_alloc_arrays ++;
1172 1752802 return 1;
1173 }
1174 3184 allocate_array_merror:
1175 18594 *res = ENC_SYM_MERROR;
1176 18594 return 0;
1177 }
1178
1179 923229 int lbm_heap_allocate_array(lbm_value *res, lbm_uint size){
1180 923229 return lbm_heap_allocate_array_base(res, true, size);
1181 }
1182
1183 848167 int lbm_heap_allocate_lisp_array(lbm_value *res, lbm_uint size) {
1184 848167 return lbm_heap_allocate_array_base(res, false, size);
1185 }
1186
1187 3238 int lbm_lift_array(lbm_value *value, char *data, lbm_uint num_elt) {
1188
1189 3238 lbm_array_header_t *array = NULL;
1190 3238 lbm_value cell = lbm_heap_allocate_cell(LBM_TYPE_CONS, ENC_SYM_NIL, ENC_SYM_ARRAY_TYPE);
1191
1192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3238 times.
3238 if (cell == ENC_SYM_MERROR) {
1193 ✗ *value = cell;
1194 ✗ return 0;
1195 }
1196
1197 3238 array = (lbm_array_header_t*)lbm_malloc(sizeof(lbm_array_header_t));
1198
1199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3238 times.
3238 if (array == NULL) {
1200 ✗ lbm_set_car_and_cdr(cell, ENC_SYM_NIL, ENC_SYM_NIL);
1201 ✗ *value = ENC_SYM_MERROR;
1202 ✗ return 0;
1203 }
1204
1205 3238 array->data = (lbm_uint*)data;
1206 3238 array->size = num_elt;
1207
1208 3238 lbm_set_car(cell, (lbm_uint)array);
1209
1210 3238 cell = lbm_set_ptr_type(cell, LBM_TYPE_ARRAY);
1211 3238 *value = cell;
1212 3238 return 1;
1213 }
1214
1215 712757 lbm_int lbm_heap_array_get_size(lbm_value arr) {
1216
1217 712757 lbm_int r = -1;
1218 712757 lbm_array_header_t *header = lbm_dec_array_r(arr);
1219
1/2
✓ Branch 0 taken 712757 times.
✗ Branch 1 not taken.
712757 if (header) {
1220 712757 r = (lbm_int)header->size;
1221 }
1222 712757 return r;
1223 }
1224
1225 356559 const uint8_t *lbm_heap_array_get_data_ro(lbm_value arr) {
1226 356559 uint8_t *r = NULL;
1227 356559 lbm_array_header_t *header = lbm_dec_array_r(arr);
1228
1/2
✓ Branch 0 taken 356559 times.
✗ Branch 1 not taken.
356559 if (header) {
1229 356559 r = (uint8_t*)header->data;
1230 }
1231 356559 return r;
1232 }
1233
1234 /* Explicitly freeing an array.
1235
1236 This is a highly unsafe operation and can only be safely
1237 used if the heap cell that points to the array has not been made
1238 accessible to the program.
1239
1240 So This function can be used to free an array in case an array
1241 is being constructed and some error case appears while doing so
1242 If the array still have not become available it can safely be
1243 "explicitly" freed.
1244
1245 The problem is that if the "array" heap-cell is made available to
1246 the program, this cell can easily be duplicated and we would have
1247 to search the entire heap to find all cells pointing to the array
1248 memory in question and "null"-them out before freeing the memory
1249 */
1250
1251 336 int lbm_heap_explicit_free_array(lbm_value arr) {
1252
1253 336 int r = 0;
1254
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) {
1255 336 lbm_array_header_t *header = (lbm_array_header_t*)lbm_car(arr);
1256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
336 if (header == NULL) {
1257 ✗ return 0;
1258 }
1259 336 lbm_memory_free((lbm_uint*)header->data);
1260 336 lbm_memory_free((lbm_uint*)header);
1261
1262 336 arr = lbm_set_ptr_type(arr, LBM_TYPE_CONS);
1263 336 lbm_set_car(arr, ENC_SYM_NIL);
1264 336 lbm_set_cdr(arr, ENC_SYM_NIL);
1265 336 r = 1;
1266 }
1267
1268 336 return r;
1269 }
1270
1271 ✗ static bool dummy_flash_write(lbm_uint ix, lbm_uint val) {
1272 (void)ix;
1273 (void)val;
1274 ✗ return false;
1275 }
1276
1277 static const_heap_write_fun const_heap_write = dummy_flash_write;
1278
1279 67085 int lbm_const_heap_init(const_heap_write_fun w_fun,
1280 lbm_const_heap_t *heap,
1281 lbm_uint *addr) {
1282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67085 times.
67085 if (((uintptr_t)addr % 4) != 0) return 0;
1283
1284
2/2
✓ Branch 0 taken 66992 times.
✓ Branch 1 taken 93 times.
67085 if (!lbm_const_heap_mutex_initialized) {
1285 66992 lbm_mutex_init(&lbm_const_heap_mutex);
1286 66992 lbm_const_heap_mutex_initialized = true;
1287 }
1288
1289
2/2
✓ Branch 0 taken 66992 times.
✓ Branch 1 taken 93 times.
67085 if (!lbm_mark_mutex_initialized) {
1290 66992 lbm_mutex_init(&lbm_mark_mutex);
1291 66992 lbm_mark_mutex_initialized = true;
1292 }
1293
1294 67085 const_heap_write = w_fun;
1295
1296 67085 heap->heap = addr;
1297 67085 heap->size = 0;
1298 67085 heap->next = 0;
1299
1300 67085 lbm_const_heap_state = heap;
1301 // ref_cell views the lbm_uint array as an lbm_cons_t array
1302 67085 lbm_heaps[LBM_CONST_HEAP] = (lbm_cons_t*)addr;
1303 67085 return 1;
1304 }
1305
1306 40229 lbm_flash_status lbm_allocate_const_cell(lbm_value *res) {
1307 40229 lbm_flash_status r = LBM_FLASH_FULL;
1308
1309 40229 lbm_mutex_lock(&lbm_const_heap_mutex);
1310 // waste a cell if we have ended up unaligned after writing an array to flash.
1311
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 40136 times.
40229 if (lbm_const_heap_state->next % 2 == 1) {
1312 93 lbm_const_heap_state->next++;
1313 }
1314
1315
1/2
✓ Branch 0 taken 40229 times.
✗ Branch 1 not taken.
40229 if (lbm_const_heap_state &&
1316
2/2
✓ Branch 0 taken 40227 times.
✓ Branch 1 taken 2 times.
40229 (lbm_const_heap_state->next+1) < (uint32_t)lbm_image_get_write_index()) {
1317 // A cons cell uses two words.
1318 40227 lbm_value cell = lbm_const_heap_state->next;
1319 40227 lbm_const_heap_state->next += 2;
1320 40227 *res = ((cell >> 1) << LBM_ADDRESS_SHIFT) | LBM_PTR_BIT | LBM_TYPE_CONS | LBM_PTR_TO_CONSTANT_BIT;
1321 40227 r = LBM_FLASH_WRITE_OK;
1322 }
1323 40229 lbm_mutex_unlock(&lbm_const_heap_mutex);
1324 40229 return r;
1325 }
1326
1327 32804 lbm_flash_status lbm_allocate_const_raw(lbm_uint nwords, lbm_uint *res) {
1328 32804 lbm_flash_status r = LBM_FLASH_FULL;
1329
1330
1/2
✓ Branch 0 taken 32804 times.
✗ Branch 1 not taken.
32804 if (lbm_const_heap_state &&
1331
2/2
✓ Branch 0 taken 32803 times.
✓ Branch 1 taken 1 times.
32804 (lbm_const_heap_state->next + nwords) <= (uint32_t)lbm_image_get_write_index()) {
1332 32803 lbm_uint ix = lbm_const_heap_state->next;
1333 32803 *res = (lbm_uint)&lbm_const_heap_state->heap[ix];
1334 32803 lbm_const_heap_state->next += nwords;
1335 32803 r = LBM_FLASH_WRITE_OK;
1336 }
1337 32804 return r;
1338 }
1339
1340 763 lbm_flash_status lbm_write_const_raw(lbm_uint *data, lbm_uint n, lbm_uint *res) {
1341
1342 763 lbm_flash_status r = LBM_FLASH_FULL;
1343
1344
1/2
✓ Branch 0 taken 763 times.
✗ Branch 1 not taken.
763 if (lbm_const_heap_state &&
1345
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()) {
1346 763 lbm_uint ix = lbm_const_heap_state->next;
1347
1348
2/2
✓ Branch 0 taken 1545 times.
✓ Branch 1 taken 763 times.
2308 for (unsigned int i = 0; i < n; i ++) {
1349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1545 times.
1545 if (!const_heap_write(((lbm_uint*)data)[i],ix + i))
1350 ✗ return LBM_FLASH_WRITE_ERROR;
1351 }
1352 763 lbm_const_heap_state->next += n;
1353 763 *res = (lbm_uint)&lbm_const_heap_state->heap[ix];
1354 763 r = LBM_FLASH_WRITE_OK;
1355 }
1356 763 return r;
1357 }
1358
1359 252 lbm_flash_status lbm_const_write(lbm_uint *tgt, lbm_uint val) {
1360
1361
1/2
✓ Branch 0 taken 252 times.
✗ Branch 1 not taken.
252 if (lbm_const_heap_state) {
1362 252 lbm_uint flash = (lbm_uint)lbm_const_heap_state->heap;
1363 252 lbm_uint ix = (((lbm_uint)tgt - flash) / sizeof(lbm_uint)); // byte address to ix
1364
1/2
✓ Branch 0 taken 252 times.
✗ Branch 1 not taken.
252 if (const_heap_write(val, ix)) {
1365 252 return LBM_FLASH_WRITE_OK;
1366 }
1367 ✗ return LBM_FLASH_WRITE_ERROR;
1368 }
1369 ✗ return LBM_FLASH_FULL;
1370 }
1371
1372 7506 lbm_flash_status write_const_cdr(lbm_value cell, lbm_value val) {
1373 7506 lbm_uint addr = lbm_dec_ptr(cell) << 1;
1374
1/2
✓ Branch 0 taken 7506 times.
✗ Branch 1 not taken.
7506 if (const_heap_write(val, addr+1))
1375 7506 return LBM_FLASH_WRITE_OK;
1376 ✗ return LBM_FLASH_WRITE_ERROR;
1377 }
1378
1379 7509 lbm_flash_status write_const_car(lbm_value cell, lbm_value val) {
1380 7509 lbm_uint addr = lbm_dec_ptr(cell) << 1;
1381
2/2
✓ Branch 0 taken 7507 times.
✓ Branch 1 taken 2 times.
7509 if (const_heap_write(val, addr))
1382 7507 return LBM_FLASH_WRITE_OK;
1383 2 return LBM_FLASH_WRITE_ERROR;
1384 }
1385
1386 9 lbm_uint lbm_flash_memory_usage(void) {
1387 9 return lbm_const_heap_state->next;
1388 }
1389
1390
1391 // ////////////////////////////////////////////////////////////
1392 // pointer reversal traversal
1393 //
1394 // Caveats:
1395 // * Structures on the constant heap cannot be traversed using
1396 // pointer reversal. If a dynamic structure is pointing into the
1397 // constant heap, the 'f' will be applied to the constant cons cell on
1398 // the border and then traversal will retreat.
1399 //
1400 // * It may be impossible to detect all kinds of cycles
1401 // if attempting to use and restore the GC in a single pass
1402 // over the tree. To detect cycles all visited nodes must
1403 // remain detectable when traversing all branches!
1404 //
1405 // * Potential fix is to run GC after a complete traversal of the
1406 // entire value in order to restore the GC bits.
1407 //
1408 // * If we leave GC bits set when traversing values, we can use this
1409 // to detect cycles that happen in multiple steps accross values
1410 // in the environment.
1411 //
1412 // * lbm_ptr_rev_trav with the "do_nothing" travfun is the same thing
1413 // as a GC mark phase! Maybe utilize this for code-size
1414 // purposes. This also increases the amount of testing the
1415 // ptr_rev_trav function is subjected to.
1416
1417 3013 void lbm_ptr_rev_trav(trav_fun f, lbm_value v, void* arg) {
1418
1419 3013 lbm_value curr = v;
1420 3013 lbm_value prev = lbm_enc_cons_ptr(LBM_PTR_NULL);
1421 while (true) {
1422
1423 // Run leftwards and process conses until
1424 // hitting a leaf in the left direction.
1425
1426 // If curr is marked here there is a cycle in the graph.
1427 // In case of a cycle or leaf, this first loop is exited.
1428
2/2
✓ Branch 0 taken 28616 times.
✓ Branch 1 taken 75 times.
83459 while (((lbm_is_cons_rw(curr)) ||
1429
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28616 times.
57307 (lbm_is_lisp_array_rw(curr)) ||
1430
4/4
✓ Branch 0 taken 28691 times.
✓ Branch 1 taken 26077 times.
✓ Branch 2 taken 25775 times.
✓ Branch 3 taken 377 times.
83459 (lbm_is_channel(curr))) && !gc_marked(curr)) {
1431 25775 lbm_cons_t *cell = lbm_ref_cell(curr);
1432
2/2
✓ Branch 0 taken 25715 times.
✓ Branch 1 taken 60 times.
25775 if (lbm_is_cons(curr)) {
1433 // In-order traversal
1434
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 25633 times.
25715 if (f(curr, false, arg) == TRAV_FUN_SUBTREE_DONE) {
1435 82 lbm_gc_mark_phase(curr);
1436 82 goto trav_backtrack;
1437 }
1438 25633 gc_mark(curr);
1439
1440 25633 lbm_value next = 0;
1441 25633 value_assign(&next, cell->car);
1442 25633 value_assign(&cell->car, prev);
1443 25633 value_assign(&prev, curr);
1444 25633 value_assign(&curr, next);
1445
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 } else if (lbm_is_channel (curr)) {
1446 ✗ if (f(curr, false, arg) == TRAV_FUN_SUBTREE_DONE) {
1447 ✗ lbm_gc_mark_phase(curr);
1448 ✗ goto trav_backtrack;
1449 }
1450 ✗ gc_mark(curr);
1451
1452 ✗ lbm_char_channel_t *chan = (lbm_char_channel_t*)cell->car;
1453 ✗ lbm_value next = 0;
1454 ✗ value_assign(&next, chan->dependency);
1455 ✗ value_assign(&chan->dependency, prev);
1456 ✗ value_assign(&prev, curr);
1457 ✗ value_assign(&curr, next);
1458 } else { // it is an array
1459
1460 60 lbm_array_header_extended_t *arr = (lbm_array_header_extended_t*)cell->car;
1461 60 lbm_value *arr_data = (lbm_value *)arr->data;
1462 60 uint32_t index = arr->index;
1463
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 54 times.
60 if (arr->size == 0) break;
1464
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
1465
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if (f(curr, false, arg) == TRAV_FUN_SUBTREE_DONE) {
1466 ✗ lbm_gc_mark_phase(curr);
1467 ✗ break;
1468 }
1469 54 arr->index = 1;
1470 54 gc_mark(curr);
1471
1472 54 lbm_value next = 0;
1473 54 value_assign(&next, arr_data[0]);
1474 54 value_assign(&arr_data[0], prev);
1475 54 value_assign(&prev, curr);
1476 54 value_assign(&curr, next);
1477 }
1478 }
1479 }
1480 // Currently there are a few different users of this traversal.
1481 // size, flatten and detect_sharing.
1482 // detect_sharing make use of the shared (true) argument in f(curr, true, arg)
1483 // while the other do not. detect_sharing also assumes it is run once per env item
1484 // while not resetting any GC-bits in between. This detects global sharing.
1485
1486
4/4
✓ Branch 0 taken 1088 times.
✓ Branch 1 taken 27911 times.
✓ Branch 2 taken 377 times.
✓ Branch 3 taken 711 times.
28999 if (lbm_is_ptr(curr) && gc_marked(curr)) {
1487 // gc bit set so this subtree is already traversed.
1488 // f is called with true to indicate visited node.
1489 // if this happens during a sharing discovery phase, curr will be added to sharing table.
1490 377 f(curr, true, arg);
1491 // In this case f should not be able to return subtree continue.
1492 // The only correct return value from f is PROCEED.
1493
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 28619 times.
28622 } else if (!lbm_is_cons(curr) || // Found a leaf
1494
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 (curr & LBM_PTR_TO_CONSTANT_BIT)) {
1495
4/4
✓ Branch 0 taken 711 times.
✓ Branch 1 taken 27911 times.
✓ Branch 2 taken 708 times.
✓ Branch 3 taken 3 times.
28622 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.
1496 28622 f(curr, false, arg);
1497 }
1498
1499 // Now either prev has the "flag" set or it doesnt.
1500 // If the flag is set that means that the prev node
1501 // have had both its car and cdr visited. So that node is done!
1502 //
1503 // If the flag is not set, jump down to SWAP
1504
1505 ✗ trav_backtrack:
1506
2/2
✓ Branch 0 taken 51266 times.
✓ Branch 1 taken 3013 times.
109047 while ((lbm_is_cons(prev) &&
1507
2/2
✓ Branch 0 taken 25633 times.
✓ Branch 1 taken 25633 times.
105545 (lbm_dec_ptr(prev) != LBM_PTR_NULL) && // is LBM_NULL a cons type?
1508
2/2
✓ Branch 0 taken 489 times.
✓ Branch 1 taken 28646 times.
80401 lbm_get_gc_flag(lbm_car(prev))) ||
1509
3/4
✓ Branch 0 taken 54279 times.
✓ Branch 1 taken 489 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 28646 times.
112060 lbm_is_lisp_array_rw(prev) ||
1510 28646 lbm_is_channel(prev)) {
1511 26122 lbm_cons_t *cell = lbm_ref_cell(prev);
1512
2/2
✓ Branch 0 taken 25633 times.
✓ Branch 1 taken 489 times.
26122 if (lbm_is_cons(prev)) {
1513
1514 // clear the flag
1515 // This means that we are done with a "CDR" child.
1516 // prev = [ a , b ][flag = 1]
1517 // =>
1518 // prev = [ a , b ][flag = 0]
1519
1520 //gc_clear_mark(prev);
1521 25633 cell->car = lbm_clr_gc_flag(cell->car);
1522 // Move on downwards until
1523 // finding a cons cell without flag or NULL
1524
1525 // curr = c
1526 // prev = [ a , b ][flag = 0]
1527 // =>
1528 // prev = [ a , c ][flag = 0]
1529 // curr = prev
1530 // prev = b
1531
1532 25633 lbm_value next = 0;
1533 25633 value_assign(&next, cell->cdr);
1534 25633 value_assign(&cell->cdr, curr);
1535 25633 value_assign(&curr, prev);
1536 25633 value_assign(&prev, next);
1537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 489 times.
489 } else if (lbm_is_channel(prev)) {
1538 ✗ lbm_char_channel_t *chan = (lbm_char_channel_t*)cell->car;
1539 ✗ lbm_value next = 0;
1540 ✗ value_assign(&next, chan->dependency);
1541 ✗ value_assign(&chan->dependency, curr);
1542 ✗ value_assign(&curr, prev);
1543 ✗ value_assign(&prev, next);
1544 } else { // is an array
1545 489 lbm_array_header_extended_t *arr = (lbm_array_header_extended_t*)cell->car;
1546 489 lbm_value *arr_data = (lbm_value *)arr->data;
1547 489 size_t arr_size = (size_t)arr->size / sizeof(lbm_value);
1548 489 lbm_value next = 0;
1549
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 435 times.
489 if (arr->index == arr_size) {
1550 54 value_assign(&next, arr_data[arr->index-1]);
1551 54 value_assign(&arr_data[arr->index-1], curr);
1552 54 value_assign(&curr, prev);
1553 54 value_assign(&prev, next);
1554 54 arr->index = 0;
1555 } else {
1556 435 break;
1557 }
1558 }
1559 }
1560
1561 // SWAP
1562
1563 // if the prev node is NULL we have traced backwards all the
1564 // way back to where curr == v. Another alternative is that
1565 // the input v was an Atom. We are done!
1566
3/4
✓ Branch 0 taken 29081 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3013 times.
✓ Branch 3 taken 26068 times.
58162 if (lbm_is_ptr(prev) &&
1567 29081 lbm_dec_ptr(prev) == LBM_PTR_NULL) {
1568 3013 if (lbm_is_cons(curr)) {
1569 //gc_clear_mark(curr);
1570 }
1571 //done = true;
1572 3013 break;
1573 }
1574
1575 // if the prev node is not NULL then we should move
1576 // down to the prev node and start process its remaining child.
1577
2/2
✓ Branch 0 taken 25633 times.
✓ Branch 1 taken 435 times.
26068 else if (lbm_is_cons(prev)) {
1578
1579 25633 lbm_cons_t *cell = lbm_ref_cell(prev);
1580 25633 lbm_value next = 0;
1581
1582
1583 // prev = [ p , cdr ][flag = 0]
1584 // =>
1585 // prev = [ p , cdr ][flag = 1]
1586
1587 25633 cell->car = lbm_set_gc_flag(cell->car);
1588
1589 // switch to processing the cdr field and set the flag.
1590 // curr = c
1591 // prev = [ a, b ][flag = 1]
1592 // =>
1593 // prev = [ c, a ][flag = 1]
1594 // curr = b
1595
1596 25633 value_assign(&next, cell->car);
1597 25633 value_assign(&cell->car, curr);
1598 25633 value_assign(&curr, cell->cdr);
1599 25633 value_assign(&cell->cdr, next);
1600
1/2
✓ Branch 0 taken 435 times.
✗ Branch 1 not taken.
435 } else if (lbm_is_lisp_array_rw(prev)) {
1601 435 lbm_cons_t *cell = lbm_ref_cell(prev);
1602 435 lbm_array_header_extended_t *arr = (lbm_array_header_extended_t*)cell->car;
1603 435 lbm_value *arr_data = (lbm_value *)arr->data;
1604 435 lbm_value next = 0;
1605
1606 435 value_assign(&next, arr_data[arr->index-1]);
1607 435 value_assign(&arr_data[arr->index-1], curr);
1608 435 value_assign(&curr, arr_data[arr->index]);
1609 435 value_assign(&arr_data[arr->index], next);
1610 435 arr->index = arr->index + 1;
1611 }
1612 }
1613 3013 }
1614