diff options
Diffstat (limited to 'Projects/P-NUCLEO-WB55.Nucleo/Applications/Zigbee/Zigbee_OnOff_Server_Distrib/STM32CubeIDE/Application/User/Core/sysmem.c')
-rw-r--r-- | Projects/P-NUCLEO-WB55.Nucleo/Applications/Zigbee/Zigbee_OnOff_Server_Distrib/STM32CubeIDE/Application/User/Core/sysmem.c | 78 |
1 files changed, 50 insertions, 28 deletions
diff --git a/Projects/P-NUCLEO-WB55.Nucleo/Applications/Zigbee/Zigbee_OnOff_Server_Distrib/STM32CubeIDE/Application/User/Core/sysmem.c b/Projects/P-NUCLEO-WB55.Nucleo/Applications/Zigbee/Zigbee_OnOff_Server_Distrib/STM32CubeIDE/Application/User/Core/sysmem.c index 4665417ed..23180b659 100644 --- a/Projects/P-NUCLEO-WB55.Nucleo/Applications/Zigbee/Zigbee_OnOff_Server_Distrib/STM32CubeIDE/Application/User/Core/sysmem.c +++ b/Projects/P-NUCLEO-WB55.Nucleo/Applications/Zigbee/Zigbee_OnOff_Server_Distrib/STM32CubeIDE/Application/User/Core/sysmem.c @@ -1,12 +1,12 @@ /** ****************************************************************************** * @file sysmem.c - * @author Auto-generated by STM32CubeIDE - * @brief STM32CubeIDE Minimal System Memory calls file + * @author Generated by STM32CubeIDE + * @brief STM32CubeIDE System Memory calls file * - * For more information about which c-functions + * For more information about which C functions * need which of these lowlevel functions - * please consult the Newlib libc-manual + * please consult the newlib libc manual ****************************************************************************** * @attention * @@ -23,36 +23,58 @@ /* Includes */ #include <errno.h> -#include <stdio.h> +#include <stdint.h> -/* Variables */ -extern int errno; -register char * stack_ptr asm("sp"); - -/* Functions */ +/** + * Pointer to the current high watermark of the heap usage + */ +static uint8_t *__sbrk_heap_end = NULL; /** - _sbrk - Increase program data space. Malloc and related functions depend on this -**/ -caddr_t _sbrk(int incr) + * @brief _sbrk() allocates memory to the newlib heap and is used by malloc + * and others from the C library + * + * @verbatim + * ############################################################################ + * # .data # .bss # newlib heap # MSP stack # + * # # # # Reserved by _Min_Stack_Size # + * ############################################################################ + * ^-- RAM start ^-- _end _estack, RAM end --^ + * @endverbatim + * + * This implementation starts allocating at the '_end' linker symbol + * The '_Min_Stack_Size' linker symbol reserves a memory for the MSP stack + * The implementation considers '_estack' linker symbol to be RAM end + * NOTE: If the MSP stack, at any point during execution, grows larger than the + * reserved size, please increase the '_Min_Stack_Size'. + * + * @param incr Memory size + * @return Pointer to allocated memory + */ +void *_sbrk(ptrdiff_t incr) { - extern char end asm("end"); - static char *heap_end; - char *prev_heap_end; + extern uint8_t _end; /* Symbol defined in the linker script */ + extern uint8_t _estack; /* Symbol defined in the linker script */ + extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */ + const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size; + const uint8_t *max_heap = (uint8_t *)stack_limit; + uint8_t *prev_heap_end; - if (heap_end == 0) - heap_end = &end; + /* Initalize heap end at first call */ + if (NULL == __sbrk_heap_end) + { + __sbrk_heap_end = &_end; + } - prev_heap_end = heap_end; - if (heap_end + incr > stack_ptr) - { - errno = ENOMEM; - return (caddr_t) -1; - } + /* Protect heap from growing into the reserved MSP stack */ + if (__sbrk_heap_end + incr > max_heap) + { + errno = ENOMEM; + return (void *)-1; + } - heap_end += incr; + prev_heap_end = __sbrk_heap_end; + __sbrk_heap_end += incr; - return (caddr_t) prev_heap_end; + return (void *)prev_heap_end; } - |