GCC Code Coverage Report


Directory: ../src/
File: lbm_memory.c
Date: 2026-10-16 14:49:47
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 57838 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 29256 times.
✓ Branch 1 taken 28582 times.
57838 if (!lbm_mem_mutex_initialized) {
63 29256 lbm_mutex_init(&lbm_mem_mutex);
64 29256 lbm_mem_mutex_initialized = true;
65 }
66
67 57838 alloc_offset = 0;
68
69 57838 lbm_mutex_lock(&lbm_mem_mutex);
70 57838 bool res = false;
71
4/4
✓ Branch 0 taken 57833 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 57830 times.
✓ Branch 3 taken 3 times.
57838 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 57830 times.
✗ Branch 1 not taken.
57830 if ((lbm_uint)data % sizeof(lbm_uint) == 0 && // Alignment req data.
79
1/2
✓ Branch 0 taken 57830 times.
✗ Branch 1 not taken.
57830 (lbm_uint)bits % sizeof(lbm_uint) == 0 && // Alignment req bitmap.
80
2/2
✓ Branch 0 taken 57825 times.
✓ Branch 1 taken 5 times.
57830 data_size *2 <= (bits_size * sizeof(lbm_uint)) * 8) { // size requirement.
81 57825 bitmap = bits;
82 57825 bitmap_size = bits_size;
83
84
2/2
✓ Branch 0 taken 90204796 times.
✓ Branch 1 taken 57825 times.
90262621 for (lbm_uint i = 0; i < bitmap_size; i ++) {
85 90204796 bitmap[i] = 0;
86 }
87
88 57825 memory = data;
89 57825 memory_base_address = (lbm_uint)data;
90 57825 memory_size = data_size;
91 57825 memory_min_free = data_size;
92 57825 memory_num_free = data_size;
93 57825 memory_reserve_level = (lbm_uint)(0.1 * (lbm_float)data_size);
94 57825 res = true;
95 }
96 }
97 57838 lbm_mutex_unlock(&lbm_mem_mutex);
98 57838 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 4167857 static inline lbm_uint address_to_bitmap_ix(lbm_uint *ptr) {
113 #ifndef LBM64
114 3362738 return ((lbm_uint)ptr - memory_base_address) >> 2;
115 #else
116 805119 return ((lbm_uint)ptr - memory_base_address) >> 3;
117 #endif
118 }
119
120 30964 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 30964 return (lbm_int)address_to_bitmap_ix(ptr);
125 }
126
127
128 4364925 static inline lbm_uint *bitmap_ix_to_address(lbm_uint ix) {
129 4364925 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 252562770 static inline lbm_uint status(lbm_uint i) {
143
144 252562770 lbm_uint ix = i << 1; // * 2
145 252562770 lbm_uint word_ix = ix >> WORD_IX_SHIFT; // / 32
146 252562770 lbm_uint bit_ix = ix & WORD_MOD_MASK; // % 32
147
148 252562770 lbm_uint mask = ((lbm_uint)3) << bit_ix; // 000110..0
149 252562770 return (bitmap[word_ix] & mask) >> bit_ix;
150 }
151
152 16695534 static inline void set_status(lbm_uint i, lbm_uint status) {
153 16695534 lbm_uint ix = i << 1; // * 2
154 16695534 lbm_uint word_ix = ix >> WORD_IX_SHIFT; // / 32
155 16695534 lbm_uint bit_ix = ix & WORD_MOD_MASK; // % 32
156
157 16695534 lbm_uint clr_mask = ~(((lbm_uint)3) << bit_ix);
158 16695534 lbm_uint mask = status << bit_ix;
159
160 16695534 bitmap[word_ix] &= clr_mask;
161 16695534 bitmap[word_ix] |= mask;
162 16695534 }
163
164 37 lbm_uint lbm_memory_num_words(void) {
165 37 return memory_size;
166 }
167
168 58274 lbm_uint lbm_memory_num_free(void) {
169 58274 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 802494 void lbm_memory_update_min_free(void) {
177
2/2
✓ Branch 0 taken 4251 times.
✓ Branch 1 taken 798243 times.
802494 if (memory_num_free < memory_min_free)
178 4251 memory_min_free = memory_num_free;
179 802494 }
180
181 5678 lbm_uint lbm_memory_longest_free(void) {
182
3/4
✓ Branch 0 taken 5677 times.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 5677 times.
5678 if (memory == NULL || bitmap == NULL) {
183 1 return 0;
184 }
185 5677 lbm_mutex_lock(&lbm_mem_mutex);
186 5677 unsigned int state = INIT;
187 5677 lbm_uint max_length = 0;
188
189 5677 lbm_uint curr_length = 0;
190
2/2
✓ Branch 0 taken 22885120 times.
✓ Branch 1 taken 5677 times.
22890797 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 22700935 times.
✓ Branch 2 taken 88313 times.
✓ Branch 3 taken 88313 times.
✓ Branch 4 taken 7559 times.
22885120 switch(status(i)) {
194
3/4
✓ Branch 0 taken 6662 times.
✓ Branch 1 taken 17497998 times.
✓ Branch 2 taken 5196275 times.
✗ Branch 3 not taken.
22700935 case FREE_OR_USED:
195 switch (state) {
196 6662 case INIT:
197 6662 curr_length = 1;
198
2/2
✓ Branch 0 taken 5677 times.
✓ Branch 1 taken 985 times.
6662 if (curr_length > max_length) max_length = curr_length;
199 6662 state = FREE_LENGTH_CHECK;
200 6662 break;
201 17497998 case FREE_LENGTH_CHECK:
202 17497998 curr_length ++;
203
2/2
✓ Branch 0 taken 17454607 times.
✓ Branch 1 taken 43391 times.
17497998 if (curr_length > max_length) max_length = curr_length;
204 17497998 state = FREE_LENGTH_CHECK;
205 17497998 break;
206 5196275 case SKIP:
207 5196275 break;
208 }
209 22700935 break;
210 88313 case END:
211 88313 state = INIT;
212 88313 break;
213 88313 case START:
214 88313 state = SKIP;
215 88313 break;
216 7559 default: // START_END
217 7559 state = INIT;
218 7559 break;
219 }
220 }
221 5677 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 5668 times.
✓ Branch 1 taken 9 times.
5677 if (memory_num_free - max_length < memory_reserve_level) {
227 5668 lbm_uint n = memory_reserve_level - (memory_num_free - max_length);
228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5668 times.
5668 if (n >= max_length) {
229 ✗ max_length = 0;
230 } else {
231 5668 max_length -= n;
232 }
233 }
234 5677 return max_length;
235 }
236
237 4376443 static lbm_uint *lbm_memory_allocate_internal(lbm_uint num_words, bool use_reserve) {
238
239
3/4
✓ Branch 0 taken 4376442 times.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 4376442 times.
4376443 if (memory == NULL || bitmap == NULL) {
240 1 return NULL;
241 }
242
243 4376442 lbm_mutex_lock(&lbm_mem_mutex);
244
245
2/2
✓ Branch 0 taken 6081 times.
✓ Branch 1 taken 4370361 times.
4376442 if (num_words > memory_num_free) {
246 6081 lbm_request_gc();
247 6081 lbm_mutex_unlock(&lbm_mem_mutex);
248 6081 return NULL;
249 }
250
251
4/4
✓ Branch 0 taken 4303172 times.
✓ Branch 1 taken 67189 times.
✓ Branch 2 taken 5436 times.
✓ Branch 3 taken 4297736 times.
4370361 if (!use_reserve && memory_num_free - num_words < memory_reserve_level) {
252 5436 lbm_request_gc();
253 5436 lbm_mutex_unlock(&lbm_mem_mutex);
254 5436 return NULL;
255 }
256
257 4364925 lbm_uint start_ix = 0;
258 4364925 lbm_uint end_ix = 0;
259 4364925 lbm_uint free_length = 0;
260 4364925 unsigned int state = INIT;
261
262
1/2
✓ Branch 0 taken 79521122 times.
✗ Branch 1 not taken.
79521122 for (lbm_uint i = 0; i < memory_size; i ++) {
263
4/4
✓ Branch 1 taken 75192017 times.
✓ Branch 2 taken 4136902 times.
✓ Branch 3 taken 28809 times.
✓ Branch 4 taken 163394 times.
79521122 switch(status(alloc_offset)) {
264
3/4
✓ Branch 0 taken 4368170 times.
✓ Branch 1 taken 70100177 times.
✓ Branch 2 taken 723670 times.
✗ Branch 3 not taken.
75192017 case FREE_OR_USED:
265 switch (state) {
266 4368170 case INIT:
267 4368170 start_ix = alloc_offset;
268
2/2
✓ Branch 0 taken 161037 times.
✓ Branch 1 taken 4207133 times.
4368170 if (num_words == 1) {
269 161037 end_ix = alloc_offset;
270 161037 state = ALLOC_DONE;
271 } else {
272 4207133 state = FREE_LENGTH_CHECK;
273 4207133 free_length = 1;
274 }
275 4368170 break;
276 70100177 case FREE_LENGTH_CHECK:
277 70100177 free_length ++;
278
2/2
✓ Branch 0 taken 4203888 times.
✓ Branch 1 taken 65896289 times.
70100177 if (free_length == num_words) {
279 4203888 end_ix = alloc_offset;
280 4203888 state = ALLOC_DONE;
281 } else {
282 65896289 state = FREE_LENGTH_CHECK;
283 }
284 70100177 break;
285 723670 case SKIP:
286 723670 break;
287 }
288 75192017 break;
289 4136902 case END:
290 4136902 state = INIT;
291 4136902 break;
292 28809 case START:
293 28809 state = SKIP;
294 28809 break;
295 163394 default: // START_END
296 163394 state = INIT;
297 163394 break;
298 }
299
300
2/2
✓ Branch 0 taken 4364925 times.
✓ Branch 1 taken 75156197 times.
79521122 if (state == ALLOC_DONE) break;
301
302 75156197 alloc_offset++;
303
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 75156145 times.
75156197 if (alloc_offset == memory_size ) {
304 52 free_length = 0;
305 52 alloc_offset = 0;
306 52 state = INIT;
307 }
308 }
309
310
1/2
✓ Branch 0 taken 4364925 times.
✗ Branch 1 not taken.
4364925 if (state == ALLOC_DONE) {
311
2/2
✓ Branch 0 taken 161037 times.
✓ Branch 1 taken 4203888 times.
4364925 if (start_ix == end_ix) {
312 161037 set_status(start_ix, START_END);
313 } else {
314 4203888 set_status(start_ix, START);
315 4203888 set_status(end_ix, END);
316 }
317 4364925 memory_num_free -= num_words;
318 4364925 lbm_mutex_unlock(&lbm_mem_mutex);
319 4364925 return bitmap_ix_to_address(start_ix);
320 }
321 ✗ lbm_mutex_unlock(&lbm_mem_mutex);
322 ✗ return NULL;
323 }
324
325 63590 lbm_uint *lbm_memory_allocate(lbm_uint num_words) {
326 63590 return lbm_memory_allocate_internal(num_words, false);
327 }
328
329 4185682 int lbm_memory_free(lbm_uint *ptr) {
330 4185682 int r = 0;
331
2/2
✓ Branch 1 taken 4121132 times.
✓ Branch 2 taken 64550 times.
4185682 if (lbm_memory_ptr_inside(ptr)) {
332 4121132 lbm_mutex_lock(&lbm_mem_mutex);
333 4121132 lbm_uint ix = address_to_bitmap_ix(ptr);
334 4121132 lbm_uint count_freed = 0;
335 4121132 alloc_offset = ix;
336
3/3
✓ Branch 1 taken 3974077 times.
✓ Branch 2 taken 147051 times.
✓ Branch 3 taken 4 times.
4121132 switch(status(ix)) {
337 3974077 case START:
338 3974077 set_status(ix, FREE_OR_USED);
339
1/2
✓ Branch 0 taken 44997743 times.
✗ Branch 1 not taken.
44997743 for (lbm_uint i = ix; i < memory_size; i ++) {
340 44997743 count_freed ++;
341
2/2
✓ Branch 1 taken 3974077 times.
✓ Branch 2 taken 41023666 times.
44997743 if (status(i) == END) {
342 3974077 set_status(i, FREE_OR_USED);
343 3974077 r = 1;
344 3974077 break;
345 }
346 }
347 3974077 break;
348 147051 case START_END:
349 147051 set_status(ix, FREE_OR_USED);
350 147051 count_freed = 1;
351 147051 r = 1;
352 147051 break;
353 4 default:
354 4 break;
355 }
356
2/2
✓ Branch 0 taken 4121128 times.
✓ Branch 1 taken 4 times.
4121132 if (r) {
357
4/4
✓ Branch 0 taken 87987897 times.
✓ Branch 1 taken 1175 times.
✓ Branch 3 taken 83867944 times.
✓ Branch 4 taken 4119953 times.
87989072 while (alloc_offset > 0 && status(alloc_offset - 1) == FREE_OR_USED) {
358 83867944 alloc_offset--;
359 }
360 }
361 4121132 memory_num_free += count_freed;
362 4121132 lbm_mutex_unlock(&lbm_mem_mutex);
363 }
364 4185682 return r;
365 }
366 //Malloc/free like interface
367 4245665 void* lbm_malloc(size_t size) {
368
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4245664 times.
4245665 if (size == 0) return NULL;
369 lbm_uint alloc_size;
370
371 4245664 alloc_size = size / sizeof(lbm_uint);
372
2/2
✓ Branch 0 taken 296866 times.
✓ Branch 1 taken 3948798 times.
4245664 if (size % sizeof(lbm_uint)) alloc_size += 1;
373
374 4245664 return lbm_memory_allocate_internal(alloc_size, false);
375 }
376
377 67191 void* lbm_malloc_reserve(size_t size) {
378
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 67189 times.
67191 if (size == 0) return NULL;
379 lbm_uint alloc_size;
380
381 67189 alloc_size = size / sizeof(lbm_uint);
382
2/2
✓ Branch 0 taken 54463 times.
✓ Branch 1 taken 12726 times.
67189 if (size % sizeof(lbm_uint)) alloc_size += 1;
383
384 67189 return lbm_memory_allocate_internal(alloc_size, true);
385 }
386
387 58740 void lbm_free(void *ptr) {
388 58740 lbm_memory_free(ptr);
389 58740 }
390
391
392 15766 bool lbm_memory_shrink(lbm_uint *ptr, lbm_uint n) {
393
4/4
✓ Branch 1 taken 15763 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 15761 times.
15766 if (!lbm_memory_ptr_inside(ptr) || n == 0) return 0;
394
395 15761 lbm_uint ix = address_to_bitmap_ix(ptr);
396
397 15761 lbm_mutex_lock(&lbm_mem_mutex);
398
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 15759 times.
15761 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 15759 times.
15759 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 15759 bool done = false;
408 15759 unsigned int i = 0;
409
410
1/2
✓ Branch 0 taken 132824 times.
✗ Branch 1 not taken.
132824 for (i = 0; i < (memory_size - ix); i ++) {
411
3/4
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 132823 times.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
132824 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 15758 times.
✓ Branch 1 taken 117065 times.
132823 if (i == (n-1)) {
417
2/4
✓ Branch 1 taken 15758 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 15758 times.
31516 if (status(ix+i) == END ||
418 15758 status(ix+i) == START_END) {
419 // Shrink to current size case.
420 ✗ done = true;
421 }
422
2/2
✓ Branch 0 taken 2892 times.
✓ Branch 1 taken 12866 times.
15758 if (i == 0) {
423 2892 set_status(ix+i, START_END);
424 }
425 else {
426 12866 set_status(ix+i, END);
427 }
428 15758 break;
429 }
430 }
431 15758 alloc_offset = ix+i;
432
433 15758 lbm_uint count = 0;
434
1/2
✓ Branch 0 taken 15758 times.
✗ Branch 1 not taken.
15758 if (!done) {
435 15758 i++; // move to next position, prev position should be END or START_END
436
1/2
✓ Branch 0 taken 12853896 times.
✗ Branch 1 not taken.
12853896 for (;i < (memory_size - ix); i ++) {
437 12853896 count ++;
438
2/2
✓ Branch 1 taken 15758 times.
✓ Branch 2 taken 12838138 times.
12853896 if (status(ix+i) == END) {
439 15758 set_status(ix+i, FREE_OR_USED);
440 15758 break;
441 }
442 }
443 }
444
445 15758 memory_num_free += count;
446 15758 lbm_mutex_unlock(&lbm_mem_mutex);
447 15758 return true;
448 }
449
450 4219767 int lbm_memory_ptr_inside(lbm_uint *ptr) {
451
2/2
✓ Branch 0 taken 4136916 times.
✓ Branch 1 taken 82851 times.
8356683 return ((lbm_uint)ptr >= (lbm_uint)memory &&
452
2/2
✓ Branch 0 taken 4136907 times.
✓ Branch 1 taken 9 times.
4136916 (lbm_uint)ptr < (lbm_uint)&memory[memory_size]);
453 }
454