GCC Code Coverage Report


Directory: ../src/
File: lbm_memory.c
Date: 2026-02-08 10:07:39
Coverage Exec Excl Total
Lines: 97.6% 241 0 247
Functions: 100.0% 21 0 21
Branches: 85.3% 110 0 129

Line Branch Exec Source
1 /*
2 Copyright 2020 - 2026 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 132735 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 66488 times.
✓ Branch 1 taken 66247 times.
132735 if (!lbm_mem_mutex_initialized) {
63 66488 lbm_mutex_init(&lbm_mem_mutex);
64 66488 lbm_mem_mutex_initialized = true;
65 }
66
67 132735 alloc_offset = 0;
68
69 132735 lbm_mutex_lock(&lbm_mem_mutex);
70 132735 bool res = false;
71
4/4
✓ Branch 0 taken 132730 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 132727 times.
✓ Branch 3 taken 3 times.
132735 if (data && bits) {
72
73
1/2
✓ Branch 0 taken 132727 times.
✗ Branch 1 not taken.
132727 if (((lbm_uint)data % sizeof(lbm_uint) != 0) || // Alignment requirement
74
2/2
✓ Branch 0 taken 132722 times.
✓ Branch 1 taken 5 times.
132727 (data_size * 2) != (bits_size * sizeof(lbm_uint) * 8) ||
75
1/2
✓ Branch 0 taken 132722 times.
✗ Branch 1 not taken.
132722 data_size % 4 != 0 ||
76
2/4
✓ Branch 0 taken 132722 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 132722 times.
✗ Branch 3 not taken.
132722 ((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 132722 bitmap = bits;
85 132722 bitmap_size = bits_size;
86
87
2/2
✓ Branch 0 taken 207035996 times.
✓ Branch 1 taken 132722 times.
207168718 for (lbm_uint i = 0; i < bitmap_size; i ++) {
88 207035996 bitmap[i] = 0;
89 }
90
91 132722 memory = data;
92 132722 memory_base_address = (lbm_uint)data;
93 132722 memory_size = data_size;
94 132722 memory_min_free = data_size;
95 132722 memory_num_free = data_size;
96 132722 memory_reserve_level = (lbm_uint)(0.1 * (lbm_float)data_size);
97 132722 res = true;
98 }
99 }
100 132735 lbm_mutex_unlock(&lbm_mem_mutex);
101 132735 return res;
102 }
103
104 // TODO: how are set- and get_reserve used?
105 // should they use the mutex ? or is
106 // the expectation that these are used only at init time?
107 7 void lbm_memory_set_reserve(lbm_uint num_words) {
108 7 memory_reserve_level = num_words;
109 7 }
110
111 3 lbm_uint lbm_memory_get_reserve(void) {
112 3 return memory_reserve_level;
113 }
114
115 22453034 static inline lbm_uint address_to_bitmap_ix(lbm_uint *ptr) {
116 #ifndef LBM64
117 20582325 return ((lbm_uint)ptr - memory_base_address) >> 2;
118 #else
119 1870709 return ((lbm_uint)ptr - memory_base_address) >> 3;
120 #endif
121 }
122
123 69809 lbm_int lbm_memory_address_to_ix(lbm_uint *ptr) {
124 /* TODO: assuming that index
125 will have more then enough room in the
126 positive half of a 28bit integer */
127 69809 return (lbm_int)address_to_bitmap_ix(ptr);
128 }
129
130
131 22862083 static inline lbm_uint *bitmap_ix_to_address(lbm_uint ix) {
132 22862083 return &((lbm_uint*)(memory_base_address))[ix];// + (ix << 2));
133 }
134
135 #ifndef LBM64
136 #define WORD_IX_SHIFT 5
137 #define WORD_MOD_MASK 0x1F
138 #define BITMAP_SIZE_SHIFT 4 // 16 statuses per bitmap word
139 #else
140 #define WORD_IX_SHIFT 6 // divide by 64
141 #define WORD_MOD_MASK 0x3F // mod 64
142 #define BITMAP_SIZE_SHIFT 5 // times 32, 32 statuses per bitmap word
143 #endif
144
145 761959907 static inline lbm_uint status(lbm_uint i) {
146
147 761959907 lbm_uint ix = i << 1; // * 2
148 761959907 lbm_uint word_ix = ix >> WORD_IX_SHIFT; // / 32
149 761959907 lbm_uint bit_ix = ix & WORD_MOD_MASK; // % 32
150
151 761959907 lbm_uint mask = ((lbm_uint)3) << bit_ix; // 000110..0
152 761959907 return (bitmap[word_ix] & mask) >> bit_ix;
153 }
154
155 89901250 static inline void set_status(lbm_uint i, lbm_uint status) {
156 89901250 lbm_uint ix = i << 1; // * 2
157 89901250 lbm_uint word_ix = ix >> WORD_IX_SHIFT; // / 32
158 89901250 lbm_uint bit_ix = ix & WORD_MOD_MASK; // % 32
159
160 89901250 lbm_uint clr_mask = ~(((lbm_uint)3) << bit_ix);
161 89901250 lbm_uint mask = status << bit_ix;
162
163 89901250 bitmap[word_ix] &= clr_mask;
164 89901250 bitmap[word_ix] |= mask;
165 89901250 }
166
167 85 lbm_uint lbm_memory_num_words(void) {
168 85 return memory_size;
169 }
170
171 135432 lbm_uint lbm_memory_num_free(void) {
172 135432 return memory_num_free;
173 }
174
175 4 lbm_uint lbm_memory_maximum_used(void) {
176 4 return (memory_size - memory_min_free);
177 }
178
179 1606431 void lbm_memory_update_min_free(void) {
180
2/2
✓ Branch 0 taken 9533 times.
✓ Branch 1 taken 1596898 times.
1606431 if (memory_num_free < memory_min_free)
181 9533 memory_min_free = memory_num_free;
182 1606431 }
183
184 12417 lbm_uint lbm_memory_longest_free(void) {
185
3/4
✓ Branch 0 taken 12416 times.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 12416 times.
12417 if (memory == NULL || bitmap == NULL) {
186 1 return 0;
187 }
188 12416 lbm_mutex_lock(&lbm_mem_mutex);
189 12416 unsigned int state = INIT;
190 12416 lbm_uint max_length = 0;
191
192 12416 lbm_uint curr_length = 0;
193
2/2
✓ Branch 0 taken 50660096 times.
✓ Branch 1 taken 12416 times.
50672512 for (unsigned int i = 0; i < memory_size; i ++) {
194
195 // The status field is 2 bits and this 4 cases is exhaustive!
196
4/4
✓ Branch 1 taken 50281577 times.
✓ Branch 2 taken 182330 times.
✓ Branch 3 taken 182330 times.
✓ Branch 4 taken 13859 times.
50660096 switch(status(i)) {
197
3/4
✓ Branch 0 taken 14658 times.
✓ Branch 1 taken 39153586 times.
✓ Branch 2 taken 11113333 times.
✗ Branch 3 not taken.
50281577 case FREE_OR_USED:
198 switch (state) {
199 14658 case INIT:
200 14658 curr_length = 1;
201
2/2
✓ Branch 0 taken 12416 times.
✓ Branch 1 taken 2242 times.
14658 if (curr_length > max_length) max_length = curr_length;
202 14658 state = FREE_LENGTH_CHECK;
203 14658 break;
204 39153586 case FREE_LENGTH_CHECK:
205 39153586 curr_length ++;
206
2/2
✓ Branch 0 taken 39059737 times.
✓ Branch 1 taken 93849 times.
39153586 if (curr_length > max_length) max_length = curr_length;
207 39153586 state = FREE_LENGTH_CHECK;
208 39153586 break;
209 11113333 case SKIP:
210 11113333 break;
211 }
212 50281577 break;
213 182330 case END:
214 182330 state = INIT;
215 182330 break;
216 182330 case START:
217 182330 state = SKIP;
218 182330 break;
219 13859 default: // START_END
220 13859 state = INIT;
221 13859 break;
222 }
223 }
224 12416 lbm_mutex_unlock(&lbm_mem_mutex);
225 // max_length is computed from the status array
226 // and if we assume that the statemachine above and
227 // the maintaining of the status is correct,
228 // then max_length <= memory_num_free
229
2/2
✓ Branch 0 taken 12414 times.
✓ Branch 1 taken 2 times.
12416 if (memory_num_free - max_length < memory_reserve_level) {
230 12414 lbm_uint n = memory_reserve_level - (memory_num_free - max_length);
231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12414 times.
12414 if (n >= max_length) {
232 ✗ max_length = 0;
233 } else {
234 12414 max_length -= n;
235 }
236 }
237 12416 return max_length;
238 }
239
240 22889763 static lbm_uint *lbm_memory_allocate_internal(lbm_uint num_words, bool use_reserve) {
241
242
3/4
✓ Branch 0 taken 22889762 times.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 22889762 times.
22889763 if (memory == NULL || bitmap == NULL) {
243 1 return NULL;
244 }
245
246 22889762 lbm_mutex_lock(&lbm_mem_mutex);
247
248
2/2
✓ Branch 0 taken 14173 times.
✓ Branch 1 taken 22875589 times.
22889762 if (num_words > memory_num_free) {
249 14173 lbm_request_gc();
250 14173 lbm_mutex_unlock(&lbm_mem_mutex);
251 14173 return NULL;
252 }
253
254
4/4
✓ Branch 0 taken 22724097 times.
✓ Branch 1 taken 151492 times.
✓ Branch 2 taken 13506 times.
✓ Branch 3 taken 22710591 times.
22875589 if (!use_reserve && memory_num_free - num_words < memory_reserve_level) {
255 13506 lbm_request_gc();
256 13506 lbm_mutex_unlock(&lbm_mem_mutex);
257 13506 return NULL;
258 }
259
260 22862083 lbm_uint start_ix = 0;
261 22862083 lbm_uint end_ix = 0;
262 22862083 lbm_uint free_length = 0;
263 22862083 unsigned int state = INIT;
264
265
1/2
✓ Branch 0 taken 212474941 times.
✗ Branch 1 not taken.
212474941 for (lbm_uint i = 0; i < memory_size; i ++) {
266
4/4
✓ Branch 1 taken 189705803 times.
✓ Branch 2 taken 22407866 times.
✓ Branch 3 taken 47597 times.
✓ Branch 4 taken 313675 times.
212474941 switch(status(alloc_offset)) {
267
3/4
✓ Branch 0 taken 22867143 times.
✓ Branch 1 taken 166106497 times.
✓ Branch 2 taken 732163 times.
✗ Branch 3 not taken.
189705803 case FREE_OR_USED:
268 switch (state) {
269 22867143 case INIT:
270 22867143 start_ix = alloc_offset;
271
2/2
✓ Branch 0 taken 308274 times.
✓ Branch 1 taken 22558869 times.
22867143 if (num_words == 1) {
272 308274 end_ix = alloc_offset;
273 308274 state = ALLOC_DONE;
274 } else {
275 22558869 state = FREE_LENGTH_CHECK;
276 22558869 free_length = 1;
277 }
278 22867143 break;
279 166106497 case FREE_LENGTH_CHECK:
280 166106497 free_length ++;
281
2/2
✓ Branch 0 taken 22553809 times.
✓ Branch 1 taken 143552688 times.
166106497 if (free_length == num_words) {
282 22553809 end_ix = alloc_offset;
283 22553809 state = ALLOC_DONE;
284 } else {
285 143552688 state = FREE_LENGTH_CHECK;
286 }
287 166106497 break;
288 732163 case SKIP:
289 732163 break;
290 }
291 189705803 break;
292 22407866 case END:
293 22407866 state = INIT;
294 22407866 break;
295 47597 case START:
296 47597 state = SKIP;
297 47597 break;
298 313675 default: // START_END
299 313675 state = INIT;
300 313675 break;
301 }
302
303
2/2
✓ Branch 0 taken 22862083 times.
✓ Branch 1 taken 189612858 times.
212474941 if (state == ALLOC_DONE) break;
304
305 189612858 alloc_offset++;
306
2/2
✓ Branch 0 taken 133 times.
✓ Branch 1 taken 189612725 times.
189612858 if (alloc_offset == memory_size ) {
307 133 free_length = 0;
308 133 alloc_offset = 0;
309 133 state = INIT;
310 }
311 }
312
313
1/2
✓ Branch 0 taken 22862083 times.
✗ Branch 1 not taken.
22862083 if (state == ALLOC_DONE) {
314
2/2
✓ Branch 0 taken 308274 times.
✓ Branch 1 taken 22553809 times.
22862083 if (start_ix == end_ix) {
315 308274 set_status(start_ix, START_END);
316 } else {
317 22553809 set_status(start_ix, START);
318 22553809 set_status(end_ix, END);
319 }
320 22862083 memory_num_free -= num_words;
321 22862083 lbm_mutex_unlock(&lbm_mem_mutex);
322 22862083 return bitmap_ix_to_address(start_ix);
323 }
324 ✗ lbm_mutex_unlock(&lbm_mem_mutex);
325 ✗ return NULL;
326 }
327
328 141632 lbm_uint *lbm_memory_allocate(lbm_uint num_words) {
329 141632 return lbm_memory_allocate_internal(num_words, false);
330 }
331
332 22495339 int lbm_memory_free(lbm_uint *ptr) {
333 22495339 int r = 0;
334
2/2
✓ Branch 1 taken 22349047 times.
✓ Branch 2 taken 146292 times.
22495339 if (lbm_memory_ptr_inside(ptr)) {
335 22349047 lbm_mutex_lock(&lbm_mem_mutex);
336 22349047 lbm_uint ix = address_to_bitmap_ix(ptr);
337 22349047 lbm_uint count_freed = 0;
338 22349047 alloc_offset = ix;
339
3/3
✓ Branch 1 taken 22067963 times.
✓ Branch 2 taken 281082 times.
✓ Branch 3 taken 2 times.
22349047 switch(status(ix)) {
340 22067963 case START:
341 22067963 set_status(ix, FREE_OR_USED);
342
1/2
✓ Branch 0 taken 127320951 times.
✗ Branch 1 not taken.
127320951 for (lbm_uint i = ix; i < memory_size; i ++) {
343 127320951 count_freed ++;
344
2/2
✓ Branch 1 taken 22067963 times.
✓ Branch 2 taken 105252988 times.
127320951 if (status(i) == END) {
345 22067963 set_status(i, FREE_OR_USED);
346 22067963 r = 1;
347 22067963 break;
348 }
349 }
350 22067963 break;
351 281082 case START_END:
352 281082 set_status(ix, FREE_OR_USED);
353 281082 count_freed = 1;
354 281082 r = 1;
355 281082 break;
356 2 default:
357 2 break;
358 }
359
2/2
✓ Branch 0 taken 22349045 times.
✓ Branch 1 taken 2 times.
22349047 if (r) {
360
4/4
✓ Branch 0 taken 319897682 times.
✓ Branch 1 taken 1327 times.
✓ Branch 3 taken 297549964 times.
✓ Branch 4 taken 22347718 times.
319899009 while (alloc_offset > 0 && status(alloc_offset - 1) == FREE_OR_USED) {
361 297549964 alloc_offset--;
362 }
363 }
364 22349047 memory_num_free += count_freed;
365 22349047 lbm_mutex_unlock(&lbm_mem_mutex);
366 }
367 22495339 return r;
368 }
369 //Malloc/free like interface
370 22596640 void* lbm_malloc(size_t size) {
371
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 22596639 times.
22596640 if (size == 0) return NULL;
372 lbm_uint alloc_size;
373
374 22596639 alloc_size = size / sizeof(lbm_uint);
375
2/2
✓ Branch 0 taken 680351 times.
✓ Branch 1 taken 21916288 times.
22596639 if (size % sizeof(lbm_uint)) alloc_size += 1;
376
377 22596639 return lbm_memory_allocate_internal(alloc_size, false);
378 }
379
380 151494 void* lbm_malloc_reserve(size_t size) {
381
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 151492 times.
151494 if (size == 0) return NULL;
382 lbm_uint alloc_size;
383
384 151492 alloc_size = size / sizeof(lbm_uint);
385
2/2
✓ Branch 0 taken 125131 times.
✓ Branch 1 taken 26361 times.
151492 if (size % sizeof(lbm_uint)) alloc_size += 1;
386
387 151492 return lbm_memory_allocate_internal(alloc_size, true);
388 }
389
390 119048 void lbm_free(void *ptr) {
391 119048 lbm_memory_free(ptr);
392 119048 }
393
394 34185 int lbm_memory_shrink(lbm_uint *ptr, lbm_uint n) {
395
4/4
✓ Branch 1 taken 34182 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 34178 times.
34185 if (!lbm_memory_ptr_inside(ptr) || n == 0) return 0;
396
397 34178 lbm_uint ix = address_to_bitmap_ix(ptr);
398
399 34178 lbm_mutex_lock(&lbm_mem_mutex);
400
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 34176 times.
34178 if (status(ix) == START_END) {
401 2 lbm_mutex_unlock(&lbm_mem_mutex);
402 2 return 1; // A one word arrays always succeeds at remaining at 1 word
403 }
404
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 34176 times.
34176 if (status(ix) != START) {
405 ✗ lbm_mutex_unlock(&lbm_mem_mutex);
406 ✗ return 0; // ptr does not point to the start of an allocated range.
407 }
408
409 34176 bool done = false;
410 34176 unsigned int i = 0;
411
412
1/2
✓ Branch 0 taken 304157 times.
✗ Branch 1 not taken.
304157 for (i = 0; i < (memory_size - ix); i ++) {
413
3/4
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 304156 times.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
304157 if (status(ix+i) == END && i < n) {
414 1 lbm_mutex_unlock(&lbm_mem_mutex);
415 1 return 0; // cannot shrink allocation to a larger size
416 }
417
418
2/2
✓ Branch 0 taken 34175 times.
✓ Branch 1 taken 269981 times.
304156 if (i == (n-1)) {
419
2/4
✓ Branch 1 taken 34175 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 34175 times.
68350 if (status(ix+i) == END ||
420 34175 status(ix+i) == START_END) {
421 ✗ done = true;
422 }
423
2/2
✓ Branch 0 taken 6510 times.
✓ Branch 1 taken 27665 times.
34175 if (i == 0) {
424 6510 set_status(ix+i, START_END);
425 }
426 else {
427 27665 set_status(ix+i, END);
428 }
429 34175 break;
430 }
431 }
432 34175 alloc_offset = ix+i;
433
434 34175 lbm_uint count = 0;
435
1/2
✓ Branch 0 taken 34175 times.
✗ Branch 1 not taken.
34175 if (!done) {
436 34175 i++; // move to next position, prev position should be END or START_END
437
1/2
✓ Branch 0 taken 28816329 times.
✗ Branch 1 not taken.
28816329 for (;i < (memory_size - ix); i ++) {
438 28816329 count ++;
439
2/2
✓ Branch 1 taken 34175 times.
✓ Branch 2 taken 28782154 times.
28816329 if (status(ix+i) == END) {
440 34175 set_status(ix+i, FREE_OR_USED);
441 34175 break;
442 }
443 }
444 }
445
446 34175 memory_num_free += count;
447 34175 lbm_mutex_unlock(&lbm_mem_mutex);
448 34175 return 1;
449 }
450
451 22541221 int lbm_memory_ptr_inside(lbm_uint *ptr) {
452
2/2
✓ Branch 0 taken 22383249 times.
✓ Branch 1 taken 157972 times.
44924470 return ((lbm_uint)ptr >= (lbm_uint)memory &&
453
2/2
✓ Branch 0 taken 22383240 times.
✓ Branch 1 taken 9 times.
22383249 (lbm_uint)ptr < (lbm_uint)&memory[memory_size]);
454 }
455