diff --git a/heap.c b/heap.c index 6d0edc1..820f4ac 100644 --- a/heap.c +++ b/heap.c @@ -3,7 +3,7 @@ static mblock_t* mblock_get_next(mblock_t* block) { - return (mblock_t*)((u8*)block+(block->size & ~7)); + return (mblock_t*)((u8*)block+MBLOCK_ALIGN_SIZE(block->size)); } static mblock_t* mblock_by_data(void* data) @@ -13,7 +13,7 @@ static mblock_t* mblock_by_data(void* data) static u64 mblock_get_size(mblock_t* block) { - return block->size & ~7; + return MBLOCK_ALIGN_SIZE(block->size); } void heap_init(mheap_t* heap, void* data, uword size) @@ -40,7 +40,7 @@ void heap_join(mblock_t* start, int dir) if(dir > 0) { start->next = cur; - start->size = blk_size & ~7; + start->size = MBLOCK_ALIGN_SIZE(blk_size); if(cur) { if(cur->next) cur->next->prev = start; @@ -51,7 +51,7 @@ void heap_join(mblock_t* start, int dir) if(cur) { cur = cur->next; - cur->size = blk_size & ~7; + cur->size = MBLOCK_ALIGN_SIZE(blk_size); cur->next = start->next; } } @@ -70,7 +70,7 @@ void heap_split(mblock_t* block, size_t req_size) split->next = block->next; block->next = split; - split->size = (tot_size-req_size) & ~7; + split->size = MBLOCK_ALIGN_SIZE(tot_size-req_size); } void* heap_alloc(mheap_t* heap, size_t size) diff --git a/heap.h b/heap.h index 1bb3f7d..2d2b859 100644 --- a/heap.h +++ b/heap.h @@ -16,6 +16,8 @@ typedef struct { } 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) diff --git a/test.c b/test.c index ae6c626..b17fc68 100644 --- a/test.c +++ b/test.c @@ -196,7 +196,7 @@ int main() char str5[8] = {0}; iword val5 = 0; cu_sscanf((char*)str4, "\"%s\" %d", str5, 8, &val5); - printf("cu_sscanf\t%s\t%u\n", str5, val5); + printf("cu_sscanf\t%s\t%u\n", str5, val5);*/ printf("[heap]\n"); static u8 heap_data[4096]; @@ -213,7 +213,7 @@ int main() printf("heap_alloc\t%p\n", m3); printf("heap_alloc\t%p\n", m4); void* m5 = heap_realloc(&heap, m4, 10); - printf("heap_realloc\t%p\t%p\n", m4, m5);*/ + printf("heap_realloc\t%p\t%p\n", m4, m5); cutil_exit(); return 0;