Browse Source

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>
pull/255/head
Jithu Joseph 10 years ago committed by Gerrit Code Review
parent
commit
4ad7f2a5a3
  1. 25
      lib/libc/newlib/libc-hooks.c
  2. 7
      misc/Kconfig

25
lib/libc/newlib/libc-hooks.c

@ -17,10 +17,23 @@ @@ -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) @@ -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 {

7
misc/Kconfig

@ -135,13 +135,6 @@ config MINIMAL_LIBC_EXTENDED @@ -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

Loading…
Cancel
Save