Fix inital alignment
This commit is contained in:
@@ -19,7 +19,9 @@
|
||||
|
||||
|
||||
#define ALIGNMENT 16
|
||||
#define ALIGN(n) (((n) + (ALIGNMENT - 1)) & ~(ALIGNMENT - 1))
|
||||
#define ALIGN_UP(n) (((n) + (ALIGNMENT - 1)) & ~(ALIGNMENT - 1))
|
||||
#define ALIGN_DOWN(n) ((n) & ~(ALIGNMENT - 1))
|
||||
|
||||
#define NULL_BLOCK (0xffffffff)
|
||||
#define BLOCK_FREE ((uint32_t)1)
|
||||
|
||||
@@ -103,7 +105,7 @@ void small_allocator_init(SmallAllocator* a, void* backing_store, uint32_t size)
|
||||
a->size = size;
|
||||
|
||||
BlockHeader* first = backing_store;
|
||||
uint32_t usable = ALIGN(size - sizeof(BlockHeader));
|
||||
uint32_t usable = ALIGN_DOWN(size - sizeof(BlockHeader));
|
||||
block_set_free(first);
|
||||
block_set_size(first, usable);
|
||||
first->next = NULL_BLOCK;
|
||||
@@ -115,7 +117,7 @@ void small_allocator_init(SmallAllocator* a, void* backing_store, uint32_t size)
|
||||
|
||||
static void split_block(SmallAllocator *a, BlockHeader* block, uint32_t size)
|
||||
{
|
||||
size = ALIGN(size);
|
||||
size = ALIGN_UP(size);
|
||||
uint32_t bsize = block_size(block);
|
||||
|
||||
if (bsize < size + sizeof(BlockHeader) + ALIGNMENT)
|
||||
@@ -125,7 +127,7 @@ static void split_block(SmallAllocator *a, BlockHeader* block, uint32_t size)
|
||||
BlockHeader* new_block =
|
||||
(BlockHeader*)(base + sizeof(BlockHeader) + size);
|
||||
|
||||
uint32_t new_size = ALIGN(bsize - size - sizeof(BlockHeader));
|
||||
uint32_t new_size = ALIGN_UP(bsize - size - sizeof(BlockHeader));
|
||||
|
||||
block_set_size(block, size);
|
||||
block_set_used(block);
|
||||
@@ -149,7 +151,7 @@ static void merge_with_next(SmallAllocator *a, BlockHeader* block)
|
||||
if (!next || !block_is_free(next))
|
||||
return;
|
||||
|
||||
uint32_t new_size = ALIGN(block_size(block) +
|
||||
uint32_t new_size = ALIGN_UP(block_size(block) +
|
||||
sizeof(BlockHeader) +
|
||||
block_size(next));
|
||||
|
||||
@@ -162,7 +164,7 @@ static void merge_with_next(SmallAllocator *a, BlockHeader* block)
|
||||
|
||||
void* small_alloc(SmallAllocator* a, uint32_t size)
|
||||
{
|
||||
size = ALIGN(size);
|
||||
size = ALIGN_UP(size);
|
||||
|
||||
BlockHeader* start = a->last_alloc ? a->last_alloc : a->heap_start;
|
||||
BlockHeader* curr = start;
|
||||
@@ -221,7 +223,7 @@ void debug_alloc(const SmallAllocator *a)
|
||||
puts("--debug end--");
|
||||
}
|
||||
|
||||
char buff[1024*1024];
|
||||
char buff[1023*1023];
|
||||
int main(void)
|
||||
{
|
||||
printf("bh size %zu\n", sizeof(BlockHeader));
|
||||
|
||||
Reference in New Issue
Block a user