aboutsummaryrefslogtreecommitdiff
path: root/src
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
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')
-rw-r--r--src/kern/main.c4
-rw-r--r--src/kern/mem.c17
2 files changed, 13 insertions, 8 deletions
diff --git a/src/kern/main.c b/src/kern/main.c
index 1eedb59..a6743d5 100644
--- a/src/kern/main.c
+++ b/src/kern/main.c
@@ -2,6 +2,8 @@
#include "arch/stm32l4xxx/peripherals/clock.h"
#include "arch/stm32l4xxx/peripherals/system.h"
#include "kern/log.h"
+#include "kern/panic.h"
+#include "kern/init.h"
void on_systick() /* Overrides weak-symbol on_systick. */
{
@@ -14,6 +16,8 @@ void on_systick() /* Overrides weak-symbol on_systick. */
int main()
{
klogf("Hello, World! Clock Mhz: %d\n", (uint32_t)get_clock_mhz());
+ klogf("Heap Start: %p\n", &HEAP_START);
+ klogf("Heap End : %p\n", &HEAP_STOP);
/* Set the countdown to start from 10,000,0000. */
SCB.strv_r = 10000000;
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;
}