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.
This commit is contained in:
Jean-François Milants 2023-03-26 14:49:03 +02:00 committed by JF
parent 9e808a65ec
commit 1911e2d928
4 changed files with 36 additions and 6 deletions

View File

@ -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

View File

@ -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

View File

@ -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 <stdlib.h> /*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 <FreeRTOS.h> /*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.

27
src/stdlib.c Normal file
View File

@ -0,0 +1,27 @@
#include <stdlib.h>
#include <FreeRTOS.h>
// 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);
}