From 1911e2d9285cb5b0a6ecd3bf3de864783f2b0e3e Mon Sep 17 00:00:00 2001 From: Jean-François Milants Date: Sun, 26 Mar 2023 14:49:03 +0200 Subject: 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/stdlib.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/stdlib.c (limited to 'src/stdlib.c') 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); +} -- cgit v1.2.3-70-g09d2