cJSON更換默認的malloc函數


通用方法 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM