From 62a3cd24473506b9280138400fc89db92b7d8579 Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Tue, 24 May 2022 23:15:58 +0300 Subject: [PATCH] fix heap_realloc that it will do aligned resize in mblock_t units --- heap.c | 2 +- test.c | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/heap.c b/heap.c index a415e05..83b461b 100644 --- a/heap.c +++ b/heap.c @@ -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) diff --git a/test.c b/test.c index 763d490..f4252eb 100644 --- a/test.c +++ b/test.c @@ -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) {