Skip to content

Commit 1911e2d

Browse files
committedMay 18, 2023
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.
1 parent 9e808a6 commit 1911e2d

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed
 

‎src/CMakeLists.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ list(APPEND IMAGE_FILES
367367
displayapp/icons/battery/batteryicon.c
368368
)
369369
list(APPEND SOURCE_FILES
370+
stdlib.c
370371
FreeRTOS/heap_4_infinitime.c
371372
BootloaderVersion.cpp
372373
logging/NrfLogger.cpp
@@ -496,6 +497,7 @@ list(APPEND SOURCE_FILES
496497
)
497498

498499
list(APPEND RECOVERY_SOURCE_FILES
500+
stdlib.c
499501
FreeRTOS/heap_4_infinitime.c
500502

501503
BootloaderVersion.cpp
@@ -560,6 +562,7 @@ list(APPEND RECOVERY_SOURCE_FILES
560562
)
561563

562564
list(APPEND RECOVERYLOADER_SOURCE_FILES
565+
stdlib.c
563566
FreeRTOS/heap_4_infinitime.c
564567

565568
# FreeRTOS
@@ -786,7 +789,7 @@ add_definitions(-DOS_CPUTIME_FREQ)
786789
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)
787790
add_definitions(-DFREERTOS)
788791
add_definitions(-D__STACK_SIZE=1024)
789-
add_definitions(-D__HEAP_SIZE=4096)
792+
add_definitions(-D__HEAP_SIZE=0)
790793
add_definitions(-DMYNEWT_VAL_BLE_LL_RFMGMT_ENABLE_TIME=1500)
791794

792795
# Note: Only use this for debugging

‎src/FreeRTOSConfig.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
#define configTICK_RATE_HZ 1024
6363
#define configMAX_PRIORITIES (3)
6464
#define configMINIMAL_STACK_SIZE (120)
65-
#define configTOTAL_HEAP_SIZE (1024 * 17)
65+
#define configTOTAL_HEAP_SIZE (1024 * 40)
6666
#define configMAX_TASK_NAME_LEN (4)
6767
#define configUSE_16_BIT_TICKS 0
6868
#define configIDLE_SHOULD_YIELD 1

‎src/libs/lv_conf.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ typedef int16_t lv_coord_t;
7171
* The graphical objects and other related data are stored here. */
7272

7373
/* 1: use custom malloc/free, 0: use the built-in `lv_mem_alloc` and `lv_mem_free` */
74-
#define LV_MEM_CUSTOM 0
74+
#define LV_MEM_CUSTOM 1
7575
#if LV_MEM_CUSTOM == 0
7676
/* Size of the memory used by `lv_mem_alloc` in bytes (>= 2kB)*/
7777
#define LV_MEM_SIZE (14U * 1024U)
@@ -86,9 +86,9 @@ typedef int16_t lv_coord_t;
8686
/* Automatically defrag. on free. Defrag. means joining the adjacent free cells. */
8787
#define LV_MEM_AUTO_DEFRAG 1
8888
#else /*LV_MEM_CUSTOM*/
89-
#define LV_MEM_CUSTOM_INCLUDE <stdlib.h> /*Header for the dynamic memory function*/
90-
#define LV_MEM_CUSTOM_ALLOC malloc /*Wrapper to malloc*/
91-
#define LV_MEM_CUSTOM_FREE free /*Wrapper to free*/
89+
#define LV_MEM_CUSTOM_INCLUDE <FreeRTOS.h> /*Header for the dynamic memory function*/
90+
#define LV_MEM_CUSTOM_ALLOC pvPortMalloc /*Wrapper to malloc*/
91+
#define LV_MEM_CUSTOM_FREE vPortFree /*Wrapper to free*/
9292
#endif /*LV_MEM_CUSTOM*/
9393

9494
/* Use the standard memcpy and memset instead of LVGL's own functions.

‎src/stdlib.c

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <stdlib.h>
2+
#include <FreeRTOS.h>
3+
4+
// Override malloc() and free() to use the memory manager from FreeRTOS.
5+
// According to the documentation of libc, we also need to override
6+
// calloc and realloc.
7+
// See https://www.gnu.org/software/libc/manual/html_node/Replacing-malloc.html
8+
9+
void* malloc(size_t size) {
10+
return pvPortMalloc(size);
11+
}
12+
13+
void free(void* ptr) {
14+
vPortFree(ptr);
15+
}
16+
17+
void* calloc(size_t num, size_t size) {
18+
(void)(num);
19+
(void)(size);
20+
// Not supported
21+
return NULL;
22+
}
23+
24+
void *pvPortRealloc(void *ptr, size_t xWantedSize);
25+
void* realloc( void *ptr, size_t newSize) {
26+
return pvPortRealloc(ptr, newSize);
27+
}

0 commit comments

Comments
 (0)
Please sign in to comment.