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