stm32g0xx/Core/Src/sysmem.c
Thomas Klaehn d38bd4ae27 Initial commit
Signed-off-by: Thomas Klaehn <thomas.klaehn@perinet.io>
2020-12-14 16:21:17 +01:00

29 lines
716 B
C

#include <errno.h>
#include <stdint.h>
static uint8_t *__sbrk_heap_end = NULL;
void *_sbrk(ptrdiff_t incr)
{
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 (NULL == __sbrk_heap_end) {
__sbrk_heap_end = &_end;
}
if (__sbrk_heap_end + incr > max_heap) {
errno = ENOMEM;
return (void *)-1;
}
prev_heap_end = __sbrk_heap_end;
__sbrk_heap_end += incr;
return (void *)prev_heap_end;
}