From ee82304211b885ab1a5370e5dd426e89e64bfde3 Mon Sep 17 00:00:00 2001 From: Andre Guedes Date: Fri, 17 Jul 2015 10:21:31 -0300 Subject: [PATCH] galileo: Implement _sbrk_r syscall This patch implements the _sbrk_r() system call for Galileo platform. This system call is required by newlib's malloc() implementation. Next patch will introduce the initial support for stdio library which requires a working malloc() function for proper operation. We are not sure about the heap size we should use. Preliminary tests have shown that stdio library requests 1032 bytes heap. So, as an initial guess, a 2Kb heap size should be enough for now. --- platform/galileo/newlib-syscalls.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/platform/galileo/newlib-syscalls.c b/platform/galileo/newlib-syscalls.c index 5a7831857..6adcccd19 100644 --- a/platform/galileo/newlib-syscalls.c +++ b/platform/galileo/newlib-syscalls.c @@ -31,6 +31,11 @@ #include #include +#define HEAP_MAX_SIZE 2048 + +static char _heap[HEAP_MAX_SIZE]; +static char *prog_break = _heap; + int _close_r(struct _reent *ptr, int file) { @@ -81,9 +86,21 @@ _fstat_r(struct _reent *ptr, int file, struct stat *st) caddr_t _sbrk_r(struct _reent *ptr, int incr) { - /* Stubbed function */ - ptr->_errno = ENOTSUP; - return NULL; + char *prev_prog_break; + + /* If the new program break overruns the maximum heap address, we return + * "Out of Memory" error to the user. + */ + if(prog_break + incr > _heap + HEAP_MAX_SIZE) { + ptr->_errno = ENOMEM; + return NULL; + } + + prev_prog_break = prog_break; + + prog_break += incr; + + return prev_prog_break; } /*---------------------------------------------------------------------------*/ void