make heap working on 32 bit ARMv7

This commit is contained in:
mykola2312 2022-05-21 09:15:47 +03:00
parent 089366af77
commit 5448323450
3 changed files with 9 additions and 7 deletions

10
heap.c
View file

@ -3,7 +3,7 @@
static mblock_t* mblock_get_next(mblock_t* block) 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) 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) 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) 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) if(dir > 0)
{ {
start->next = cur; start->next = cur;
start->size = blk_size & ~7; start->size = MBLOCK_ALIGN_SIZE(blk_size);
if(cur) if(cur)
{ {
if(cur->next) cur->next->prev = start; if(cur->next) cur->next->prev = start;
@ -51,7 +51,7 @@ void heap_join(mblock_t* start, int dir)
if(cur) if(cur)
{ {
cur = cur->next; cur = cur->next;
cur->size = blk_size & ~7; cur->size = MBLOCK_ALIGN_SIZE(blk_size);
cur->next = start->next; cur->next = start->next;
} }
} }
@ -70,7 +70,7 @@ void heap_split(mblock_t* block, size_t req_size)
split->next = block->next; split->next = block->next;
block->next = split; 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) void* heap_alloc(mheap_t* heap, size_t size)

2
heap.h
View file

@ -16,6 +16,8 @@ typedef struct {
} mheap_t; } mheap_t;
#define MBLOCK_SIZE (CU_WORD_SIZE*3) #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 MBLOCK_ATTR_ALLOC (1<<0)
#define IS_MBLOCK_ALLOC(mblock) (mblock->size & MBLOCK_ATTR_ALLOC) #define IS_MBLOCK_ALLOC(mblock) (mblock->size & MBLOCK_ATTR_ALLOC)

4
test.c
View file

@ -196,7 +196,7 @@ int main()
char str5[8] = {0}; char str5[8] = {0};
iword val5 = 0; iword val5 = 0;
cu_sscanf((char*)str4, "\"%s\" %d", str5, 8, &val5); 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"); printf("[heap]\n");
static u8 heap_data[4096]; static u8 heap_data[4096];
@ -213,7 +213,7 @@ int main()
printf("heap_alloc\t%p\n", m3); printf("heap_alloc\t%p\n", m3);
printf("heap_alloc\t%p\n", m4); printf("heap_alloc\t%p\n", m4);
void* m5 = heap_realloc(&heap, m4, 10); 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(); cutil_exit();
return 0; return 0;