diff options
Diffstat (limited to 'src/kern/mem.c')
-rw-r--r-- | src/kern/mem.c | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/src/kern/mem.c b/src/kern/mem.c index 31756e5..aa221ff 100644 --- a/src/kern/mem.c +++ b/src/kern/mem.c @@ -44,16 +44,19 @@ typedef struct KALLOC_NODE { uint8_t mem[]; /* The memory to use. */ } PACKED kalloc_node_t; -kalloc_node_t* kalloc_start; +#ifdef ARCH_PC +typedef uint64_t ptrint_t; +#else +typedef uint32_t ptrint_t; +#endif -#define CANARY 0xdeadbeee +#define CANARY ((uint32_t) 0xdeadbeee) #define kalloc_node_in_use(node) ((node)->used_and_canary & 1) #define kalloc_node_get_canary(node) ((node)->used_and_canary & (~1)) #define WORD_SIZE (sizeof(uint32_t)) #define SIZEOF_KALLOC_NODE_WORDS (sizeof(kalloc_node_t) / WORD_SIZE) -#define HEAP_START_ADDR ((ptrdiff_t)&HEAP_START) -#define REAL_HEAP_START \ - (*((unsigned char*)((HEAP_START_ADDR & (~3)) + (HEAP_START_ADDR % 4 != 0)))) +#define HEAP_START_ADDR ((ptrint_t)&HEAP_START) +#define REAL_HEAP_START *(uint8_t*)(HEAP_START_ADDR + (4 - HEAP_START_ADDR % 4)) #define MAX_HEAP_SIZE ((&HEAP_STOP - &REAL_HEAP_START)) #define MAX_HEAP_SIZE_WORDS (MAX_HEAP_SIZE / WORD_SIZE) #define kalloc_node_out_of_range(node) ((void*)(node) >= (void*)&HEAP_STOP) @@ -74,6 +77,8 @@ kalloc_node_t* kalloc_start; #define size_for(n) (((n) / 4) + ((n) % 4 != 0)) +kalloc_node_t* kalloc_start; + void kalloc_init() { kalloc_start = (kalloc_node_t*)&REAL_HEAP_START; @@ -158,7 +163,8 @@ static void coalesce(kalloc_node_t* cur) next_used->prev = kalloc_node_get_off(last_freed); } - last_freed->size_words = ((uint8_t*)next_used - (last_freed->mem)) / WORD_SIZE; + last_freed->size_words = + ((uint8_t*)next_used - (last_freed->mem)) / WORD_SIZE; } void kfree(void* mem) |