GCC Code Coverage Report


Directory: ../src/
File: lbm_memory.c
Date: 2026-07-02 10:10:49
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 132738 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 66501 times.
✓ Branch 1 taken 66237 times.
132738 if (!lbm_mem_mutex_initialized) {
63 66501 lbm_mutex_init(&lbm_mem_mutex);
64 66501 lbm_mem_mutex_initialized = true;
65 }
66
67 132738 alloc_offset = 0;
68
69 132738 lbm_mutex_lock(&lbm_mem_mutex);
70 132738 bool res = false;
71
4/4
✓ Branch 0 taken 132733 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 132730 times.
✓ Branch 3 taken 3 times.
132738 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 132730 times.
✗ Branch 1 not taken.
132730 if ((lbm_uint)data % sizeof(lbm_uint) == 0 && // Alignment req data.
79
1/2
✓ Branch 0 taken 132730 times.
✗ Branch 1 not taken.
132730 (lbm_uint)bits % sizeof(lbm_uint) == 0 && // Alignment req bitmap.
80
2/2
✓ Branch 0 taken 132725 times.
✓ Branch 1 taken 5 times.
132730 data_size *2 <= (bits_size * sizeof(lbm_uint)) * 8) { // size requirement.
81 132725 bitmap = bits;
82 132725 bitmap_size = bits_size;
83
84
2/2
✓ Branch 0 taken 207085148 times.
✓ Branch 1 taken 132725 times.
207217873 for (lbm_uint i = 0; i < bitmap_size; i ++) {
85 207085148 bitmap[i] = 0;
86 }
87
88 132725 memory = data;
89 132725 memory_base_address = (lbm_uint)data;
90 132725 memory_size = data_size;
91 132725 memory_min_free = data_size;
92 132725 memory_num_free = data_size;
93 132725 memory_reserve_level = (lbm_uint)(0.1 * (lbm_float)data_size);
94 132725 res = true;
95 }
96 }
97 132738 lbm_mutex_unlock(&lbm_mem_mutex);
98 132738 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 22453436 static inline lbm_uint address_to_bitmap_ix(lbm_uint *ptr) {
113 #ifndef LBM64
114 20582544 return ((lbm_uint)ptr - memory_base_address) >> 2;
115 #else
116 1870892 return ((lbm_uint)ptr - memory_base_address) >> 3;
117 #endif
118 }
119
120 69827 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 69827 return (lbm_int)address_to_bitmap_ix(ptr);
125 }
126
127
128 22863497 static inline lbm_uint *bitmap_ix_to_address(lbm_uint ix) {
129 22863497 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 762381797 static inline lbm_uint status(lbm_uint i) {
143
144 762381797 lbm_uint ix = i << 1; // * 2
145 762381797 lbm_uint word_ix = ix >> WORD_IX_SHIFT; // / 32
146 762381797 lbm_uint bit_ix = ix & WORD_MOD_MASK; // % 32
147
148 762381797 lbm_uint mask = ((lbm_uint)3) << bit_ix; // 000110..0
149 762381797 return (bitmap[word_ix] & mask) >> bit_ix;
150 }
151
152 89904543 static inline void set_status(lbm_uint i, lbm_uint status) {
153 89904543 lbm_uint ix = i << 1; // * 2
154 89904543 lbm_uint word_ix = ix >> WORD_IX_SHIFT; // / 32
155 89904543 lbm_uint bit_ix = ix & WORD_MOD_MASK; // % 32
156
157 89904543 lbm_uint clr_mask = ~(((lbm_uint)3) << bit_ix);
158 89904543 lbm_uint mask = status << bit_ix;
159
160 89904543 bitmap[word_ix] &= clr_mask;
161 89904543 bitmap[word_ix] |= mask;
162 89904543 }
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 1606411 void lbm_memory_update_min_free(void) {
177
2/2
✓ Branch 0 taken 9575 times.
✓ Branch 1 taken 1596836 times.
1606411 if (memory_num_free < memory_min_free)
178 9575 memory_min_free = memory_num_free;
179 1606411 }
180
181 12458 lbm_uint lbm_memory_longest_free(void) {
182
3/4
✓ Branch 0 taken 12457 times.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 12457 times.
12458 if (memory == NULL || bitmap == NULL) {
183 1 return 0;
184 }
185 12457 lbm_mutex_lock(&lbm_mem_mutex);
186 12457 unsigned int state = INIT;
187 12457 lbm_uint max_length = 0;
188
189 12457 lbm_uint curr_length = 0;
190
2/2
✓ Branch 0 taken 50765056 times.
✓ Branch 1 taken 12457 times.
50777513 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 50381971 times.
✓ Branch 2 taken 184392 times.
✓ Branch 3 taken 184392 times.
✓ Branch 4 taken 14301 times.
50765056 switch(status(i)) {
194
3/4
✓ Branch 0 taken 14699 times.
✓ Branch 1 taken 39216292 times.
✓ Branch 2 taken 11150980 times.
✗ Branch 3 not taken.
50381971 case FREE_OR_USED:
195 switch (state) {
196 14699 case INIT:
197 14699 curr_length = 1;
198
2/2
✓ Branch 0 taken 12457 times.
✓ Branch 1 taken 2242 times.
14699 if (curr_length > max_length) max_length = curr_length;
199 14699 state = FREE_LENGTH_CHECK;
200 14699 break;
201 39216292 case FREE_LENGTH_CHECK:
202 39216292 curr_length ++;
203
2/2
✓ Branch 0 taken 39122443 times.
✓ Branch 1 taken 93849 times.
39216292 if (curr_length > max_length) max_length = curr_length;
204 39216292 state = FREE_LENGTH_CHECK;
205 39216292 break;
206 11150980 case SKIP:
207 11150980 break;
208 }
209 50381971 break;
210 184392 case END:
211 184392 state = INIT;
212 184392 break;
213 184392 case START:
214 184392 state = SKIP;
215 184392 break;
216 14301 default: // START_END
217 14301 state = INIT;
218 14301 break;
219 }
220 }
221 12457 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 12455 times.
✓ Branch 1 taken 2 times.
12457 if (memory_num_free - max_length < memory_reserve_level) {
227 12455 lbm_uint n = memory_reserve_level - (memory_num_free - max_length);
228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12455 times.
12455 if (n >= max_length) {
229 ✗ max_length = 0;
230 } else {
231 12455 max_length -= n;
232 }
233 }
234 12457 return max_length;
235 }
236
237 22891176 static lbm_uint *lbm_memory_allocate_internal(lbm_uint num_words, bool use_reserve) {
238
239
3/4
✓ Branch 0 taken 22891175 times.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 22891175 times.
22891176 if (memory == NULL || bitmap == NULL) {
240 1 return NULL;
241 }
242
243 22891175 lbm_mutex_lock(&lbm_mem_mutex);
244
245
2/2
✓ Branch 0 taken 14173 times.
✓ Branch 1 taken 22877002 times.
22891175 if (num_words > memory_num_free) {
246 14173 lbm_request_gc();
247 14173 lbm_mutex_unlock(&lbm_mem_mutex);
248 14173 return NULL;
249 }
250
251
4/4
✓ Branch 0 taken 22725413 times.
✓ Branch 1 taken 151589 times.
✓ Branch 2 taken 13505 times.
✓ Branch 3 taken 22711908 times.
22877002 if (!use_reserve && memory_num_free - num_words < memory_reserve_level) {
252 13505 lbm_request_gc();
253 13505 lbm_mutex_unlock(&lbm_mem_mutex);
254 13505 return NULL;
255 }
256
257 22863497 lbm_uint start_ix = 0;
258 22863497 lbm_uint end_ix = 0;
259 22863497 lbm_uint free_length = 0;
260 22863497 unsigned int state = INIT;
261
262
1/2
✓ Branch 0 taken 212680425 times.
✗ Branch 1 not taken.
212680425 for (lbm_uint i = 0; i < memory_size; i ++) {
263
4/4
✓ Branch 1 taken 189910702 times.
✓ Branch 2 taken 22409084 times.
✓ Branch 3 taken 47087 times.
✓ Branch 4 taken 313552 times.
212680425 switch(status(alloc_offset)) {
264
3/4
✓ Branch 0 taken 22868500 times.
✓ Branch 1 taken 166330606 times.
✓ Branch 2 taken 711596 times.
✗ Branch 3 not taken.
189910702 case FREE_OR_USED:
265 switch (state) {
266 22868500 case INIT:
267 22868500 start_ix = alloc_offset;
268
2/2
✓ Branch 0 taken 308479 times.
✓ Branch 1 taken 22560021 times.
22868500 if (num_words == 1) {
269 308479 end_ix = alloc_offset;
270 308479 state = ALLOC_DONE;
271 } else {
272 22560021 state = FREE_LENGTH_CHECK;
273 22560021 free_length = 1;
274 }
275 22868500 break;
276 166330606 case FREE_LENGTH_CHECK:
277 166330606 free_length ++;
278
2/2
✓ Branch 0 taken 22555018 times.
✓ Branch 1 taken 143775588 times.
166330606 if (free_length == num_words) {
279 22555018 end_ix = alloc_offset;
280 22555018 state = ALLOC_DONE;
281 } else {
282 143775588 state = FREE_LENGTH_CHECK;
283 }
284 166330606 break;
285 711596 case SKIP:
286 711596 break;
287 }
288 189910702 break;
289 22409084 case END:
290 22409084 state = INIT;
291 22409084 break;
292 47087 case START:
293 47087 state = SKIP;
294 47087 break;
295 313552 default: // START_END
296 313552 state = INIT;
297 313552 break;
298 }
299
300
2/2
✓ Branch 0 taken 22863497 times.
✓ Branch 1 taken 189816928 times.
212680425 if (state == ALLOC_DONE) break;
301
302 189816928 alloc_offset++;
303
2/2
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 189816818 times.
189816928 if (alloc_offset == memory_size ) {
304 110 free_length = 0;
305 110 alloc_offset = 0;
306 110 state = INIT;
307 }
308 }
309
310
1/2
✓ Branch 0 taken 22863497 times.
✗ Branch 1 not taken.
22863497 if (state == ALLOC_DONE) {
311
2/2
✓ Branch 0 taken 308479 times.
✓ Branch 1 taken 22555018 times.
22863497 if (start_ix == end_ix) {
312 308479 set_status(start_ix, START_END);
313 } else {
314 22555018 set_status(start_ix, START);
315 22555018 set_status(end_ix, END);
316 }
317 22863497 memory_num_free -= num_words;
318 22863497 lbm_mutex_unlock(&lbm_mem_mutex);
319 22863497 return bitmap_ix_to_address(start_ix);
320 }
321 ✗ lbm_mutex_unlock(&lbm_mem_mutex);
322 ✗ return NULL;
323 }
324
325 141675 lbm_uint *lbm_memory_allocate(lbm_uint num_words) {
326 141675 return lbm_memory_allocate_internal(num_words, false);
327 }
328
329 22495711 int lbm_memory_free(lbm_uint *ptr) {
330 22495711 int r = 0;
331
2/2
✓ Branch 1 taken 22349380 times.
✓ Branch 2 taken 146331 times.
22495711 if (lbm_memory_ptr_inside(ptr)) {
332 22349380 lbm_mutex_lock(&lbm_mem_mutex);
333 22349380 lbm_uint ix = address_to_bitmap_ix(ptr);
334 22349380 lbm_uint count_freed = 0;
335 22349380 alloc_offset = ix;
336
3/3
✓ Branch 1 taken 22068198 times.
✓ Branch 2 taken 281180 times.
✓ Branch 3 taken 2 times.
22349380 switch(status(ix)) {
337 22068198 case START:
338 22068198 set_status(ix, FREE_OR_USED);
339
1/2
✓ Branch 0 taken 127328107 times.
✗ Branch 1 not taken.
127328107 for (lbm_uint i = ix; i < memory_size; i ++) {
340 127328107 count_freed ++;
341
2/2
✓ Branch 1 taken 22068198 times.
✓ Branch 2 taken 105259909 times.
127328107 if (status(i) == END) {
342 22068198 set_status(i, FREE_OR_USED);
343 22068198 r = 1;
344 22068198 break;
345 }
346 }
347 22068198 break;
348 281180 case START_END:
349 281180 set_status(ix, FREE_OR_USED);
350 281180 count_freed = 1;
351 281180 r = 1;
352 281180 break;
353 2 default:
354 2 break;
355 }
356
2/2
✓ Branch 0 taken 22349378 times.
✓ Branch 1 taken 2 times.
22349380 if (r) {
357
4/4
✓ Branch 0 taken 319953240 times.
✓ Branch 1 taken 1319 times.
✓ Branch 3 taken 297605181 times.
✓ Branch 4 taken 22348059 times.
319954559 while (alloc_offset > 0 && status(alloc_offset - 1) == FREE_OR_USED) {
358 297605181 alloc_offset--;
359 }
360 }
361 22349380 memory_num_free += count_freed;
362 22349380 lbm_mutex_unlock(&lbm_mem_mutex);
363 }
364 22495711 return r;
365 }
366 //Malloc/free like interface
367 22597913 void* lbm_malloc(size_t size) {
368
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 22597912 times.
22597913 if (size == 0) return NULL;
369 lbm_uint alloc_size;
370
371 22597912 alloc_size = size / sizeof(lbm_uint);
372
2/2
✓ Branch 0 taken 680774 times.
✓ Branch 1 taken 21917138 times.
22597912 if (size % sizeof(lbm_uint)) alloc_size += 1;
373
374 22597912 return lbm_memory_allocate_internal(alloc_size, false);
375 }
376
377 151591 void* lbm_malloc_reserve(size_t size) {
378
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 151589 times.
151591 if (size == 0) return NULL;
379 lbm_uint alloc_size;
380
381 151589 alloc_size = size / sizeof(lbm_uint);
382
2/2
✓ Branch 0 taken 125223 times.
✓ Branch 1 taken 26366 times.
151589 if (size % sizeof(lbm_uint)) alloc_size += 1;
383
384 151589 return lbm_memory_allocate_internal(alloc_size, true);
385 }
386
387 118836 void lbm_free(void *ptr) {
388 118836 lbm_memory_free(ptr);
389 118836 }
390
391 34236 int lbm_memory_shrink(lbm_uint *ptr, lbm_uint n) {
392
4/4
✓ Branch 1 taken 34233 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 34229 times.
34236 if (!lbm_memory_ptr_inside(ptr) || n == 0) return 0;
393
394 34229 lbm_uint ix = address_to_bitmap_ix(ptr);
395
396 34229 lbm_mutex_lock(&lbm_mem_mutex);
397
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 34227 times.
34229 if (status(ix) == START_END) {
398 2 lbm_mutex_unlock(&lbm_mem_mutex);
399 2 return 1; // A one word arrays always succeeds at remaining at 1 word
400 }
401
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 34227 times.
34227 if (status(ix) != START) {
402 ✗ lbm_mutex_unlock(&lbm_mem_mutex);
403 ✗ return 0; // ptr does not point to the start of an allocated range.
404 }
405
406 34227 bool done = false;
407 34227 unsigned int i = 0;
408
409
1/2
✓ Branch 0 taken 304577 times.
✗ Branch 1 not taken.
304577 for (i = 0; i < (memory_size - ix); i ++) {
410
3/4
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 304576 times.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
304577 if (status(ix+i) == END && i < n) {
411 1 lbm_mutex_unlock(&lbm_mem_mutex);
412 1 return 0; // cannot shrink allocation to a larger size
413 }
414
415
2/2
✓ Branch 0 taken 34226 times.
✓ Branch 1 taken 270350 times.
304576 if (i == (n-1)) {
416
2/4
✓ Branch 1 taken 34226 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 34226 times.
68452 if (status(ix+i) == END ||
417 34226 status(ix+i) == START_END) {
418 ✗ done = true;
419 }
420
2/2
✓ Branch 0 taken 6253 times.
✓ Branch 1 taken 27973 times.
34226 if (i == 0) {
421 6253 set_status(ix+i, START_END);
422 }
423 else {
424 27973 set_status(ix+i, END);
425 }
426 34226 break;
427 }
428 }
429 34226 alloc_offset = ix+i;
430
431 34226 lbm_uint count = 0;
432
1/2
✓ Branch 0 taken 34226 times.
✗ Branch 1 not taken.
34226 if (!done) {
433 34226 i++; // move to next position, prev position should be END or START_END
434
1/2
✓ Branch 0 taken 28864104 times.
✗ Branch 1 not taken.
28864104 for (;i < (memory_size - ix); i ++) {
435 28864104 count ++;
436
2/2
✓ Branch 1 taken 34226 times.
✓ Branch 2 taken 28829878 times.
28864104 if (status(ix+i) == END) {
437 34226 set_status(ix+i, FREE_OR_USED);
438 34226 break;
439 }
440 }
441 }
442
443 34226 memory_num_free += count;
444 34226 lbm_mutex_unlock(&lbm_mem_mutex);
445 34226 return 1;
446 }
447
448 22544362 int lbm_memory_ptr_inside(lbm_uint *ptr) {
449
2/2
✓ Branch 0 taken 22383632 times.
✓ Branch 1 taken 160730 times.
44927994 return ((lbm_uint)ptr >= (lbm_uint)memory &&
450
2/2
✓ Branch 0 taken 22383624 times.
✓ Branch 1 taken 8 times.
22383632 (lbm_uint)ptr < (lbm_uint)&memory[memory_size]);
451 }
452