From 98b6f6ec2ffc690f59176c81280e4268b6936031 Mon Sep 17 00:00:00 2001 From: James Benton Date: Thu, 14 Jun 2018 09:32:37 +0100 Subject: [PATCH] wutnewlib: Allow custom fixed sbrk heap size. So now either uses 90% of the available base heap, or allocates a fixed sized heap from the default heap. --- libraries/wutnewlib/CMakeLists.txt | 1 - libraries/wutnewlib/wut_malloc.c | 88 ------------------------------ libraries/wutnewlib/wut_sbrk.c | 44 +++++++++++++-- 3 files changed, 38 insertions(+), 95 deletions(-) delete mode 100644 libraries/wutnewlib/wut_malloc.c diff --git a/libraries/wutnewlib/CMakeLists.txt b/libraries/wutnewlib/CMakeLists.txt index a66ae03..9b011d9 100644 --- a/libraries/wutnewlib/CMakeLists.txt +++ b/libraries/wutnewlib/CMakeLists.txt @@ -5,7 +5,6 @@ add_library(wutnewlib wut_clock.c wut_gettod_r.c wut_lock.c - wut_malloc.c wut_malloc_lock.c wut_nanosleep.c wut_newlib.c diff --git a/libraries/wutnewlib/wut_malloc.c b/libraries/wutnewlib/wut_malloc.c deleted file mode 100644 index 2d960de..0000000 --- a/libraries/wutnewlib/wut_malloc.c +++ /dev/null @@ -1,88 +0,0 @@ -#include -#include -#include -#include -#include - -void * -_malloc_r(struct _reent *r, size_t size) -{ - return MEMAllocFromDefaultHeap(size); -} - -void -_free_r(struct _reent *r, void *ptr) -{ - MEMFreeToDefaultHeap(ptr); -} - -void * -_realloc_r(struct _reent *r, void *ptr, size_t size) -{ - void *new_ptr = _malloc_r(r, size); - if (!ptr || !new_ptr) { - return new_ptr; - } - - memcpy(new_ptr, ptr, MEMGetSizeForMBlockExpHeap(ptr)); - _free_r(r, ptr); - return new_ptr; -} - -void * -_calloc_r(struct _reent *r, size_t num, size_t size) -{ - void *ptr = _malloc_r(r, num * size); - if (ptr) { - memset(ptr, 0, num * size); - } - - return ptr; -} - -void * -_memalign_r(struct _reent *r, size_t align, size_t size) -{ - return MEMAllocFromDefaultHeapEx(size, align); -} - -struct mallinfo _mallinfo_r(struct _reent *r) -{ - struct mallinfo info = { 0 }; - return info; -} - -void -_malloc_stats_r(struct _reent *r) -{ -} - -int -_mallopt_r(struct _reent *r, int param, int value) -{ - return 0; -} - -size_t -_malloc_usable_size_r(struct _reent *r, void *ptr) -{ - return MEMGetSizeForMBlockExpHeap(ptr); -} - -void * -_valloc_r(struct _reent *r, size_t size) -{ - return _memalign_r(r, OS_PAGE_SIZE, size); -} - -void * -_pvalloc_r(struct _reent *r, size_t size) -{ - return _memalign_r(r, OS_PAGE_SIZE, (size + (OS_PAGE_SIZE - 1)) & ~(OS_PAGE_SIZE - 1)); -} - -int -_malloc_trim_r(struct _reent *r, size_t pad) -{ - return 0; -} diff --git a/libraries/wutnewlib/wut_sbrk.c b/libraries/wutnewlib/wut_sbrk.c index ec0e612..b8bf017 100644 --- a/libraries/wutnewlib/wut_sbrk.c +++ b/libraries/wutnewlib/wut_sbrk.c @@ -1,10 +1,15 @@ #include "wut_newlib.h" #include +#include #include +#include -static uint8_t *sHeapBase = NULL; -static uint32_t sHeapMaxSize = 128 * 1024; +extern uint32_t __attribute__((weak)) __wut_heap_max_size; + +static MEMHeapHandle sHeapHandle = NULL; +static void *sHeapBase = NULL; +static uint32_t sHeapMaxSize = 0; static volatile uint32_t sHeapSize = 0; void * @@ -23,21 +28,48 @@ __wut_sbrk_r(struct _reent *r, } } while (!OSCompareAndSwapAtomic(&sHeapSize, oldSize, newSize)); - return sHeapBase + oldSize; + return ((uint8_t *)sHeapBase) + oldSize; } void __init_wut_sbrk_heap() { - sHeapMaxSize = 128 * 1024; - sHeapBase = (uint8_t *)MEMAllocFromDefaultHeap(sHeapMaxSize); + if (sHeapBase) { + // Already initialised + return; + } + + if (__wut_heap_max_size) { + // Use default heap + sHeapBase = MEMAllocFromDefaultHeap(__wut_heap_max_size); + sHeapMaxSize = __wut_heap_max_size; + } else { + // No max size specified, use 90% of base MEM2 heap + sHeapHandle = MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2); + + uint32_t freeSize = MEMGetAllocatableSizeForExpHeapEx(sHeapHandle, 4); + sHeapMaxSize = (uint32_t)(0.9f * (float)freeSize) & ~0xFFF; + + sHeapBase = MEMAllocFromExpHeapEx(sHeapHandle, sHeapMaxSize, 4); + } + sHeapSize = 0; } void __fini_wut_sbrk_heap() { - MEMFreeToDefaultHeap(sHeapBase); + if (!sHeapBase) { + // Already finalised + return; + } + + if (sHeapHandle) { + MEMFreeToExpHeap(sHeapHandle, sHeapBase); + } else { + MEMFreeToDefaultHeap(sHeapBase); + } + sHeapBase = NULL; sHeapSize = 0; sHeapMaxSize = 0;