| 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 | 133329 | 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 66995 times.
✓ Branch 1 taken 66334 times.
|
133329 | if (!lbm_mem_mutex_initialized) { |
| 63 | 66995 | lbm_mutex_init(&lbm_mem_mutex); | |
| 64 | 66995 | lbm_mem_mutex_initialized = true; | |
| 65 | } | ||
| 66 | |||
| 67 | 133329 | alloc_offset = 0; | |
| 68 | |||
| 69 | 133329 | lbm_mutex_lock(&lbm_mem_mutex); | |
| 70 | 133329 | bool res = false; | |
| 71 |
4/4✓ Branch 0 taken 133324 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 133321 times.
✓ Branch 3 taken 3 times.
|
133329 | 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 133321 times.
✗ Branch 1 not taken.
|
133321 | if ((lbm_uint)data % sizeof(lbm_uint) == 0 && // Alignment req data. |
| 79 |
1/2✓ Branch 0 taken 133321 times.
✗ Branch 1 not taken.
|
133321 | (lbm_uint)bits % sizeof(lbm_uint) == 0 && // Alignment req bitmap. |
| 80 |
2/2✓ Branch 0 taken 133316 times.
✓ Branch 1 taken 5 times.
|
133321 | data_size *2 <= (bits_size * sizeof(lbm_uint)) * 8) { // size requirement. |
| 81 | 133316 | bitmap = bits; | |
| 82 | 133316 | bitmap_size = bits_size; | |
| 83 | |||
| 84 |
2/2✓ Branch 0 taken 207771388 times.
✓ Branch 1 taken 133316 times.
|
207904704 | for (lbm_uint i = 0; i < bitmap_size; i ++) { |
| 85 | 207771388 | bitmap[i] = 0; | |
| 86 | } | ||
| 87 | |||
| 88 | 133316 | memory = data; | |
| 89 | 133316 | memory_base_address = (lbm_uint)data; | |
| 90 | 133316 | memory_size = data_size; | |
| 91 | 133316 | memory_min_free = data_size; | |
| 92 | 133316 | memory_num_free = data_size; | |
| 93 | 133316 | memory_reserve_level = (lbm_uint)(0.1 * (lbm_float)data_size); | |
| 94 | 133316 | res = true; | |
| 95 | } | ||
| 96 | } | ||
| 97 | 133329 | lbm_mutex_unlock(&lbm_mem_mutex); | |
| 98 | 133329 | 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 | 22505970 | static inline lbm_uint address_to_bitmap_ix(lbm_uint *ptr) { | |
| 113 | #ifndef LBM64 | ||
| 114 | 20638721 | return ((lbm_uint)ptr - memory_base_address) >> 2; | |
| 115 | #else | ||
| 116 | 1867249 | return ((lbm_uint)ptr - memory_base_address) >> 3; | |
| 117 | #endif | ||
| 118 | } | ||
| 119 | |||
| 120 | 70622 | 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 | 70622 | return (lbm_int)address_to_bitmap_ix(ptr); | |
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | 22926209 | static inline lbm_uint *bitmap_ix_to_address(lbm_uint ix) { | |
| 129 | 22926209 | 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 | 769214472 | static inline lbm_uint status(lbm_uint i) { | |
| 143 | |||
| 144 | 769214472 | lbm_uint ix = i << 1; // * 2 | |
| 145 | 769214472 | lbm_uint word_ix = ix >> WORD_IX_SHIFT; // / 32 | |
| 146 | 769214472 | lbm_uint bit_ix = ix & WORD_MOD_MASK; // % 32 | |
| 147 | |||
| 148 | 769214472 | lbm_uint mask = ((lbm_uint)3) << bit_ix; // 000110..0 | |
| 149 | 769214472 | return (bitmap[word_ix] & mask) >> bit_ix; | |
| 150 | } | ||
| 151 | |||
| 152 | 90092332 | static inline void set_status(lbm_uint i, lbm_uint status) { | |
| 153 | 90092332 | lbm_uint ix = i << 1; // * 2 | |
| 154 | 90092332 | lbm_uint word_ix = ix >> WORD_IX_SHIFT; // / 32 | |
| 155 | 90092332 | lbm_uint bit_ix = ix & WORD_MOD_MASK; // % 32 | |
| 156 | |||
| 157 | 90092332 | lbm_uint clr_mask = ~(((lbm_uint)3) << bit_ix); | |
| 158 | 90092332 | lbm_uint mask = status << bit_ix; | |
| 159 | |||
| 160 | 90092332 | bitmap[word_ix] &= clr_mask; | |
| 161 | 90092332 | bitmap[word_ix] |= mask; | |
| 162 | 90092332 | } | |
| 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 | 1610738 | void lbm_memory_update_min_free(void) { | |
| 177 |
2/2✓ Branch 0 taken 9540 times.
✓ Branch 1 taken 1601198 times.
|
1610738 | if (memory_num_free < memory_min_free) |
| 178 | 9540 | memory_min_free = memory_num_free; | |
| 179 | 1610738 | } | |
| 180 | |||
| 181 | 12698 | lbm_uint lbm_memory_longest_free(void) { | |
| 182 |
3/4✓ Branch 0 taken 12697 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12697 times.
|
12698 | if (memory == NULL || bitmap == NULL) { |
| 183 | 1 | return 0; | |
| 184 | } | ||
| 185 | 12697 | lbm_mutex_lock(&lbm_mem_mutex); | |
| 186 | 12697 | unsigned int state = INIT; | |
| 187 | 12697 | lbm_uint max_length = 0; | |
| 188 | |||
| 189 | 12697 | lbm_uint curr_length = 0; | |
| 190 |
2/2✓ Branch 0 taken 51639040 times.
✓ Branch 1 taken 12697 times.
|
51651737 | 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 51241311 times.
✓ Branch 1 taken 191161 times.
✓ Branch 2 taken 191161 times.
✓ Branch 3 taken 15407 times.
|
51639040 | switch(status(i)) { |
| 194 | 51241311 | case FREE_OR_USED: | |
| 195 |
3/4✓ Branch 0 taken 14954 times.
✓ Branch 1 taken 39745053 times.
✓ Branch 2 taken 11481304 times.
✗ Branch 3 not taken.
|
51241311 | switch (state) { |
| 196 | 14954 | case INIT: | |
| 197 | 14954 | curr_length = 1; | |
| 198 |
2/2✓ Branch 0 taken 12697 times.
✓ Branch 1 taken 2257 times.
|
14954 | if (curr_length > max_length) max_length = curr_length; |
| 199 | 14954 | state = FREE_LENGTH_CHECK; | |
| 200 | 14954 | break; | |
| 201 | 39745053 | case FREE_LENGTH_CHECK: | |
| 202 | 39745053 | curr_length ++; | |
| 203 |
2/2✓ Branch 0 taken 39648550 times.
✓ Branch 1 taken 96503 times.
|
39745053 | if (curr_length > max_length) max_length = curr_length; |
| 204 | 39745053 | state = FREE_LENGTH_CHECK; | |
| 205 | 39745053 | break; | |
| 206 | 11481304 | case SKIP: | |
| 207 | 11481304 | break; | |
| 208 | } | ||
| 209 | 51241311 | break; | |
| 210 | 191161 | case END: | |
| 211 | 191161 | state = INIT; | |
| 212 | 191161 | break; | |
| 213 | 191161 | case START: | |
| 214 | 191161 | state = SKIP; | |
| 215 | 191161 | break; | |
| 216 | 15407 | default: // START_END | |
| 217 | 15407 | state = INIT; | |
| 218 | 15407 | break; | |
| 219 | } | ||
| 220 | } | ||
| 221 | 12697 | 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 9 times.
|
12697 | 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 | 12697 | return max_length; | |
| 235 | } | ||
| 236 | |||
| 237 | 22954895 | static lbm_uint *lbm_memory_allocate_internal(lbm_uint num_words, bool use_reserve) { | |
| 238 | |||
| 239 |
3/4✓ Branch 0 taken 22954894 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 22954894 times.
|
22954895 | if (memory == NULL || bitmap == NULL) { |
| 240 | 1 | return NULL; | |
| 241 | } | ||
| 242 | |||
| 243 | 22954894 | lbm_mutex_lock(&lbm_mem_mutex); | |
| 244 | |||
| 245 |
2/2✓ Branch 0 taken 14177 times.
✓ Branch 1 taken 22940717 times.
|
22954894 | 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 22795454 times.
✓ Branch 1 taken 145263 times.
✓ Branch 2 taken 14508 times.
✓ Branch 3 taken 22780946 times.
|
22940717 | 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 | 22926209 | lbm_uint start_ix = 0; | |
| 258 | 22926209 | lbm_uint end_ix = 0; | |
| 259 | 22926209 | lbm_uint free_length = 0; | |
| 260 | 22926209 | unsigned int state = INIT; | |
| 261 | |||
| 262 |
1/2✓ Branch 0 taken 215758499 times.
✗ Branch 1 not taken.
|
215758499 | for (lbm_uint i = 0; i < memory_size; i ++) { |
| 263 |
4/4✓ Branch 0 taken 192924356 times.
✓ Branch 1 taken 22450237 times.
✓ Branch 2 taken 49932 times.
✓ Branch 3 taken 333974 times.
|
215758499 | switch(status(alloc_offset)) { |
| 264 | 192924356 | case FREE_OR_USED: | |
| 265 |
3/4✓ Branch 0 taken 22931423 times.
✓ Branch 1 taken 169091341 times.
✓ Branch 2 taken 901592 times.
✗ Branch 3 not taken.
|
192924356 | switch (state) { |
| 266 | 22931423 | case INIT: | |
| 267 | 22931423 | start_ix = alloc_offset; | |
| 268 |
2/2✓ Branch 0 taken 329612 times.
✓ Branch 1 taken 22601811 times.
|
22931423 | if (num_words == 1) { |
| 269 | 329612 | end_ix = alloc_offset; | |
| 270 | 329612 | state = ALLOC_DONE; | |
| 271 | } else { | ||
| 272 | 22601811 | state = FREE_LENGTH_CHECK; | |
| 273 | 22601811 | free_length = 1; | |
| 274 | } | ||
| 275 | 22931423 | break; | |
| 276 | 169091341 | case FREE_LENGTH_CHECK: | |
| 277 | 169091341 | free_length ++; | |
| 278 |
2/2✓ Branch 0 taken 22596597 times.
✓ Branch 1 taken 146494744 times.
|
169091341 | if (free_length == num_words) { |
| 279 | 22596597 | end_ix = alloc_offset; | |
| 280 | 22596597 | state = ALLOC_DONE; | |
| 281 | } else { | ||
| 282 | 146494744 | state = FREE_LENGTH_CHECK; | |
| 283 | } | ||
| 284 | 169091341 | break; | |
| 285 | 901592 | case SKIP: | |
| 286 | 901592 | break; | |
| 287 | } | ||
| 288 | 192924356 | break; | |
| 289 | 22450237 | case END: | |
| 290 | 22450237 | state = INIT; | |
| 291 | 22450237 | break; | |
| 292 | 49932 | case START: | |
| 293 | 49932 | state = SKIP; | |
| 294 | 49932 | break; | |
| 295 | 333974 | default: // START_END | |
| 296 | 333974 | state = INIT; | |
| 297 | 333974 | break; | |
| 298 | } | ||
| 299 | |||
| 300 |
2/2✓ Branch 0 taken 22926209 times.
✓ Branch 1 taken 192832290 times.
|
215758499 | if (state == ALLOC_DONE) break; |
| 301 | |||
| 302 | 192832290 | alloc_offset++; | |
| 303 |
2/2✓ Branch 0 taken 112 times.
✓ Branch 1 taken 192832178 times.
|
192832290 | if (alloc_offset == memory_size ) { |
| 304 | 112 | free_length = 0; | |
| 305 | 112 | alloc_offset = 0; | |
| 306 | 112 | state = INIT; | |
| 307 | } | ||
| 308 | } | ||
| 309 | |||
| 310 |
1/2✓ Branch 0 taken 22926209 times.
✗ Branch 1 not taken.
|
22926209 | if (state == ALLOC_DONE) { |
| 311 |
2/2✓ Branch 0 taken 329612 times.
✓ Branch 1 taken 22596597 times.
|
22926209 | if (start_ix == end_ix) { |
| 312 | 329612 | set_status(start_ix, START_END); | |
| 313 | } else { | ||
| 314 | 22596597 | set_status(start_ix, START); | |
| 315 | 22596597 | set_status(end_ix, END); | |
| 316 | } | ||
| 317 | 22926209 | memory_num_free -= num_words; | |
| 318 | 22926209 | lbm_mutex_unlock(&lbm_mem_mutex); | |
| 319 | 22926209 | return bitmap_ix_to_address(start_ix); | |
| 320 | } | ||
| 321 | ✗ | lbm_mutex_unlock(&lbm_mem_mutex); | |
| 322 | ✗ | return NULL; | |
| 323 | } | ||
| 324 | |||
| 325 | 143363 | lbm_uint *lbm_memory_allocate(lbm_uint num_words) { | |
| 326 | 143363 | return lbm_memory_allocate_internal(num_words, false); | |
| 327 | } | ||
| 328 | |||
| 329 | 22555657 | int lbm_memory_free(lbm_uint *ptr) { | |
| 330 | 22555657 | int r = 0; | |
| 331 |
2/2✓ Branch 0 taken 22408085 times.
✓ Branch 1 taken 147572 times.
|
22555657 | if (lbm_memory_ptr_inside(ptr)) { |
| 332 | 22408085 | lbm_mutex_lock(&lbm_mem_mutex); | |
| 333 | 22408085 | lbm_uint ix = address_to_bitmap_ix(ptr); | |
| 334 | 22408085 | lbm_uint count_freed = 0; | |
| 335 | 22408085 | alloc_offset = ix; | |
| 336 |
3/3✓ Branch 0 taken 22106925 times.
✓ Branch 1 taken 301156 times.
✓ Branch 2 taken 4 times.
|
22408085 | switch(status(ix)) { |
| 337 | 22106925 | case START: | |
| 338 | 22106925 | set_status(ix, FREE_OR_USED); | |
| 339 |
1/2✓ Branch 0 taken 128377167 times.
✗ Branch 1 not taken.
|
128377167 | for (lbm_uint i = ix; i < memory_size; i ++) { |
| 340 | 128377167 | count_freed ++; | |
| 341 |
2/2✓ Branch 0 taken 22106925 times.
✓ Branch 1 taken 106270242 times.
|
128377167 | if (status(i) == END) { |
| 342 | 22106925 | set_status(i, FREE_OR_USED); | |
| 343 | 22106925 | r = 1; | |
| 344 | 22106925 | break; | |
| 345 | } | ||
| 346 | } | ||
| 347 | 22106925 | break; | |
| 348 | 301156 | case START_END: | |
| 349 | 301156 | set_status(ix, FREE_OR_USED); | |
| 350 | 301156 | count_freed = 1; | |
| 351 | 301156 | r = 1; | |
| 352 | 301156 | break; | |
| 353 | 4 | default: | |
| 354 | 4 | break; | |
| 355 | } | ||
| 356 |
2/2✓ Branch 0 taken 22408081 times.
✓ Branch 1 taken 4 times.
|
22408085 | if (r) { |
| 357 |
4/4✓ Branch 0 taken 321409299 times.
✓ Branch 1 taken 1319 times.
✓ Branch 2 taken 299002537 times.
✓ Branch 3 taken 22406762 times.
|
321410618 | while (alloc_offset > 0 && status(alloc_offset - 1) == FREE_OR_USED) { |
| 358 | 299002537 | alloc_offset--; | |
| 359 | } | ||
| 360 | } | ||
| 361 | 22408085 | memory_num_free += count_freed; | |
| 362 | 22408085 | lbm_mutex_unlock(&lbm_mem_mutex); | |
| 363 | } | ||
| 364 | 22555657 | return r; | |
| 365 | } | ||
| 366 | //Malloc/free like interface | ||
| 367 | 22666270 | void* lbm_malloc(size_t size) { | |
| 368 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 22666269 times.
|
22666270 | if (size == 0) return NULL; |
| 369 | lbm_uint alloc_size; | ||
| 370 | |||
| 371 | 22666269 | alloc_size = size / sizeof(lbm_uint); | |
| 372 |
2/2✓ Branch 0 taken 684525 times.
✓ Branch 1 taken 21981744 times.
|
22666269 | if (size % sizeof(lbm_uint)) alloc_size += 1; |
| 373 | |||
| 374 | 22666269 | return lbm_memory_allocate_internal(alloc_size, false); | |
| 375 | } | ||
| 376 | |||
| 377 | 145265 | void* lbm_malloc_reserve(size_t size) { | |
| 378 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 145263 times.
|
145265 | if (size == 0) return NULL; |
| 379 | lbm_uint alloc_size; | ||
| 380 | |||
| 381 | 145263 | alloc_size = size / sizeof(lbm_uint); | |
| 382 |
2/2✓ Branch 0 taken 117954 times.
✓ Branch 1 taken 27309 times.
|
145263 | if (size % sizeof(lbm_uint)) alloc_size += 1; |
| 383 | |||
| 384 | 145263 | return lbm_memory_allocate_internal(alloc_size, true); | |
| 385 | } | ||
| 386 | |||
| 387 | 120560 | void lbm_free(void *ptr) { | |
| 388 | 120560 | lbm_memory_free(ptr); | |
| 389 | 120560 | } | |
| 390 | |||
| 391 | |||
| 392 | 27268 | bool lbm_memory_shrink(lbm_uint *ptr, lbm_uint n) { | |
| 393 |
4/4✓ Branch 0 taken 27265 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 27263 times.
|
27268 | if (!lbm_memory_ptr_inside(ptr) || n == 0) return 0; |
| 394 | |||
| 395 | 27263 | lbm_uint ix = address_to_bitmap_ix(ptr); | |
| 396 | |||
| 397 | 27263 | lbm_mutex_lock(&lbm_mem_mutex); | |
| 398 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 27261 times.
|
27263 | 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 0 not taken.
✓ Branch 1 taken 27261 times.
|
27261 | 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 | 27261 | bool done = false; | |
| 408 | 27261 | unsigned int i = 0; | |
| 409 | |||
| 410 |
1/2✓ Branch 0 taken 291671 times.
✗ Branch 1 not taken.
|
291671 | for (i = 0; i < (memory_size - ix); i ++) { |
| 411 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 291670 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
291671 | 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 27260 times.
✓ Branch 1 taken 264410 times.
|
291670 | if (i == (n-1)) { |
| 417 |
2/4✓ Branch 0 taken 27260 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 27260 times.
|
54520 | if (status(ix+i) == END || |
| 418 | 27260 | status(ix+i) == START_END) { | |
| 419 | // Shrink to current size case. | ||
| 420 | ✗ | done = true; | |
| 421 | } | ||
| 422 |
2/2✓ Branch 0 taken 5449 times.
✓ Branch 1 taken 21811 times.
|
27260 | if (i == 0) { |
| 423 | 5449 | set_status(ix+i, START_END); | |
| 424 | } | ||
| 425 | else { | ||
| 426 | 21811 | set_status(ix+i, END); | |
| 427 | } | ||
| 428 | 27260 | break; | |
| 429 | } | ||
| 430 | } | ||
| 431 | 27260 | alloc_offset = ix+i; | |
| 432 | |||
| 433 | 27260 | lbm_uint count = 0; | |
| 434 |
1/2✓ Branch 0 taken 27260 times.
✗ Branch 1 not taken.
|
27260 | if (!done) { |
| 435 | 27260 | i++; // move to next position, prev position should be END or START_END | |
| 436 |
1/2✓ Branch 0 taken 29221667 times.
✗ Branch 1 not taken.
|
29221667 | for (;i < (memory_size - ix); i ++) { |
| 437 | 29221667 | count ++; | |
| 438 |
2/2✓ Branch 0 taken 27260 times.
✓ Branch 1 taken 29194407 times.
|
29221667 | if (status(ix+i) == END) { |
| 439 | 27260 | set_status(ix+i, FREE_OR_USED); | |
| 440 | 27260 | break; | |
| 441 | } | ||
| 442 | } | ||
| 443 | } | ||
| 444 | |||
| 445 | 27260 | memory_num_free += count; | |
| 446 | 27260 | lbm_mutex_unlock(&lbm_mem_mutex); | |
| 447 | 27260 | return true; | |
| 448 | } | ||
| 449 | |||
| 450 | 22599969 | int lbm_memory_ptr_inside(lbm_uint *ptr) { | |
| 451 |
2/2✓ Branch 0 taken 22435371 times.
✓ Branch 1 taken 164598 times.
|
45035340 | return ((lbm_uint)ptr >= (lbm_uint)memory && |
| 452 |
2/2✓ Branch 0 taken 22435362 times.
✓ Branch 1 taken 9 times.
|
22435371 | (lbm_uint)ptr < (lbm_uint)&memory[memory_size]); |
| 453 | } | ||
| 454 |