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