GCC Code Coverage Report


Directory: ../src/
File: lbm_memory.c
Date: 2026-09-06 17:54:43
Coverage Exec Excl Total
Lines: 97.6% 240 0 246
Functions: 100.0% 21 0 21
Branches: 86.4% 108 0 125

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 133326 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 66992 times.
✓ Branch 1 taken 66334 times.
133326 if (!lbm_mem_mutex_initialized) {
63 66992 lbm_mutex_init(&lbm_mem_mutex);
64 66992 lbm_mem_mutex_initialized = true;
65 }
66
67 133326 alloc_offset = 0;
68
69 133326 lbm_mutex_lock(&lbm_mem_mutex);
70 133326 bool res = false;
71
4/4
✓ Branch 0 taken 133321 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 133318 times.
✓ Branch 3 taken 3 times.
133326 if (data && bits) {
72
73 // Data_size, size of the memory in words.
74 // Every word of memory needs 2x status bits.
75 // so bitmap bits_size should be 2x BITS compared to num words data.
76 // Really the number of bits needed should be less than the number of bits we have.
77
78
1/2
✓ Branch 0 taken 133318 times.
✗ Branch 1 not taken.
133318 if ((lbm_uint)data % sizeof(lbm_uint) == 0 && // Alignment req data.
79
1/2
✓ Branch 0 taken 133318 times.
✗ Branch 1 not taken.
133318 (lbm_uint)bits % sizeof(lbm_uint) == 0 && // Alignment req bitmap.
80
2/2
✓ Branch 0 taken 133313 times.
✓ Branch 1 taken 5 times.
133318 data_size *2 <= (bits_size * sizeof(lbm_uint)) * 8) { // size requirement.
81 133313 bitmap = bits;
82 133313 bitmap_size = bits_size;
83
84
2/2
✓ Branch 0 taken 207770908 times.
✓ Branch 1 taken 133313 times.
207904221 for (lbm_uint i = 0; i < bitmap_size; i ++) {
85 207770908 bitmap[i] = 0;
86 }
87
88 133313 memory = data;
89 133313 memory_base_address = (lbm_uint)data;
90 133313 memory_size = data_size;
91 133313 memory_min_free = data_size;
92 133313 memory_num_free = data_size;
93 133313 memory_reserve_level = (lbm_uint)(0.1 * (lbm_float)data_size);
94 133313 res = true;
95 }
96 }
97 133326 lbm_mutex_unlock(&lbm_mem_mutex);
98 133326 return res;
99 }
100
101 // TODO: how are set- and get_reserve used?
102 // should they use the mutex ? or is
103 // the expectation that these are used only at init time?
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 22512294 static inline lbm_uint address_to_bitmap_ix(lbm_uint *ptr) {
113 #ifndef LBM64
114 20641464 return ((lbm_uint)ptr - memory_base_address) >> 2;
115 #else
116 1870830 return ((lbm_uint)ptr - memory_base_address) >> 3;
117 #endif
118 }
119
120 70607 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 70607 return (lbm_int)address_to_bitmap_ix(ptr);
125 }
126
127
128 22934518 static inline lbm_uint *bitmap_ix_to_address(lbm_uint ix) {
129 22934518 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 769284264 static inline lbm_uint status(lbm_uint i) {
143
144 769284264 lbm_uint ix = i << 1; // * 2
145 769284264 lbm_uint word_ix = ix >> WORD_IX_SHIFT; // / 32
146 769284264 lbm_uint bit_ix = ix & WORD_MOD_MASK; // % 32
147
148 769284264 lbm_uint mask = ((lbm_uint)3) << bit_ix; // 000110..0
149 769284264 return (bitmap[word_ix] & mask) >> bit_ix;
150 }
151
152 90120732 static inline void set_status(lbm_uint i, lbm_uint status) {
153 90120732 lbm_uint ix = i << 1; // * 2
154 90120732 lbm_uint word_ix = ix >> WORD_IX_SHIFT; // / 32
155 90120732 lbm_uint bit_ix = ix & WORD_MOD_MASK; // % 32
156
157 90120732 lbm_uint clr_mask = ~(((lbm_uint)3) << bit_ix);
158 90120732 lbm_uint mask = status << bit_ix;
159
160 90120732 bitmap[word_ix] &= clr_mask;
161 90120732 bitmap[word_ix] |= mask;
162 90120732 }
163
164 85 lbm_uint lbm_memory_num_words(void) {
165 85 return memory_size;
166 }
167
168 135602 lbm_uint lbm_memory_num_free(void) {
169 135602 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 1612280 void lbm_memory_update_min_free(void) {
177
2/2
✓ Branch 0 taken 9649 times.
✓ Branch 1 taken 1602631 times.
1612280 if (memory_num_free < memory_min_free)
178 9649 memory_min_free = memory_num_free;
179 1612280 }
180
181 12695 lbm_uint lbm_memory_longest_free(void) {
182
3/4
✓ Branch 0 taken 12694 times.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 12694 times.
12695 if (memory == NULL || bitmap == NULL) {
183 1 return 0;
184 }
185 12694 lbm_mutex_lock(&lbm_mem_mutex);
186 12694 unsigned int state = INIT;
187 12694 lbm_uint max_length = 0;
188
189 12694 lbm_uint curr_length = 0;
190
2/2
✓ Branch 0 taken 51631360 times.
✓ Branch 1 taken 12694 times.
51644054 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 1 taken 51233703 times.
✓ Branch 2 taken 191125 times.
✓ Branch 3 taken 191125 times.
✓ Branch 4 taken 15407 times.
51631360 switch(status(i)) {
194
3/4
✓ Branch 0 taken 14948 times.
✓ Branch 1 taken 39740073 times.
✓ Branch 2 taken 11478682 times.
✗ Branch 3 not taken.
51233703 case FREE_OR_USED:
195 switch (state) {
196 14948 case INIT:
197 14948 curr_length = 1;
198
2/2
✓ Branch 0 taken 12694 times.
✓ Branch 1 taken 2254 times.
14948 if (curr_length > max_length) max_length = curr_length;
199 14948 state = FREE_LENGTH_CHECK;
200 14948 break;
201 39740073 case FREE_LENGTH_CHECK:
202 39740073 curr_length ++;
203
2/2
✓ Branch 0 taken 39644428 times.
✓ Branch 1 taken 95645 times.
39740073 if (curr_length > max_length) max_length = curr_length;
204 39740073 state = FREE_LENGTH_CHECK;
205 39740073 break;
206 11478682 case SKIP:
207 11478682 break;
208 }
209 51233703 break;
210 191125 case END:
211 191125 state = INIT;
212 191125 break;
213 191125 case START:
214 191125 state = SKIP;
215 191125 break;
216 15407 default: // START_END
217 15407 state = INIT;
218 15407 break;
219 }
220 }
221 12694 lbm_mutex_unlock(&lbm_mem_mutex);
222 // max_length is computed from the status array
223 // and if we assume that the statemachine above and
224 // the maintaining of the status is correct,
225 // then max_length <= memory_num_free
226
2/2
✓ Branch 0 taken 12688 times.
✓ Branch 1 taken 6 times.
12694 if (memory_num_free - max_length < memory_reserve_level) {
227 12688 lbm_uint n = memory_reserve_level - (memory_num_free - max_length);
228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12688 times.
12688 if (n >= max_length) {
229 ✗ max_length = 0;
230 } else {
231 12688 max_length -= n;
232 }
233 }
234 12694 return max_length;
235 }
236
237 22963204 static lbm_uint *lbm_memory_allocate_internal(lbm_uint num_words, bool use_reserve) {
238
239
3/4
✓ Branch 0 taken 22963203 times.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 22963203 times.
22963204 if (memory == NULL || bitmap == NULL) {
240 1 return NULL;
241 }
242
243 22963203 lbm_mutex_lock(&lbm_mem_mutex);
244
245
2/2
✓ Branch 0 taken 14177 times.
✓ Branch 1 taken 22949026 times.
22963203 if (num_words > memory_num_free) {
246 14177 lbm_request_gc();
247 14177 lbm_mutex_unlock(&lbm_mem_mutex);
248 14177 return NULL;
249 }
250
251
4/4
✓ Branch 0 taken 22796549 times.
✓ Branch 1 taken 152477 times.
✓ Branch 2 taken 14508 times.
✓ Branch 3 taken 22782041 times.
22949026 if (!use_reserve && memory_num_free - num_words < memory_reserve_level) {
252 14508 lbm_request_gc();
253 14508 lbm_mutex_unlock(&lbm_mem_mutex);
254 14508 return NULL;
255 }
256
257 22934518 lbm_uint start_ix = 0;
258 22934518 lbm_uint end_ix = 0;
259 22934518 lbm_uint free_length = 0;
260 22934518 unsigned int state = INIT;
261
262
1/2
✓ Branch 0 taken 215775546 times.
✗ Branch 1 not taken.
215775546 for (lbm_uint i = 0; i < memory_size; i ++) {
263
4/4
✓ Branch 1 taken 192933899 times.
✓ Branch 2 taken 22456143 times.
✓ Branch 3 taken 49627 times.
✓ Branch 4 taken 335877 times.
215775546 switch(status(alloc_offset)) {
264
3/4
✓ Branch 0 taken 22939611 times.
✓ Branch 1 taken 169103681 times.
✓ Branch 2 taken 890607 times.
✗ Branch 3 not taken.
192933899 case FREE_OR_USED:
265 switch (state) {
266 22939611 case INIT:
267 22939611 start_ix = alloc_offset;
268
2/2
✓ Branch 0 taken 330551 times.
✓ Branch 1 taken 22609060 times.
22939611 if (num_words == 1) {
269 330551 end_ix = alloc_offset;
270 330551 state = ALLOC_DONE;
271 } else {
272 22609060 state = FREE_LENGTH_CHECK;
273 22609060 free_length = 1;
274 }
275 22939611 break;
276 169103681 case FREE_LENGTH_CHECK:
277 169103681 free_length ++;
278
2/2
✓ Branch 0 taken 22603967 times.
✓ Branch 1 taken 146499714 times.
169103681 if (free_length == num_words) {
279 22603967 end_ix = alloc_offset;
280 22603967 state = ALLOC_DONE;
281 } else {
282 146499714 state = FREE_LENGTH_CHECK;
283 }
284 169103681 break;
285 890607 case SKIP:
286 890607 break;
287 }
288 192933899 break;
289 22456143 case END:
290 22456143 state = INIT;
291 22456143 break;
292 49627 case START:
293 49627 state = SKIP;
294 49627 break;
295 335877 default: // START_END
296 335877 state = INIT;
297 335877 break;
298 }
299
300
2/2
✓ Branch 0 taken 22934518 times.
✓ Branch 1 taken 192841028 times.
215775546 if (state == ALLOC_DONE) break;
301
302 192841028 alloc_offset++;
303
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 192840919 times.
192841028 if (alloc_offset == memory_size ) {
304 109 free_length = 0;
305 109 alloc_offset = 0;
306 109 state = INIT;
307 }
308 }
309
310
1/2
✓ Branch 0 taken 22934518 times.
✗ Branch 1 not taken.
22934518 if (state == ALLOC_DONE) {
311
2/2
✓ Branch 0 taken 330551 times.
✓ Branch 1 taken 22603967 times.
22934518 if (start_ix == end_ix) {
312 330551 set_status(start_ix, START_END);
313 } else {
314 22603967 set_status(start_ix, START);
315 22603967 set_status(end_ix, END);
316 }
317 22934518 memory_num_free -= num_words;
318 22934518 lbm_mutex_unlock(&lbm_mem_mutex);
319 22934518 return bitmap_ix_to_address(start_ix);
320 }
321 ✗ lbm_mutex_unlock(&lbm_mem_mutex);
322 ✗ return NULL;
323 }
324
325 143333 lbm_uint *lbm_memory_allocate(lbm_uint num_words) {
326 143333 return lbm_memory_allocate_internal(num_words, false);
327 }
328
329 22554854 int lbm_memory_free(lbm_uint *ptr) {
330 22554854 int r = 0;
331
2/2
✓ Branch 1 taken 22407205 times.
✓ Branch 2 taken 147649 times.
22554854 if (lbm_memory_ptr_inside(ptr)) {
332 22407205 lbm_mutex_lock(&lbm_mem_mutex);
333 22407205 lbm_uint ix = address_to_bitmap_ix(ptr);
334 22407205 lbm_uint count_freed = 0;
335 22407205 alloc_offset = ix;
336
3/3
✓ Branch 1 taken 22106088 times.
✓ Branch 2 taken 301113 times.
✓ Branch 3 taken 4 times.
22407205 switch(status(ix)) {
337 22106088 case START:
338 22106088 set_status(ix, FREE_OR_USED);
339
1/2
✓ Branch 0 taken 128366082 times.
✗ Branch 1 not taken.
128366082 for (lbm_uint i = ix; i < memory_size; i ++) {
340 128366082 count_freed ++;
341
2/2
✓ Branch 1 taken 22106088 times.
✓ Branch 2 taken 106259994 times.
128366082 if (status(i) == END) {
342 22106088 set_status(i, FREE_OR_USED);
343 22106088 r = 1;
344 22106088 break;
345 }
346 }
347 22106088 break;
348 301113 case START_END:
349 301113 set_status(ix, FREE_OR_USED);
350 301113 count_freed = 1;
351 301113 r = 1;
352 301113 break;
353 4 default:
354 4 break;
355 }
356
2/2
✓ Branch 0 taken 22407201 times.
✓ Branch 1 taken 4 times.
22407205 if (r) {
357
4/4
✓ Branch 0 taken 321416374 times.
✓ Branch 1 taken 1319 times.
✓ Branch 3 taken 299010492 times.
✓ Branch 4 taken 22405882 times.
321417693 while (alloc_offset > 0 && status(alloc_offset - 1) == FREE_OR_USED) {
358 299010492 alloc_offset--;
359 }
360 }
361 22407205 memory_num_free += count_freed;
362 22407205 lbm_mutex_unlock(&lbm_mem_mutex);
363 }
364 22554854 return r;
365 }
366 //Malloc/free like interface
367 22667395 void* lbm_malloc(size_t size) {
368
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 22667394 times.
22667395 if (size == 0) return NULL;
369 lbm_uint alloc_size;
370
371 22667394 alloc_size = size / sizeof(lbm_uint);
372
2/2
✓ Branch 0 taken 684246 times.
✓ Branch 1 taken 21983148 times.
22667394 if (size % sizeof(lbm_uint)) alloc_size += 1;
373
374 22667394 return lbm_memory_allocate_internal(alloc_size, false);
375 }
376
377 152479 void* lbm_malloc_reserve(size_t size) {
378
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 152477 times.
152479 if (size == 0) return NULL;
379 lbm_uint alloc_size;
380
381 152477 alloc_size = size / sizeof(lbm_uint);
382
2/2
✓ Branch 0 taken 125175 times.
✓ Branch 1 taken 27302 times.
152477 if (size % sizeof(lbm_uint)) alloc_size += 1;
383
384 152477 return lbm_memory_allocate_internal(alloc_size, true);
385 }
386
387 120579 void lbm_free(void *ptr) {
388 120579 lbm_memory_free(ptr);
389 120579 }
390
391
392 34487 bool lbm_memory_shrink(lbm_uint *ptr, lbm_uint n) {
393
4/4
✓ Branch 1 taken 34484 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 34482 times.
34487 if (!lbm_memory_ptr_inside(ptr) || n == 0) return 0;
394
395 34482 lbm_uint ix = address_to_bitmap_ix(ptr);
396
397 34482 lbm_mutex_lock(&lbm_mem_mutex);
398
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 34480 times.
34482 if (status(ix) == START_END) {
399 2 lbm_mutex_unlock(&lbm_mem_mutex);
400 2 return true; // A one word arrays always succeeds at remaining at 1 word
401 }
402
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 34480 times.
34480 if (status(ix) != START) {
403 ✗ lbm_mutex_unlock(&lbm_mem_mutex);
404 ✗ return false; // ptr does not point to the start of an allocated range.
405 }
406
407 34480 bool done = false;
408 34480 unsigned int i = 0;
409
410
1/2
✓ Branch 0 taken 305026 times.
✗ Branch 1 not taken.
305026 for (i = 0; i < (memory_size - ix); i ++) {
411
3/4
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 305025 times.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
305026 if (status(ix+i) == END && i < (n - 1)) {
412 1 lbm_mutex_unlock(&lbm_mem_mutex);
413 1 return false; // cannot shrink allocation to a larger size
414 }
415
416
2/2
✓ Branch 0 taken 34479 times.
✓ Branch 1 taken 270546 times.
305025 if (i == (n-1)) {
417
2/4
✓ Branch 1 taken 34479 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 34479 times.
68958 if (status(ix+i) == END ||
418 34479 status(ix+i) == START_END) {
419 // Shrink to current size case.
420 ✗ done = true;
421 }
422
2/2
✓ Branch 0 taken 6532 times.
✓ Branch 1 taken 27947 times.
34479 if (i == 0) {
423 6532 set_status(ix+i, START_END);
424 }
425 else {
426 27947 set_status(ix+i, END);
427 }
428 34479 break;
429 }
430 }
431 34479 alloc_offset = ix+i;
432
433 34479 lbm_uint count = 0;
434
1/2
✓ Branch 0 taken 34479 times.
✗ Branch 1 not taken.
34479 if (!done) {
435 34479 i++; // move to next position, prev position should be END or START_END
436
1/2
✓ Branch 0 taken 29244751 times.
✗ Branch 1 not taken.
29244751 for (;i < (memory_size - ix); i ++) {
437 29244751 count ++;
438
2/2
✓ Branch 1 taken 34479 times.
✓ Branch 2 taken 29210272 times.
29244751 if (status(ix+i) == END) {
439 34479 set_status(ix+i, FREE_OR_USED);
440 34479 break;
441 }
442 }
443 }
444
445 34479 memory_num_free += count;
446 34479 lbm_mutex_unlock(&lbm_mem_mutex);
447 34479 return true;
448 }
449
450 22606385 int lbm_memory_ptr_inside(lbm_uint *ptr) {
451
2/2
✓ Branch 0 taken 22441710 times.
✓ Branch 1 taken 164675 times.
45048095 return ((lbm_uint)ptr >= (lbm_uint)memory &&
452
2/2
✓ Branch 0 taken 22441701 times.
✓ Branch 1 taken 9 times.
22441710 (lbm_uint)ptr < (lbm_uint)&memory[memory_size]);
453 }
454