Clean up the allocator code

This commit is contained in:
Luke Benstead 2023-09-01 20:25:21 +01:00
parent 36de063756
commit b19b9d498a

View File

@ -116,41 +116,23 @@ void* alloc_next_available_ex(void* pool, size_t required_size, size_t* start_su
uint8_t* end = pool_header.block_usage + pool_header.block_count; uint8_t* end = pool_header.block_usage + pool_header.block_count;
/* Anything gte to 2048 must be aligned to a 2048 boundary */
bool requires_alignment = required_size >= 2048;
if(required_subblocks_out) { if(required_subblocks_out) {
*required_subblocks_out = required_subblocks; *required_subblocks_out = required_subblocks;
} }
while(it < end) { while(it < end) {
// Skip full blocks
while((*it) == 255) {
++it;
if(it >= pool_header.block_usage + sizeof(pool_header.block_usage)) {
return NULL;
}
continue;
}
uint32_t found_subblocks = 0; uint32_t found_subblocks = 0;
/* Anything gte to 2048 must be aligned to a 2048 boundary */
bool requires_alignment = required_size >= 2048;
/* We just need to find enough consecutive blocks */ /* We just need to find enough consecutive blocks */
while(found_subblocks < required_subblocks) { while(found_subblocks < required_subblocks) {
uint8_t t = *it; uint8_t t = *it;
/* Optimisation only. Skip over full blocks */ /* Optimisation only. Skip over full blocks */
if(t == 255) { if(t == 255) {
++it;
found_subblocks = 0; found_subblocks = 0;
} else {
if(it >= end) {
return NULL;
}
continue;
}
/* Now let's see how many consecutive blocks we can find */ /* Now let's see how many consecutive blocks we can find */
for(int i = 0; i < 8; ++i) { for(int i = 0; i < 8; ++i) {
if((t & 0x80) == 0) { if((t & 0x80) == 0) {
@ -165,6 +147,18 @@ void* alloc_next_available_ex(void* pool, size_t required_size, size_t* start_su
uintptr_t offset = (it - pool_header.block_usage) * 8; uintptr_t offset = (it - pool_header.block_usage) * 8;
offset += (i + 1); offset += (i + 1);
offset -= required_subblocks; offset -= required_subblocks;
return pool_header.base_address + (offset * 256);
}
}
} else {
found_subblocks = 0;
} else {
found_subblocks++;
if(found_subblocks >= required_subblocks) {
/* We found space! Now calculate the address */
uintptr_t offset = (it - pool_header.block_usage) * 8;
offset += (i + 1);
offset -= required_subblocks;
if(start_subblock_out) { if(start_subblock_out) {
*start_subblock_out = offset; *start_subblock_out = offset;
@ -173,19 +167,16 @@ void* alloc_next_available_ex(void* pool, size_t required_size, size_t* start_su
return pool_header.base_address + (offset * 256); return pool_header.base_address + (offset * 256);
} }
} }
} else {
found_subblocks = 0;
}
t <<= 1; t <<= 1;
} }
}
++it; ++it;
if(it >= end) { if(it >= end) {
return NULL; return NULL;
} }
} }
} }
return NULL; return NULL;
@ -279,7 +270,6 @@ void* alloc_malloc(void* pool, size_t size) {
pool_header.block_usage[block++] |= mask; pool_header.block_usage[block++] |= mask;
} }
/* Insert allocations in the list by size descending so that when we /* Insert allocations in the list by size descending so that when we
* defrag we can move the larger blocks before the smaller ones without * defrag we can move the larger blocks before the smaller ones without
* much effort */ * much effort */