通用方法 cJSON_InitHooks() 函數
首先需要創建一個cJSON_Hooks結構體,然后在freertos
開始調度前調用此函數即可。
static cJSON_Hooks m_iot_hooks;
m_iot_hooks.malloc_fn = pvPortMalloc;
m_iot_hooks.free_fn = vPortFree;
cJSON_InitHooks(&m_iot_hooks);
直接修改 cJSON.c 文件
老版本中存在以下代碼
static void *(*cJSON_malloc)(size_t sz) = malloc;
static void (*cJSON_free)(void *ptr) = free;
這里直接將malloc與free修改即可
static void *(*cJSON_malloc)(size_t sz) = pvPortMalloc;
static void (*cJSON_free)(void *ptr) = vPortFree;
新版本修改地方如下
/*修改前*/
#define internal_malloc malloc
#define internal_free free
#define internal_realloc realloc
/*修改后*/
#define internal_malloc pvPortMalloc
#define internal_free vPortFree
#define internal_realloc NULL