| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | Copyright 2018, 2020, 2022 - 2026 Joel Svensson svenssonjoel@yahoo.se | ||
| 3 | 2022 Benjamin Vedder | ||
| 4 | |||
| 5 | This program is free software: you can redistribute it and/or modify | ||
| 6 | it under the terms of the GNU General Public License as published by | ||
| 7 | the Free Software Foundation, either version 3 of the License, or | ||
| 8 | (at your option) any later version. | ||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | GNU General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License | ||
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | */ | ||
| 18 | |||
| 19 | #include <stdio.h> | ||
| 20 | #include <stdlib.h> | ||
| 21 | #include <stdint.h> | ||
| 22 | #include <stdarg.h> | ||
| 23 | #include <inttypes.h> | ||
| 24 | #include <lbm_memory.h> | ||
| 25 | #include <lbm_custom_type.h> | ||
| 26 | #include <lbm_defrag_mem.h> | ||
| 27 | #include <lbm_image.h> | ||
| 28 | |||
| 29 | #include "heap.h" | ||
| 30 | #include "symrepr.h" | ||
| 31 | #include "stack.h" | ||
| 32 | #include "lbm_channel.h" | ||
| 33 | #include "platform_mutex.h" | ||
| 34 | #include "eval_cps.h" | ||
| 35 | #ifdef VISUALIZE_HEAP | ||
| 36 | #include "heap_vis.h" | ||
| 37 | #endif | ||
| 38 | |||
| 39 | 176049206 | static inline lbm_value lbm_set_gc_mark(lbm_value x) { | |
| 40 | 176049206 | return x | LBM_GC_MARKED; | |
| 41 | } | ||
| 42 | 175453275 | static inline lbm_value lbm_clr_gc_mark(lbm_value x) { | |
| 43 | 175453275 | return x & ~LBM_GC_MASK; | |
| 44 | } | ||
| 45 | |||
| 46 | 3994601806 | static inline bool lbm_get_gc_mark(lbm_value x) { | |
| 47 | 3994601806 | return x & LBM_GC_MASK; | |
| 48 | } | ||
| 49 | |||
| 50 | 24276 | static inline void gc_mark(lbm_value c) { | |
| 51 | //c must be a cons cell. | ||
| 52 | 24276 | lbm_cons_t *cell = lbm_ref_cell(c); | |
| 53 | 24276 | cell->cdr = lbm_set_gc_mark(cell->cdr); | |
| 54 | 24276 | } | |
| 55 | |||
| 56 | 24611 | static inline bool gc_marked(lbm_value c) { | |
| 57 | 24611 | lbm_cons_t *cell = lbm_ref_cell(c); | |
| 58 | 24611 | 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 | 46974 | static inline bool lbm_get_gc_flag(lbm_value x) { | |
| 69 | 46974 | return x & LBM_GC_MARKED; | |
| 70 | } | ||
| 71 | |||
| 72 | 23487 | static inline lbm_value lbm_set_gc_flag(lbm_value x) { | |
| 73 | 23487 | return x | LBM_GC_MARKED; | |
| 74 | } | ||
| 75 | |||
| 76 | 23487 | static inline lbm_value lbm_clr_gc_flag(lbm_value x) { | |
| 77 | 23487 | 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 | 8510923 | lbm_value lbm_enc_i32(int32_t x) { | |
| 111 | #ifndef LBM64 | ||
| 112 | 5675892 | lbm_value i = lbm_cons((lbm_uint)x, ENC_SYM_RAW_I_TYPE); | |
| 113 |
2/2✓ Branch 1 taken 1234 times.
✓ Branch 2 taken 5674658 times.
|
5675892 | if (lbm_type_of(i) == LBM_TYPE_SYMBOL) return i; |
| 114 | 5674658 | return lbm_set_ptr_type(i, LBM_TYPE_I32); | |
| 115 | #else | ||
| 116 | 2835031 | return (((lbm_uint)x) << LBM_VAL_SHIFT) | LBM_TYPE_I32; | |
| 117 | #endif | ||
| 118 | } | ||
| 119 | |||
| 120 | 10205051 | lbm_value lbm_enc_u32(uint32_t x) { | |
| 121 | #ifndef LBM64 | ||
| 122 | 7366801 | lbm_value u = lbm_cons(x, ENC_SYM_RAW_U_TYPE); | |
| 123 |
2/2✓ Branch 1 taken 2176 times.
✓ Branch 2 taken 7364625 times.
|
7366801 | if (lbm_type_of(u) == LBM_TYPE_SYMBOL) return u; |
| 124 | 7364625 | return lbm_set_ptr_type(u, LBM_TYPE_U32); | |
| 125 | #else | ||
| 126 | 2838250 | 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 | 689407222 | lbm_value lbm_enc_float(float x) { | |
| 173 | #ifndef LBM64 | ||
| 174 | lbm_uint t; | ||
| 175 | 459788098 | memcpy(&t, &x, sizeof(lbm_float)); | |
| 176 | 459788098 | lbm_value f = lbm_cons(t, ENC_SYM_RAW_F_TYPE); | |
| 177 |
2/2✓ Branch 1 taken 294488 times.
✓ Branch 2 taken 459493610 times.
|
459788098 | if (lbm_type_of(f) == LBM_TYPE_SYMBOL) return f; |
| 178 | 459493610 | 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 | 16856771 | static lbm_value enc_64_on_32(uint8_t *source, lbm_uint type_qual, lbm_uint type) { | |
| 188 | 16856771 | lbm_value res = lbm_cons(ENC_SYM_NIL,ENC_SYM_NIL); | |
| 189 |
2/2✓ Branch 1 taken 16852399 times.
✓ Branch 2 taken 4372 times.
|
16856771 | if (lbm_type_of(res) != LBM_TYPE_SYMBOL) { |
| 190 | 16852399 | uint8_t* storage = lbm_malloc(sizeof(uint64_t)); | |
| 191 |
2/2✓ Branch 0 taken 16847143 times.
✓ Branch 1 taken 5256 times.
|
16852399 | if (storage) { |
| 192 | 16847143 | memcpy(storage,source, sizeof(uint64_t)); | |
| 193 | 16847143 | lbm_set_car_and_cdr(res, (lbm_uint)storage, type_qual); | |
| 194 | 16847143 | res = lbm_set_ptr_type(res, type); | |
| 195 | } else { | ||
| 196 | 5256 | res = ENC_SYM_MERROR; | |
| 197 | } | ||
| 198 | } | ||
| 199 | 16856771 | return res; | |
| 200 | } | ||
| 201 | #endif | ||
| 202 | |||
| 203 | 13472630 | lbm_value lbm_enc_i64(int64_t x) { | |
| 204 | #ifndef LBM64 | ||
| 205 | 8983246 | 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 1 taken 1232 times.
✓ Branch 2 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 | 10110284 | lbm_value lbm_enc_u64(uint64_t x) { | |
| 214 | #ifndef LBM64 | ||
| 215 | 6741536 | 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 1 taken 876 times.
✓ Branch 2 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 | 1697557 | lbm_value lbm_enc_double(double x) { | |
| 224 | #ifndef LBM64 | ||
| 225 | 1131989 | 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 1 taken 332 times.
✓ Branch 2 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 | 1000359518 | float lbm_dec_float(lbm_value x) { | |
| 240 | #ifndef LBM64 | ||
| 241 | float f_tmp; | ||
| 242 | 667125457 | lbm_uint tmp = lbm_car(x); | |
| 243 | 667125457 | memcpy(&f_tmp, &tmp, sizeof(float)); | |
| 244 | 667125457 | return f_tmp; | |
| 245 | #else | ||
| 246 | 333234061 | uint32_t tmp = (uint32_t)(x >> LBM_VAL_SHIFT); | |
| 247 | float f_tmp; | ||
| 248 | 333234061 | memcpy(&f_tmp, &tmp, sizeof(float)); | |
| 249 | 333234061 | return f_tmp; | |
| 250 | #endif | ||
| 251 | } | ||
| 252 | |||
| 253 | 1694821 | double lbm_dec_double(lbm_value x) { | |
| 254 | #ifndef LBM64 | ||
| 255 | 1130177 | double d = 0.0; | |
| 256 |
1/2✓ Branch 1 taken 1130177 times.
✗ Branch 2 not taken.
|
1130177 | if (lbm_is_ptr(x)) { |
| 257 | 1130177 | uint32_t *data = (uint32_t*)lbm_ref_cell(x)->car; | |
| 258 | 1130177 | memcpy(&d, data, sizeof(double)); | |
| 259 | } | ||
| 260 | 1130177 | 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 | 21035053 | uint64_t lbm_dec_u64(lbm_value x) { | |
| 270 | #ifndef LBM64 | ||
| 271 | 14025265 | uint64_t u = 0; | |
| 272 |
1/2✓ Branch 1 taken 14025265 times.
✗ Branch 2 not taken.
|
14025265 | if (lbm_is_ptr(x)) { |
| 273 | 14025265 | uint32_t *data = (uint32_t*)lbm_ref_cell(x)->car; | |
| 274 | 14025265 | memcpy(&u, data, 8); | |
| 275 | } | ||
| 276 | 14025265 | return u; | |
| 277 | #else | ||
| 278 | 7009788 | return (uint64_t)lbm_car(x); | |
| 279 | #endif | ||
| 280 | } | ||
| 281 | |||
| 282 | 27756148 | int64_t lbm_dec_i64(lbm_value x) { | |
| 283 | #ifndef LBM64 | ||
| 284 | 18506144 | int64_t i = 0; | |
| 285 |
1/2✓ Branch 1 taken 18506144 times.
✗ Branch 2 not taken.
|
18506144 | if (lbm_is_ptr(x)) { |
| 286 | 18506144 | uint32_t *data = (uint32_t*)lbm_ref_cell(x)->car; | |
| 287 | 18506144 | memcpy(&i, data, 8); | |
| 288 | } | ||
| 289 | 18506144 | return i; | |
| 290 | #else | ||
| 291 | 9250004 | return (int64_t)lbm_car(x); | |
| 292 | #endif | ||
| 293 | } | ||
| 294 | |||
| 295 | 1459681 | char *lbm_dec_str(lbm_value val) { | |
| 296 | 1459681 | char *res = 0; | |
| 297 |
2/2✓ Branch 1 taken 1459253 times.
✓ Branch 2 taken 428 times.
|
1459681 | if (lbm_is_array_r(val)) { |
| 298 | 1459253 | lbm_array_header_t *array = (lbm_array_header_t *)lbm_car(val); | |
| 299 |
1/2✓ Branch 0 taken 1459253 times.
✗ Branch 1 not taken.
|
1459253 | if (array) { |
| 300 | 1459253 | res = (char *)array->data; | |
| 301 | } | ||
| 302 | } | ||
| 303 | 1459681 | return res; | |
| 304 | } | ||
| 305 | |||
| 306 | 1803365 | lbm_array_header_t *lbm_dec_array_r(lbm_value val) { | |
| 307 | 1803365 | lbm_array_header_t *array = NULL; | |
| 308 |
2/2✓ Branch 1 taken 1799290 times.
✓ Branch 2 taken 4075 times.
|
1803365 | if (lbm_is_array_r(val)) { |
| 309 | 1799290 | array = (lbm_array_header_t *)lbm_car(val); | |
| 310 | } | ||
| 311 | 1803365 | return array; | |
| 312 | } | ||
| 313 | |||
| 314 | 178700 | lbm_array_header_t *lbm_dec_array_rw(lbm_value val) { | |
| 315 | 178700 | lbm_array_header_t *array = NULL; | |
| 316 |
2/2✓ Branch 1 taken 178446 times.
✓ Branch 2 taken 254 times.
|
178700 | if (lbm_is_array_rw(val)) { |
| 317 | 178446 | array = (lbm_array_header_t *)lbm_car(val); | |
| 318 | } | ||
| 319 | 178700 | return array; | |
| 320 | } | ||
| 321 | |||
| 322 | 36429485 | lbm_char_channel_t *lbm_dec_channel(lbm_value val) { | |
| 323 | 36429485 | lbm_char_channel_t *res = NULL; | |
| 324 | |||
| 325 |
1/2✓ Branch 1 taken 36429485 times.
✗ Branch 2 not taken.
|
36429485 | if (lbm_type_of(val) == LBM_TYPE_CHANNEL) { |
| 326 | 36429485 | res = (lbm_char_channel_t *)lbm_car(val); | |
| 327 | } | ||
| 328 | 36429485 | return res; | |
| 329 | } | ||
| 330 | |||
| 331 | 3374 | lbm_uint lbm_dec_custom(lbm_value val) { | |
| 332 | 3374 | lbm_uint res = 0; | |
| 333 |
1/2✓ Branch 1 taken 3374 times.
✗ Branch 2 not taken.
|
3374 | if (lbm_type_of(val) == LBM_TYPE_CUSTOM) { |
| 334 | 3374 | res = (lbm_uint)lbm_car(val); | |
| 335 | } | ||
| 336 | 3374 | return res; | |
| 337 | } | ||
| 338 | |||
| 339 | 182824 | uint8_t lbm_dec_as_char(lbm_value a) { | |
| 340 | 182824 | uint8_t r = 0; | |
| 341 |
10/10✓ Branch 1 taken 182113 times.
✓ Branch 2 taken 108 times.
✓ Branch 3 taken 86 times.
✓ Branch 4 taken 85 times.
✓ Branch 5 taken 85 times.
✓ Branch 6 taken 85 times.
✓ Branch 7 taken 85 times.
✓ Branch 8 taken 85 times.
✓ Branch 9 taken 85 times.
✓ Branch 10 taken 7 times.
|
182824 | switch (lbm_type_of_functional(a)) { |
| 342 | 182113 | case LBM_TYPE_CHAR: | |
| 343 | 182113 | 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 | 182824 | return r; | |
| 362 | } | ||
| 363 | |||
| 364 | 19147687 | uint32_t lbm_dec_as_u32(lbm_value a) { | |
| 365 | 19147687 | uint32_t r = 0; | |
| 366 |
9/9✓ Branch 1 taken 1405424 times.
✓ Branch 2 taken 4177043 times.
✓ Branch 3 taken 297317 times.
✓ Branch 4 taken 13267385 times.
✓ Branch 5 taken 88 times.
✓ Branch 6 taken 85 times.
✓ Branch 7 taken 253 times.
✓ Branch 8 taken 85 times.
✓ Branch 9 taken 7 times.
|
19147687 | switch (lbm_type_of_functional(a)) { |
| 367 | 1405424 | case LBM_TYPE_CHAR: | |
| 368 | 1405424 | r = (uint32_t)lbm_dec_char(a); break; | |
| 369 | 4177043 | case LBM_TYPE_I: | |
| 370 | 4177043 | r = (uint32_t)lbm_dec_i(a); break; | |
| 371 | 297317 | case LBM_TYPE_U: | |
| 372 | 297317 | r = (uint32_t)lbm_dec_u(a); break; | |
| 373 | 13267385 | case LBM_TYPE_I32: /* fall through */ | |
| 374 | case LBM_TYPE_U32: | ||
| 375 | 13267385 | 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 | 19147687 | return r; | |
| 386 | } | ||
| 387 | |||
| 388 | 505540662 | int32_t lbm_dec_as_i32(lbm_value a) { | |
| 389 | 505540662 | int32_t r = 0; | |
| 390 |
10/10✓ Branch 1 taken 13851638 times.
✓ Branch 2 taken 480647515 times.
✓ Branch 3 taken 24422 times.
✓ Branch 4 taken 11014313 times.
✓ Branch 5 taken 87 times.
✓ Branch 6 taken 85 times.
✓ Branch 7 taken 169 times.
✓ Branch 8 taken 169 times.
✓ Branch 9 taken 85 times.
✓ Branch 10 taken 2179 times.
|
505540662 | switch (lbm_type_of_functional(a)) { |
| 391 | 13851638 | case LBM_TYPE_CHAR: | |
| 392 | 13851638 | r = (int32_t)lbm_dec_char(a); break; | |
| 393 | 480647515 | case LBM_TYPE_I: | |
| 394 | 480647515 | 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 | 11014313 | case LBM_TYPE_I32: | |
| 398 | 11014313 | 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 | 505540664 | return r; | |
| 411 | } | ||
| 412 | |||
| 413 | 253669400 | int64_t lbm_dec_as_i64(lbm_value a) { | |
| 414 | 253669400 | int64_t r = 0; | |
| 415 |
10/10✓ Branch 1 taken 7813529 times.
✓ Branch 2 taken 231561280 times.
✓ Branch 3 taken 506 times.
✓ Branch 4 taken 841 times.
✓ Branch 5 taken 1793 times.
✓ Branch 6 taken 181 times.
✓ Branch 7 taken 14290749 times.
✓ Branch 8 taken 337 times.
✓ Branch 9 taken 169 times.
✓ Branch 10 taken 15 times.
|
253669400 | switch (lbm_type_of_functional(a)) { |
| 416 | 7813529 | case LBM_TYPE_CHAR: | |
| 417 | 7813529 | r = (int64_t)lbm_dec_char(a); break; | |
| 418 | 231561280 | case LBM_TYPE_I: | |
| 419 | 231561280 | 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 | 14290749 | case LBM_TYPE_I64: | |
| 429 | 14290749 | 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 | 253669400 | return r; | |
| 436 | } | ||
| 437 | |||
| 438 | 13559144 | uint64_t lbm_dec_as_u64(lbm_value a) { | |
| 439 | 13559144 | uint64_t r = 0; | |
| 440 |
10/10✓ Branch 1 taken 1687425 times.
✓ Branch 2 taken 919297 times.
✓ Branch 3 taken 19410 times.
✓ Branch 4 taken 673 times.
✓ Branch 5 taken 673 times.
✓ Branch 6 taken 169 times.
✓ Branch 7 taken 505 times.
✓ Branch 8 taken 10930816 times.
✓ Branch 9 taken 169 times.
✓ Branch 10 taken 7 times.
|
13559144 | switch (lbm_type_of_functional(a)) { |
| 441 | 1687425 | case LBM_TYPE_CHAR: | |
| 442 | 1687425 | r = (uint64_t)lbm_dec_char(a); break; | |
| 443 | 919297 | case LBM_TYPE_I: | |
| 444 | 919297 | 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 | 10930816 | case LBM_TYPE_U64: | |
| 456 | 10930816 | 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 | 13559144 | return r; | |
| 461 | } | ||
| 462 | |||
| 463 | 1126211681 | float lbm_dec_as_float(lbm_value a) { | |
| 464 | 1126211681 | float r = 0; | |
| 465 |
10/10✓ Branch 1 taken 311097780 times.
✓ Branch 2 taken 504152184 times.
✓ Branch 3 taken 478 times.
✓ Branch 4 taken 421 times.
✓ Branch 5 taken 534 times.
✓ Branch 6 taken 310959246 times.
✓ Branch 7 taken 421 times.
✓ Branch 8 taken 421 times.
✓ Branch 9 taken 85 times.
✓ Branch 10 taken 111 times.
|
1126211681 | switch (lbm_type_of_functional(a)) { |
| 466 | 311097780 | case LBM_TYPE_CHAR: | |
| 467 | 311097780 | r = (float)lbm_dec_char(a); break; | |
| 468 | 504152184 | case LBM_TYPE_I: | |
| 469 | 504152184 | 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 | 310959246 | case LBM_TYPE_FLOAT: | |
| 477 | 310959246 | 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 | 1126211681 | return r; | |
| 486 | } | ||
| 487 | |||
| 488 | 1692313 | double lbm_dec_as_double(lbm_value a) { | |
| 489 | 1692313 | double r = 0; | |
| 490 |
10/10✓ Branch 1 taken 843314 times.
✓ Branch 2 taken 842439 times.
✓ Branch 3 taken 422 times.
✓ Branch 4 taken 421 times.
✓ Branch 5 taken 421 times.
✓ Branch 6 taken 1115 times.
✓ Branch 7 taken 421 times.
✓ Branch 8 taken 421 times.
✓ Branch 9 taken 3332 times.
✓ Branch 10 taken 7 times.
|
1692313 | switch (lbm_type_of_functional(a)) { |
| 491 | 843314 | case LBM_TYPE_CHAR: | |
| 492 | 843314 | r = (double)lbm_dec_char(a); break; | |
| 493 | 842439 | case LBM_TYPE_I: | |
| 494 | 842439 | 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 | 3332 | case LBM_TYPE_DOUBLE: | |
| 508 | 3332 | r = (double)lbm_dec_double(a); break; | |
| 509 | } | ||
| 510 | 1692313 | return r; | |
| 511 | } | ||
| 512 | |||
| 513 | /****************************************************/ | ||
| 514 | /* HEAP MANAGEMENT */ | ||
| 515 | |||
| 516 | 66575 | static bool generate_freelist(size_t num_cells) { | |
| 517 | 66575 | size_t i = 0; | |
| 518 | |||
| 519 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66575 times.
|
66575 | if (!lbm_heap_state.heap) return false; |
| 520 | |||
| 521 | 66575 | 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 615181297 times.
✓ Branch 1 taken 66575 times.
|
615247872 | for (i = 1; i < num_cells; i ++) { |
| 527 | 615181297 | t = lbm_ref_cell(lbm_enc_cons_ptr(i-1)); | |
| 528 | 615181297 | t->car = ENC_SYM_RECOVERED; // all cars in free list are "RECOVERED" | |
| 529 | 615181297 | t->cdr = lbm_enc_cons_ptr(i); | |
| 530 | } | ||
| 531 | |||
| 532 | // Replace the incorrect pointer at the last cell. | ||
| 533 | 66575 | t = lbm_ref_cell(lbm_enc_cons_ptr(num_cells-1)); | |
| 534 | 66575 | t->car = ENC_SYM_RECOVERED; | |
| 535 | 66575 | t->cdr = ENC_SYM_NIL; | |
| 536 | |||
| 537 | 66575 | return true; | |
| 538 | } | ||
| 539 | |||
| 540 | 1606427 | void lbm_nil_freelist(void) { | |
| 541 | 1606427 | lbm_heap_state.freelist = ENC_SYM_NIL; | |
| 542 | 1606427 | lbm_heap_state.num_free = 0; | |
| 543 | 1606427 | } | |
| 544 | |||
| 545 | 66575 | static void heap_init_state(lbm_cons_t *addr, lbm_uint num_cells, | |
| 546 | lbm_uint* gc_stack_storage, lbm_uint gc_stack_size) { | ||
| 547 | 66575 | lbm_heap_state.heap = addr; | |
| 548 | 66575 | lbm_heap_state.heap_bytes = (unsigned int)(num_cells * sizeof(lbm_cons_t)); | |
| 549 | 66575 | lbm_heap_state.heap_size = num_cells; | |
| 550 | |||
| 551 | 66575 | lbm_stack_create(&lbm_heap_state.gc_stack, gc_stack_storage, gc_stack_size); | |
| 552 | |||
| 553 | 66575 | lbm_heap_state.num_free = num_cells; | |
| 554 | 66575 | lbm_heap_state.num_alloc_arrays = 0; | |
| 555 | 66575 | lbm_heap_state.gc_num = 0; | |
| 556 | 66575 | lbm_heap_state.gc_marked = 0; | |
| 557 | 66575 | lbm_heap_state.gc_recovered = 0; | |
| 558 | 66575 | lbm_heap_state.gc_recovered_arrays = 0; | |
| 559 | 66575 | lbm_heap_state.gc_least_free = num_cells; | |
| 560 | 66575 | lbm_heap_state.gc_last_free = num_cells; | |
| 561 | 66575 | } | |
| 562 | |||
| 563 | 1606426 | void lbm_heap_new_freelist_length(void) { | |
| 564 | 1606426 | lbm_heap_state.gc_last_free = lbm_heap_state.num_free; | |
| 565 |
2/2✓ Branch 0 taken 12247 times.
✓ Branch 1 taken 1594179 times.
|
1606426 | if (lbm_heap_state.num_free < lbm_heap_state.gc_least_free) |
| 566 | 12247 | lbm_heap_state.gc_least_free = lbm_heap_state.num_free; | |
| 567 | 1606426 | } | |
| 568 | |||
| 569 | 66575 | bool lbm_heap_init(lbm_cons_t *addr, lbm_uint num_cells, | |
| 570 | lbm_uint gc_stack_size) { | ||
| 571 | |||
| 572 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66575 times.
|
66575 | if (((uintptr_t)addr % 8) != 0) return false; |
| 573 | |||
| 574 | 66575 | memset(addr,0, sizeof(lbm_cons_t) * num_cells); | |
| 575 | |||
| 576 | 66575 | lbm_uint *gc_stack_storage = (lbm_uint*)lbm_malloc(gc_stack_size * sizeof(lbm_uint)); | |
| 577 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66575 times.
|
66575 | if (gc_stack_storage == NULL) return 0; |
| 578 | |||
| 579 | 66575 | heap_init_state(addr, num_cells, | |
| 580 | gc_stack_storage, gc_stack_size); | ||
| 581 | |||
| 582 | 66575 | lbm_heaps[LBM_RAM_HEAP] = addr; | |
| 583 | |||
| 584 | 66575 | return generate_freelist(num_cells); | |
| 585 | } | ||
| 586 | |||
| 587 | |||
| 588 | 1414436116 | lbm_value lbm_heap_allocate_cell(lbm_type ptr_type, lbm_value car, lbm_value cdr) { | |
| 589 | lbm_value r; | ||
| 590 | 1414436116 | lbm_value cell = lbm_heap_state.freelist; | |
| 591 |
2/2✓ Branch 0 taken 1414116964 times.
✓ Branch 1 taken 319152 times.
|
1414436116 | if (cell) { |
| 592 | 1414116964 | lbm_uint heap_ix = lbm_dec_ptr(cell); | |
| 593 | 1414116964 | lbm_heap_state.freelist = lbm_heap_state.heap[heap_ix].cdr; | |
| 594 | 1414116964 | lbm_heap_state.num_free--; | |
| 595 | 1414116964 | lbm_heap_state.heap[heap_ix].car = car; | |
| 596 | 1414116964 | lbm_heap_state.heap[heap_ix].cdr = cdr; | |
| 597 | 1414116964 | r = lbm_set_ptr_type(cell, ptr_type); | |
| 598 | } else { | ||
| 599 | 319152 | r = ENC_SYM_MERROR; | |
| 600 | } | ||
| 601 | 1414436116 | return r; | |
| 602 | } | ||
| 603 | |||
| 604 | 3768758 | lbm_value lbm_heap_allocate_list(lbm_uint n) { | |
| 605 |
2/2✓ Branch 0 taken 10032 times.
✓ Branch 1 taken 3758726 times.
|
3768758 | if (n == 0) return ENC_SYM_NIL; |
| 606 |
2/2✓ Branch 1 taken 4656 times.
✓ Branch 2 taken 3754070 times.
|
3758726 | if (lbm_heap_num_free() < n) return ENC_SYM_MERROR; |
| 607 | // Here the freelist is guaranteed to be a cons_cell. | ||
| 608 | |||
| 609 | 3754070 | lbm_value curr = lbm_heap_state.freelist; | |
| 610 | 3754070 | lbm_value res = curr; | |
| 611 | |||
| 612 | 3754070 | lbm_cons_t *c_cell = NULL; | |
| 613 | 3754070 | lbm_uint count = 0; | |
| 614 | do { | ||
| 615 | 19397204 | c_cell = lbm_ref_cell(curr); | |
| 616 | 19397204 | c_cell->car = ENC_SYM_NIL; | |
| 617 | 19397204 | curr = c_cell->cdr; | |
| 618 | 19397204 | count ++; | |
| 619 |
2/2✓ Branch 0 taken 15643134 times.
✓ Branch 1 taken 3754070 times.
|
19397204 | } while (count < n); |
| 620 | 3754070 | lbm_heap_state.freelist = curr; | |
| 621 | 3754070 | c_cell->cdr = ENC_SYM_NIL; | |
| 622 | 3754070 | lbm_heap_state.num_free-=count; | |
| 623 | 3754070 | return res; | |
| 624 | } | ||
| 625 | |||
| 626 | 65904491 | lbm_value lbm_heap_allocate_list_init_va(unsigned int n, va_list valist) { | |
| 627 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65904491 times.
|
65904491 | if (n == 0) return ENC_SYM_NIL; |
| 628 |
2/2✓ Branch 1 taken 99564 times.
✓ Branch 2 taken 65804927 times.
|
65904491 | if (lbm_heap_num_free() < n) return ENC_SYM_MERROR; |
| 629 | |||
| 630 | 65804927 | lbm_value curr = lbm_heap_state.freelist; | |
| 631 | 65804927 | lbm_value res = curr; | |
| 632 | |||
| 633 | 65804927 | lbm_cons_t *c_cell = NULL; | |
| 634 | 65804927 | unsigned int count = 0; | |
| 635 | do { | ||
| 636 | 132461324 | c_cell = lbm_ref_cell(curr); | |
| 637 | 132461324 | c_cell->car = va_arg(valist, lbm_value); | |
| 638 | 132461324 | curr = c_cell->cdr; | |
| 639 | 132461324 | count ++; | |
| 640 |
2/2✓ Branch 0 taken 66656397 times.
✓ Branch 1 taken 65804927 times.
|
132461324 | } while (count < n); |
| 641 | 65804927 | lbm_heap_state.freelist = curr; | |
| 642 | 65804927 | c_cell->cdr = ENC_SYM_NIL; | |
| 643 | 65804927 | lbm_heap_state.num_free-=count; | |
| 644 | 65804927 | return res; | |
| 645 | } | ||
| 646 | |||
| 647 | 65904491 | lbm_value lbm_heap_allocate_list_init(unsigned int n, ...) { | |
| 648 | va_list valist; | ||
| 649 | 65904491 | va_start(valist, n); | |
| 650 | 65904491 | lbm_value r = lbm_heap_allocate_list_init_va(n, valist); | |
| 651 | 65904491 | va_end(valist); | |
| 652 | 65904491 | return r; | |
| 653 | } | ||
| 654 | |||
| 655 | 5 | lbm_uint lbm_heap_num_allocated(void) { | |
| 656 | 5 | return lbm_heap_state.heap_size - lbm_heap_state.num_free; | |
| 657 | } | ||
| 658 | 5 | lbm_uint lbm_heap_size(void) { | |
| 659 | 5 | return lbm_heap_state.heap_size; | |
| 660 | } | ||
| 661 | |||
| 662 | 4 | lbm_uint lbm_heap_size_bytes(void) { | |
| 663 | 4 | return lbm_heap_state.heap_bytes; | |
| 664 | } | ||
| 665 | |||
| 666 | 765 | void lbm_get_heap_state(lbm_heap_state_t *res) { | |
| 667 | 765 | *res = lbm_heap_state; | |
| 668 | 765 | } | |
| 669 | |||
| 670 | 6 | lbm_uint lbm_get_gc_stack_max(void) { | |
| 671 | 6 | return lbm_get_max_stack(&lbm_heap_state.gc_stack); | |
| 672 | } | ||
| 673 | |||
| 674 | 5 | lbm_uint lbm_get_gc_stack_size(void) { | |
| 675 | 5 | return lbm_heap_state.gc_stack.size; | |
| 676 | } | ||
| 677 | |||
| 678 | 284016 | static inline void value_assign(lbm_value *a, lbm_value b) { | |
| 679 | 284016 | lbm_value a_old = *a & LBM_GC_MASK; | |
| 680 | 284016 | *a = a_old | (b & ~LBM_GC_MASK); | |
| 681 | 284016 | } | |
| 682 | |||
| 683 | #ifdef LBM_USE_GC_PTR_REV | ||
| 684 | /* ************************************************************ | ||
| 685 | Deutch-Schorr-Waite (DSW) pointer reversal GC for 2-ptr cells | ||
| 686 | with a hack-solution for the lisp-array case (n-ptr cells). | ||
| 687 | |||
| 688 | DSW visits each branch node 3 times compared to 2 times for | ||
| 689 | the stack based recursive mark. | ||
| 690 | Where the stack based recursive mark performs a stack push/pop, | ||
| 691 | DSW rearranges the, current, prev, next and a ptr field on | ||
| 692 | the heap. | ||
| 693 | |||
| 694 | DSW changes the structure of the heap and it introduces an | ||
| 695 | invalid pointer (LBM_PTR_NULL) temporarily during marking. | ||
| 696 | Since the heap will be "messed up" while marking, a mutex | ||
| 697 | is introuded to keep other processes out of the heap while | ||
| 698 | marking. | ||
| 699 | */ | ||
| 700 | |||
| 701 | static int do_nothing(lbm_value v, bool shared, void *arg) { | ||
| 702 | (void) v; | ||
| 703 | (void) shared; | ||
| 704 | (void) arg; | ||
| 705 | return TRAV_FUN_SUBTREE_CONTINUE; | ||
| 706 | } | ||
| 707 | |||
| 708 | void lbm_gc_mark_phase(lbm_value root) { | ||
| 709 | lbm_mutex_lock(&lbm_const_heap_mutex); | ||
| 710 | lbm_ptr_rev_trav(do_nothing, root, NULL); | ||
| 711 | lbm_mutex_unlock(&lbm_const_heap_mutex); | ||
| 712 | } | ||
| 713 | |||
| 714 | #else | ||
| 715 | // //////////////////////////////////////////////////////////// | ||
| 716 | // Check if a value is currently on the stack | ||
| 717 | |||
| 718 | 66822 | bool active_ptr(lbm_value p) { | |
| 719 | 66822 | lbm_stack_t *s = &lbm_heap_state.gc_stack; | |
| 720 | 66822 | bool r = false; | |
| 721 |
2/2✓ Branch 0 taken 330 times.
✓ Branch 1 taken 66822 times.
|
67152 | for (lbm_uint i = 0; i < s->sp; i ++) { |
| 722 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 330 times.
|
330 | if (p == s->data[i]) { |
| 723 | ✗ | r = true; | |
| 724 | ✗ | break; | |
| 725 | } | ||
| 726 | } | ||
| 727 | 66822 | return r; | |
| 728 | } | ||
| 729 | |||
| 730 | // //////////////////////////////////////////////////////////// | ||
| 731 | // Explicit stack "recursive" mark phase | ||
| 732 | |||
| 733 | // Trees are marked in a left subtree before rigth subtree, car first then cdr, | ||
| 734 | // way to favor lisp lists. This means that stack will grow slowly when | ||
| 735 | // marking right-leaning (cdr-recursive) data-structures while left-leaning | ||
| 736 | // (car-recursive) structures uses a lot of stack. | ||
| 737 | |||
| 738 | // Lisp arrays contain an extra book-keeping field to keep track | ||
| 739 | // of how far into the array the marking process has gone. | ||
| 740 | |||
| 741 | // TODO: DSW should be used as a last-resort if the GC stack is exhausted. | ||
| 742 | // If we use DSW as last-resort can we get away with a way smaller | ||
| 743 | // GC stack and unchanged performance (on sensible programs)? | ||
| 744 | |||
| 745 | 25381288 | void lbm_gc_mark_phase(lbm_value root) { | |
| 746 | lbm_value t_ptr; | ||
| 747 | 25381288 | lbm_stack_t *s = &lbm_heap_state.gc_stack; | |
| 748 | 25381288 | s->data[s->sp++] = root; | |
| 749 | |||
| 750 |
2/2✓ Branch 1 taken 123453199 times.
✓ Branch 2 taken 25381287 times.
|
148834486 | while (!lbm_stack_is_empty(s)) { |
| 751 | lbm_value curr; | ||
| 752 | 123453199 | lbm_pop(s, &curr); | |
| 753 | |||
| 754 | 277207119 | mark_shortcut: | |
| 755 | |||
| 756 |
2/2✓ Branch 1 taken 168479684 times.
✓ Branch 2 taken 108727435 times.
|
277207119 | if (!lbm_is_ptr(curr) || |
| 757 |
2/2✓ Branch 0 taken 319 times.
✓ Branch 1 taken 168479365 times.
|
168479684 | (curr & LBM_PTR_TO_CONSTANT_BIT)) { |
| 758 | 119149055 | continue; | |
| 759 | } | ||
| 760 | |||
| 761 | 168479365 | lbm_cons_t *cell = &lbm_heap_state.heap[lbm_dec_ptr(curr)]; | |
| 762 | |||
| 763 |
2/2✓ Branch 1 taken 10357205 times.
✓ Branch 2 taken 158122160 times.
|
168479365 | if (lbm_get_gc_mark(cell->cdr)) { |
| 764 | 10357205 | continue; | |
| 765 | } | ||
| 766 | |||
| 767 | 158122160 | t_ptr = lbm_type_of(curr); | |
| 768 | |||
| 769 | // An array is marked in O(N) time using an additional 32bit | ||
| 770 | // value per array that keeps track of how far into the array GC | ||
| 771 | // has progressed. | ||
| 772 |
2/2✓ Branch 0 taken 66822 times.
✓ Branch 1 taken 158055338 times.
|
158122160 | if (t_ptr == LBM_TYPE_LISPARRAY) { |
| 773 | // O(stack_size) cost when seeing array. This protects | ||
| 774 | // against gc recursing into arrays via cycles. | ||
| 775 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 66822 times.
|
66822 | if (active_ptr(curr)) { |
| 776 | ✗ | continue; // Already in process of marking this array, abort cycle! | |
| 777 | } | ||
| 778 | 66822 | lbm_array_header_extended_t *arr = (lbm_array_header_extended_t*)cell->car; | |
| 779 | 66822 | lbm_value *arrdata = (lbm_value *)arr->data; | |
| 780 | 66822 | lbm_push(s, curr); // put array back as bookkeeping. | |
| 781 | // Example A: Array with 10 elements | ||
| 782 | // A: assume arr->index == 9 | ||
| 783 | // Example B: Array with 0 elements | ||
| 784 | // B: assume arr->index == 0 | ||
| 785 |
2/2✓ Branch 0 taken 66220 times.
✓ Branch 1 taken 602 times.
|
66822 | if (arr->index < ((arr->size/(sizeof(lbm_value))))) { // A: 9 < 10 |
| 786 | // A: True path | ||
| 787 | // B: 0 < 0 | ||
| 788 | // B: False path | ||
| 789 |
3/4✓ Branch 1 taken 3188 times.
✓ Branch 2 taken 63032 times.
✓ Branch 3 taken 3188 times.
✗ Branch 4 not taken.
|
66220 | if (lbm_is_ptr(arrdata[arr->index]) && ((arrdata[arr->index] & LBM_PTR_TO_CONSTANT_BIT) == 0) && |
| 790 |
2/2✓ Branch 0 taken 2726 times.
✓ Branch 1 taken 462 times.
|
3188 | !((arrdata[arr->index] & LBM_CONTINUATION_INTERNAL) == LBM_CONTINUATION_INTERNAL)) { |
| 791 | |||
| 792 | 2726 | lbm_cons_t *elt = &lbm_heap_state.heap[lbm_dec_ptr(arrdata[arr->index])]; | |
| 793 |
1/2✓ Branch 1 taken 2726 times.
✗ Branch 2 not taken.
|
2726 | if (!lbm_get_gc_mark(elt->cdr)) { |
| 794 | 2726 | curr = arrdata[arr->index]; | |
| 795 | 2726 | arr->index++; | |
| 796 | 2726 | goto mark_shortcut; | |
| 797 | } | ||
| 798 | } | ||
| 799 | // We need one more round of the loop if there is at least one element left in the array. | ||
| 800 | // If we skip this check things should work the same, but there will be one more recurse | ||
| 801 | // before we discover in the earlier conditional that we have reached the end of the array. | ||
| 802 |
2/2✓ Branch 0 taken 57064 times.
✓ Branch 1 taken 6430 times.
|
63494 | if (arr->index < ((arr->size/(sizeof(lbm_value))) - 1)) { // A: 9 < 9 |
| 803 | // A: False path | ||
| 804 | 57064 | arr->index++; | |
| 805 | 57064 | continue; | |
| 806 | } | ||
| 807 | } | ||
| 808 | 7032 | arr->index = 0; | |
| 809 | 7032 | lbm_stack_drop(s,1); // Drop the bookkeeping element. | |
| 810 | // elem_dropped == cell | ||
| 811 | // Cell is the array reference. so this marks the array reference itself | ||
| 812 | // after having marked all the elements. | ||
| 813 | 7032 | cell->cdr = lbm_set_gc_mark(cell->cdr); | |
| 814 | 7032 | lbm_heap_state.gc_marked ++; | |
| 815 | 7032 | continue; | |
| 816 |
2/2✓ Branch 0 taken 815425 times.
✓ Branch 1 taken 157239913 times.
|
158055338 | } else if (t_ptr == LBM_TYPE_CHANNEL) { |
| 817 | 815425 | cell->cdr = lbm_set_gc_mark(cell->cdr); | |
| 818 | 815425 | lbm_heap_state.gc_marked ++; | |
| 819 | // TODO: Can channels be explicitly freed ? | ||
| 820 |
1/2✓ Branch 0 taken 815425 times.
✗ Branch 1 not taken.
|
815425 | if (cell->car != ENC_SYM_NIL) { |
| 821 | 815425 | lbm_char_channel_t *chan = (lbm_char_channel_t *)cell->car; | |
| 822 | 815425 | curr = chan->dependency; | |
| 823 | 815425 | goto mark_shortcut; | |
| 824 | } | ||
| 825 | ✗ | continue; | |
| 826 | } | ||
| 827 | |||
| 828 | 157239913 | cell->cdr = lbm_set_gc_mark(cell->cdr); | |
| 829 | 157239913 | lbm_heap_state.gc_marked ++; | |
| 830 | |||
| 831 |
2/2✓ Branch 0 taken 152935770 times.
✓ Branch 1 taken 4304143 times.
|
157239913 | if (t_ptr == LBM_TYPE_CONS) { |
| 832 |
2/2✓ Branch 1 taken 98012378 times.
✓ Branch 2 taken 54923392 times.
|
152935770 | if (lbm_is_ptr(cell->cdr)) { |
| 833 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 2 taken 98012377 times.
|
98012378 | if (!lbm_push(s, cell->cdr)) { |
| 834 | 1 | lbm_critical_error(); | |
| 835 | ✗ | break; | |
| 836 | } | ||
| 837 | } | ||
| 838 | 152935769 | curr = cell->car; | |
| 839 | 152935769 | goto mark_shortcut; // Skip a push/pop | |
| 840 | } | ||
| 841 | } | ||
| 842 | 25381287 | } | |
| 843 | #endif | ||
| 844 | |||
| 845 | //Environments are proper lists with a 2 element list stored in each car. | ||
| 846 | 53049949 | void lbm_gc_mark_env(lbm_value env) { | |
| 847 | 53049949 | lbm_value curr = env; | |
| 848 | lbm_cons_t *c; | ||
| 849 | |||
| 850 |
2/2✓ Branch 1 taken 8981280 times.
✓ Branch 2 taken 53049948 times.
|
62031228 | while (lbm_is_ptr(curr)) { |
| 851 | 8981280 | c = lbm_ref_cell(curr); | |
| 852 | 8981280 | c->cdr = lbm_set_gc_mark(c->cdr); // mark the environent list structure. | |
| 853 | 8981280 | lbm_cons_t *b = lbm_ref_cell(c->car); | |
| 854 | 8981280 | b->cdr = lbm_set_gc_mark(b->cdr); // mark the binding list head cell. | |
| 855 | 8981280 | lbm_gc_mark_phase(b->cdr); // mark the bound object. | |
| 856 | 8981279 | lbm_heap_state.gc_marked +=2; | |
| 857 | 8981279 | curr = c->cdr; | |
| 858 | } | ||
| 859 | 53049948 | } | |
| 860 | |||
| 861 | |||
| 862 | 1644308 | void lbm_gc_mark_aux(lbm_uint *aux_data, lbm_uint aux_size) { | |
| 863 |
2/2✓ Branch 0 taken 31554026 times.
✓ Branch 1 taken 1644308 times.
|
33198334 | for (lbm_uint i = 0; i < aux_size; i ++) { |
| 864 |
2/2✓ Branch 1 taken 18839699 times.
✓ Branch 2 taken 12714327 times.
|
31554026 | if (lbm_is_ptr(aux_data[i])) { |
| 865 | 18839699 | lbm_type pt_t = lbm_type_of(aux_data[i]); | |
| 866 | 18839699 | lbm_uint pt_v = lbm_dec_ptr(aux_data[i]); | |
| 867 |
3/4✓ Branch 0 taken 18839699 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8430808 times.
✓ Branch 3 taken 10408891 times.
|
18839699 | if( pt_t >= LBM_POINTER_TYPE_FIRST && |
| 868 | 8430808 | pt_t <= LBM_POINTER_TYPE_LAST && | |
| 869 |
1/2✓ Branch 0 taken 8430808 times.
✗ Branch 1 not taken.
|
8430808 | pt_v < lbm_heap_state.heap_size) { |
| 870 | 8430808 | lbm_gc_mark_phase(aux_data[i]); | |
| 871 | } | ||
| 872 | } | ||
| 873 | } | ||
| 874 | 1644308 | } | |
| 875 | |||
| 876 | 3299227 | void lbm_gc_mark_roots(lbm_uint *roots, lbm_uint num_roots) { | |
| 877 |
2/2✓ Branch 0 taken 6613485 times.
✓ Branch 1 taken 3299227 times.
|
9912712 | for (lbm_uint i = 0; i < num_roots; i ++) { |
| 878 | 6613485 | lbm_gc_mark_phase(roots[i]); | |
| 879 | } | ||
| 880 | 3299227 | } | |
| 881 | |||
| 882 | // Sweep moves non-marked heap objects to the free list. | ||
| 883 | 1606426 | int lbm_gc_sweep_phase(void) { | |
| 884 | 1606426 | unsigned int i = 0; | |
| 885 | 1606426 | lbm_cons_t *heap = (lbm_cons_t *)lbm_heap_state.heap; | |
| 886 | |||
| 887 |
2/2✓ Branch 0 taken 3826095104 times.
✓ Branch 1 taken 1606426 times.
|
3827701530 | for (i = 0; i < lbm_heap_state.heap_size; i ++) { |
| 888 |
2/2✓ Branch 1 taken 175453275 times.
✓ Branch 2 taken 3650641829 times.
|
3826095104 | if ( lbm_get_gc_mark(heap[i].cdr)) { |
| 889 | 175453275 | heap[i].cdr = lbm_clr_gc_mark(heap[i].cdr); | |
| 890 | } else { | ||
| 891 | // Check if this cell is a pointer to an array | ||
| 892 | // and free it. | ||
| 893 |
2/2✓ Branch 1 taken 642935485 times.
✓ Branch 2 taken 3007706344 times.
|
3650641829 | if (lbm_type_of(heap[i].cdr) == LBM_TYPE_SYMBOL) { |
| 894 |
7/7✓ Branch 0 taken 16793870 times.
✓ Branch 1 taken 86997 times.
✓ Branch 2 taken 1728870 times.
✓ Branch 3 taken 914919 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 84 times.
✓ Branch 6 taken 623410742 times.
|
642935485 | switch(heap[i].cdr) { |
| 895 | |||
| 896 | 16793870 | case ENC_SYM_IND_I_TYPE: /* fall through */ | |
| 897 | case ENC_SYM_IND_U_TYPE: | ||
| 898 | case ENC_SYM_IND_F_TYPE: | ||
| 899 | 16793870 | lbm_memory_free((lbm_uint*)heap[i].car); | |
| 900 | 16793870 | break; | |
| 901 | 86997 | case ENC_SYM_DEFRAG_LISPARRAY_TYPE: /* fall through */ | |
| 902 | case ENC_SYM_DEFRAG_ARRAY_TYPE: | ||
| 903 | 86997 | lbm_defrag_mem_free((lbm_uint*)heap[i].car); | |
| 904 | 86997 | break; | |
| 905 | 1728870 | case ENC_SYM_LISPARRAY_TYPE: /* fall through */ | |
| 906 | case ENC_SYM_ARRAY_TYPE:{ | ||
| 907 | 1728870 | lbm_array_header_t *arr = (lbm_array_header_t*)heap[i].car; | |
| 908 | 1728870 | lbm_memory_free((lbm_uint *)arr->data); | |
| 909 | 1728870 | lbm_heap_state.gc_recovered_arrays++; | |
| 910 | 1728870 | lbm_memory_free((lbm_uint *)arr); | |
| 911 | 1728870 | } break; | |
| 912 | 914919 | case ENC_SYM_CHANNEL_TYPE:{ | |
| 913 | 914919 | lbm_char_channel_t *chan = (lbm_char_channel_t*)heap[i].car; | |
| 914 | 914919 | lbm_memory_free((lbm_uint*)chan->state); | |
| 915 | 914919 | lbm_memory_free((lbm_uint*)chan); | |
| 916 | 914919 | } break; | |
| 917 | 3 | case ENC_SYM_CUSTOM_TYPE: { | |
| 918 | 3 | lbm_uint *t = (lbm_uint*)heap[i].car; | |
| 919 | 3 | lbm_custom_type_destroy(t); | |
| 920 | 3 | lbm_memory_free(t); | |
| 921 | 3 | } break; | |
| 922 | 84 | case ENC_SYM_DEFRAG_MEM_TYPE: { | |
| 923 | 84 | lbm_uint *ptr = (lbm_uint *)heap[i].car; | |
| 924 | 84 | lbm_defrag_mem_destroy(ptr); | |
| 925 | 84 | } break; | |
| 926 | 623410742 | default: | |
| 927 | 623410742 | break; | |
| 928 | } | ||
| 929 | } | ||
| 930 | // create pointer to use as new freelist | ||
| 931 | 3650641829 | lbm_uint addr = lbm_enc_cons_ptr(i); | |
| 932 | |||
| 933 | // Clear the "freed" cell. | ||
| 934 | 3650641829 | heap[i].car = ENC_SYM_RECOVERED; | |
| 935 | 3650641829 | heap[i].cdr = lbm_heap_state.freelist; | |
| 936 | 3650641829 | lbm_heap_state.freelist = addr; | |
| 937 | 3650641829 | lbm_heap_state.num_free++; | |
| 938 | 3650641829 | lbm_heap_state.gc_recovered ++; | |
| 939 | } | ||
| 940 | } | ||
| 941 | 1606426 | return 1; | |
| 942 | } | ||
| 943 | |||
| 944 | 1606427 | void lbm_gc_state_inc(void) { | |
| 945 | 1606427 | lbm_heap_state.gc_num ++; | |
| 946 | 1606427 | lbm_heap_state.gc_recovered = 0; | |
| 947 | 1606427 | lbm_heap_state.gc_marked = 0; | |
| 948 | 1606427 | } | |
| 949 | |||
| 950 | // construct, alter and break apart | ||
| 951 | 1411341139 | lbm_value lbm_cons(lbm_value car, lbm_value cdr) { | |
| 952 | 1411341139 | return lbm_heap_allocate_cell(LBM_TYPE_CONS, car, cdr); | |
| 953 | } | ||
| 954 | |||
| 955 | 821777160 | lbm_value lbm_car(lbm_value c){ | |
| 956 |
2/2✓ Branch 1 taken 821774660 times.
✓ Branch 2 taken 2500 times.
|
821777160 | if (lbm_is_ptr(c) ){ |
| 957 | 821774660 | lbm_cons_t *cell = lbm_ref_cell(c); | |
| 958 | 821774660 | return cell->car; | |
| 959 | } | ||
| 960 |
2/2✓ Branch 0 taken 103 times.
✓ Branch 1 taken 2397 times.
|
2500 | return c ? ENC_SYM_TERROR : c; //nil if c == nil |
| 961 | } | ||
| 962 | |||
| 963 | // TODO: Many comparisons "is this the nil symbol" can be | ||
| 964 | // streamlined a bit. NIL is 0 and cannot be confused with any other | ||
| 965 | // lbm_value. | ||
| 966 | |||
| 967 | 138919 | lbm_value lbm_caar(lbm_value c) { | |
| 968 | 138919 | lbm_value tmp = ENC_SYM_NIL; | |
| 969 |
1/2✓ Branch 1 taken 138919 times.
✗ Branch 2 not taken.
|
138919 | if (lbm_is_ptr(c)) { |
| 970 | 138919 | tmp = lbm_ref_cell(c)->car; | |
| 971 |
1/2✓ Branch 1 taken 138919 times.
✗ Branch 2 not taken.
|
138919 | if (lbm_is_ptr(tmp)) { |
| 972 | 138919 | return lbm_ref_cell(tmp)->car; | |
| 973 | } | ||
| 974 | } | ||
| 975 | ✗ | return c || tmp ? ENC_SYM_TERROR : c; //nil if not something else | |
| 976 | } | ||
| 977 | |||
| 978 | |||
| 979 | 35173 | lbm_value lbm_cadr(lbm_value c) { | |
| 980 | 35173 | lbm_value tmp = ENC_SYM_NIL; | |
| 981 |
1/2✓ Branch 1 taken 35173 times.
✗ Branch 2 not taken.
|
35173 | if (lbm_is_ptr(c)) { |
| 982 | 35173 | tmp = lbm_ref_cell(c)->cdr; | |
| 983 |
1/2✓ Branch 1 taken 35173 times.
✗ Branch 2 not taken.
|
35173 | if (lbm_is_ptr(tmp)) { |
| 984 | 35173 | return lbm_ref_cell(tmp)->car; | |
| 985 | } | ||
| 986 | } | ||
| 987 | ✗ | return c || tmp ? ENC_SYM_TERROR : c; | |
| 988 | } | ||
| 989 | |||
| 990 | 91634820 | lbm_value lbm_cdr(lbm_value c){ | |
| 991 |
2/2✓ Branch 1 taken 89935312 times.
✓ Branch 2 taken 1699508 times.
|
91634820 | if (lbm_is_ptr(c)) { |
| 992 | 89935312 | lbm_cons_t *cell = lbm_ref_cell(c); | |
| 993 | 89935312 | return cell->cdr; | |
| 994 | } | ||
| 995 |
1/2✓ Branch 0 taken 1699508 times.
✗ Branch 1 not taken.
|
1699508 | return c ? ENC_SYM_TERROR: c; |
| 996 | } | ||
| 997 | |||
| 998 | 5 | lbm_value lbm_cddr(lbm_value c) { | |
| 999 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
|
5 | if (lbm_is_ptr(c)) { |
| 1000 | 3 | lbm_value tmp = lbm_ref_cell(c)->cdr; | |
| 1001 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 time.
|
3 | if (lbm_is_ptr(tmp)) { |
| 1002 | 2 | return lbm_ref_cell(tmp)->cdr; | |
| 1003 | } | ||
| 1004 | } | ||
| 1005 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
|
3 | return c ? ENC_SYM_TERROR : c; |
| 1006 | } | ||
| 1007 | |||
| 1008 | /* calculate length of a proper list */ | ||
| 1009 | 3751348 | lbm_uint lbm_list_length(lbm_value c) { | |
| 1010 | 3751348 | lbm_uint len = 0; | |
| 1011 | |||
| 1012 |
2/2✓ Branch 1 taken 17893634 times.
✓ Branch 2 taken 3751348 times.
|
21644982 | while (lbm_is_cons(c)){ |
| 1013 | 17893634 | len ++; | |
| 1014 | 17893634 | c = lbm_ref_cell(c)->cdr; | |
| 1015 | } | ||
| 1016 | 3751348 | return len; | |
| 1017 | } | ||
| 1018 | |||
| 1019 | 32610 | lbm_value lbm_list_destructive_reverse(lbm_value list) { | |
| 1020 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 32606 times.
|
32610 | if (!lbm_is_cons(list)) return list; |
| 1021 | 32606 | lbm_value curr = list; | |
| 1022 | 32606 | lbm_value last_cell = ENC_SYM_NIL; | |
| 1023 | |||
| 1024 |
2/2✓ Branch 1 taken 83733 times.
✓ Branch 2 taken 32606 times.
|
116339 | while (lbm_is_cons_rw(curr)) { |
| 1025 | 83733 | lbm_cons_t *cell = lbm_ref_cell(curr); | |
| 1026 | 83733 | lbm_value next = cell->cdr; | |
| 1027 | 83733 | cell->cdr = last_cell; | |
| 1028 | 83733 | last_cell = curr; | |
| 1029 | 83733 | curr = next; | |
| 1030 | } | ||
| 1031 | 32606 | return last_cell; | |
| 1032 | } | ||
| 1033 | |||
| 1034 | |||
| 1035 | 990722 | lbm_value lbm_list_copy(int *m, lbm_value list) { | |
| 1036 | 990722 | lbm_value curr = list; | |
| 1037 | 990722 | lbm_uint n = lbm_list_length(list); | |
| 1038 | 990722 | lbm_uint copy_n = n; | |
| 1039 |
4/4✓ Branch 0 taken 18542 times.
✓ Branch 1 taken 972180 times.
✓ Branch 2 taken 16266 times.
✓ Branch 3 taken 2276 times.
|
990722 | if (*m >= 0 && (lbm_uint)*m < n) { |
| 1040 | 16266 | copy_n = (lbm_uint)*m; | |
| 1041 |
2/2✓ Branch 0 taken 887213 times.
✓ Branch 1 taken 87243 times.
|
974456 | } else if (*m == -1) { |
| 1042 | 887213 | *m = (int)n; // TODO: smaller range in target variable. | |
| 1043 | } | ||
| 1044 |
2/2✓ Branch 0 taken 679 times.
✓ Branch 1 taken 990043 times.
|
990722 | if (copy_n == 0) return ENC_SYM_NIL; |
| 1045 | 990043 | lbm_uint new_list = lbm_heap_allocate_list(copy_n); | |
| 1046 |
2/2✓ Branch 1 taken 1578 times.
✓ Branch 2 taken 988465 times.
|
990043 | if (lbm_is_symbol(new_list)) return new_list; |
| 1047 | 988465 | lbm_value curr_targ = new_list; | |
| 1048 | |||
| 1049 |
4/4✓ Branch 1 taken 11299458 times.
✓ Branch 2 taken 972445 times.
✓ Branch 3 taken 11283438 times.
✓ Branch 4 taken 16020 times.
|
12271903 | while (lbm_is_cons(curr) && copy_n > 0) { |
| 1050 | 11283438 | lbm_cons_t *curr_cell = lbm_ref_cell(curr); | |
| 1051 | 11283438 | lbm_cons_t *targ_cell = lbm_ref_cell(curr_targ); | |
| 1052 | 11283438 | targ_cell->car = curr_cell->car; | |
| 1053 | 11283438 | curr_targ = targ_cell->cdr; | |
| 1054 | 11283438 | curr = curr_cell->cdr; | |
| 1055 | 11283438 | copy_n --; | |
| 1056 | } | ||
| 1057 | 988465 | return new_list; | |
| 1058 | } | ||
| 1059 | |||
| 1060 | // Append for proper lists only | ||
| 1061 | // Destructive update of list1. | ||
| 1062 | 72400 | lbm_value lbm_list_append(lbm_value list1, lbm_value list2) { | |
| 1063 | |||
| 1064 |
2/4✓ Branch 1 taken 72400 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 72400 times.
✗ Branch 4 not taken.
|
144800 | if(lbm_is_list_rw(list1) && |
| 1065 | 72400 | lbm_is_list(list2)) { | |
| 1066 | |||
| 1067 | 72400 | lbm_value curr = list1; | |
| 1068 | 72400 | lbm_cons_t *cell = lbm_ref_cell(curr); | |
| 1069 |
2/2✓ Branch 1 taken 94941 times.
✓ Branch 2 taken 72400 times.
|
167341 | while(lbm_is_cons_rw(cell->cdr)) { |
| 1070 | 94941 | curr = cell->cdr; | |
| 1071 | 94941 | cell = lbm_ref_cell(cell->cdr); | |
| 1072 | } | ||
| 1073 |
2/2✓ Branch 1 taken 86 times.
✓ Branch 2 taken 72314 times.
|
72400 | if (lbm_is_symbol_nil(curr)) return list2; |
| 1074 | 72314 | lbm_set_cdr(curr, list2); | |
| 1075 | 72314 | return list1; | |
| 1076 | } | ||
| 1077 | ✗ | return ENC_SYM_EERROR; | |
| 1078 | } | ||
| 1079 | |||
| 1080 | 252 | lbm_value lbm_list_drop(unsigned int n, lbm_value ls) { | |
| 1081 | 252 | lbm_value curr = ls; | |
| 1082 |
4/4✓ Branch 1 taken 2184 times.
✓ Branch 2 taken 168 times.
✓ Branch 3 taken 2100 times.
✓ Branch 4 taken 84 times.
|
2352 | while (lbm_is_cons(curr) && n > 0) { |
| 1083 | 2100 | curr = lbm_ref_cell(curr)->cdr; | |
| 1084 | 2100 | n --; | |
| 1085 | } | ||
| 1086 | 252 | return curr; | |
| 1087 | } | ||
| 1088 | |||
| 1089 | 486464 | lbm_value lbm_index_list(lbm_value l, int32_t n) { | |
| 1090 | 486464 | lbm_value curr = l; | |
| 1091 | |||
| 1092 |
2/2✓ Branch 0 taken 337 times.
✓ Branch 1 taken 486127 times.
|
486464 | if (n < 0) { |
| 1093 | 337 | int32_t len = (int32_t)lbm_list_length(l); | |
| 1094 | 337 | n = len + n; | |
| 1095 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 337 times.
|
337 | if (n < 0) return ENC_SYM_NIL; |
| 1096 | } | ||
| 1097 | |||
| 1098 |
4/4✓ Branch 1 taken 735122 times.
✓ Branch 2 taken 86 times.
✓ Branch 3 taken 248744 times.
✓ Branch 4 taken 486378 times.
|
735208 | while (lbm_is_cons(curr) && n > 0) { |
| 1099 | 248744 | curr = lbm_ref_cell(curr)->cdr; | |
| 1100 | 248744 | n --; | |
| 1101 | } | ||
| 1102 |
2/2✓ Branch 1 taken 486378 times.
✓ Branch 2 taken 86 times.
|
486464 | if (lbm_is_cons(curr)) { |
| 1103 | 486378 | return lbm_ref_cell(curr)->car; | |
| 1104 | } else { | ||
| 1105 | 86 | return ENC_SYM_NIL; | |
| 1106 | } | ||
| 1107 | } | ||
| 1108 | |||
| 1109 | // High-level arrays are just bytearrays but with a different tag and pointer type. | ||
| 1110 | // These arrays will be inspected by GC and the elements of the array will be marked. | ||
| 1111 | |||
| 1112 | // Arrays are part of the heap module because their lifespan is managed | ||
| 1113 | // by the garbage collector. The data in the array is not stored | ||
| 1114 | // in the "heap of cons cells". | ||
| 1115 | 1744796 | int lbm_heap_allocate_array_base(lbm_value *res, bool byte_array, lbm_uint size){ | |
| 1116 | |||
| 1117 | 1744796 | lbm_uint tag = ENC_SYM_ARRAY_TYPE; | |
| 1118 | 1744796 | lbm_uint type = LBM_TYPE_ARRAY; | |
| 1119 | 1744796 | lbm_array_header_t *array = NULL; | |
| 1120 | 1744796 | lbm_array_header_extended_t *ext_array = NULL; | |
| 1121 | |||
| 1122 |
2/2✓ Branch 0 taken 896786 times.
✓ Branch 1 taken 848010 times.
|
1744796 | if (byte_array) { |
| 1123 | 896786 | array = (lbm_array_header_t*)lbm_malloc(sizeof(lbm_array_header_t)); | |
| 1124 | } else { | ||
| 1125 | 848010 | tag = ENC_SYM_LISPARRAY_TYPE; | |
| 1126 | 848010 | type = LBM_TYPE_LISPARRAY; | |
| 1127 | 848010 | size = sizeof(lbm_value) * size; | |
| 1128 | 848010 | array = (lbm_array_header_t*)lbm_malloc(sizeof(lbm_array_header_extended_t)); | |
| 1129 | 848010 | ext_array = (lbm_array_header_extended_t*)array; | |
| 1130 | } | ||
| 1131 |
2/2✓ Branch 0 taken 1741611 times.
✓ Branch 1 taken 3185 times.
|
1744796 | if (array) { |
| 1132 |
2/2✓ Branch 0 taken 845430 times.
✓ Branch 1 taken 896181 times.
|
1741611 | if (!byte_array) ext_array->index = 0; |
| 1133 | |||
| 1134 | 1741611 | array->data = NULL; | |
| 1135 | 1741611 | array->size = size; | |
| 1136 |
2/2✓ Branch 0 taken 1741178 times.
✓ Branch 1 taken 433 times.
|
1741611 | if ( size > 0) { |
| 1137 | 1741178 | array->data = (lbm_uint*)lbm_malloc(size); | |
| 1138 |
2/2✓ Branch 0 taken 14236 times.
✓ Branch 1 taken 1726942 times.
|
1741178 | if (array->data == NULL) { |
| 1139 | 14236 | lbm_memory_free((lbm_uint*)array); | |
| 1140 | 14236 | goto allocate_array_merror; | |
| 1141 | } | ||
| 1142 | // It is more important to zero out high-level arrays. | ||
| 1143 | // 0 is symbol NIL which is perfectly safe for the GC to inspect. | ||
| 1144 | 1726942 | memset(array->data, 0, size); | |
| 1145 | } | ||
| 1146 | // allocating a cell for array's heap-presence | ||
| 1147 | 1727375 | lbm_value cell = lbm_heap_allocate_cell(type, (lbm_uint) array, tag); | |
| 1148 |
2/2✓ Branch 0 taken 1117 times.
✓ Branch 1 taken 1726258 times.
|
1727375 | if (cell == ENC_SYM_MERROR) { |
| 1149 | 1117 | lbm_memory_free((lbm_uint*)array->data); | |
| 1150 | 1117 | lbm_memory_free((lbm_uint*)array); | |
| 1151 | 1117 | goto allocate_array_merror; | |
| 1152 | } | ||
| 1153 | 1726258 | *res = cell; | |
| 1154 | 1726258 | lbm_heap_state.num_alloc_arrays ++; | |
| 1155 | 1726258 | return 1; | |
| 1156 | } | ||
| 1157 | 3185 | allocate_array_merror: | |
| 1158 | 18538 | *res = ENC_SYM_MERROR; | |
| 1159 | 18538 | return 0; | |
| 1160 | } | ||
| 1161 | |||
| 1162 | 896786 | int lbm_heap_allocate_array(lbm_value *res, lbm_uint size){ | |
| 1163 | 896786 | return lbm_heap_allocate_array_base(res, true, size); | |
| 1164 | } | ||
| 1165 | |||
| 1166 | 848010 | int lbm_heap_allocate_lisp_array(lbm_value *res, lbm_uint size) { | |
| 1167 | 848010 | return lbm_heap_allocate_array_base(res, false, size); | |
| 1168 | } | ||
| 1169 | |||
| 1170 | 1129 | int lbm_lift_array(lbm_value *value, char *data, lbm_uint num_elt) { | |
| 1171 | |||
| 1172 | 1129 | lbm_array_header_t *array = NULL; | |
| 1173 | 1129 | lbm_value cell = lbm_heap_allocate_cell(LBM_TYPE_CONS, ENC_SYM_NIL, ENC_SYM_ARRAY_TYPE); | |
| 1174 | |||
| 1175 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1129 times.
|
1129 | if (cell == ENC_SYM_MERROR) { |
| 1176 | ✗ | *value = cell; | |
| 1177 | ✗ | return 0; | |
| 1178 | } | ||
| 1179 | |||
| 1180 | 1129 | array = (lbm_array_header_t*)lbm_malloc(sizeof(lbm_array_header_t)); | |
| 1181 | |||
| 1182 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1129 times.
|
1129 | if (array == NULL) { |
| 1183 | ✗ | lbm_set_car_and_cdr(cell, ENC_SYM_NIL, ENC_SYM_NIL); | |
| 1184 | ✗ | *value = ENC_SYM_MERROR; | |
| 1185 | ✗ | return 0; | |
| 1186 | } | ||
| 1187 | |||
| 1188 | 1129 | array->data = (lbm_uint*)data; | |
| 1189 | 1129 | array->size = num_elt; | |
| 1190 | |||
| 1191 | 1129 | lbm_set_car(cell, (lbm_uint)array); | |
| 1192 | |||
| 1193 | 1129 | cell = lbm_set_ptr_type(cell, LBM_TYPE_ARRAY); | |
| 1194 | 1129 | *value = cell; | |
| 1195 | 1129 | return 1; | |
| 1196 | } | ||
| 1197 | |||
| 1198 | 712737 | lbm_int lbm_heap_array_get_size(lbm_value arr) { | |
| 1199 | |||
| 1200 | 712737 | lbm_int r = -1; | |
| 1201 | 712737 | lbm_array_header_t *header = lbm_dec_array_r(arr); | |
| 1202 |
1/2✓ Branch 0 taken 712737 times.
✗ Branch 1 not taken.
|
712737 | if (header) { |
| 1203 | 712737 | r = (lbm_int)header->size; | |
| 1204 | } | ||
| 1205 | 712737 | return r; | |
| 1206 | } | ||
| 1207 | |||
| 1208 | 356547 | const uint8_t *lbm_heap_array_get_data_ro(lbm_value arr) { | |
| 1209 | 356547 | uint8_t *r = NULL; | |
| 1210 | 356547 | lbm_array_header_t *header = lbm_dec_array_r(arr); | |
| 1211 |
1/2✓ Branch 0 taken 356547 times.
✗ Branch 1 not taken.
|
356547 | if (header) { |
| 1212 | 356547 | r = (uint8_t*)header->data; | |
| 1213 | } | ||
| 1214 | 356547 | return r; | |
| 1215 | } | ||
| 1216 | |||
| 1217 | /* Explicitly freeing an array. | ||
| 1218 | |||
| 1219 | This is a highly unsafe operation and can only be safely | ||
| 1220 | used if the heap cell that points to the array has not been made | ||
| 1221 | accessible to the program. | ||
| 1222 | |||
| 1223 | So This function can be used to free an array in case an array | ||
| 1224 | is being constructed and some error case appears while doing so | ||
| 1225 | If the array still have not become available it can safely be | ||
| 1226 | "explicitly" freed. | ||
| 1227 | |||
| 1228 | The problem is that if the "array" heap-cell is made available to | ||
| 1229 | the program, this cell can easily be duplicated and we would have | ||
| 1230 | to search the entire heap to find all cells pointing to the array | ||
| 1231 | memory in question and "null"-them out before freeing the memory | ||
| 1232 | */ | ||
| 1233 | |||
| 1234 | 336 | int lbm_heap_explicit_free_array(lbm_value arr) { | |
| 1235 | |||
| 1236 | 336 | int r = 0; | |
| 1237 |
2/4✓ Branch 1 taken 336 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 336 times.
✗ Branch 5 not taken.
|
336 | if (lbm_is_array_rw(arr) && lbm_cdr(arr) == ENC_SYM_ARRAY_TYPE) { |
| 1238 | 336 | lbm_array_header_t *header = (lbm_array_header_t*)lbm_car(arr); | |
| 1239 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
|
336 | if (header == NULL) { |
| 1240 | ✗ | return 0; | |
| 1241 | } | ||
| 1242 | 336 | lbm_memory_free((lbm_uint*)header->data); | |
| 1243 | 336 | lbm_memory_free((lbm_uint*)header); | |
| 1244 | |||
| 1245 | 336 | arr = lbm_set_ptr_type(arr, LBM_TYPE_CONS); | |
| 1246 | 336 | lbm_set_car(arr, ENC_SYM_NIL); | |
| 1247 | 336 | lbm_set_cdr(arr, ENC_SYM_NIL); | |
| 1248 | 336 | r = 1; | |
| 1249 | } | ||
| 1250 | |||
| 1251 | 336 | return r; | |
| 1252 | } | ||
| 1253 | |||
| 1254 | ✗ | static bool dummy_flash_write(lbm_uint ix, lbm_uint val) { | |
| 1255 | (void)ix; | ||
| 1256 | (void)val; | ||
| 1257 | ✗ | return false; | |
| 1258 | } | ||
| 1259 | |||
| 1260 | static const_heap_write_fun const_heap_write = dummy_flash_write; | ||
| 1261 | |||
| 1262 | 66575 | int lbm_const_heap_init(const_heap_write_fun w_fun, | |
| 1263 | lbm_const_heap_t *heap, | ||
| 1264 | lbm_uint *addr) { | ||
| 1265 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66575 times.
|
66575 | if (((uintptr_t)addr % 4) != 0) return 0; |
| 1266 | |||
| 1267 |
2/2✓ Branch 0 taken 66485 times.
✓ Branch 1 taken 90 times.
|
66575 | if (!lbm_const_heap_mutex_initialized) { |
| 1268 | 66485 | lbm_mutex_init(&lbm_const_heap_mutex); | |
| 1269 | 66485 | lbm_const_heap_mutex_initialized = true; | |
| 1270 | } | ||
| 1271 | |||
| 1272 |
2/2✓ Branch 0 taken 66485 times.
✓ Branch 1 taken 90 times.
|
66575 | if (!lbm_mark_mutex_initialized) { |
| 1273 | 66485 | lbm_mutex_init(&lbm_mark_mutex); | |
| 1274 | 66485 | lbm_mark_mutex_initialized = true; | |
| 1275 | } | ||
| 1276 | |||
| 1277 | 66575 | const_heap_write = w_fun; | |
| 1278 | |||
| 1279 | 66575 | heap->heap = addr; | |
| 1280 | 66575 | heap->size = 0; | |
| 1281 | 66575 | heap->next = 0; | |
| 1282 | |||
| 1283 | 66575 | lbm_const_heap_state = heap; | |
| 1284 | // ref_cell views the lbm_uint array as an lbm_cons_t array | ||
| 1285 | 66575 | lbm_heaps[LBM_CONST_HEAP] = (lbm_cons_t*)addr; | |
| 1286 | 66575 | return 1; | |
| 1287 | } | ||
| 1288 | |||
| 1289 | 7393 | lbm_flash_status lbm_allocate_const_cell(lbm_value *res) { | |
| 1290 | 7393 | lbm_flash_status r = LBM_FLASH_FULL; | |
| 1291 | |||
| 1292 | 7393 | lbm_mutex_lock(&lbm_const_heap_mutex); | |
| 1293 | // waste a cell if we have ended up unaligned after writing an array to flash. | ||
| 1294 |
2/2✓ Branch 0 taken 457 times.
✓ Branch 1 taken 6936 times.
|
7393 | if (lbm_const_heap_state->next % 2 == 1) { |
| 1295 | 457 | lbm_const_heap_state->next++; | |
| 1296 | } | ||
| 1297 | |||
| 1298 |
1/2✓ Branch 0 taken 7393 times.
✗ Branch 1 not taken.
|
7393 | if (lbm_const_heap_state && |
| 1299 |
1/2✓ Branch 1 taken 7393 times.
✗ Branch 2 not taken.
|
7393 | (lbm_const_heap_state->next+1) < (uint32_t)lbm_image_get_write_index()) { |
| 1300 | // A cons cell uses two words. | ||
| 1301 | 7393 | lbm_value cell = lbm_const_heap_state->next; | |
| 1302 | 7393 | lbm_const_heap_state->next += 2; | |
| 1303 | 7393 | *res = ((cell >> 1) << LBM_ADDRESS_SHIFT) | LBM_PTR_BIT | LBM_TYPE_CONS | LBM_PTR_TO_CONSTANT_BIT; | |
| 1304 | 7393 | r = LBM_FLASH_WRITE_OK; | |
| 1305 | } | ||
| 1306 | 7393 | lbm_mutex_unlock(&lbm_const_heap_mutex); | |
| 1307 | 7393 | return r; | |
| 1308 | } | ||
| 1309 | |||
| 1310 | 84 | lbm_flash_status lbm_allocate_const_raw(lbm_uint nwords, lbm_uint *res) { | |
| 1311 | 84 | lbm_flash_status r = LBM_FLASH_FULL; | |
| 1312 | |||
| 1313 |
1/2✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
|
84 | if (lbm_const_heap_state && |
| 1314 |
1/2✓ Branch 1 taken 84 times.
✗ Branch 2 not taken.
|
84 | (lbm_const_heap_state->next + nwords) < (uint32_t)lbm_image_get_write_index()) { |
| 1315 | 84 | lbm_uint ix = lbm_const_heap_state->next; | |
| 1316 | 84 | *res = (lbm_uint)&lbm_const_heap_state->heap[ix]; | |
| 1317 | 84 | lbm_const_heap_state->next += nwords; | |
| 1318 | 84 | r = LBM_FLASH_WRITE_OK; | |
| 1319 | } | ||
| 1320 | 84 | return r; | |
| 1321 | } | ||
| 1322 | |||
| 1323 | 528410 | lbm_flash_status lbm_write_const_raw(lbm_uint *data, lbm_uint n, lbm_uint *res) { | |
| 1324 | |||
| 1325 | 528410 | lbm_flash_status r = LBM_FLASH_FULL; | |
| 1326 | |||
| 1327 |
1/2✓ Branch 0 taken 528410 times.
✗ Branch 1 not taken.
|
528410 | if (lbm_const_heap_state && |
| 1328 |
1/2✓ Branch 1 taken 528410 times.
✗ Branch 2 not taken.
|
528410 | (lbm_const_heap_state->next + n) < (uint32_t)lbm_image_get_write_index()) { |
| 1329 | 528410 | lbm_uint ix = lbm_const_heap_state->next; | |
| 1330 | |||
| 1331 |
2/2✓ Branch 0 taken 568788 times.
✓ Branch 1 taken 528410 times.
|
1097198 | for (unsigned int i = 0; i < n; i ++) { |
| 1332 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 568788 times.
|
568788 | if (!const_heap_write(((lbm_uint*)data)[i],ix + i)) |
| 1333 | ✗ | return LBM_FLASH_WRITE_ERROR; | |
| 1334 | } | ||
| 1335 | 528410 | lbm_const_heap_state->next += n; | |
| 1336 | 528410 | *res = (lbm_uint)&lbm_const_heap_state->heap[ix]; | |
| 1337 | 528410 | r = LBM_FLASH_WRITE_OK; | |
| 1338 | } | ||
| 1339 | 528410 | return r; | |
| 1340 | } | ||
| 1341 | |||
| 1342 | 252 | lbm_flash_status lbm_const_write(lbm_uint *tgt, lbm_uint val) { | |
| 1343 | |||
| 1344 |
1/2✓ Branch 0 taken 252 times.
✗ Branch 1 not taken.
|
252 | if (lbm_const_heap_state) { |
| 1345 | 252 | lbm_uint flash = (lbm_uint)lbm_const_heap_state->heap; | |
| 1346 | 252 | lbm_uint ix = (((lbm_uint)tgt - flash) / sizeof(lbm_uint)); // byte address to ix | |
| 1347 |
1/2✓ Branch 1 taken 252 times.
✗ Branch 2 not taken.
|
252 | if (const_heap_write(val, ix)) { |
| 1348 | 252 | return LBM_FLASH_WRITE_OK; | |
| 1349 | } | ||
| 1350 | ✗ | return LBM_FLASH_WRITE_ERROR; | |
| 1351 | } | ||
| 1352 | ✗ | return LBM_FLASH_FULL; | |
| 1353 | } | ||
| 1354 | |||
| 1355 | 7393 | lbm_flash_status write_const_cdr(lbm_value cell, lbm_value val) { | |
| 1356 | 7393 | lbm_uint addr = lbm_dec_ptr(cell) << 1; | |
| 1357 |
1/2✓ Branch 1 taken 7393 times.
✗ Branch 2 not taken.
|
7393 | if (const_heap_write(val, addr+1)) |
| 1358 | 7393 | return LBM_FLASH_WRITE_OK; | |
| 1359 | ✗ | return LBM_FLASH_WRITE_ERROR; | |
| 1360 | } | ||
| 1361 | |||
| 1362 | 7393 | lbm_flash_status write_const_car(lbm_value cell, lbm_value val) { | |
| 1363 | 7393 | lbm_uint addr = lbm_dec_ptr(cell) << 1; | |
| 1364 |
1/2✓ Branch 1 taken 7393 times.
✗ Branch 2 not taken.
|
7393 | if (const_heap_write(val, addr)) |
| 1365 | 7393 | return LBM_FLASH_WRITE_OK; | |
| 1366 | ✗ | return LBM_FLASH_WRITE_ERROR; | |
| 1367 | } | ||
| 1368 | |||
| 1369 | 9 | lbm_uint lbm_flash_memory_usage(void) { | |
| 1370 | 9 | return lbm_const_heap_state->next; | |
| 1371 | } | ||
| 1372 | |||
| 1373 | |||
| 1374 | // //////////////////////////////////////////////////////////// | ||
| 1375 | // pointer reversal traversal | ||
| 1376 | // | ||
| 1377 | // Caveats: | ||
| 1378 | // * Structures on the constant heap cannot be traversed using | ||
| 1379 | // pointer reversal. If a dynamic structure is pointing into the | ||
| 1380 | // constant heap, the 'f' will be applied to the constant cons cell on | ||
| 1381 | // the border and then traversal will retreat. | ||
| 1382 | // | ||
| 1383 | // * It may be impossible to detect all kinds of cycles | ||
| 1384 | // if attempting to use and restore the GC in a single pass | ||
| 1385 | // over the tree. To detect cycles all visited nodes must | ||
| 1386 | // remain detectable when traversing all branches! | ||
| 1387 | // | ||
| 1388 | // * Potential fix is to run GC after a complete traversal of the | ||
| 1389 | // entire value in order to restore the GC bits. | ||
| 1390 | // | ||
| 1391 | // * If we leave GC bits set when traversing values, we can use this | ||
| 1392 | // to detect cycles that happen in multiple steps accross values | ||
| 1393 | // in the environment. | ||
| 1394 | // | ||
| 1395 | // * lbm_ptr_rev_trav with the "do_nothing" travfun is the same thing | ||
| 1396 | // as a GC mark phase! Maybe utilize this for code-size | ||
| 1397 | // purposes. This also increases the amount of testing the | ||
| 1398 | // ptr_rev_trav function is subjected to. | ||
| 1399 | |||
| 1400 | 957 | void lbm_ptr_rev_trav(trav_fun f, lbm_value v, void* arg) { | |
| 1401 | |||
| 1402 | 957 | lbm_value curr = v; | |
| 1403 | 957 | lbm_value prev = lbm_enc_cons_ptr(LBM_PTR_NULL); | |
| 1404 | while (true) { | ||
| 1405 | |||
| 1406 | // Run leftwards and process conses until | ||
| 1407 | // hitting a leaf in the left direction. | ||
| 1408 | |||
| 1409 | // If curr is marked here there is a cycle in the graph. | ||
| 1410 | // In case of a cycle or leaf, this first loop is exited. | ||
| 1411 |
2/2✓ Branch 1 taken 75 times.
✓ Branch 2 taken 24669 times.
|
73164 | while (((lbm_is_cons_rw(curr)) || |
| 1412 |
4/4✓ Branch 0 taken 24744 times.
✓ Branch 1 taken 23676 times.
✓ Branch 4 taken 23629 times.
✓ Branch 5 taken 122 times.
|
48495 | (lbm_is_lisp_array_rw(curr))) && !gc_marked(curr)) { |
| 1413 | 23629 | lbm_cons_t *cell = lbm_ref_cell(curr); | |
| 1414 |
2/2✓ Branch 1 taken 23569 times.
✓ Branch 2 taken 60 times.
|
23629 | if (lbm_is_cons(curr)) { |
| 1415 | // In-order traversal | ||
| 1416 |
2/2✓ Branch 1 taken 82 times.
✓ Branch 2 taken 23487 times.
|
23569 | if (f(curr, false, arg) == TRAV_FUN_SUBTREE_DONE) { |
| 1417 | 82 | lbm_gc_mark_phase(curr); | |
| 1418 | 82 | goto trav_backtrack; | |
| 1419 | } | ||
| 1420 | 23487 | gc_mark(curr); | |
| 1421 | |||
| 1422 | 23487 | lbm_value next = 0; | |
| 1423 | 23487 | value_assign(&next, cell->car); | |
| 1424 | 23487 | value_assign(&cell->car, prev); | |
| 1425 | 23487 | value_assign(&prev, curr); | |
| 1426 | 23487 | value_assign(&curr, next); | |
| 1427 | } else { // it is an array | ||
| 1428 | |||
| 1429 | 60 | lbm_array_header_extended_t *arr = (lbm_array_header_extended_t*)cell->car; | |
| 1430 | 60 | lbm_value *arr_data = (lbm_value *)arr->data; | |
| 1431 | 60 | uint32_t index = arr->index; | |
| 1432 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 54 times.
|
60 | if (arr->size == 0) break; |
| 1433 |
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 |
| 1434 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 54 times.
|
54 | if (f(curr, false, arg) == TRAV_FUN_SUBTREE_DONE) { |
| 1435 | ✗ | lbm_gc_mark_phase(curr); | |
| 1436 | ✗ | break; | |
| 1437 | } | ||
| 1438 | 54 | arr->index = 1; | |
| 1439 | 54 | gc_mark(curr); | |
| 1440 | |||
| 1441 | 54 | lbm_value next = 0; | |
| 1442 | 54 | value_assign(&next, arr_data[0]); | |
| 1443 | 54 | value_assign(&arr_data[0], prev); | |
| 1444 | 54 | value_assign(&prev, curr); | |
| 1445 | 54 | value_assign(&curr, next); | |
| 1446 | } | ||
| 1447 | } | ||
| 1448 | } | ||
| 1449 | // Currently there are a few different users of this traversal. | ||
| 1450 | // size, flatten and detect_sharing. | ||
| 1451 | // detect_sharing make use of the shared (true) argument in f(curr, true, arg) | ||
| 1452 | // while the other do not. detect_sharing also assumes it is run once per env item | ||
| 1453 | // while not resetting any GC-bits in between. This detects global sharing. | ||
| 1454 | |||
| 1455 |
4/4✓ Branch 1 taken 860 times.
✓ Branch 2 taken 23937 times.
✓ Branch 4 taken 122 times.
✓ Branch 5 taken 738 times.
|
24797 | if (lbm_is_ptr(curr) && gc_marked(curr)) { |
| 1456 | // gc bit set so this subtree is already traversed. | ||
| 1457 | // f is called with true to indicate visited node. | ||
| 1458 | // if this happens during a sharing discovery phase, curr will be added to sharing table. | ||
| 1459 | 122 | f(curr, true, arg); | |
| 1460 | // In this case f should not be able to return subtree continue. | ||
| 1461 | // The only correct return value from f is PROCEED. | ||
| 1462 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 24672 times.
|
24675 | } else if (!lbm_is_cons(curr) || // Found a leaf |
| 1463 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | (curr & LBM_PTR_TO_CONSTANT_BIT)) { |
| 1464 |
4/4✓ Branch 1 taken 738 times.
✓ Branch 2 taken 23937 times.
✓ Branch 3 taken 735 times.
✓ Branch 4 taken 3 times.
|
24675 | 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. |
| 1465 | 24675 | f(curr, false, arg); | |
| 1466 | } | ||
| 1467 | |||
| 1468 | // Now either prev has the "flag" set or it doesnt. | ||
| 1469 | // If the flag is set that means that the prev node | ||
| 1470 | // have had both its car and cdr visited. So that node is done! | ||
| 1471 | // | ||
| 1472 | // If the flag is not set, jump down to SWAP | ||
| 1473 | |||
| 1474 | ✗ | trav_backtrack: | |
| 1475 |
2/2✓ Branch 1 taken 46974 times.
✓ Branch 2 taken 957 times.
|
96351 | while ((lbm_is_cons(prev) && |
| 1476 |
2/2✓ Branch 1 taken 23487 times.
✓ Branch 2 taken 23487 times.
|
94905 | (lbm_dec_ptr(prev) != LBM_PTR_NULL) && // is LBM_NULL a cons type? |
| 1477 |
4/4✓ Branch 0 taken 47931 times.
✓ Branch 1 taken 489 times.
✓ Branch 4 taken 489 times.
✓ Branch 5 taken 24444 times.
|
120327 | lbm_get_gc_flag(lbm_car(prev))) || |
| 1478 | 24933 | lbm_is_lisp_array_rw(prev)) { | |
| 1479 | 23976 | lbm_cons_t *cell = lbm_ref_cell(prev); | |
| 1480 |
2/2✓ Branch 1 taken 23487 times.
✓ Branch 2 taken 489 times.
|
23976 | if (lbm_is_cons(prev)) { |
| 1481 | |||
| 1482 | // clear the flag | ||
| 1483 | // This means that we are done with a "CDR" child. | ||
| 1484 | // prev = [ a , b ][flag = 1] | ||
| 1485 | // => | ||
| 1486 | // prev = [ a , b ][flag = 0] | ||
| 1487 | |||
| 1488 | //gc_clear_mark(prev); | ||
| 1489 | 23487 | cell->car = lbm_clr_gc_flag(cell->car); | |
| 1490 | // Move on downwards until | ||
| 1491 | // finding a cons cell without flag or NULL | ||
| 1492 | |||
| 1493 | // curr = c | ||
| 1494 | // prev = [ a , b ][flag = 0] | ||
| 1495 | // => | ||
| 1496 | // prev = [ a , c ][flag = 0] | ||
| 1497 | // curr = prev | ||
| 1498 | // prev = b | ||
| 1499 | |||
| 1500 | 23487 | lbm_value next = 0; | |
| 1501 | 23487 | value_assign(&next, cell->cdr); | |
| 1502 | 23487 | value_assign(&cell->cdr, curr); | |
| 1503 | 23487 | value_assign(&curr, prev); | |
| 1504 | 23487 | value_assign(&prev, next); | |
| 1505 | } else { // is an array | ||
| 1506 | 489 | lbm_array_header_extended_t *arr = (lbm_array_header_extended_t*)cell->car; | |
| 1507 | 489 | lbm_value *arr_data = (lbm_value *)arr->data; | |
| 1508 | 489 | size_t arr_size = (size_t)arr->size / sizeof(lbm_value); | |
| 1509 | 489 | lbm_value next = 0; | |
| 1510 |
2/2✓ Branch 0 taken 54 times.
✓ Branch 1 taken 435 times.
|
489 | if (arr->index == arr_size) { |
| 1511 | 54 | value_assign(&next, arr_data[arr->index-1]); | |
| 1512 | 54 | value_assign(&arr_data[arr->index-1], curr); | |
| 1513 | 54 | value_assign(&curr, prev); | |
| 1514 | 54 | value_assign(&prev, next); | |
| 1515 | 54 | arr->index = 0; | |
| 1516 | } else { | ||
| 1517 | 435 | break; | |
| 1518 | } | ||
| 1519 | } | ||
| 1520 | } | ||
| 1521 | |||
| 1522 | // SWAP | ||
| 1523 | |||
| 1524 | // if the prev node is NULL we have traced backwards all the | ||
| 1525 | // way back to where curr == v. Another alternative is that | ||
| 1526 | // the input v was an Atom. We are done! | ||
| 1527 |
3/4✓ Branch 1 taken 24879 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 957 times.
✓ Branch 4 taken 23922 times.
|
49758 | if (lbm_is_ptr(prev) && |
| 1528 | 24879 | lbm_dec_ptr(prev) == LBM_PTR_NULL) { | |
| 1529 | 957 | if (lbm_is_cons(curr)) { | |
| 1530 | //gc_clear_mark(curr); | ||
| 1531 | } | ||
| 1532 | //done = true; | ||
| 1533 | 957 | break; | |
| 1534 | } | ||
| 1535 | |||
| 1536 | // if the prev node is not NULL then we should move | ||
| 1537 | // down to the prev node and start process its remaining child. | ||
| 1538 |
2/2✓ Branch 1 taken 23487 times.
✓ Branch 2 taken 435 times.
|
23922 | else if (lbm_is_cons(prev)) { |
| 1539 | |||
| 1540 | 23487 | lbm_cons_t *cell = lbm_ref_cell(prev); | |
| 1541 | 23487 | lbm_value next = 0; | |
| 1542 | |||
| 1543 | |||
| 1544 | // prev = [ p , cdr ][flag = 0] | ||
| 1545 | // => | ||
| 1546 | // prev = [ p , cdr ][flag = 1] | ||
| 1547 | |||
| 1548 | 23487 | cell->car = lbm_set_gc_flag(cell->car); | |
| 1549 | |||
| 1550 | // switch to processing the cdr field and set the flag. | ||
| 1551 | // curr = c | ||
| 1552 | // prev = [ a, b ][flag = 1] | ||
| 1553 | // => | ||
| 1554 | // prev = [ c, a ][flag = 1] | ||
| 1555 | // curr = b | ||
| 1556 | |||
| 1557 | 23487 | value_assign(&next, cell->car); | |
| 1558 | 23487 | value_assign(&cell->car, curr); | |
| 1559 | 23487 | value_assign(&curr, cell->cdr); | |
| 1560 | 23487 | value_assign(&cell->cdr, next); | |
| 1561 |
1/2✓ Branch 1 taken 435 times.
✗ Branch 2 not taken.
|
435 | } else if (lbm_is_lisp_array_rw(prev)) { |
| 1562 | 435 | lbm_cons_t *cell = lbm_ref_cell(prev); | |
| 1563 | 435 | lbm_array_header_extended_t *arr = (lbm_array_header_extended_t*)cell->car; | |
| 1564 | 435 | lbm_value *arr_data = (lbm_value *)arr->data; | |
| 1565 | 435 | lbm_value next = 0; | |
| 1566 | |||
| 1567 | 435 | value_assign(&next, arr_data[arr->index-1]); | |
| 1568 | 435 | value_assign(&arr_data[arr->index-1], curr); | |
| 1569 | 435 | value_assign(&curr, arr_data[arr->index]); | |
| 1570 | 435 | value_assign(&arr_data[arr->index], next); | |
| 1571 | 435 | arr->index = arr->index + 1; | |
| 1572 | } | ||
| 1573 | } | ||
| 1574 | 957 | } | |
| 1575 |