aboutsummaryrefslogtreecommitdiff
path: root/src/kern/mem.c
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2020-11-24 15:59:23 -0700
committerJosh Rahm <joshuarahm@gmail.com>2020-11-24 15:59:23 -0700
commit0c0f5c3d8397ba5168f0cd01b25ba70c238b36e0 (patch)
treed986372cf7266cd1e090b7974e744c77c1871bdc /src/kern/mem.c
parentecbcb2509f4b811bce0a56e07de9737d14815251 (diff)
downloadstm32l4-0c0f5c3d8397ba5168f0cd01b25ba70c238b36e0.tar.gz
stm32l4-0c0f5c3d8397ba5168f0cd01b25ba70c238b36e0.tar.bz2
stm32l4-0c0f5c3d8397ba5168f0cd01b25ba70c238b36e0.zip
Fix kalloc. Now the HEAP START and HEAP END globals are defined by the linker script.
Diffstat (limited to 'src/kern/mem.c')
-rw-r--r--src/kern/mem.c17
1 files changed, 9 insertions, 8 deletions
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;
}