| 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 | 133277 | 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 66956 times.
✓ Branch 1 taken 66321 times.
|
133277 | if (!lbm_mem_mutex_initialized) { |
| 63 | 66956 | lbm_mutex_init(&lbm_mem_mutex); | |
| 64 | 66956 | lbm_mem_mutex_initialized = true; | |
| 65 | } | ||
| 66 | |||
| 67 | 133277 | alloc_offset = 0; | |
| 68 | |||
| 69 | 133277 | lbm_mutex_lock(&lbm_mem_mutex); | |
| 70 | 133277 | bool res = false; | |
| 71 |
4/4✓ Branch 0 taken 133272 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 133269 times.
✓ Branch 3 taken 3 times.
|
133277 | 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 133269 times.
✗ Branch 1 not taken.
|
133269 | if ((lbm_uint)data % sizeof(lbm_uint) == 0 && // Alignment req data. |
| 79 |
1/2✓ Branch 0 taken 133269 times.
✗ Branch 1 not taken.
|
133269 | (lbm_uint)bits % sizeof(lbm_uint) == 0 && // Alignment req bitmap. |
| 80 |
2/2✓ Branch 0 taken 133264 times.
✓ Branch 1 taken 5 times.
|
133269 | data_size *2 <= (bits_size * sizeof(lbm_uint)) * 8) { // size requirement. |
| 81 | 133264 | bitmap = bits; | |
| 82 | 133264 | bitmap_size = bits_size; | |
| 83 | |||
| 84 |
2/2✓ Branch 0 taken 207373692 times.
✓ Branch 1 taken 133264 times.
|
207506956 | for (lbm_uint i = 0; i < bitmap_size; i ++) { |
| 85 | 207373692 | bitmap[i] = 0; | |
| 86 | } | ||
| 87 | |||
| 88 | 133264 | memory = data; | |
| 89 | 133264 | memory_base_address = (lbm_uint)data; | |
| 90 | 133264 | memory_size = data_size; | |
| 91 | 133264 | memory_min_free = data_size; | |
| 92 | 133264 | memory_num_free = data_size; | |
| 93 | 133264 | memory_reserve_level = (lbm_uint)(0.1 * (lbm_float)data_size); | |
| 94 | 133264 | res = true; | |
| 95 | } | ||
| 96 | } | ||
| 97 | 133277 | lbm_mutex_unlock(&lbm_mem_mutex); | |
| 98 | 133277 | 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 | 22507351 | static inline lbm_uint address_to_bitmap_ix(lbm_uint *ptr) { | |
| 113 | #ifndef LBM64 | ||
| 114 | 20639669 | return ((lbm_uint)ptr - memory_base_address) >> 2; | |
| 115 | #else | ||
| 116 | 1867682 | return ((lbm_uint)ptr - memory_base_address) >> 3; | |
| 117 | #endif | ||
| 118 | } | ||
| 119 | |||
| 120 | 70550 | 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 | 70550 | return (lbm_int)address_to_bitmap_ix(ptr); | |
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | 22926824 | static inline lbm_uint *bitmap_ix_to_address(lbm_uint ix) { | |
| 129 | 22926824 | 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 | 767459892 | static inline lbm_uint status(lbm_uint i) { | |
| 143 | |||
| 144 | 767459892 | lbm_uint ix = i << 1; // * 2 | |
| 145 | 767459892 | lbm_uint word_ix = ix >> WORD_IX_SHIFT; // / 32 | |
| 146 | 767459892 | lbm_uint bit_ix = ix & WORD_MOD_MASK; // % 32 | |
| 147 | |||
| 148 | 767459892 | lbm_uint mask = ((lbm_uint)3) << bit_ix; // 000110..0 | |
| 149 | 767459892 | return (bitmap[word_ix] & mask) >> bit_ix; | |
| 150 | } | ||
| 151 | |||
| 152 | 90096582 | static inline void set_status(lbm_uint i, lbm_uint status) { | |
| 153 | 90096582 | lbm_uint ix = i << 1; // * 2 | |
| 154 | 90096582 | lbm_uint word_ix = ix >> WORD_IX_SHIFT; // / 32 | |
| 155 | 90096582 | lbm_uint bit_ix = ix & WORD_MOD_MASK; // % 32 | |
| 156 | |||
| 157 | 90096582 | lbm_uint clr_mask = ~(((lbm_uint)3) << bit_ix); | |
| 158 | 90096582 | lbm_uint mask = status << bit_ix; | |
| 159 | |||
| 160 | 90096582 | bitmap[word_ix] &= clr_mask; | |
| 161 | 90096582 | bitmap[word_ix] |= mask; | |
| 162 | 90096582 | } | |
| 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 | 1610809 | void lbm_memory_update_min_free(void) { | |
| 177 |
2/2✓ Branch 0 taken 9600 times.
✓ Branch 1 taken 1601209 times.
|
1610809 | if (memory_num_free < memory_min_free) |
| 178 | 9600 | memory_min_free = memory_num_free; | |
| 179 | 1610809 | } | |
| 180 | |||
| 181 | 12694 | lbm_uint lbm_memory_longest_free(void) { | |
| 182 |
3/4✓ Branch 0 taken 12693 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12693 times.
|
12694 | if (memory == NULL || bitmap == NULL) { |
| 183 | 1 | return 0; | |
| 184 | } | ||
| 185 | 12693 | lbm_mutex_lock(&lbm_mem_mutex); | |
| 186 | 12693 | unsigned int state = INIT; | |
| 187 | 12693 | lbm_uint max_length = 0; | |
| 188 | |||
| 189 | 12693 | lbm_uint curr_length = 0; | |
| 190 |
2/2✓ Branch 0 taken 51369216 times.
✓ Branch 1 taken 12693 times.
|
51381909 | 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 50971615 times.
✓ Branch 1 taken 191098 times.
✓ Branch 2 taken 191098 times.
✓ Branch 3 taken 15405 times.
|
51369216 | switch(status(i)) { |
| 194 | 50971615 | case FREE_OR_USED: | |
| 195 |
3/4✓ Branch 0 taken 14947 times.
✓ Branch 1 taken 39579314 times.
✓ Branch 2 taken 11377354 times.
✗ Branch 3 not taken.
|
50971615 | switch (state) { |
| 196 | 14947 | case INIT: | |
| 197 | 14947 | curr_length = 1; | |
| 198 |
2/2✓ Branch 0 taken 12693 times.
✓ Branch 1 taken 2254 times.
|
14947 | if (curr_length > max_length) max_length = curr_length; |
| 199 | 14947 | state = FREE_LENGTH_CHECK; | |
| 200 | 14947 | break; | |
| 201 | 39579314 | case FREE_LENGTH_CHECK: | |
| 202 | 39579314 | curr_length ++; | |
| 203 |
2/2✓ Branch 0 taken 39483669 times.
✓ Branch 1 taken 95645 times.
|
39579314 | if (curr_length > max_length) max_length = curr_length; |
| 204 | 39579314 | state = FREE_LENGTH_CHECK; | |
| 205 | 39579314 | break; | |
| 206 | 11377354 | case SKIP: | |
| 207 | 11377354 | break; | |
| 208 | } | ||
| 209 | 50971615 | break; | |
| 210 | 191098 | case END: | |
| 211 | 191098 | state = INIT; | |
| 212 | 191098 | break; | |
| 213 | 191098 | case START: | |
| 214 | 191098 | state = SKIP; | |
| 215 | 191098 | break; | |
| 216 | 15405 | default: // START_END | |
| 217 | 15405 | state = INIT; | |
| 218 | 15405 | break; | |
| 219 | } | ||
| 220 | } | ||
| 221 | 12693 | 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 12687 times.
✓ Branch 1 taken 6 times.
|
12693 | if (memory_num_free - max_length < memory_reserve_level) { |
| 227 | 12687 | lbm_uint n = memory_reserve_level - (memory_num_free - max_length); | |
| 228 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12687 times.
|
12687 | if (n >= max_length) { |
| 229 | ✗ | max_length = 0; | |
| 230 | } else { | ||
| 231 | 12687 | max_length -= n; | |
| 232 | } | ||
| 233 | } | ||
| 234 | 12693 | return max_length; | |
| 235 | } | ||
| 236 | |||
| 237 | 22955511 | static lbm_uint *lbm_memory_allocate_internal(lbm_uint num_words, bool use_reserve) { | |
| 238 | |||
| 239 |
3/4✓ Branch 0 taken 22955510 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 22955510 times.
|
22955511 | if (memory == NULL || bitmap == NULL) { |
| 240 | 1 | return NULL; | |
| 241 | } | ||
| 242 | |||
| 243 | 22955510 | lbm_mutex_lock(&lbm_mem_mutex); | |
| 244 | |||
| 245 |
2/2✓ Branch 0 taken 14177 times.
✓ Branch 1 taken 22941333 times.
|
22955510 | 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 22794359 times.
✓ Branch 1 taken 146974 times.
✓ Branch 2 taken 14508 times.
✓ Branch 3 taken 22779851 times.
|
22941333 | 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 | 22926825 | lbm_uint start_ix = 0; | |
| 258 | 22926825 | lbm_uint end_ix = 0; | |
| 259 | 22926825 | lbm_uint free_length = 0; | |
| 260 | 22926825 | unsigned int state = INIT; | |
| 261 | |||
| 262 |
2/2✓ Branch 0 taken 214404122 times.
✓ Branch 1 taken 1 times.
|
214404123 | for (lbm_uint i = 0; i < memory_size; i ++) { |
| 263 |
4/4✓ Branch 0 taken 191569097 times.
✓ Branch 1 taken 22450717 times.
✓ Branch 2 taken 50059 times.
✓ Branch 3 taken 334249 times.
|
214404122 | switch(status(alloc_offset)) { |
| 264 | 191569097 | case FREE_OR_USED: | |
| 265 |
3/4✓ Branch 0 taken 22932056 times.
✓ Branch 1 taken 167712056 times.
✓ Branch 2 taken 924985 times.
✗ Branch 3 not taken.
|
191569097 | switch (state) { |
| 266 | 22932056 | case INIT: | |
| 267 | 22932056 | start_ix = alloc_offset; | |
| 268 |
2/2✓ Branch 0 taken 329590 times.
✓ Branch 1 taken 22602466 times.
|
22932056 | if (num_words == 1) { |
| 269 | 329590 | end_ix = alloc_offset; | |
| 270 | 329590 | state = ALLOC_DONE; | |
| 271 | } else { | ||
| 272 | 22602466 | state = FREE_LENGTH_CHECK; | |
| 273 | 22602466 | free_length = 1; | |
| 274 | } | ||
| 275 | 22932056 | break; | |
| 276 | 167712056 | case FREE_LENGTH_CHECK: | |
| 277 | 167712056 | free_length ++; | |
| 278 |
2/2✓ Branch 0 taken 22597234 times.
✓ Branch 1 taken 145114822 times.
|
167712056 | if (free_length == num_words) { |
| 279 | 22597234 | end_ix = alloc_offset; | |
| 280 | 22597234 | state = ALLOC_DONE; | |
| 281 | } else { | ||
| 282 | 145114822 | state = FREE_LENGTH_CHECK; | |
| 283 | } | ||
| 284 | 167712056 | break; | |
| 285 | 924985 | case SKIP: | |
| 286 | 924985 | break; | |
| 287 | } | ||
| 288 | 191569097 | break; | |
| 289 | 22450717 | case END: | |
| 290 | 22450717 | state = INIT; | |
| 291 | 22450717 | break; | |
| 292 | 50059 | case START: | |
| 293 | 50059 | state = SKIP; | |
| 294 | 50059 | break; | |
| 295 | 334249 | default: // START_END | |
| 296 | 334249 | state = INIT; | |
| 297 | 334249 | break; | |
| 298 | } | ||
| 299 | |||
| 300 |
2/2✓ Branch 0 taken 22926824 times.
✓ Branch 1 taken 191477298 times.
|
214404122 | if (state == ALLOC_DONE) break; |
| 301 | |||
| 302 | 191477298 | alloc_offset++; | |
| 303 |
2/2✓ Branch 0 taken 127 times.
✓ Branch 1 taken 191477171 times.
|
191477298 | if (alloc_offset == memory_size ) { |
| 304 | 127 | free_length = 0; | |
| 305 | 127 | alloc_offset = 0; | |
| 306 | 127 | state = INIT; | |
| 307 | } | ||
| 308 | } | ||
| 309 | |||
| 310 |
2/2✓ Branch 0 taken 22926824 times.
✓ Branch 1 taken 1 times.
|
22926825 | if (state == ALLOC_DONE) { |
| 311 |
2/2✓ Branch 0 taken 329590 times.
✓ Branch 1 taken 22597234 times.
|
22926824 | if (start_ix == end_ix) { |
| 312 | 329590 | set_status(start_ix, START_END); | |
| 313 | } else { | ||
| 314 | 22597234 | set_status(start_ix, START); | |
| 315 | 22597234 | set_status(end_ix, END); | |
| 316 | } | ||
| 317 | 22926824 | memory_num_free -= num_words; | |
| 318 | 22926824 | lbm_mutex_unlock(&lbm_mem_mutex); | |
| 319 | 22926824 | return bitmap_ix_to_address(start_ix); | |
| 320 | } | ||
| 321 | 1 | lbm_mutex_unlock(&lbm_mem_mutex); | |
| 322 | 1 | return NULL; | |
| 323 | } | ||
| 324 | |||
| 325 | 143163 | lbm_uint *lbm_memory_allocate(lbm_uint num_words) { | |
| 326 | 143163 | return lbm_memory_allocate_internal(num_words, false); | |
| 327 | } | ||
| 328 | |||
| 329 | 22555285 | int lbm_memory_free(lbm_uint *ptr) { | |
| 330 | 22555285 | int r = 0; | |
| 331 |
2/2✓ Branch 0 taken 22407770 times.
✓ Branch 1 taken 147515 times.
|
22555285 | if (lbm_memory_ptr_inside(ptr)) { |
| 332 | 22407770 | lbm_mutex_lock(&lbm_mem_mutex); | |
| 333 | 22407770 | lbm_uint ix = address_to_bitmap_ix(ptr); | |
| 334 | 22407770 | lbm_uint count_freed = 0; | |
| 335 | 22407770 | alloc_offset = ix; | |
| 336 |
3/3✓ Branch 0 taken 22106700 times.
✓ Branch 1 taken 301068 times.
✓ Branch 2 taken 2 times.
|
22407770 | switch(status(ix)) { |
| 337 | 22106700 | case START: | |
| 338 | 22106700 | set_status(ix, FREE_OR_USED); | |
| 339 |
1/2✓ Branch 0 taken 128359566 times.
✗ Branch 1 not taken.
|
128359566 | for (lbm_uint i = ix; i < memory_size; i ++) { |
| 340 | 128359566 | count_freed ++; | |
| 341 |
2/2✓ Branch 0 taken 22106700 times.
✓ Branch 1 taken 106252866 times.
|
128359566 | if (status(i) == END) { |
| 342 | 22106700 | set_status(i, FREE_OR_USED); | |
| 343 | 22106700 | r = 1; | |
| 344 | 22106700 | break; | |
| 345 | } | ||
| 346 | } | ||
| 347 | 22106700 | break; | |
| 348 | 301068 | case START_END: | |
| 349 | 301068 | set_status(ix, FREE_OR_USED); | |
| 350 | 301068 | count_freed = 1; | |
| 351 | 301068 | r = 1; | |
| 352 | 301068 | break; | |
| 353 | 2 | default: | |
| 354 | 2 | break; | |
| 355 | } | ||
| 356 |
2/2✓ Branch 0 taken 22407768 times.
✓ Branch 1 taken 2 times.
|
22407770 | if (r) { |
| 357 |
4/4✓ Branch 0 taken 321403605 times.
✓ Branch 1 taken 1319 times.
✓ Branch 2 taken 298997156 times.
✓ Branch 3 taken 22406449 times.
|
321404924 | while (alloc_offset > 0 && status(alloc_offset - 1) == FREE_OR_USED) { |
| 358 | 298997156 | alloc_offset--; | |
| 359 | } | ||
| 360 | } | ||
| 361 | 22407770 | memory_num_free += count_freed; | |
| 362 | 22407770 | lbm_mutex_unlock(&lbm_mem_mutex); | |
| 363 | } | ||
| 364 | 22555285 | return r; | |
| 365 | } | ||
| 366 | //Malloc/free like interface | ||
| 367 | 22665375 | void* lbm_malloc(size_t size) { | |
| 368 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 22665374 times.
|
22665375 | if (size == 0) return NULL; |
| 369 | lbm_uint alloc_size; | ||
| 370 | |||
| 371 | 22665374 | alloc_size = size / sizeof(lbm_uint); | |
| 372 |
2/2✓ Branch 0 taken 684370 times.
✓ Branch 1 taken 21981004 times.
|
22665374 | if (size % sizeof(lbm_uint)) alloc_size += 1; |
| 373 | |||
| 374 | 22665374 | return lbm_memory_allocate_internal(alloc_size, false); | |
| 375 | } | ||
| 376 | |||
| 377 | 146976 | void* lbm_malloc_reserve(size_t size) { | |
| 378 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 146974 times.
|
146976 | if (size == 0) return NULL; |
| 379 | lbm_uint alloc_size; | ||
| 380 | |||
| 381 | 146974 | alloc_size = size / sizeof(lbm_uint); | |
| 382 |
2/2✓ Branch 0 taken 119698 times.
✓ Branch 1 taken 27276 times.
|
146974 | if (size % sizeof(lbm_uint)) alloc_size += 1; |
| 383 | |||
| 384 | 146974 | return lbm_memory_allocate_internal(alloc_size, true); | |
| 385 | } | ||
| 386 | |||
| 387 | 120422 | void lbm_free(void *ptr) { | |
| 388 | 120422 | lbm_memory_free(ptr); | |
| 389 | 120422 | } | |
| 390 | |||
| 391 | 29038 | int lbm_memory_shrink(lbm_uint *ptr, lbm_uint n) { | |
| 392 |
4/4✓ Branch 0 taken 29035 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 29031 times.
|
29038 | if (!lbm_memory_ptr_inside(ptr) || n == 0) return 0; |
| 393 | |||
| 394 | 29031 | lbm_uint ix = address_to_bitmap_ix(ptr); | |
| 395 | |||
| 396 | 29031 | lbm_mutex_lock(&lbm_mem_mutex); | |
| 397 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 29029 times.
|
29031 | 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 0 not taken.
✓ Branch 1 taken 29029 times.
|
29029 | 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 | 29029 | bool done = false; | |
| 407 | 29029 | unsigned int i = 0; | |
| 408 | |||
| 409 |
1/2✓ Branch 0 taken 294902 times.
✗ Branch 1 not taken.
|
294902 | for (i = 0; i < (memory_size - ix); i ++) { |
| 410 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 294901 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
294902 | 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 29028 times.
✓ Branch 1 taken 265873 times.
|
294901 | if (i == (n-1)) { |
| 416 |
2/4✓ Branch 0 taken 29028 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 29028 times.
|
58056 | if (status(ix+i) == END || |
| 417 | 29028 | status(ix+i) == START_END) { | |
| 418 | ✗ | done = true; | |
| 419 | } | ||
| 420 |
2/2✓ Branch 0 taken 5754 times.
✓ Branch 1 taken 23274 times.
|
29028 | if (i == 0) { |
| 421 | 5754 | set_status(ix+i, START_END); | |
| 422 | } | ||
| 423 | else { | ||
| 424 | 23274 | set_status(ix+i, END); | |
| 425 | } | ||
| 426 | 29028 | break; | |
| 427 | } | ||
| 428 | } | ||
| 429 | 29028 | alloc_offset = ix+i; | |
| 430 | |||
| 431 | 29028 | lbm_uint count = 0; | |
| 432 |
1/2✓ Branch 0 taken 29028 times.
✗ Branch 1 not taken.
|
29028 | if (!done) { |
| 433 | 29028 | i++; // move to next position, prev position should be END or START_END | |
| 434 |
1/2✓ Branch 0 taken 29104595 times.
✗ Branch 1 not taken.
|
29104595 | for (;i < (memory_size - ix); i ++) { |
| 435 | 29104595 | count ++; | |
| 436 |
2/2✓ Branch 0 taken 29028 times.
✓ Branch 1 taken 29075567 times.
|
29104595 | if (status(ix+i) == END) { |
| 437 | 29028 | set_status(ix+i, FREE_OR_USED); | |
| 438 | 29028 | break; | |
| 439 | } | ||
| 440 | } | ||
| 441 | } | ||
| 442 | |||
| 443 | 29028 | memory_num_free += count; | |
| 444 | 29028 | lbm_mutex_unlock(&lbm_mem_mutex); | |
| 445 | 29028 | return 1; | |
| 446 | } | ||
| 447 | |||
| 448 | 22599715 | int lbm_memory_ptr_inside(lbm_uint *ptr) { | |
| 449 |
2/2✓ Branch 0 taken 22436825 times.
✓ Branch 1 taken 162890 times.
|
45036540 | return ((lbm_uint)ptr >= (lbm_uint)memory && |
| 450 |
2/2✓ Branch 0 taken 22436817 times.
✓ Branch 1 taken 8 times.
|
22436825 | (lbm_uint)ptr < (lbm_uint)&memory[memory_size]); |
| 451 | } | ||
| 452 |