GCC Code Coverage Report


Directory: ../src/
File: /home/joels/Current/lispbm/src/lbm_memory.c
Date: 2025-12-14 15:34:39
Exec Total Coverage
Lines: 245 249 98.4%
Functions: 21 21 100.0%
Branches: 112 129 86.8%

Line Branch Exec Source
1 /*
2 Copyright 2020 - 2025 Joel Svensson svenssonjoel@yahoo.se
3 2024 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 <stdint.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22
23 #include "lbm_memory.h"
24 #include "platform_mutex.h"
25
26 // pull in from eval_cps
27 void lbm_request_gc(void);
28
29 /* Status bit patterns */
30 #define FREE_OR_USED 0 //00b
31 #define END 1 //01b
32 #define START 2 //10b
33 #define START_END 3 //11b
34
35 /* States in memory_allocate state-machine*/
36 #define INIT 0
37 #define FREE_LENGTH_CHECK 1
38 #define SKIP 2
39 #define ALLOC_DONE 0xF00DF00D
40 #define ALLOC_FAILED 0xDEADBEAF
41
42 static lbm_uint *bitmap = NULL;
43 static lbm_uint *memory = NULL;
44 static lbm_uint memory_size; // in 4 or 8 byte words depending on 32 or 64 bit platform
45 static lbm_uint bitmap_size; // in 4 or 8 byte words
46 static lbm_uint memory_base_address = 0;
47 static lbm_uint memory_num_free = 0;
48 static lbm_uint memory_min_free = 0;
49 static volatile lbm_uint memory_reserve_level = 0;
50 static lbm_mutex_t lbm_mem_mutex;
51 static bool lbm_mem_mutex_initialized;
52 static lbm_uint alloc_offset = 0;
53
54 // TODO: Go over the size requirements here.
55 // There may be some extra tight constraints set on bitmap size.
56 //
57 // The data_size and bitmap_size arguments are already in
58 // Number of words.
59 132725 bool lbm_memory_init(lbm_uint *data, lbm_uint data_size,
60 lbm_uint *bits, lbm_uint bits_size) {
61
62
2/2
✓ Branch 0 taken 66478 times.
✓ Branch 1 taken 66247 times.
132725 if (!lbm_mem_mutex_initialized) {
63 66478 lbm_mutex_init(&lbm_mem_mutex);
64 66478 lbm_mem_mutex_initialized = true;
65 }
66
67 132725 alloc_offset = 0;
68
69 132725 lbm_mutex_lock(&lbm_mem_mutex);
70 132725 bool res = false;
71
4/4
✓ Branch 0 taken 132720 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 132717 times.
✓ Branch 3 taken 3 times.
132725 if (data && bits) {
72
73
1/2
✓ Branch 0 taken 132717 times.
✗ Branch 1 not taken.
132717 if (((lbm_uint)data % sizeof(lbm_uint) != 0) || // Alignment requirement
74
2/2
✓ Branch 0 taken 132712 times.
✓ Branch 1 taken 5 times.
132717 (data_size * 2) != (bits_size * sizeof(lbm_uint) * 8) ||
75
1/2
✓ Branch 0 taken 132712 times.
✗ Branch 1 not taken.
132712 data_size % 4 != 0 ||
76
2/4
✓ Branch 0 taken 132712 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 132712 times.
✗ Branch 3 not taken.
132712 ((lbm_uint)bits % sizeof(lbm_uint) != 0) || // Alignment requirement
77 bits_size < 1) {
78 //bits_size % 4 != 0) { // lets try without this requirement a while
79 // data is not aligned to sizeof lbm_uint
80 // size is too small
81 // or size is not a multiple of 4
82 } else {
83
84 132712 bitmap = bits;
85 132712 bitmap_size = bits_size;
86
87
2/2
✓ Branch 0 taken 207034396 times.
✓ Branch 1 taken 132712 times.
207167108 for (lbm_uint i = 0; i < bitmap_size; i ++) {
88 207034396 bitmap[i] = 0;
89 }
90
91 132712 memory = data;
92 132712 memory_base_address = (lbm_uint)data;
93 132712 memory_size = data_size;
94 132712 memory_min_free = data_size;
95 132712 memory_num_free = data_size;
96 132712 memory_reserve_level = (lbm_uint)(0.1 * (lbm_float)data_size);
97 132712 res = true;
98 }
99 }
100 132725 lbm_mutex_unlock(&lbm_mem_mutex);
101 132725 return res;
102 }
103
104 7 void lbm_memory_set_reserve(lbm_uint num_words) {
105 7 memory_reserve_level = num_words;
106 7 }
107
108 3 lbm_uint lbm_memory_get_reserve(void) {
109 3 return memory_reserve_level;
110 }
111
112 22448585 static inline lbm_uint address_to_bitmap_ix(lbm_uint *ptr) {
113 #ifndef LBM64
114 20583106 return ((lbm_uint)ptr - memory_base_address) >> 2;
115 #else
116 1865479 return ((lbm_uint)ptr - memory_base_address) >> 3;
117 #endif
118 }
119
120 69799 lbm_int lbm_memory_address_to_ix(lbm_uint *ptr) {
121 /* TODO: assuming that index
122 will have more then enough room in the
123 positive half of a 28bit integer */
124 69799 return (lbm_int)address_to_bitmap_ix(ptr);
125 }
126
127
128 22857329 static inline lbm_uint *bitmap_ix_to_address(lbm_uint ix) {
129 22857329 return &((lbm_uint*)(memory_base_address))[ix];// + (ix << 2));
130 }
131
132 #ifndef LBM64
133 #define WORD_IX_SHIFT 5
134 #define WORD_MOD_MASK 0x1F
135 #define BITMAP_SIZE_SHIFT 4 // 16 statuses per bitmap word
136 #else
137 #define WORD_IX_SHIFT 6 // divide by 64
138 #define WORD_MOD_MASK 0x3F // mod 64
139 #define BITMAP_SIZE_SHIFT 5 // times 32, 32 statuses per bitmap word
140 #endif
141
142 820377655 static inline lbm_uint status(lbm_uint i) {
143
144 820377655 lbm_uint ix = i << 1; // * 2
145 820377655 lbm_uint word_ix = ix >> WORD_IX_SHIFT; // / 32
146 820377655 lbm_uint bit_ix = ix & WORD_MOD_MASK; // % 32
147
148 820377655 lbm_uint mask = ((lbm_uint)3) << bit_ix; // 000110..0
149 820377655 return (bitmap[word_ix] & mask) >> bit_ix;
150 }
151
152 89882954 static inline void set_status(lbm_uint i, lbm_uint status) {
153 89882954 lbm_uint ix = i << 1; // * 2
154 89882954 lbm_uint word_ix = ix >> WORD_IX_SHIFT; // / 32
155 89882954 lbm_uint bit_ix = ix & WORD_MOD_MASK; // % 32
156
157 89882954 lbm_uint clr_mask = ~(((lbm_uint)3) << bit_ix);
158 89882954 lbm_uint mask = status << bit_ix;
159
160 89882954 bitmap[word_ix] &= clr_mask;
161 89882954 bitmap[word_ix] |= mask;
162 89882954 }
163
164 85 lbm_uint lbm_memory_num_words(void) {
165 85 return memory_size;
166 }
167
168 135432 lbm_uint lbm_memory_num_free(void) {
169 135432 return memory_num_free;
170 }
171
172 4 lbm_uint lbm_memory_maximum_used(void) {
173 4 return (memory_size - memory_min_free);
174 }
175
176 1605005 void lbm_memory_update_min_free(void) {
177
2/2
✓ Branch 0 taken 9501 times.
✓ Branch 1 taken 1595504 times.
1605005 if (memory_num_free < memory_min_free)
178 9501 memory_min_free = memory_num_free;
179 1605005 }
180
181 12417 lbm_uint lbm_memory_longest_free(void) {
182
3/4
✓ Branch 0 taken 12416 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12416 times.
12417 if (memory == NULL || bitmap == NULL) {
183 1 return 0;
184 }
185 12416 lbm_mutex_lock(&lbm_mem_mutex);
186 12416 unsigned int state = INIT;
187 12416 lbm_uint max_length = 0;
188
189 12416 lbm_uint curr_length = 0;
190
2/2
✓ Branch 0 taken 50660096 times.
✓ Branch 1 taken 12416 times.
50672512 for (unsigned int i = 0; i < memory_size; i ++) {
191
192 // The status field is 2 bits and this 4 cases is exhaustive!
193
4/4
✓ Branch 0 taken 50281577 times.
✓ Branch 1 taken 182330 times.
✓ Branch 2 taken 182330 times.
✓ Branch 3 taken 13859 times.
50660096 switch(status(i)) {
194 50281577 case FREE_OR_USED:
195
3/4
✓ Branch 0 taken 14658 times.
✓ Branch 1 taken 39166126 times.
✓ Branch 2 taken 11100793 times.
✗ Branch 3 not taken.
50281577 switch (state) {
196 14658 case INIT:
197 14658 curr_length = 1;
198
2/2
✓ Branch 0 taken 12416 times.
✓ Branch 1 taken 2242 times.
14658 if (curr_length > max_length) max_length = curr_length;
199 14658 state = FREE_LENGTH_CHECK;
200 14658 break;
201 39166126 case FREE_LENGTH_CHECK:
202 39166126 curr_length ++;
203
2/2
✓ Branch 0 taken 39072328 times.
✓ Branch 1 taken 93798 times.
39166126 if (curr_length > max_length) max_length = curr_length;
204 39166126 state = FREE_LENGTH_CHECK;
205 39166126 break;
206 11100793 case SKIP:
207 11100793 break;
208 }
209 50281577 break;
210 182330 case END:
211 182330 state = INIT;
212 182330 break;
213 182330 case START:
214 182330 state = SKIP;
215 182330 break;
216 13859 default: // START_END
217 13859 state = INIT;
218 13859 break;
219 }
220 }
221 12416 lbm_mutex_unlock(&lbm_mem_mutex);
222
2/2
✓ Branch 0 taken 12414 times.
✓ Branch 1 taken 2 times.
12416 if (memory_num_free - max_length < memory_reserve_level) {
223 12414 lbm_uint n = memory_reserve_level - (memory_num_free - max_length);
224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12414 times.
12414 if (n >= max_length) {
225 ✗ max_length = 0;
226 } else {
227 12414 max_length -= n;
228 }
229 }
230 12416 return max_length;
231 }
232
233 22871503 static lbm_uint *lbm_memory_allocate_internal(lbm_uint num_words) {
234
235
3/4
✓ Branch 0 taken 22871502 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 22871502 times.
22871503 if (memory == NULL || bitmap == NULL) {
236 1 return NULL;
237 }
238
239 22871502 lbm_mutex_lock(&lbm_mem_mutex);
240
241 22871502 lbm_uint start_ix = 0;
242 22871502 lbm_uint end_ix = 0;
243 22871502 lbm_uint free_length = 0;
244 22871502 unsigned int state = INIT;
245
246
2/2
✓ Branch 0 taken 270320794 times.
✓ Branch 1 taken 14173 times.
270334967 for (lbm_uint i = 0; i < memory_size; i ++) {
247
4/4
✓ Branch 0 taken 247254349 times.
✓ Branch 1 taken 22554945 times.
✓ Branch 2 taken 198110 times.
✓ Branch 3 taken 313390 times.
270320794 switch(status(alloc_offset)) {
248 247254349 case FREE_OR_USED:
249
3/4
✓ Branch 0 taken 22883306 times.
✓ Branch 1 taken 176694298 times.
✓ Branch 2 taken 47676745 times.
✗ Branch 3 not taken.
247254349 switch (state) {
250 22883306 case INIT:
251 22883306 start_ix = alloc_offset;
252
2/2
✓ Branch 0 taken 308223 times.
✓ Branch 1 taken 22575083 times.
22883306 if (num_words == 1) {
253 308223 end_ix = alloc_offset;
254 308223 state = ALLOC_DONE;
255 } else {
256 22575083 state = FREE_LENGTH_CHECK;
257 22575083 free_length = 1;
258 }
259 22883306 break;
260 176694298 case FREE_LENGTH_CHECK:
261 176694298 free_length ++;
262
2/2
✓ Branch 0 taken 22549106 times.
✓ Branch 1 taken 154145192 times.
176694298 if (free_length == num_words) {
263 22549106 end_ix = alloc_offset;
264 22549106 state = ALLOC_DONE;
265 } else {
266 154145192 state = FREE_LENGTH_CHECK;
267 }
268 176694298 break;
269 47676745 case SKIP:
270 47676745 break;
271 }
272 247254349 break;
273 22554945 case END:
274 22554945 state = INIT;
275 22554945 break;
276 198110 case START:
277 198110 state = SKIP;
278 198110 break;
279 313390 default: // START_END
280 313390 state = INIT;
281 313390 break;
282 }
283
284
2/2
✓ Branch 0 taken 22857329 times.
✓ Branch 1 taken 247463465 times.
270320794 if (state == ALLOC_DONE) break;
285
286 247463465 alloc_offset++;
287
2/2
✓ Branch 0 taken 14191 times.
✓ Branch 1 taken 247449274 times.
247463465 if (alloc_offset == memory_size ) {
288 14191 free_length = 0;
289 14191 alloc_offset = 0;
290 14191 state = INIT;
291 }
292 }
293
294
2/2
✓ Branch 0 taken 22857329 times.
✓ Branch 1 taken 14173 times.
22871502 if (state == ALLOC_DONE) {
295
2/2
✓ Branch 0 taken 308223 times.
✓ Branch 1 taken 22549106 times.
22857329 if (start_ix == end_ix) {
296 308223 set_status(start_ix, START_END);
297 } else {
298 22549106 set_status(start_ix, START);
299 22549106 set_status(end_ix, END);
300 }
301 22857329 memory_num_free -= num_words;
302 22857329 lbm_mutex_unlock(&lbm_mem_mutex);
303 22857329 return bitmap_ix_to_address(start_ix);
304 }
305 14173 lbm_mutex_unlock(&lbm_mem_mutex);
306 14173 return NULL;
307 }
308
309 141611 lbm_uint *lbm_memory_allocate(lbm_uint num_words) {
310
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 141610 times.
141611 if (memory_num_free - num_words < memory_reserve_level) {
311 1 lbm_request_gc();
312 1 return NULL;
313 }
314 141610 return lbm_memory_allocate_internal(num_words);
315 }
316
317 22496766 int lbm_memory_free(lbm_uint *ptr) {
318 22496766 int r = 0;
319
2/2
✓ Branch 0 taken 22350596 times.
✓ Branch 1 taken 146170 times.
22496766 if (lbm_memory_ptr_inside(ptr)) {
320 22350596 lbm_mutex_lock(&lbm_mem_mutex);
321 22350596 lbm_uint ix = address_to_bitmap_ix(ptr);
322 22350596 lbm_uint count_freed = 0;
323 22350596 alloc_offset = ix;
324
3/3
✓ Branch 0 taken 22069551 times.
✓ Branch 1 taken 281043 times.
✓ Branch 2 taken 2 times.
22350596 switch(status(ix)) {
325 22069551 case START:
326 22069551 set_status(ix, FREE_OR_USED);
327
1/2
✓ Branch 0 taken 127255097 times.
✗ Branch 1 not taken.
127255097 for (lbm_uint i = ix; i < memory_size; i ++) {
328 127255097 count_freed ++;
329
2/2
✓ Branch 0 taken 22069551 times.
✓ Branch 1 taken 105185546 times.
127255097 if (status(i) == END) {
330 22069551 set_status(i, FREE_OR_USED);
331 22069551 r = 1;
332 22069551 break;
333 }
334 }
335 22069551 break;
336 281043 case START_END:
337 281043 set_status(ix, FREE_OR_USED);
338 281043 count_freed = 1;
339 281043 r = 1;
340 281043 break;
341 2 default:
342 2 break;
343 }
344
2/2
✓ Branch 0 taken 22350594 times.
✓ Branch 1 taken 2 times.
22350596 if (r) {
345
4/4
✓ Branch 0 taken 320579202 times.
✓ Branch 1 taken 1327 times.
✓ Branch 2 taken 298229935 times.
✓ Branch 3 taken 22349267 times.
320580529 while (alloc_offset > 0 && status(alloc_offset - 1) == FREE_OR_USED) {
346 298229935 alloc_offset--;
347 }
348 }
349 22350596 memory_num_free += count_freed;
350 22350596 lbm_mutex_unlock(&lbm_mem_mutex);
351 }
352 22496766 return r;
353 }
354 //Malloc/free like interface
355 22597624 void* lbm_malloc(size_t size) {
356
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 22597623 times.
22597624 if (size == 0) return NULL;
357 lbm_uint alloc_size;
358
359 22597623 alloc_size = size / sizeof(lbm_uint);
360
2/2
✓ Branch 0 taken 684558 times.
✓ Branch 1 taken 21913065 times.
22597623 if (size % sizeof(lbm_uint)) alloc_size += 1;
361
362
2/2
✓ Branch 0 taken 13215 times.
✓ Branch 1 taken 22584408 times.
22597623 if (memory_num_free - alloc_size < memory_reserve_level) {
363 13215 lbm_request_gc();
364 13215 return NULL;
365 }
366 22584408 return lbm_memory_allocate_internal(alloc_size);
367 }
368
369 145487 void* lbm_malloc_reserve(size_t size) {
370
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 145485 times.
145487 if (size == 0) return NULL;
371 lbm_uint alloc_size;
372
373 145485 alloc_size = size / sizeof(lbm_uint);
374
2/2
✓ Branch 0 taken 119116 times.
✓ Branch 1 taken 26369 times.
145485 if (size % sizeof(lbm_uint)) alloc_size += 1;
375
376
2/2
✓ Branch 0 taken 317 times.
✓ Branch 1 taken 145168 times.
145485 if (memory_num_free - alloc_size < memory_reserve_level) {
377 317 lbm_request_gc();
378 }
379 145485 return lbm_memory_allocate_internal(alloc_size);
380 }
381
382 118203 void lbm_free(void *ptr) {
383 118203 lbm_memory_free(ptr);
384 118203 }
385
386 28197 int lbm_memory_shrink(lbm_uint *ptr, lbm_uint n) {
387
4/4
✓ Branch 0 taken 28194 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 28190 times.
28197 if (!lbm_memory_ptr_inside(ptr) || n == 0) return 0;
388
389 28190 lbm_uint ix = address_to_bitmap_ix(ptr);
390
391 28190 lbm_mutex_lock(&lbm_mem_mutex);
392
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 28188 times.
28190 if (status(ix) == START_END) {
393 2 lbm_mutex_unlock(&lbm_mem_mutex);
394 2 return 1; // A one word arrays always succeeds at remaining at 1 word
395 }
396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28188 times.
28188 if (status(ix) != START) {
397 ✗ lbm_mutex_unlock(&lbm_mem_mutex);
398 ✗ return 0; // ptr does not point to the start of an allocated range.
399 }
400
401 28188 bool done = false;
402 28188 unsigned int i = 0;
403
404
1/2
✓ Branch 0 taken 293420 times.
✗ Branch 1 not taken.
293420 for (i = 0; i < (memory_size - ix); i ++) {
405
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 293419 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
293420 if (status(ix+i) == END && i < n) {
406 1 lbm_mutex_unlock(&lbm_mem_mutex);
407 1 return 0; // cannot shrink allocation to a larger size
408 }
409
410
2/2
✓ Branch 0 taken 28187 times.
✓ Branch 1 taken 265232 times.
293419 if (i == (n-1)) {
411
2/4
✓ Branch 0 taken 28187 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 28187 times.
56374 if (status(ix+i) == END ||
412 28187 status(ix+i) == START_END) {
413 ✗ done = true;
414 }
415
2/2
✓ Branch 0 taken 5271 times.
✓ Branch 1 taken 22916 times.
28187 if (i == 0) {
416 5271 set_status(ix+i, START_END);
417 }
418 else {
419 22916 set_status(ix+i, END);
420 }
421 28187 break;
422 }
423 }
424 28187 alloc_offset = ix+i;
425
426 28187 lbm_uint count = 0;
427
1/2
✓ Branch 0 taken 28187 times.
✗ Branch 1 not taken.
28187 if (!done) {
428 28187 i++; // move to next position, prev position should be END or START_END
429
1/2
✓ Branch 0 taken 28805698 times.
✗ Branch 1 not taken.
28805698 for (;i < (memory_size - ix); i ++) {
430 28805698 count ++;
431
2/2
✓ Branch 0 taken 28187 times.
✓ Branch 1 taken 28777511 times.
28805698 if (status(ix+i) == END) {
432 28187 set_status(ix+i, FREE_OR_USED);
433 28187 break;
434 }
435 }
436 }
437
438 28187 memory_num_free += count;
439 28187 lbm_mutex_unlock(&lbm_mem_mutex);
440 28187 return 1;
441 }
442
443 22536212 int lbm_memory_ptr_inside(lbm_uint *ptr) {
444
2/2
✓ Branch 0 taken 22378810 times.
✓ Branch 1 taken 157402 times.
44915022 return ((lbm_uint)ptr >= (lbm_uint)memory &&
445
2/2
✓ Branch 0 taken 22378801 times.
✓ Branch 1 taken 9 times.
22378810 (lbm_uint)ptr < (lbm_uint)memory + (memory_size * sizeof(lbm_uint)));
446 }
447