diff options
author | Jithu Joseph <jithu.joseph@intel.com> | 2016-02-22 19:47:06 -0800 |
---|---|---|
committer | Gerrit Code Review <gerrit@zephyrproject.org> | 2016-02-27 09:57:19 +0000 |
commit | 4ad7f2a5a3ab6b51fff865a1c6ab0806c7d518b1 (patch) | |
tree | 5fd599d56c5d940ec2ab8bd37640fd957b1c98d1 /lib | |
parent | aab47a4e153221c609d29169ca7a8ea7dd3614d3 (diff) |
libc-hooks: newlib's heap may use entire unused RAM
Use linker symbol and board configs to determine the start
and extent of remaining RAM present in a board and use
it as newlib's heap.
Change-Id: I7128cf2857664331d83f212f27e8af7ad3bb8936
Signed-off-by: Jithu Joseph <jithu.joseph@intel.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libc/newlib/libc-hooks.c | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/lib/libc/newlib/libc-hooks.c b/lib/libc/newlib/libc-hooks.c index a494e126c..2dba65b7d 100644 --- a/lib/libc/newlib/libc-hooks.c +++ b/lib/libc/newlib/libc-hooks.c @@ -17,10 +17,23 @@ #include <errno.h> #include <stdio.h> #include <sys/stat.h> - -#define HEAP_SIZE CONFIG_NEWLIB_HEAP_SIZE -unsigned char heap[HEAP_SIZE]; -unsigned int heap_sz; +#include <linker-defs.h> +#include <misc/util.h> + +#define USED_RAM_END_ADDR POINTER_TO_UINT(&_end) +#if defined(CONFIG_ARM) +#define USED_RAM_SIZE (USED_RAM_END_ADDR - CONFIG_SRAM_BASE_ADDRESS) +#define MAX_HEAP_SIZE ((KB(CONFIG_SRAM_SIZE)) - USED_RAM_SIZE) +#elif defined(CONFIG_ARC) +#define USED_RAM_SIZE (USED_RAM_END_ADDR - CONFIG_RAM_START) +#define MAX_HEAP_SIZE ((KB(CONFIG_RAM_SIZE)) - USED_RAM_SIZE) +#else /* X86 */ +#define USED_RAM_SIZE (USED_RAM_END_ADDR - CONFIG_PHYS_RAM_ADDR) +#define MAX_HEAP_SIZE ((KB(CONFIG_RAM_SIZE)) - USED_RAM_SIZE) +#endif + +static unsigned char *heap_base = UINT_TO_POINTER(USED_RAM_END_ADDR); +static unsigned int heap_sz; static int _stdout_hook_default(int c) { @@ -126,9 +139,9 @@ int lseek(int file, int ptr, int dir) void *sbrk(int count) { - void *ptr = heap + heap_sz; + void *ptr = heap_base + heap_sz; - if ((heap_sz + count) <= HEAP_SIZE) { + if ((heap_sz + count) < MAX_HEAP_SIZE) { heap_sz += count; return ptr; } else { |