From 4ad7f2a5a3ab6b51fff865a1c6ab0806c7d518b1 Mon Sep 17 00:00:00 2001 From: Jithu Joseph Date: Mon, 22 Feb 2016 19:47:06 -0800 Subject: [PATCH] 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 --- lib/libc/newlib/libc-hooks.c | 25 +++++++++++++++++++------ misc/Kconfig | 7 ------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/lib/libc/newlib/libc-hooks.c b/lib/libc/newlib/libc-hooks.c index a494e126c64..2dba65b7da3 100644 --- a/lib/libc/newlib/libc-hooks.c +++ b/lib/libc/newlib/libc-hooks.c @@ -17,10 +17,23 @@ #include #include #include - -#define HEAP_SIZE CONFIG_NEWLIB_HEAP_SIZE -unsigned char heap[HEAP_SIZE]; -unsigned int heap_sz; +#include +#include + +#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 { diff --git a/misc/Kconfig b/misc/Kconfig index 3136ba49e53..6ee8f1a9bca 100644 --- a/misc/Kconfig +++ b/misc/Kconfig @@ -135,13 +135,6 @@ config MINIMAL_LIBC_EXTENDED use any of the functions in an application you probably should be linking against a full lib c implementation instead. -config NEWLIB_HEAP_SIZE - int "Size in bytes of the heap available for Newlib's malloc() function" - default 4096 - depends on NEWLIB_LIBC - help - Sets the size of the heap available for dynamic memory allocation when - using Newlib. endmenu