DaveGamble / cJSON

Ultralightweight JSON parser in ANSI C
MIT License
10.28k stars 3.15k forks source link

Configuring allocators when using FreeRTOS? #824

Open escherstair opened 5 months ago

escherstair commented 5 months ago

FreeRTOS uses its own memory management that provides custom implementations for the memory allocators.

Each provided implementation is contained in a separate source file (heap_1.c, heap_2.c, heap_3.c, heap_4.c and heap_5.c respectively)

The allocators are:

void * pvPortMalloc( size_t xWantedSize );
void vPortFree( void * pv );

I wonder if when cJSON is used in a project that runs on the top of FreeRTOS, customizing the allocators with cJSON_InitHooks is mandatory or optional. Does this depend on which FreeRTOS implementation is used (i.e., heap_x.c)?

daschfg commented 5 months ago

I'm not familiar with FreeRTOS, but in general:

If you don't use cJSON_InitHooks, cJSON will per default call the standard functions malloc, realloc and free. So I guess the call to cJSON_InitHooks being mandatory or optional depends on if a call to these default functions is available and acceptable for your system or not.

This depends a lot on your context. But as the standard functions are not thread safe and you are probably in a multithreaded environment, I would prefer the memory management provided by FreeRTOS.