Fix issues with the allocator

This commit is contained in:
Luke Benstead 2023-09-06 07:59:40 +01:00
parent e5a4f4f716
commit 12bd6f474f

View File

@ -1,6 +1,7 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include "alloc.h" #include "alloc.h"
@ -42,6 +43,7 @@
* *
* - Allocations < 2048 can still cross boundaries * - Allocations < 2048 can still cross boundaries
* - Only operates on one pool (ignores what you pass) * - Only operates on one pool (ignores what you pass)
* - If there are no 2048 aligned blocks, we should fall-back to unaligned
*/ */
#include <assert.h> #include <assert.h>
@ -222,6 +224,11 @@ static inline uint32_t subblock_from_pointer(void* p) {
return (ptr - pool_header.base_address) / 256; return (ptr - pool_header.base_address) / 256;
} }
static inline void block_and_offset_from_subblock(size_t sb, size_t* b, uint8_t* off) {
*b = sb / 8;
*off = (sb % 8);
}
void* alloc_malloc(void* pool, size_t size) { void* alloc_malloc(void* pool, size_t size) {
size_t start_subblock, required_subblocks; size_t start_subblock, required_subblocks;
void* ret = alloc_next_available_ex(pool, size, &start_subblock, &required_subblocks); void* ret = alloc_next_available_ex(pool, size, &start_subblock, &required_subblocks);
@ -231,13 +238,17 @@ void* alloc_malloc(void* pool, size_t size) {
} }
if(ret) { if(ret) {
size_t offset = start_subblock % 8; size_t block;
size_t block = start_subblock / 8; uint8_t offset;
block_and_offset_from_subblock(start_subblock, &block, &offset);
uint8_t mask = 0; uint8_t mask = 0;
/* Toggle any bits for the first block */ /* Toggle any bits for the first block */
for(int i = offset - 1; i >= 0; --i) { int c = (required_subblocks < 8) ? required_subblocks : 8;
mask |= (1 << i); for(int i = 0; i < c; ++i) {
mask |= (1 << (7 - (offset + i)));
required_subblocks--; required_subblocks--;
} }
@ -309,8 +320,10 @@ void alloc_free(void* pool, void* p) {
if(it->pointer == p) { if(it->pointer == p) {
size_t used_subblocks = size_to_subblock_count(it->size); size_t used_subblocks = size_to_subblock_count(it->size);
size_t subblock = subblock_from_pointer(p); size_t subblock = subblock_from_pointer(p);
size_t block = subblock / 8; size_t block;
size_t offset = subblock % 8; uint8_t offset;
block_and_offset_from_subblock(subblock, &block, &offset);
uint8_t mask = 0; uint8_t mask = 0;
/* Wipe out any leading subblocks */ /* Wipe out any leading subblocks */