Compare commits

..

1 Commits

Author SHA1 Message Date
Matte23
d0f42d43ae Define heap in linker and place other sections correctly 2024-11-03 19:41:58 +01:00
3 changed files with 46 additions and 14 deletions

View File

@@ -1,15 +1,40 @@
ENTRY(_start) ENTRY(_start)
MEMORY
{
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 1M
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 128K
}
_Min_Heap_Size = 0x200 ;
_Min_Stack_Size = 0x200 ; /* Required amount of stack. Used by main(), then re-used as the interrupt stack after the kernel starts. */
_estack = ORIGIN(RAM) + LENGTH(RAM);
SECTIONS SECTIONS
{ {
/* text section (code)*/ .text : { *(.text*) } > FLASH
.text : { *(.text*) } .rodata : { *(.rodata*) } > FLASH
/* data section, initialized variables */ .bss : { *(.bss*) } > RAM
.data : { *(.data) } .data : { *(.data*) } > RAM
ram_start = 0x20000000; .heap : {
/* bss section, uninitialized variables */ . = ALIGN(8);
.bss : { *(.bss*) } PROVIDE ( end = . );
/* stack section */ PROVIDE ( _end = . );
/* The stack is placed at the end of the RAM */ _heap_bottom = .;
stack_top = 0x2001ffff; . = . + _Min_Heap_Size;
_heap_top = .;
. = . + _Min_Stack_Size;
. = ALIGN(8);
} >RAM
.stack (NOLOAD) : {
. = ORIGIN(RAM) + LENGTH(RAM);
stack_top = .;
} > RAM
__StackLimit = stack_top - _Min_Stack_Size;
/* Check if data + heap + stack exceeds RAM limit */
ASSERT(__StackLimit >= _heap_top, "region RAM overflowed with stack")
} }

View File

@@ -1,14 +1,16 @@
#include "alloc.h" #include "alloc.h"
#include "stdint.h" #include "stdint.h"
void* *first_mem_avail = (void*) 0x20000000; HeapArena heap_arena;
extern void * _heap_top;
void init_allocator() { void init_allocator() {
*first_mem_avail = (void*) 0x20000000 +sizeof(void*); heap_arena.wilderness = &_heap_top;
} }
void* malloc(size_t size) { void* malloc(size_t size) {
void* block = *first_mem_avail; void* block = heap_arena.wilderness;
*first_mem_avail += size; heap_arena.wilderness += size;
return block; return block;
} }

View File

@@ -1,4 +1,9 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h>
typedef struct {
void * wilderness;
} HeapArena;
void init_allocator(); void init_allocator();
void* malloc (size_t size); void* malloc (size_t size);