diff --git a/heap.c b/heap.c index 820f4ac..d372d9c 100644 --- a/heap.c +++ b/heap.c @@ -11,9 +11,9 @@ static mblock_t* mblock_by_data(void* data) return (mblock_t*)((u8*)data - MBLOCK_SIZE); } -static u64 mblock_get_size(mblock_t* block) +static size_t mblock_get_size(mblock_t* block) { - return MBLOCK_ALIGN_SIZE(block->size); + return MBLOCK_ALIGN_SIZE(block->size & ~MBLOCK_ATTR_ALLOC); } void heap_init(mheap_t* heap, void* data, uword size) @@ -25,7 +25,7 @@ void heap_init(mheap_t* heap, void* data, uword size) void heap_join(mblock_t* start, int dir) { mblock_t* cur; - u64 blk_size; + size_t blk_size; blk_size = 0; cur = start; @@ -62,7 +62,7 @@ void heap_split(mblock_t* block, size_t req_size) size_t tot_size = mblock_get_size(block); mblock_t* split; - block->size = (req_size)|MBLOCK_ATTR_ALLOC; + block->size = MBLOCK_ALIGN_SIZE(req_size)|MBLOCK_ATTR_ALLOC; split = mblock_get_next(block); //Init prev and next diff --git a/heap.h b/heap.h index 2d2b859..aa4cc02 100644 --- a/heap.h +++ b/heap.h @@ -15,18 +15,18 @@ typedef struct { uword size; } mheap_t; -#define MBLOCK_SIZE (CU_WORD_SIZE*3) -#define MBLOCK_SIZE_MASK ((CU_WORD_BITS/8) - 1) -#define MBLOCK_ALIGN_SIZE(size) ((size) & ~MBLOCK_SIZE_MASK) -#define MBLOCK_ATTR_ALLOC (1<<0) -#define IS_MBLOCK_ALLOC(mblock) (mblock->size & MBLOCK_ATTR_ALLOC) - #ifdef CU_64BIT # define MBLOCK_ALIGN 5 #else # define MBLOCK_ALIGN 4 #endif +#define MBLOCK_SIZE (CU_WORD_SIZE*3) +#define MBLOCK_SIZE_MASK ((CU_WORD_BITS/8) - 1) +#define MBLOCK_ALIGN_SIZE(size) (cu_round2_up((size), MBLOCK_ALIGN)) +#define MBLOCK_ATTR_ALLOC (1<<0) +#define IS_MBLOCK_ALLOC(mblock) (mblock->size & MBLOCK_ATTR_ALLOC) + void heap_init(mheap_t* heap, void* data, uword size); void heap_join(mblock_t* start, int dir); void heap_split(mblock_t* block, size_t req_size); diff --git a/test.c b/test.c index ea37957..763d490 100644 --- a/test.c +++ b/test.c @@ -58,9 +58,9 @@ u8 internal_heap[4096]; int main() { cutil_init(internal_heap, 4096); - cu_malloc = malloc; + /*cu_malloc = malloc; cu_realloc = realloc; - cu_free = free; + cu_free = free;*/ printf("[list]\n"); list_t list; @@ -116,6 +116,7 @@ int main() printf("[bitmap]\n"); printf("bit_align2\t%u\n", bit_align2(354, 3)); printf("bit_log2\t%u\n", bit_log2(34)); + printf("cu_align2_up\t%u\n", cu_round2_up(354, 3)); num = 5; bit_set(num, 1, 1); printf("bit_get\t%u\n", bit_get(num, 0));