From 0c0f5c3d8397ba5168f0cd01b25ba70c238b36e0 Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Tue, 24 Nov 2020 15:59:23 -0700 Subject: Fix kalloc. Now the HEAP START and HEAP END globals are defined by the linker script. --- src/kern/mem.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'src/kern/mem.c') diff --git a/src/kern/mem.c b/src/kern/mem.c index eb4527e..533b394 100644 --- a/src/kern/mem.c +++ b/src/kern/mem.c @@ -1,7 +1,6 @@ #include "kern/mem.h" #include "arch.h" - #include "kern/common.h" #include "kern/panic.h" @@ -55,8 +54,11 @@ static_assert(offsetof(kalloc_node_t, mem) == 4, "Offset check failed."); kalloc_node_t* kalloc_start; -#define kalloc_node_out_of_range(node) \ - ((uint8_t*)(node) == ((uint8_t*)&DATA_SEGMENT_STOP) + MAX_HEAP_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 MAX_HEAP_SIZE ((&HEAP_STOP - &REAL_HEAP_START)) +#define kalloc_node_out_of_range(node) ((void*)(node) >= (void*)&HEAP_STOP) #define kalloc_node_next(cur) \ ((kalloc_node_t*)(((uint8_t*)(cur)) + (((cur)->size + 1) * 4))) @@ -76,7 +78,7 @@ kalloc_node_t* kalloc_start; void* kalloc(size_t size) { if (!kalloc_start) { - kalloc_start = (kalloc_node_t*)DATA_SEGMENT_STOP_ADDR; + kalloc_start = (kalloc_node_t*)&REAL_HEAP_START; memset(kalloc_start, 0, sizeof(kalloc_node_t)); kalloc_start->size = (MAX_HEAP_SIZE / 4) - 1; kalloc_start->canary = CANARY; @@ -224,16 +226,15 @@ int debug_kalloc_assert_consistency(char* error, size_t len) total_size += cur->size + 1; kalloc_node_t* next = kalloc_node_next(cur); - if ((uint8_t*)next == ((uint8_t*)&DATA_SEGMENT_STOP) + MAX_HEAP_SIZE) { + if ((void*)next == (void*)&HEAP_STOP) { break; - } else if ( - (uint8_t*)next > (uint8_t*)DATA_SEGMENT_STOP_ADDR + MAX_HEAP_SIZE) { + } else if ((void*)next > (void*)&HEAP_STOP) { snprintf( error, len, "Next node points is out of bounds. %p vs max of %p\n", next, - (void*)(DATA_SEGMENT_STOP_ADDR + MAX_HEAP_SIZE)); + &HEAP_STOP); return 1; } -- cgit