TODO: rewrite dynamic memory allocator (heap), found critical issues

This commit is contained in:
mykola2312 2022-05-21 16:32:11 +03:00
parent 003e51b692
commit df1f6df916
3 changed files with 13 additions and 12 deletions

8
heap.c
View file

@ -11,9 +11,9 @@ static mblock_t* mblock_by_data(void* data)
return (mblock_t*)((u8*)data - MBLOCK_SIZE); 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) 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) void heap_join(mblock_t* start, int dir)
{ {
mblock_t* cur; mblock_t* cur;
u64 blk_size; size_t blk_size;
blk_size = 0; blk_size = 0;
cur = start; cur = start;
@ -62,7 +62,7 @@ void heap_split(mblock_t* block, size_t req_size)
size_t tot_size = mblock_get_size(block); size_t tot_size = mblock_get_size(block);
mblock_t* split; 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); split = mblock_get_next(block);
//Init prev and next //Init prev and next

12
heap.h
View file

@ -15,18 +15,18 @@ typedef struct {
uword size; uword size;
} mheap_t; } 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 #ifdef CU_64BIT
# define MBLOCK_ALIGN 5 # define MBLOCK_ALIGN 5
#else #else
# define MBLOCK_ALIGN 4 # define MBLOCK_ALIGN 4
#endif #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_init(mheap_t* heap, void* data, uword size);
void heap_join(mblock_t* start, int dir); void heap_join(mblock_t* start, int dir);
void heap_split(mblock_t* block, size_t req_size); void heap_split(mblock_t* block, size_t req_size);

5
test.c
View file

@ -58,9 +58,9 @@ u8 internal_heap[4096];
int main() int main()
{ {
cutil_init(internal_heap, 4096); cutil_init(internal_heap, 4096);
cu_malloc = malloc; /*cu_malloc = malloc;
cu_realloc = realloc; cu_realloc = realloc;
cu_free = free; cu_free = free;*/
printf("[list]\n"); printf("[list]\n");
list_t list; list_t list;
@ -116,6 +116,7 @@ int main()
printf("[bitmap]\n"); printf("[bitmap]\n");
printf("bit_align2\t%u\n", bit_align2(354, 3)); printf("bit_align2\t%u\n", bit_align2(354, 3));
printf("bit_log2\t%u\n", bit_log2(34)); printf("bit_log2\t%u\n", bit_log2(34));
printf("cu_align2_up\t%u\n", cu_round2_up(354, 3));
num = 5; num = 5;
bit_set(num, 1, 1); bit_set(num, 1, 1);
printf("bit_get\t%u\n", bit_get(num, 0)); printf("bit_get\t%u\n", bit_get(num, 0));