Mod aaa-12 修改struct2json库malloc hook

RCA:
SOL:移除hook方法,使用malloc、free函数
修改人:huangxin
检视人:huangxin
This commit is contained in:
黄昕 2019-07-04 11:29:48 +08:00
parent 37104095e9
commit 76e0ad8308
4 changed files with 8 additions and 36 deletions

View File

@ -80,32 +80,6 @@ extern "C" {
#define s2j_struct_get_struct_element(child_struct, to_struct, child_json, from_json, type, element) \
S2J_STRUCT_GET_STRUCT_ELEMENT(child_struct, to_struct, child_json, from_json, type, element)
/* s2j.c */
//extern S2jHook s2jHook;
S2jHook s2jHook = {
.malloc_fn = malloc,
.free_fn = free,
};
static void s2j_init(S2jHook *hook)
{
/* initialize cJSON library */
if(hook == NULL)
{
hook = &s2jHook;
}
cJSON_InitHooks((cJSON_Hooks *)hook);
/* initialize hooks */
if (hook) {
s2jHook.malloc_fn = (hook->malloc_fn) ? hook->malloc_fn : malloc;
s2jHook.free_fn = (hook->free_fn) ? hook->free_fn : free;
} else {
s2jHook.malloc_fn = malloc;
s2jHook.free_fn = free;
}
}
#ifdef __cplusplus
}
#endif

View File

@ -31,6 +31,7 @@
#include <cjson/cJSON.h>
#include <string.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
@ -116,11 +117,11 @@ typedef struct {
#define S2J_CREATE_STRUCT_OBJECT(struct_obj, type) \
cJSON *json_temp; \
type *struct_obj = s2jHook.malloc_fn(sizeof(type)); \
type *struct_obj = malloc(sizeof(type)); \
if (struct_obj) memset(struct_obj, 0, sizeof(type));
#define S2J_DELETE_STRUCT_OBJECT(struct_obj) \
s2jHook.free_fn(struct_obj);
free(struct_obj);
#define S2J_STRUCT_GET_BASIC_ELEMENT(to_struct, from_json, type, _element) \
S2J_STRUCT_GET_##type##_ELEMENT(to_struct, from_json, _element)

View File

@ -29,7 +29,7 @@
#ifndef __S2J_H__
#define __S2J_H__
#include <cJSON/cJSON.h>
#include <cjson/cJSON.h>
#include <string.h>
#include "s2jdef.h"
@ -80,10 +80,6 @@ extern "C" {
#define s2j_struct_get_struct_element(child_struct, to_struct, child_json, from_json, type, element) \
S2J_STRUCT_GET_STRUCT_ELEMENT(child_struct, to_struct, child_json, from_json, type, element)
/* s2j.c */
extern S2jHook s2jHook;
void s2j_init(S2jHook *hook);
#ifdef __cplusplus
}
#endif

View File

@ -29,8 +29,9 @@
#ifndef __S2JDEF_H__
#define __S2JDEF_H__
#include <cJSON/cJSON.h>
#include <cjson/cJSON.h>
#include <string.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
@ -116,11 +117,11 @@ typedef struct {
#define S2J_CREATE_STRUCT_OBJECT(struct_obj, type) \
cJSON *json_temp; \
type *struct_obj = s2jHook.malloc_fn(sizeof(type)); \
type *struct_obj = malloc(sizeof(type)); \
if (struct_obj) memset(struct_obj, 0, sizeof(type));
#define S2J_DELETE_STRUCT_OBJECT(struct_obj) \
s2jHook.free_fn(struct_obj);
free(struct_obj);
#define S2J_STRUCT_GET_BASIC_ELEMENT(to_struct, from_json, type, _element) \
S2J_STRUCT_GET_##type##_ELEMENT(to_struct, from_json, _element)