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

@ -82,13 +82,14 @@
unsigned cpu_sr; unsigned cpu_sr;
char level1_int_disabled; char level1_int_disabled;
/* Supervisor stack pointer entry. This is the "high water mark" of how far the /* Supervisor stack pointer entry. This is the "high water mark" of
supervisor stack grew down before task started. how far the supervisor stack grew down before task started. Is zero
before the scheduler starts.
After tasks start, task stacks are all allocated from the heap and After the scheduler starts, task stacks are all allocated from the
FreeRTOS checks for stack overflow. heap and FreeRTOS checks for stack overflow.
*/ */
uint32_t xPortSupervisorStackPointer; void *xPortSupervisorStackPointer;
/* /*
* Stack initialization * Stack initialization
@ -219,7 +220,7 @@ size_t xPortGetFreeHeapSize( void )
struct mallinfo mi = mallinfo(); struct mallinfo mi = mallinfo();
uint32_t brk_val = (uint32_t) sbrk(0); uint32_t brk_val = (uint32_t) sbrk(0);
uint32_t sp = xPortSupervisorStackPointer; intptr_t sp = (intptr_t)xPortSupervisorStackPointer;
if(sp == 0) /* scheduler not started */ if(sp == 0) /* scheduler not started */
SP(sp); SP(sp);
return sp - brk_val + mi.fordblks; return sp - brk_val + mi.fordblks;

View file

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

View file

@ -232,7 +232,7 @@ SECTIONS
_heap_start = ABSOLUTE(.); _heap_start = ABSOLUTE(.);
/* _stack_sentry = ALIGN(0x8); */ /* _stack_sentry = ALIGN(0x8); */
} >dram0_0_seg :dram0_0_bss_phdr } >dram0_0_seg :dram0_0_bss_phdr
/* __stack = 0x3ffc8000; */ /* __stack = 0x3ffc8000; <-- this value seems a bit odd, stack on sdk_user_start is ~0x3ffffce9 */
.lit4 : ALIGN(4) .lit4 : ALIGN(4)
{ {