From b073c19f9ec330423fa07c66d1c0604883044f6b Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Sat, 21 Nov 2020 21:24:04 -0700 Subject: Added halloc for allocating memory on the heap. The new halloc() call allocates memory on the STM32l's SRAM2 starting right above the DATA section. The implementation uses a very-dense, albeit slower, linked-list allocation as opposed to fancy B-trees or something. However, the overhead is just 1 32-bit word per allocation and thus allows for reasonably dense memory-packing on the small 16K memory chip. --- 02-usart/include/mem.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to '02-usart/include/mem.h') diff --git a/02-usart/include/mem.h b/02-usart/include/mem.h index aa4ce12..f376e7d 100644 --- a/02-usart/include/mem.h +++ b/02-usart/include/mem.h @@ -1,4 +1,27 @@ #ifndef MEM_H_ #define MEM_H_ +#include "arch.h" +#include + +#define MAX_HEAP_SIZE \ + ((16384 - (DATA_SEGMENT_STOP - DATA_SEGMENT_START)) / 4 * 4) + +/* allocates memory on the head, which is stored in sram2 */ +void* halloc(size_t n); + +/* Frees the memory allocated by halloc. */ +void hfree(void* mem); + +#ifdef FOR_TESTING + +void* debug_halloc_get_next_ptr(void* ptr); + +void* debug_halloc_get_prev_ptr(void* ptr); + +int debug_halloc_assert_consistency(char* error, size_t len); + +#endif + + #endif -- cgit