brk/malloc: Allow malloc to fail when out of RAM

Fixes #76.
This commit is contained in:
Angus Gratton 2016-02-16 22:00:29 +11:00
parent d5221e7efa
commit f0db26604f
3 changed files with 22 additions and 14 deletions

View file

@ -9,9 +9,12 @@
#include <sys/errno.h>
#include <espressif/sdk_private.h>
#include <common_macros.h>
#include <xtensa_ops.h>
#include <esp/uart.h>
#include <stdlib.h>
extern void *xPortSupervisorStackPointer;
IRAM caddr_t _sbrk_r (struct _reent *r, int incr)
{
extern char _heap_start; /* linker script defined */
@ -21,13 +24,17 @@ IRAM caddr_t _sbrk_r (struct _reent *r, int incr)
if (heap_end == NULL)
heap_end = &_heap_start;
prev_heap_end = heap_end;
/* TODO: Check stack collision
if (heap_end + incr > stack_ptr)
{
_write (1, "_sbrk: Heap collided with stack\n", 32);
abort();
}
*/
intptr_t sp = (intptr_t)xPortSupervisorStackPointer;
if(sp == 0) /* scheduler not started */
SP(sp);
if ((intptr_t)heap_end + incr >= sp)
{
r->_errno = ENOMEM;
return (caddr_t)-1;
}
heap_end += incr;
return (caddr_t) prev_heap_end;