fix heap_realloc that it will do aligned resize in mblock_t units

This commit is contained in:
mykola2312 2022-05-24 23:15:58 +03:00
parent 0570f9fdd9
commit 62a3cd2447
2 changed files with 16 additions and 1 deletions

2
heap.c
View file

@ -70,7 +70,7 @@ void heap_free(mheap_t* heap, mblock_t* block)
mblock_t* heap_realloc(mheap_t* heap, mblock_t* block, uword _size)
{
block->size &= ~MBLOCK_ALLOCATED;
uword size = cu_round2_up(_size, MBLOCK_ALIGN);
uword size = cu_round2_up(_size + sizeof(mblock_t), MBLOCK_ALIGN);
mblock_t* next = (mblock_t*)((u8*)block + block->size);
mblock_t* new;
if (!(next->size & MBLOCK_ALLOCATED) && next->size >= size)

15
test.c
View file

@ -55,6 +55,20 @@ u64 u64_test(u64 first, u64 second)
u8 internal_heap[4096];
static void heap_debug(mheap_t* heap)
{
mblock_t* block = (mblock_t*)((uint8_t*)heap->start);
mblock_t* end = (mblock_t*)((uint8_t*)heap->start + heap->size);
printf("heap\t%p\t%p\n", block, end);
while (block < end && MBLOCK_SIZE(block->size))
{
printf("block\t%p\t%u\n", block, block->size);
block = (mblock_t*)((uint8_t*)block + MBLOCK_SIZE(block->size));
}
}
extern mheap_t cu_heap;
int main()
{
cutil_init(internal_heap, 4096);
@ -96,6 +110,7 @@ int main()
uint num = 42;
array_remove(&array, 0);
array_insert(&array, 1, &num);
heap_debug(&cu_heap);
CU_ARRAY_FOREACH(&array)
{