From 1911e2d9285cb5b0a6ecd3bf3de864783f2b0e3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Milants?= Date: Sun, 26 Mar 2023 14:49:03 +0200 Subject: [PATCH] Unify all heaps (stdlib + LVGL + FreeRTOS) into a single heap managed by FreeRTOS and heap_4_infinitime.c. LVGL supports custom implementation of malloc() and free() so using pvPortMalloc() and vPortFree() is just a matter of setting the right variables. Other libraries (NimBLE, LittleFS) and InfiniTime code (new) call malloc() and free() from stdlib. InfiniTime now provides the file stdlib.c that provides a custom implementation for malloc(), free(), calloc() and realloc(). This ensures that all calls to the standard allocator are redirected to the FreeRTOS memory manager. Note that realloc() is needed by NimBLE. --- src/CMakeLists.txt | 5 ++++- src/FreeRTOSConfig.h | 2 +- src/libs/lv_conf.h | 8 ++++---- src/stdlib.c | 27 +++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 src/stdlib.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f554dc68..1935f8fc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -367,6 +367,7 @@ list(APPEND IMAGE_FILES displayapp/icons/battery/batteryicon.c ) list(APPEND SOURCE_FILES + stdlib.c FreeRTOS/heap_4_infinitime.c BootloaderVersion.cpp logging/NrfLogger.cpp @@ -496,6 +497,7 @@ list(APPEND SOURCE_FILES ) list(APPEND RECOVERY_SOURCE_FILES + stdlib.c FreeRTOS/heap_4_infinitime.c BootloaderVersion.cpp @@ -560,6 +562,7 @@ list(APPEND RECOVERY_SOURCE_FILES ) list(APPEND RECOVERYLOADER_SOURCE_FILES + stdlib.c FreeRTOS/heap_4_infinitime.c # FreeRTOS @@ -786,7 +789,7 @@ add_definitions(-DOS_CPUTIME_FREQ) add_definitions(-DNRF52 -DNRF52832 -DNRF52832_XXAA -DNRF52_PAN_74 -DNRF52_PAN_64 -DNRF52_PAN_12 -DNRF52_PAN_58 -DNRF52_PAN_54 -DNRF52_PAN_31 -DNRF52_PAN_51 -DNRF52_PAN_36 -DNRF52_PAN_15 -DNRF52_PAN_20 -DNRF52_PAN_55 -DBOARD_PCA10040) add_definitions(-DFREERTOS) add_definitions(-D__STACK_SIZE=1024) -add_definitions(-D__HEAP_SIZE=4096) +add_definitions(-D__HEAP_SIZE=0) add_definitions(-DMYNEWT_VAL_BLE_LL_RFMGMT_ENABLE_TIME=1500) # Note: Only use this for debugging diff --git a/src/FreeRTOSConfig.h b/src/FreeRTOSConfig.h index 263d8031..ab0cf1ba 100644 --- a/src/FreeRTOSConfig.h +++ b/src/FreeRTOSConfig.h @@ -62,7 +62,7 @@ #define configTICK_RATE_HZ 1024 #define configMAX_PRIORITIES (3) #define configMINIMAL_STACK_SIZE (120) -#define configTOTAL_HEAP_SIZE (1024 * 17) +#define configTOTAL_HEAP_SIZE (1024 * 40) #define configMAX_TASK_NAME_LEN (4) #define configUSE_16_BIT_TICKS 0 #define configIDLE_SHOULD_YIELD 1 diff --git a/src/libs/lv_conf.h b/src/libs/lv_conf.h index 063f1d34..795760ef 100644 --- a/src/libs/lv_conf.h +++ b/src/libs/lv_conf.h @@ -71,7 +71,7 @@ typedef int16_t lv_coord_t; * The graphical objects and other related data are stored here. */ /* 1: use custom malloc/free, 0: use the built-in `lv_mem_alloc` and `lv_mem_free` */ -#define LV_MEM_CUSTOM 0 +#define LV_MEM_CUSTOM 1 #if LV_MEM_CUSTOM == 0 /* Size of the memory used by `lv_mem_alloc` in bytes (>= 2kB)*/ #define LV_MEM_SIZE (14U * 1024U) @@ -86,9 +86,9 @@ typedef int16_t lv_coord_t; /* Automatically defrag. on free. Defrag. means joining the adjacent free cells. */ #define LV_MEM_AUTO_DEFRAG 1 #else /*LV_MEM_CUSTOM*/ -#define LV_MEM_CUSTOM_INCLUDE /*Header for the dynamic memory function*/ -#define LV_MEM_CUSTOM_ALLOC malloc /*Wrapper to malloc*/ -#define LV_MEM_CUSTOM_FREE free /*Wrapper to free*/ +#define LV_MEM_CUSTOM_INCLUDE /*Header for the dynamic memory function*/ +#define LV_MEM_CUSTOM_ALLOC pvPortMalloc /*Wrapper to malloc*/ +#define LV_MEM_CUSTOM_FREE vPortFree /*Wrapper to free*/ #endif /*LV_MEM_CUSTOM*/ /* Use the standard memcpy and memset instead of LVGL's own functions. diff --git a/src/stdlib.c b/src/stdlib.c new file mode 100644 index 00000000..3ad66b37 --- /dev/null +++ b/src/stdlib.c @@ -0,0 +1,27 @@ +#include +#include + +// Override malloc() and free() to use the memory manager from FreeRTOS. +// According to the documentation of libc, we also need to override +// calloc and realloc. +// See https://www.gnu.org/software/libc/manual/html_node/Replacing-malloc.html + +void* malloc(size_t size) { + return pvPortMalloc(size); +} + +void free(void* ptr) { + vPortFree(ptr); +} + +void* calloc(size_t num, size_t size) { + (void)(num); + (void)(size); + // Not supported + return NULL; +} + +void *pvPortRealloc(void *ptr, size_t xWantedSize); +void* realloc( void *ptr, size_t newSize) { + return pvPortRealloc(ptr, newSize); +}