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