diff --git a/cores/nRF5/syscall_newlib.c b/cores/nRF5/syscall_newlib.c index 2b28d0ed..16224a60 100644 --- a/cores/nRF5/syscall_newlib.c +++ b/cores/nRF5/syscall_newlib.c @@ -40,7 +40,38 @@ #include "Arduino.h" -// linker provide end symbol is enough for nanolib's sbrk() +// TODO linker provide end symbol is enough for nanolib's sbrk() +// Verify this later with upgrade toolchain in the next release + +// defined in linker script +extern unsigned char __HeapBase[]; +extern unsigned char __HeapLimit[]; + +static unsigned char *sbrk_heap_top = __HeapBase; + +//volatile uint32_t first_sbrk = 0; +//volatile uint32_t last_sbrk = 0; + +caddr_t _sbrk( int incr ) +{ + unsigned char *prev_heap; + + if ( sbrk_heap_top + incr > __HeapLimit ) + { + /* Out of dynamic memory heap space */ + errno = ENOMEM; + return (caddr_t) -1; + } + + prev_heap = sbrk_heap_top; + +// if ( !first_sbrk) first_sbrk = sbrk_heap_top; +// last_sbrk = sbrk_heap_top; + + sbrk_heap_top += incr; + + return (caddr_t) prev_heap; +} #if 0 void __malloc_lock(struct _reent *ptr)