Bug Summary

File:heap.c
Warning:line 1097, column 18
Access to field 'cdr' results in a dereference of a null pointer (loaded from variable 'targ_cell')

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple i386-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name heap.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu i686 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/home/joels/Current/lispbm -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -I .//include -I .//include/extensions -I .//src -I .//utils -I platform/linux/include -D _PRELUDE -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/11/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/i386-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -std=c99 -fdebug-compilation-dir=/home/joels/Current/lispbm -ferror-limit 19 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/joels/Current/lispbm/test_reports/version_0.41.0/scan-build/2026-10-16-144956-1359644-1 -x c src/heap.c

src/heap.c

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

←

.//include/heap.h

1/*
2 Copyright 2018, 2024, 2025 Joel Svensson svenssonjoel@yahoo.se
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17/** \file heap.h */
18
19#ifndef HEAP_H_
20#define HEAP_H_
21
22#include <string.h>
23#include <stdarg.h>
24
25#include "lbm_types.h"
26#include "symrepr.h"
27#include "stack.h"
28#include "lbm_memory.h"
29#include "lbm_defines.h"
30#include "lbm_channel.h"
31#include "lbm_image.h"
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37/*
38Planning for a more space efficient heap representation.
39TODO: Need to find a good reference to read up on this.
40 - List based heap
41 - Easy to implement and somewhat efficient
42
430000 0000 Size Free bits
44003F FFFF 4MB 10
45007F FFFF 8MB 9
4600FF FFFF 16MB 8
4701FF FFFF 32MB 7
4803FF FFFF 64MB 6 * Kind of heap size I am looking for
4907FF FFFF 128MB 5
500FFF FFFF 256MB 4
511FFF FFFF 512MB 3
52
53
54--- May 9 2021 ---
55Actually now I am much more interested in way smaller memories ;)
56
570000 0000 Size Free bits
580000 0FFF 4KB 20 |
590000 1FFF 8KB 19 |
600000 3FFF 16KB 18 |
610000 7FFF 32KB 17 |
620000 FFFF 64KB 16 |
630001 FFFF 128KB 15 |
640003 FFFF 256KB 14 | - This range is very interesting.
650007 FFFF 512KB 13
66000F FFFF 1MB 12
67001F FFFF 2MB 11
68003F FFFF 4MB 10
69007F FFFF 8MB 9
7000FF FFFF 16MB 8
7101FF FFFF 32MB 7
7203FF FFFF 64MB 6
7307FF FFFF 128MB 5
740FFF FFFF 256MB 4
751FFF FFFF 512MB 3
76
77Those are the kind of platforms that are fun... so a bunch of
78wasted bits in heap pointers if we run on small MCUs.
79
80-----------------
81
82it is also the case that not all addresses will be used if all "cells" are
83of the same size, 8 bytes...
84
85value 0: 0000 0000
86value 1: 0000 0008
87value 3: 0000 0010
88value 4: 0000 0018
89
90Means bits 0,1,2 will always be empty in a valid address.
91
92Cons cells also need to be have room for 2 pointers. So each ted cell from
93memory should be 8bytes.
94
95Things that needs to be represented within these bits:
96
97 - GC MARK one per cell
98 - TYPE: type of CAR and type of cons
99
100Types I would want:
101 - Full 32bit integer. Does not leave room for identification of type
102 - Float values. Same problem
103
104
105Free bits in pointers 64MB heap:
10631 30 29 28 27 26 2 1 0
1070 0 0 0 0 0 XX XXXX XXXX XXXX XXXX XXXX X 0 0 0
108
109
110Information needed for each cell:
111 Meaning | bits total | bits per car | bits per cdr
112 GC mark | 2 | 1 | 1 - only one of them will be used (the other is wasted)
113 Type | 2x | x | x
114 Ptr/!ptr | 2 | 1 | 1
115
116
117Types (unboxed):
118 - Symbols
119 - 28bit integer ( will need signed shift right functionality )
120 - 28bit unsigned integer
121 - Character
122
123If four types is all that should be possible (unboxed). then 2 bits are needed to differentiate.
1242 + 1 + 1 = 4 => 28bits for data.
125
126bit 0: ptr/!ptr
127bit 1: gc
128bit 2-3: type (if not ptr)
129bit 3 - 24 ptr (if ptr)
130bit 4 - 31 value (if value)
131
132An unboxed value can occupy a car or cdr field in a cons cell.
133
134types (boxed) extra information in pointer to cell can contain information
135 - 32 bit integer
136 - 32 bit unsigned integer
137 - 32 bit float
138
139boxed representation:
140 [ptr| cdr]
141 |
142 [Value | Aux + GC_MARK]
143
144Kinds of pointers:
145 - Pointer to cons cell.
146 - Pointer to unboxed value (fixnums not in a list, I hope this is so rare that it can be removed )
147 - integer
148 - unsigned integer
149 - symbol
150 - float
151 - Pointer to boxed value.
152 - 32 bit integer
153 - 32 bit unsigned integer
154 - 32 bit float
155 - (Maybe something else ? Vectors/strings allocated in memory not occupied by heap?)
156 - vector of int
157 - vector of uint
158 - vector of float
159 - vector of double
160 - String
161
16213 pointer"types" -> needs 4 bits
163for 64MB heap there are 6 free bits. So with this scheme going to 128MB or 256MB heap
164is also possible
165
166 a pointer to some off heap vector/string could be represented by
167
168 [ptr | cdr]
169 |
170 [full pointer | Aux + GC_MARK]
171 |
172 [VECTOR]
173
174Aux bits could be used for storing vector size. Up to 30bits should be available there
175>> This is problematic. Now the information that something is a vector is split up
176>> between 2 cons cells. This means GC needs both of these intact to be able to make
177>> proper decision.
178>> Will try to resolve this by adding some special symbols. But these must be symbols
179>> that cannot occur normally in programs. Then an array could be:
180
181 [Full pointer | ARRAY_SYM + GC_MARK]
182 |
183 [VECTOR]
184
185>> Boxed values same treatment as above.
186>> TODO: Could this be simpler?
187
188[ VALUE | TYPE_SYM + GC_MARK]
189
190
1910000 00XX XXXX XXXX XXXX XXXX XXXX X000 : 0x03FF FFF8
1921111 AA00 0000 0000 0000 0000 0000 0000 : 0xFC00 0000 (AA bits left unused for now, future heap growth?)
193 */
194
195typedef enum {
196 LBM_FLASH_WRITE_OK,
197 LBM_FLASH_FULL,
198 LBM_FLASH_WRITE_ERROR
199} lbm_flash_status;
200
201/** Struct representing a heap cons-cell.
202 *
203 */
204typedef struct {
205 lbm_value car;
206 lbm_value cdr;
207} lbm_cons_t;
208
209/**
210 * Heap state
211 */
212typedef struct {
213 lbm_cons_t *heap;
214 lbm_value freelist; // list of free cons cells.
215 lbm_stack_t gc_stack;
216
217 lbm_uint heap_size; // In number of cells.
218 lbm_uint heap_bytes; // In bytes.
219
220 lbm_uint num_free; // Number of free cells.
221 lbm_uint num_alloc_arrays; // Number of arrays allocated.
222
223 lbm_uint gc_num; // Number of times gc has been performed.
224 lbm_uint gc_marked; // Number of cells marked by mark phase.
225 lbm_uint gc_recovered; // Number of cells recovered by sweep phase.
226 lbm_uint gc_recovered_arrays;// Number of arrays recovered by sweep.
227 lbm_uint gc_least_free; // The smallest length of the freelist.
228 lbm_uint gc_last_free; // Number of elements on the freelist
229 // after most recent GC.
230} lbm_heap_state_t;
231
232extern lbm_heap_state_t lbm_heap_state;
233
234typedef bool_Bool (*const_heap_write_fun)(lbm_uint w, lbm_uint ix);
235
236typedef struct {
237 lbm_uint *heap;
238 lbm_uint next; // next free index.
239 lbm_uint size; // in lbm_uint words. (cons-cells = words / 2)
240} lbm_const_heap_t;
241
242/**
243 * The header portion of an array stored in array and symbol memory.
244 * An array is always a byte array. use the array-extensions for
245 * storing and reading larger values from arrays.
246 */
247typedef struct {
248 lbm_uint size; /// Number of elements
249 lbm_uint *data; /// pointer to lbm_memory array or C array.
250} lbm_array_header_t;
251
252typedef struct {
253 lbm_uint size;
254 lbm_uint *data;
255 uint32_t index; // Limits arrays to max 2^32-1 elements.
256} lbm_array_header_extended_t;
257
258/** Lock GC mutex
259 * Locks a mutex during GC marking when using the pointer reversal algorithm.
260 * Does nothing when using stack based GC mark.
261 */
262void lbm_gc_lock(void);
263/* Unlock GC mutex
264 */
265void lbm_gc_unlock(void);
266
267/** Initialize heap storage.
268 * \param addr Pointer to an array of lbm_cons_t elements. This array must at least be aligned 4.
269 * \param num_cells Number of lbm_cons_t elements in the array.
270 * \param gc_stack_size Size of the gc_stack in number of words.
271 * \return true on success or false for failure.
272 */
273bool_Bool lbm_heap_init(lbm_cons_t *addr, lbm_uint num_cells,
274 lbm_uint gc_stack_size);
275
276/** Add GC time statistics to heap_stats
277 *
278 * \internalonly
279 *
280 * \param dur Duration as reported by the timestamp callback.
281 */
282void lbm_heap_new_gc_time(lbm_uint dur);
283/** Add a new free_list length to the heap_stats.
284 * Calculates a new freelist length and updates
285 * the GC statistics.
286 *
287 * \internalonly
288 *
289 */
290void lbm_heap_new_freelist_length(void);
291/** Check how many lbm_cons_t cells are on the free-list
292 *
293 * \return Number of free lbm_cons_t cells.
294 */
295static inline lbm_uint lbm_heap_num_free(void) {
296 return lbm_heap_state.num_free;
297}
298
299/** Check how many lbm_cons_t cells are allocated.
300 *
301 * \return Number of lbm_cons_t cells that are currently allocated.
302 */
303lbm_uint lbm_heap_num_allocated(void);
304/** Size of the heap in number of lbm_cons_t cells.
305 *
306 * \return Size of the heap in number of lbm_cons_t cells.
307 */
308lbm_uint lbm_heap_size(void);
309/** Size of the heap in bytes.
310 *
311 * \return Size of heap in bytes.
312 */
313lbm_uint lbm_heap_size_bytes(void);
314/** Allocate an lbm_cons_t cell from the heap.
315 *
316 * \evalpaused
317 *
318 * \param type A type that can be encoded onto the cell (most often LBM_PTR_TYPE_CONS).
319 * \param car Value to write into car position of allocated cell.
320 * \param cdr Value to write into cdr position of allocated cell.
321 * \return a heap cell on success and Memory_error on failure.
322 */
323lbm_value lbm_heap_allocate_cell(lbm_type ptr_type, lbm_value car, lbm_value cdr);
324
325/** Allocate a list of n heap-cells.
326 *
327 * \evalpaused
328 *
329 * \param n The number of heap-cells to allocate.
330 * \return A list of heap-cells or Memory error if unable to allocate.
331 */
332lbm_value lbm_heap_allocate_list(lbm_uint n);
333
334/** Allocate a list of n heap-cells and initialize the values.
335 *
336 * \evalpaused
337 *
338 * \param ls The result list is passed through this ptr.
339 * \param n The length of list to allocate.
340 * \param valist The values in a va_list to initialize the list with.
341 * \return An initialized list of heap-cells or Memory error if unable to allocate.
342 */
343lbm_value lbm_heap_allocate_list_init_va(unsigned int n, va_list valist);
344
345/** Allocate a list of n heap-cells and initialize the values.
346 *
347 * \evalpaused
348 *
349 * \param n The length of list to allocate.
350 * \param ... The values to initialize the list with.
351 * \return allocated list or error symbol.
352 */
353lbm_value lbm_heap_allocate_list_init(unsigned int n, ...);
354/** Decode an lbm_value representing a string into a C string
355 *
356 * \param val Value
357 * \return allocated list or error symbol
358 */
359char *lbm_dec_str(lbm_value val);
360/** Decode a readable array into both a data pointer and its size in one
361 * call.
362 *
363 * \param val Value to decode.
364 * \param data Output: pointer to the array's raw data.
365 * \param size Output: the array's size in bytes.
366 * \return true on success, false if val is not a readable array.
367 */
368bool_Bool lbm_dec_str_size(lbm_value val, char **data, size_t *size);
369/** Decode a readable array, if the argument is not an array the result is NULL
370 * \param val value to decode.
371 * \return array pointer or NULL.
372 */
373lbm_array_header_t *lbm_dec_array_r(lbm_value val);
374/** Decode an a read/write array, if the argument is not an array the result is NULL
375 * \param val value to decode.
376 * \return array pointer or NULL.
377 */
378lbm_array_header_t *lbm_dec_array_rw(lbm_value val);
379/** Decode an lbm_value representing a char channel into an lbm_char_channel_t pointer.
380 *
381 * \param val Value
382 * \return A pointer to an lbm_char_channel_t or NULL if the value does not encode a channel.
383 */
384lbm_char_channel_t *lbm_dec_channel(lbm_value val);
385/** Decode an lbm_value representing a custom type into a lbm_uint value.
386 *
387 * \param val Value.
388 * \return The custom type payload.
389 */
390lbm_uint lbm_dec_custom(lbm_value val);
391/** Decode a numerical value as if it is char
392 *
393 * \param val Value to decode
394 * \return The value encoded in val casted to a char. Returns 0 if val does not encode a number.
395 */
396uint8_t lbm_dec_as_char(lbm_value a);
397/** Decode a numerical value as if it is unsigned
398 *
399 * \param val Value to decode
400 * \return The value encoded in val casted to an unsigned int. Returns 0 if val does not encode a number.
401 */
402uint32_t lbm_dec_as_u32(lbm_value val);
403/** Decode a numerical value as a signed integer.
404 *
405 * \param val Value to decode
406 * \return The value encoded in val casted to a signed int. Returns 0 if val does not encode a number.
407 */
408int32_t lbm_dec_as_i32(lbm_value val);
409/** Decode a numerical value as a float.
410 *
411 * \param val Value to decode.
412 * \return The value encoded in val casted to a float. Returns 0 if val does not encode a number.
413 */
414float lbm_dec_as_float(lbm_value val);
415/** Decode a numerical value as if it is a 64bit unsigned
416 *
417 * \param val Value to decode
418 * \return The value encoded in val casted to an unsigned int. Returns 0 if val does not encode a number.
419 */
420uint64_t lbm_dec_as_u64(lbm_value val);
421/** Decode a numerical value as a 64bit signed integer.
422 *
423 * \param val Value to decode
424 * \return The value encoded in val casted to a signed int. Returns 0 if val does not encode a number.
425 */
426int64_t lbm_dec_as_i64(lbm_value val);
427/** Decode a numerical value as a float.
428 *
429 * \param val Value to decode.
430 * \return The value encoded in val casted to a float. Returns 0 if val does not encode a number.
431 */
432double lbm_dec_as_double(lbm_value val);
433
434/** Decode a numerical value into the architecture defined unsigned integer type.
435 *
436 * \param val Value to decode
437 * \return The value encoded in val casted to an unsigned int. Returns 0 if val does not encode a number.
438 */
439lbm_uint lbm_dec_as_uintlbm_dec_as_u32(lbm_value val);
440
441/** Decode a numerical value into the architecture defined signed integer type.
442 *
443 * \param val Value to decode
444 * \return The value encoded in val casted to a signed int. Returns 0 if val does not encode a number.
445 */
446lbm_int lbm_dec_as_intlbm_dec_as_i32(lbm_value val);
447
448lbm_uint lbm_dec_raw(lbm_value v);
449/** Allocates an lbm_cons_t cell from the heap and populates it.
450 *
451 * \param car The value to put in the car field of the allocated lbm_cons_t.
452 * \param cdr The value to put in the cdr field of the allocated lbm_cons_t.
453 * \return A value referencing the lbm_cons_t or enc_sym(SYM_MERROR) if heap is full.
454 */
455
456lbm_value lbm_cons(lbm_value car, lbm_value cdr);
457
458/** Accesses the car field of an lbm_cons_t.
459 *
460 * \param cons Value
461 * \return The car field of the lbm_cons_t if cons is a reference to a heap cell.
462 * If cons is nil, the return value is nil. If the value
463 * is not cons or nil, the return value is enc_sym(SYM_TERROR) for type error.
464 */
465lbm_value lbm_car(lbm_value cons);
466/** Accesses the car field the car field of an lbm_cons_t.
467 *
468 * \param cons Value
469 * \return The car of car field or nil.
470 */
471lbm_value lbm_caar(lbm_value c);
472/** Accesses the car of the cdr of an cons cell
473 *
474 * \param c Value
475 * \return the cdr field or type error.
476 */
477lbm_value lbm_cadr(lbm_value c);
478/** Accesses the cdr field of an lbm_cons_t.
479 *
480 * \param cons Value
481 * \return The cdr field of the lbm_cons_t if cons is a reference to a heap cell.
482 * If cons is nil, the return value is nil. If the value
483 * if not cons or nil, the return value is enc_sym(SYM_TERROR) for type error.
484 */
485lbm_value lbm_cdr(lbm_value cons);
486/** Accesses the cdr of an cdr field of an lbm_cons_t.
487 *
488 * \param cons Value
489 * \return The cdr of the cdr field of the lbm_cons_t if cons is a reference to a heap cell.
490 * If cons is nil, the return value is nil. If the value
491 * if not cons or nil, the return value is enc_sym(SYM_TERROR) for type error.
492 */
493lbm_value lbm_cddr(lbm_value c);
494
495// List functions
496/** Calculate the length of a proper list
497 *
498 * \cycledanger
499 * \evalpaused
500 *
501 * \param c A list
502 * \return The length of the list. Unless the value is a cyclic structure on the heap, this function will terminate.
503 */
504lbm_uint lbm_list_length(lbm_value c);
505/** Reverse a proper list destroying the original.
506 *
507 * \cycledanger
508 * \evalpaused
509 *
510 * \param list A list
511 * \return The list reversed
512 */
513lbm_value lbm_list_destructive_reverse(lbm_value list);
514/** Copy a list, terminating the copy with tail instead of nil.
515 *
516 * \cycledanger
517 * \evalpaused
518 *
519 * \param m Number of elements to copy or -1 for all. If -1, m will be updated with the length of the list
520 * \param list A list.
521 * \param tail Value to use as the cdr of the last copied cell (enc_sym(SYM_NIL) for a normal copy).
522 * \return Copied list (ending in tail) or enc_sym(SYM_MERROR) if heap is full.
523 */
524lbm_value lbm_list_copy(int *m, lbm_value list, lbm_value tail);
525
526/** A destructive append of two lists
527 *
528 * \cycledanger
529 * \evalpaused
530 *
531 * \param list1 A list
532 * \param list2 A list
533 * \return list1 with list2 appended at the end.
534 */
535lbm_value lbm_list_append(lbm_value list1, lbm_value list2);
536
537/** Drop values from the head of a list.
538 * \param n Number of values to drop.
539 * \param ls List to drop values from.
540 * \return The list with the n first elements removed.
541 */
542lbm_value lbm_list_drop(unsigned int n, lbm_value ls);
543/** Index into a list.
544 * \param l List to index into.
545 * \param n Position to read out of the list.
546 * \return Value at position n of l or nil if out of bounds.
547 */
548lbm_value lbm_index_list(lbm_value l, int32_t n);
549
550// State and statistics
551/** Get a copy of the heap statistics structure.
552 *
553 * \param A pointer to an lbm_heap_state_t to populate
554 * with the current statistics.
555 */
556void lbm_get_heap_state(lbm_heap_state_t *);
557/** Get the maximum stack level of the GC stack
558 * \return maximum value the gc stack sp reached so far.
559 */
560lbm_uint lbm_get_gc_stack_max(void);
561/** Get the size of the GC stack.
562 * \return the size of the gc stack.
563 */
564lbm_uint lbm_get_gc_stack_size(void);
565// Garbage collection
566/** Increment the counter that is counting the number of times GC ran
567 *
568 * \internalonly
569 */
570void lbm_gc_state_inc(void);
571/** Set the freelist to NIL. Means that no memory will be available
572 * until after a garbage collection.
573 *
574 * \internalonly
575 */
576void lbm_nil_freelist(void);
577/** Mark all heap cells reachable from an environment.
578 *
579 * \internalonly
580 *
581 * \param environment.
582 */
583void lbm_gc_mark_env(lbm_value);
584/** Mark heap cells reachable from the lbm_value v.
585 *
586 * \internalonly
587 *
588 * \param root
589 */
590void lbm_gc_mark_phase(lbm_value root);
591/** Performs lbm_gc_mark_phase on all the values on the continuation stack.
592 * Unlike lbm_gc_mark_roots, the continuation stack also contains raw
593 * continuation markers that have the pointer bit set but are not real
594 * heap pointers, so each entry is range/type checked before being
595 * treated as a value to mark.
596 *
597 * \internalonly
598 *
599 * \param data Continuation stack data.
600 * \param n Number of elements on the continuation stack.
601 */
602void lbm_gc_mark_continuation_stack(lbm_uint *data, lbm_uint n);
603/** Performs lbm_gc_mark_phase on all the values in the roots array.
604 *
605 * \internalonly
606 *
607 * \param roots pointer to array of roots.
608 * \param num_roots size of array of roots.
609 */
610void lbm_gc_mark_roots(lbm_uint *roots, lbm_uint num_roots);
611/** Sweep up all non marked heap cells and place them on the free list.
612 *
613 * \internalonly
614 *
615 * \return 1
616 */
617int lbm_gc_sweep_phase(void);
618
619// Array functionality
620/** Allocate an bytearray in symbols and arrays memory (lispbm_memory.h)
621 * and create a heap cell that refers to this bytearray.
622 * \param res The resulting lbm_value is returned through this argument.
623 * \param size Array size in number of 32 bit words.
624 * \return 1 for success of 0 for failure.
625 */
626int lbm_heap_allocate_array(lbm_value *res, lbm_uint size);
627/** Allocate an array in symbols and arrays memory (lispbm_memory.h)
628 * and create a heap cell that refers to this array.
629 * \param res The resulting lbm_value is returned through this argument.
630 * \param size Array size in number of 32 bit words.
631 * \return 1 for success of 0 for failure.
632 */
633int lbm_heap_allocate_lisp_array(lbm_value *res, lbm_uint size);
634/** Convert a C array into an lbm array. If the C array is allocated in LBM MEMORY
635 * the lifetime of the array will be managed by GC.
636 * \param res lbm_value result pointer for storage of the result array.
637 * \param data C array.
638 * \param type The type tag to assign to the resulting LBM array.
639 * \param num_elt Number of elements in the array.
640 * \return 1 for success and 0 for failure.
641 */
642int lbm_lift_array(lbm_value *value, char *data, lbm_uint num_elt);
643/** Get the size of an array value.
644 * \param arr lbm_value array to get size of.
645 * \return -1 for failure or length of array.
646 */
647lbm_int lbm_heap_array_get_size(lbm_value arr);
648/** Get a pointer to the data of an array for read only purposes.
649 * \param arr lbm_value array to get pointer from.
650 * \return NULL or valid pointer.
651 */
652const uint8_t *lbm_heap_array_get_data_ro(lbm_value arr);
653
654/** Explicitly free an array.
655 * This function needs to be used with care and knowledge.
656 * \param arr Array value.
657 */
658int lbm_heap_explicit_free_array(lbm_value arr);
659
660int lbm_const_heap_init(const_heap_write_fun w_fun,
661 lbm_const_heap_t *heap,
662 lbm_uint *addr);
663
664lbm_flash_status lbm_allocate_const_cell(lbm_value *res);
665lbm_flash_status lbm_write_const_raw(lbm_uint *data, lbm_uint n, lbm_uint *res);
666lbm_flash_status lbm_allocate_const_raw(lbm_uint nwords, lbm_uint *res);
667lbm_flash_status write_const_cdr(lbm_value cell, lbm_value val);
668lbm_flash_status write_const_car(lbm_value cell, lbm_value val);
669lbm_flash_status lbm_const_write(lbm_uint *tgt, lbm_uint val);
670lbm_uint lbm_flash_memory_usage(void);
671
672/** Query the type information of a value.
673 *
674 * \param x Value to check the type of.
675 * \return The type information.
676 */
677static inline lbm_type lbm_type_of(lbm_value x) {
678 return (x & LBM_PTR_BIT0x00000001u) ? (x & LBM_PTR_TYPE_MASK0xFC000000u) : (x & LBM_VAL_TYPE_MASK0x0000000Cu);
679}
680
681/** Query the type information of a value.
682 * Discards information about constness of value.
683 *
684 * \param x Value to check the type of.
685 * \return The type information.
686 */
687static inline lbm_type lbm_type_of_functional(lbm_value x) {
688 return (x & LBM_PTR_BIT0x00000001u) ?
689 (x & (LBM_PTR_TO_CONSTANT_MASK~0x04000000u & LBM_PTR_TYPE_MASK0xFC000000u)) :
690 (x & LBM_VAL_TYPE_MASK0x0000000Cu);
691}
692
693static inline lbm_value lbm_enc_cons_ptr(lbm_uint x) {
694 return ((x << LBM_ADDRESS_SHIFT2) | LBM_TYPE_CONS0x10000000u | LBM_PTR_BIT0x00000001u);
695}
696
697static inline lbm_uint lbm_dec_ptr(lbm_value p) {
698 return ((LBM_PTR_VAL_MASK0x03FFFFFCu & p) >> LBM_ADDRESS_SHIFT2);
699}
700
701#define LBM_RAM_HEAP0 0
702#define LBM_CONST_HEAP1 1
703
704extern lbm_cons_t *lbm_heaps[2];
705
706static inline lbm_cons_t *lbm_dec_heap(lbm_value p) {
707 lbm_uint h = (p & LBM_PTR_TO_CONSTANT_BIT0x04000000u) >> LBM_PTR_TO_CONSTANT_SHIFT26;
708 return lbm_heaps[h];
709}
710
711static inline lbm_value lbm_set_ptr_type(lbm_value p, lbm_type t) {
712 return ((LBM_PTR_VAL_MASK0x03FFFFFCu & p) | t | LBM_PTR_BIT0x00000001u);
713}
714
715static inline lbm_value lbm_enc_sym(lbm_uint s) {
716 return (s << LBM_VAL_SHIFT4) | LBM_TYPE_SYMBOL0x00000000u;
717}
718
719static inline lbm_value lbm_enc_i(lbm_int x) {
720 return ((lbm_uint)x << LBM_VAL_SHIFT4) | LBM_TYPE_I0x00000008u;
721}
722
723static inline lbm_value lbm_enc_u(lbm_uint x) {
724 return (x << LBM_VAL_SHIFT4) | LBM_TYPE_U0x0000000Cu;
725}
726
727/** Set a known u32 value to a different u32 payload
728 * \param v Value to update.
729 * \param x new uint32_t payload.
730 * \return updated value.
731 */
732extern lbm_value lbm_set_u32(lbm_value v, uint32_t x);
733
734/** Set a known i32 value to a different u32 payload
735 * \param v Value to update.
736 * \param x new int32_t payload.
737 * \return updated value.
738 */
739extern lbm_value lbm_set_i32(lbm_value v, int32_t x);
740
741/** Set a known float value to a different u32 payload
742 * \param v Value to update.
743 * \param x new float payload.
744 * \return updated value.
745 */
746extern lbm_value lbm_set_float(lbm_value v, float x);
747
748
749/** Encode 32 bit integer into an lbm_value.
750 * \param x Value to encode.
751 * \return result encoded value.
752 */
753extern lbm_value lbm_enc_i32(int32_t x);
754
755/** Encode 32 bit unsigned integer into an lbm_value.
756 * \param x Value to encode.
757 * \return result encoded value.
758 */
759extern lbm_value lbm_enc_u32(uint32_t x);
760
761/** Encode a float into an lbm_value.
762 * \param x float value to encode.
763 * \return result encoded value.
764 */
765extern lbm_value lbm_enc_float(float x);
766
767/** Encode a 64 bit integer into an lbm_value.
768 * \param x 64 bit integer to encode.
769 * \return result encoded value.
770 */
771extern lbm_value lbm_enc_i64(int64_t x);
772
773/** Encode a 64 bit unsigned integer into an lbm_value.
774 * \param x 64 bit unsigned integer to encode.
775 * \return result encoded value.
776 */
777extern lbm_value lbm_enc_u64(uint64_t x);
778
779/** Encode a double into an lbm_value.
780 * \param x double to encode.
781 * \return result encoded value.
782 */
783extern lbm_value lbm_enc_double(double x);
784
785static inline lbm_value lbm_enc_char(uint8_t x) {
786 return ((lbm_uint)x << LBM_VAL_SHIFT4) | LBM_TYPE_CHAR0x00000004u;
787}
788
789static inline lbm_int lbm_dec_i(lbm_value x) {
790 return (lbm_int)x >> LBM_VAL_SHIFT4;
791}
792
793static inline lbm_uint lbm_dec_u(lbm_value x) {
794 return x >> LBM_VAL_SHIFT4;
795}
796
797static inline uint8_t lbm_dec_char(lbm_value x) {
798 return (uint8_t)(x >> LBM_VAL_SHIFT4);
799}
800
801static inline lbm_uint lbm_dec_sym(lbm_value x) {
802 return x >> LBM_VAL_SHIFT4;
803}
804
805/** Decode an lbm_value representing a float.
806 * \param x Value to decode.
807 * \return decoded float.
808 */
809extern float lbm_dec_float(lbm_value x);
810
811/** Decode an lbm_value representing a double.
812 * \param x Value to decode.
813 * \return decoded float.
814 */
815extern double lbm_dec_double(lbm_value x);
816
817
818static inline uint32_t lbm_dec_u32(lbm_value x) {
819#ifndef LBM64
820 return (uint32_t)lbm_car(x);
821#else
822 return (uint32_t)(x >> LBM_VAL_SHIFT4);
823#endif
824}
825
826/** Decode an lbm_value representing a 64 bit unsigned integer.
827 * \param x Value to decode.
828 * \return decoded uint64_t.
829 */
830extern uint64_t lbm_dec_u64(lbm_value x);
831
832static inline int32_t lbm_dec_i32(lbm_value x) {
833#ifndef LBM64
834 return (int32_t)lbm_car(x);
835#else
836 return (int32_t)(x >> LBM_VAL_SHIFT4);
837#endif
838}
839
840/** Decode an lbm_value representing a 64 bit integer.
841 * \param x Value to decode.
842 * \return decoded int64_t.
843 */
844extern int64_t lbm_dec_i64(lbm_value x);
845
846/**
847 * Check if a value is a heap pointer
848 * \param x Value to check
849 * \return true if x is a pointer to a heap cell, false otherwise.
850 */
851static inline bool_Bool lbm_is_ptr(lbm_value x) {
852 return (x & LBM_PTR_BIT0x00000001u);
853}
854
855static inline bool_Bool lbm_is_constant(lbm_value x) {
856 return ((x & LBM_PTR_BIT0x00000001u && x & LBM_PTR_TO_CONSTANT_BIT0x04000000u) ||
857 (!(x & LBM_PTR_BIT0x00000001u)));
858}
859
860/** Check if a ptr points to a constant.
861 * Must first check that x is a ptr type such as cons.
862 *
863 * \param x ptr value to check
864 * \return true if x points to a constant, false otherwise.
865 */
866static inline bool_Bool lbm_ptr_is_constant(lbm_value x) {
867 return (x & LBM_PTR_TO_CONSTANT_BIT0x04000000u);
868}
869
870/**
871 * Check if a value is a Read/Writeable cons cell
872 * \param x Value to check
873 * \return true if x is a Read/Writeable cons cell, false otherwise.
874 */
875static inline bool_Bool lbm_is_cons_rw(lbm_value x) {
876 return (x & (LBM_CONS_CONST_TYPE_MASK0xFF000000u | LBM_PTR_BIT0x00000001u)) == LBM_EXACT_CONS_MASK0x10000001u;
877 //return !((x & (LBM_CONS_CONST_TYPE_MASK | LBM_PTR_BIT)) ^ LBM_EXACT_CONS_MASK);
878 //return (lbm_type_of(x) == LBM_TYPE_CONS);
879}
880
881/**
882 * Check if a value is a Readable cons cell
883 * \param x Value to check
884 * \return true if x is a readable cons cell, false otherwise.
885 */
886static inline bool_Bool lbm_is_cons(lbm_value x) {
887 return (x & (LBM_CONS_TYPE_MASK0xF0000000u | LBM_PTR_BIT0x00000001u)) == LBM_EXACT_CONS_MASK0x10000001u;
888 //return !((x & (LBM_CONS_TYPE_MASK | LBM_PTR_BIT)) ^ LBM_EXACT_CONS_MASK);
889 //return lbm_is_ptr(x) && ((x & LBM_CONS_TYPE_MASK) == LBM_TYPE_CONS);
890}
891
892/**
893 * Check if a value is a symbol
894 *
895 * \param x Value to check
896 * \return true if x is a symbol, false otherwise.
897 */
898static inline bool_Bool lbm_is_symbol(lbm_value exp) {
899 return !(exp & LBM_LOW_RESERVED_BITS0x0000000Fu);
5
Assuming the condition is false
6
Returning zero, which participates in a condition later
900}
901
902/**
903 * Check if a value is a the nil symbol
904 *
905 * \param x Value to check
906 * \return true if x is a the nil symbol, false otherwise.
907 */
908static inline bool_Bool lbm_is_symbol_nil(lbm_value exp) {
909 return !exp;
910}
911
912/**
913 * Check if a value represents a number
914 *
915 * \param x Value to check.
916 * \return true is x represents a number and false otherwise.
917 */
918static inline bool_Bool lbm_is_number(lbm_value x) {
919 return
920 (x & LBM_PTR_BIT0x00000001u) ?
921 ((x & LBM_NUMBER_MASK0x08000000u) == LBM_NUMBER_MASK0x08000000u) :
922 (x & LBM_VAL_TYPE_MASK0x0000000Cu);
923}
924
925// Check if an array is valid (an invalid array has been freed by someone explicitly)
926/**
927 * Check if a value represents a valid array.
928 * An invalid array has been freed by someone explicitly.
929 *
930 * \param arr Value to check
931 * \return true is arr represents a valid array, false otherwise.
932 */
933static inline bool_Bool lbm_heap_array_valid(lbm_value arr) {
934 return !(lbm_is_symbol_nil(lbm_car(arr))); // this is an is_zero check similar to (a == NULL)
935}
936
937/** Check if value is an array that can be READ
938 * \param x Value to check.
939 * \return true if x represents a readable array and false otherwise.
940 */
941static inline bool_Bool lbm_is_array_r(lbm_value x) {
942 lbm_type t = lbm_type_of(x);
943 bool_Bool t_ok = ((t & LBM_PTR_TO_CONSTANT_MASK~0x04000000u) == LBM_TYPE_ARRAY0x80000000u);
944 bool_Bool t_valid = lbm_heap_array_valid(x);
945 return ( t_ok && t_valid ) ;
946}
947
948static inline bool_Bool lbm_is_array_rw(lbm_value x) {
949 return ((lbm_type_of(x) == LBM_TYPE_ARRAY0x80000000u) &&
950 !(x & LBM_PTR_TO_CONSTANT_BIT0x04000000u) &&
951 lbm_heap_array_valid(x));
952}
953
954static inline bool_Bool lbm_is_lisp_array_r(lbm_value x) {
955 lbm_type t = lbm_type_of(x);
956 return ((t & LBM_PTR_TO_CONSTANT_MASK~0x04000000u) == LBM_TYPE_LISPARRAY0xB0000000u);
957}
958
959static inline bool_Bool lbm_is_lisp_array_rw(lbm_value x) {
960 return( (lbm_type_of(x) == LBM_TYPE_LISPARRAY0xB0000000u) && !(x & LBM_PTR_TO_CONSTANT_BIT0x04000000u));
961}
962
963
964static inline bool_Bool lbm_is_channel(lbm_value x) {
965 return (lbm_type_of(x) == LBM_TYPE_CHANNEL0x90000000u &&
966 lbm_cdr(x) == ENC_SYM_CHANNEL_TYPE(((0x37) << 4) | 0x00000000u));
967}
968static inline bool_Bool lbm_is_char(lbm_value x) {
969 return (lbm_type_of(x) == LBM_TYPE_CHAR0x00000004u);
970}
971
972static inline bool_Bool lbm_is_special(lbm_value symrep) {
973 return (lbm_is_symbol(symrep) &&
974 (lbm_dec_sym(symrep) < SPECIAL_SYMBOLS_END0xFFFF));
975}
976
977static inline bool_Bool lbm_is_closure(lbm_value exp) {
978 return ((lbm_is_cons(exp)) && (lbm_car(exp) == ENC_SYM_CLOSURE(((0x10F) << 4) | 0x00000000u)));
979}
980
981static inline bool_Bool lbm_is_continuation(lbm_value exp) {
982 return ((lbm_type_of(exp) == LBM_TYPE_CONS0x10000000u) && (lbm_car(exp) == ENC_SYM_CONT(((0x10E) << 4) | 0x00000000u)));
983}
984
985static inline bool_Bool lbm_is_macro(lbm_value exp) {
986 return (lbm_is_cons(exp) && (lbm_car(exp) == ENC_SYM_MACRO(((0x10D) << 4) | 0x00000000u)));
987}
988
989static inline bool_Bool lbm_is_match_binder(lbm_value exp) {
990 return (lbm_is_cons(exp) && (lbm_car(exp) == ENC_SYM_MATCH_ANY(((0x41) << 4) | 0x00000000u)));
991}
992
993static inline bool_Bool lbm_is_comma_qualified_symbol(lbm_value exp) {
994 return (lbm_is_cons(exp) &&
995 (lbm_car(exp) == ENC_SYM_COMMA(((0x73) << 4) | 0x00000000u)) &&
996 (lbm_is_symbol(lbm_cadr(exp))));
997}
998
999static inline bool_Bool lbm_is_symbol_true(lbm_value exp) {
1000 return (exp == ENC_SYM_TRUE(((0x2) << 4) | 0x00000000u));
1001}
1002
1003static inline bool_Bool lbm_is_symbol_eval(lbm_value exp) {
1004 return (exp == ENC_SYM_EVAL(((0x30008) << 4) | 0x00000000u));
1005}
1006
1007static inline bool_Bool lbm_is_symbol_merror(lbm_value exp) {
1008 return (exp == ENC_SYM_MERROR(((0x23) << 4) | 0x00000000u));
1009}
1010
1011static inline bool_Bool lbm_is_list(lbm_value x) {
1012 return (lbm_is_cons(x) || lbm_is_symbol_nil(x));
1013}
1014
1015static inline bool_Bool lbm_is_list_rw(lbm_value x) {
1016 return (lbm_is_cons_rw(x) || lbm_is_symbol_nil(x));
1017}
1018
1019static inline bool_Bool lbm_is_quoted_list(lbm_value x) {
1020 return (lbm_is_cons(x) &&
1021 (lbm_car(x) == ENC_SYM_QUOTE(((0x100) << 4) | 0x00000000u)) &&
1022 lbm_is_cons(lbm_cdr(x)) &&
1023 lbm_is_cons(lbm_cadr(x)));
1024}
1025
1026#ifndef LBM64
1027#define ERROR_SYMBOL_MASK0xFFFFFF0F 0xFFFFFF0F
1028#else
1029#define ERROR_SYMBOL_MASK0xFFFFFF0F 0xFFFFFFFFFFFFF0FF
1030#endif
1031
1032// all error signaling symbols are in the range 0x20 - 0x2F
1033// encoded that is 0x200 - 0x2F0 on 32bit and
1034// 0x2000 - 0x2F00 on 64bit
1035static inline bool_Bool lbm_is_error(lbm_value v){
1036#ifndef LBM64
1037 return (v & ERROR_SYMBOL_MASK0xFFFFFF0F) == 0x200;
1038#else
1039 return (v & ERROR_SYMBOL_MASK0xFFFFFF0F) == 0x2000;
1040#endif
1041}
1042
1043// ref_cell: returns a reference to the cell addressed by bits 3 - 26
1044// Assumes user has checked that is_ptr was set
1045static inline lbm_cons_t* lbm_ref_cell(lbm_value addr) {
1046 return &lbm_dec_heap(addr)[lbm_dec_ptr(addr)];
1047 //return &lbm_heap_state.heap[lbm_dec_ptr(addr)];
1048}
1049
1050/** Update the value stored in the car field of a heap cell.
1051 *
1052 * \param c Value referring to a heap cell.
1053 * \param v Value to replace the car field with.
1054 * \return 1 on success and 0 if the c value does not refer to a heap cell.
1055 */
1056static inline int lbm_set_car(lbm_value c, lbm_value v) {
1057 int r = 0;
1058
1059 if (lbm_is_cons_rw(c)) {
1060 lbm_cons_t *cell = lbm_ref_cell(c);
1061 cell->car = v;
1062 r = 1;
1063 }
1064 return r;
1065}
1066
1067/** Update the value stored in the cdr field of a heap cell.
1068 *
1069 * \param c Value referring to a heap cell.
1070 * \param v Value to replace the cdr field with.
1071 * \return 1 on success and 0 if the c value does not refer to a heap cell.
1072 */
1073static inline int lbm_set_cdr(lbm_value c, lbm_value v) {
1074 int r = 0;
1075 if (lbm_is_cons_rw(c)){
1076 lbm_cons_t *cell = lbm_ref_cell(c);
1077 cell->cdr = v;
1078 r = 1;
1079 }
1080 return r;
1081}
1082
1083/** Update the value stored in the car and cdr fields of a heap cell.
1084 *
1085 * \param c Value referring to a heap cell.
1086 * \param car_val Value to replace the car field with.
1087 * \param cdr_val Value to replace the cdr field with.
1088 * \return 1 on success and 0 if the c value does not refer to a heap cell.
1089 */
1090static inline int lbm_set_car_and_cdr(lbm_value c, lbm_value car_val, lbm_value cdr_val) {
1091 int r = 0;
1092 if (lbm_is_cons_rw(c)) {
1093 lbm_cons_t *cell = lbm_ref_cell(c);
1094 cell->car = car_val;
1095 cell->cdr = cdr_val;
1096 r = 1;
1097 }
1098 return r;
1099}
1100
1101#define TRAV_FUN_SUBTREE_DONE0 0
1102#define TRAV_FUN_SUBTREE_CONTINUE1 1
1103#define TRAV_FUN_SUBTREE_PROCEED2 2
1104
1105typedef int (*trav_fun)(lbm_value, bool_Bool, void*);
1106
1107/**
1108 * \param f pointer to function to execute at each node in tree.
1109 * \param v Tree to traverses.
1110 * \param arg Extra argument to pass to f when applied.
1111 */
1112void lbm_ptr_rev_trav(trav_fun f, lbm_value v, void* arg);
1113
1114// lbm_uint a = lbm_heaps[0];
1115// lbm_uint b = lbm_heaps[1];
1116// lbm_uint i = (addr & LBM_PTR_TO_CONSTANT_BIT) >> LBM_PTR_TO_CONSTANT_SHIFT) - 1;
1117// lbm_uint h = (a & i) | (b & ~i);
1118
1119#ifdef LBM64
1120#define lbm_dec_as_intlbm_dec_as_i32 lbm_dec_as_i64
1121#define lbm_dec_as_uintlbm_dec_as_u32 lbm_dec_as_u64
1122#else
1123#define lbm_dec_as_intlbm_dec_as_i32 lbm_dec_as_i32
1124#define lbm_dec_as_uintlbm_dec_as_u32 lbm_dec_as_u32
1125#endif
1126
1127#ifdef __cplusplus
1128}
1129#endif
1130#endif