InfiniTime/src/stdlib.c
Jean-François Milants 1911e2d928 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.
2023-05-18 19:58:09 +02:00

28 lines
636 B
C

#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);
}