Rename internal_hooks -> internal_configuration, cJSON_New_item -> create_item

This commit is contained in:
Max Bruckner 2018-01-31 22:22:53 +01:00
parent e82f32b359
commit b277cd6a24
12 changed files with 110 additions and 110 deletions

176
cJSON.c
View File

@ -118,12 +118,12 @@ static int case_insensitive_strcmp(const unsigned char *string1, const unsigned
return tolower(*string1) - tolower(*string2);
}
typedef struct internal_hooks
typedef struct internal_configuration
{
void *(*allocate)(size_t size);
void (*deallocate)(void *pointer);
void *(*reallocate)(void *pointer, size_t size);
} internal_hooks;
} internal_configuration;
#if defined(_MSC_VER)
/* work around MSVC error C2322: '...' address of dillimport '...' is not static */
@ -145,9 +145,9 @@ static void *internal_realloc(void *pointer, size_t size)
#define internal_realloc realloc
#endif
static internal_hooks global_hooks = { internal_malloc, internal_free, internal_realloc };
static internal_configuration global_configuration = { internal_malloc, internal_free, internal_realloc };
static unsigned char* cJSON_strdup(const unsigned char* string, const internal_hooks * const hooks)
static unsigned char* custom_strdup(const unsigned char* string, const internal_configuration * const configuration)
{
size_t length = 0;
unsigned char *copy = NULL;
@ -158,7 +158,7 @@ static unsigned char* cJSON_strdup(const unsigned char* string, const internal_h
}
length = strlen((const char*)string) + sizeof("");
copy = (unsigned char*)hooks->allocate(length);
copy = (unsigned char*)configuration->allocate(length);
if (copy == NULL)
{
return NULL;
@ -173,36 +173,36 @@ CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks)
if (hooks == NULL)
{
/* Reset hooks */
global_hooks.allocate = malloc;
global_hooks.deallocate = free;
global_hooks.reallocate = realloc;
global_configuration.allocate = malloc;
global_configuration.deallocate = free;
global_configuration.reallocate = realloc;
return;
}
global_hooks.allocate = malloc;
global_configuration.allocate = malloc;
if (hooks->malloc_fn != NULL)
{
global_hooks.allocate = hooks->malloc_fn;
global_configuration.allocate = hooks->malloc_fn;
}
global_hooks.deallocate = free;
global_configuration.deallocate = free;
if (hooks->free_fn != NULL)
{
global_hooks.deallocate = hooks->free_fn;
global_configuration.deallocate = hooks->free_fn;
}
/* use realloc only if both free and malloc are used */
global_hooks.reallocate = NULL;
if ((global_hooks.allocate == malloc) && (global_hooks.deallocate == free))
global_configuration.reallocate = NULL;
if ((global_configuration.allocate == malloc) && (global_configuration.deallocate == free))
{
global_hooks.reallocate = realloc;
global_configuration.reallocate = realloc;
}
}
/* Internal constructor. */
static cJSON *cJSON_New_Item(const internal_hooks * const hooks)
static cJSON *create_item(const internal_configuration * const configuration)
{
cJSON* node = (cJSON*)hooks->allocate(sizeof(cJSON));
cJSON* node = (cJSON*)configuration->allocate(sizeof(cJSON));
if (node)
{
memset(node, '\0', sizeof(cJSON));
@ -224,13 +224,13 @@ CJSON_PUBLIC(void) cJSON_Delete(cJSON *item)
}
if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL))
{
global_hooks.deallocate(item->valuestring);
global_configuration.deallocate(item->valuestring);
}
if (!(item->type & cJSON_StringIsConst) && (item->string != NULL))
{
global_hooks.deallocate(item->string);
global_configuration.deallocate(item->string);
}
global_hooks.deallocate(item);
global_configuration.deallocate(item);
item = next;
}
}
@ -266,7 +266,7 @@ typedef struct
size_t length;
size_t offset;
size_t depth; /* How deeply nested (in arrays/objects) is the input at the current offset. */
internal_hooks hooks;
internal_configuration configuration;
} parse_buffer;
/* check if the given size is left to read in a given parse buffer (starting with 1) */
@ -356,7 +356,7 @@ typedef struct
size_t depth; /* current nesting depth (for formatted printing) */
cJSON_bool noalloc;
cJSON_bool format; /* is this print a formatted print */
internal_hooks hooks;
internal_configuration configuration;
} printbuffer;
/* realloc printbuffer if necessary to have at least "needed" bytes more */
@ -410,13 +410,13 @@ static unsigned char* ensure(printbuffer * const p, size_t needed)
newsize = needed * 2;
}
if (p->hooks.reallocate != NULL)
if (p->configuration.reallocate != NULL)
{
/* reallocate with realloc if available */
newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize);
newbuffer = (unsigned char*)p->configuration.reallocate(p->buffer, newsize);
if (newbuffer == NULL)
{
p->hooks.deallocate(p->buffer);
p->configuration.deallocate(p->buffer);
p->length = 0;
p->buffer = NULL;
@ -426,10 +426,10 @@ static unsigned char* ensure(printbuffer * const p, size_t needed)
else
{
/* otherwise reallocate manually */
newbuffer = (unsigned char*)p->hooks.allocate(newsize);
newbuffer = (unsigned char*)p->configuration.allocate(newsize);
if (!newbuffer)
{
p->hooks.deallocate(p->buffer);
p->configuration.deallocate(p->buffer);
p->length = 0;
p->buffer = NULL;
@ -439,7 +439,7 @@ static unsigned char* ensure(printbuffer * const p, size_t needed)
{
memcpy(newbuffer, p->buffer, p->offset + 1);
}
p->hooks.deallocate(p->buffer);
p->configuration.deallocate(p->buffer);
}
p->length = newsize;
p->buffer = newbuffer;
@ -731,7 +731,7 @@ static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_bu
/* This is at most how much we need for the output */
allocation_length = (size_t) (input_end - buffer_at_offset(input_buffer)) - skipped_bytes;
output = (unsigned char*)input_buffer->hooks.allocate(allocation_length + sizeof(""));
output = (unsigned char*)input_buffer->configuration.allocate(allocation_length + sizeof(""));
if (output == NULL)
{
goto fail; /* allocation failure */
@ -809,7 +809,7 @@ static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_bu
fail:
if (output != NULL)
{
input_buffer->hooks.deallocate(output);
input_buffer->configuration.deallocate(output);
}
if (input_pointer != NULL)
@ -1011,9 +1011,9 @@ CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return
buffer.content = (const unsigned char*)value;
buffer.length = strlen((const char*)value) + sizeof("");
buffer.offset = 0;
buffer.hooks = global_hooks;
buffer.configuration = global_configuration;
item = cJSON_New_Item(&global_hooks);
item = create_item(&global_configuration);
if (item == NULL) /* memory fail */
{
goto fail;
@ -1081,7 +1081,7 @@ CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value)
#define cjson_min(a, b) ((a < b) ? a : b)
static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_hooks * const hooks)
static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_configuration * const configuration)
{
static const size_t default_buffer_size = 256;
printbuffer buffer[1];
@ -1090,10 +1090,10 @@ static unsigned char *print(const cJSON * const item, cJSON_bool format, const i
memset(buffer, 0, sizeof(buffer));
/* create buffer */
buffer->buffer = (unsigned char*) hooks->allocate(default_buffer_size);
buffer->buffer = (unsigned char*) configuration->allocate(default_buffer_size);
buffer->length = default_buffer_size;
buffer->format = format;
buffer->hooks = *hooks;
buffer->configuration = *configuration;
if (buffer->buffer == NULL)
{
goto fail;
@ -1109,9 +1109,9 @@ static unsigned char *print(const cJSON * const item, cJSON_bool format, const i
/* Reallocate the buffer so that it only uses as much as it needs.
This can save up to 50% because ensure increases the buffer size by a factor of 2 */
/* check if reallocate is available */
if (hooks->reallocate != NULL)
if (configuration->reallocate != NULL)
{
printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->offset + 1);
printed = (unsigned char*) configuration->reallocate(buffer->buffer, buffer->offset + 1);
buffer->buffer = NULL;
if (printed == NULL) {
goto fail;
@ -1119,7 +1119,7 @@ static unsigned char *print(const cJSON * const item, cJSON_bool format, const i
}
else /* otherwise copy the JSON over to a new buffer */
{
printed = (unsigned char*) hooks->allocate(buffer->offset + 1);
printed = (unsigned char*) configuration->allocate(buffer->offset + 1);
if (printed == NULL)
{
goto fail;
@ -1128,7 +1128,7 @@ static unsigned char *print(const cJSON * const item, cJSON_bool format, const i
printed[buffer->offset] = '\0'; /* just to be sure */
/* free the buffer */
hooks->deallocate(buffer->buffer);
configuration->deallocate(buffer->buffer);
}
return printed;
@ -1136,12 +1136,12 @@ static unsigned char *print(const cJSON * const item, cJSON_bool format, const i
fail:
if (buffer->buffer != NULL)
{
hooks->deallocate(buffer->buffer);
configuration->deallocate(buffer->buffer);
}
if (printed != NULL)
{
hooks->deallocate(printed);
configuration->deallocate(printed);
}
return NULL;
@ -1150,12 +1150,12 @@ fail:
/* Render a cJSON item/entity/structure to text. */
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item)
{
return (char*)print(item, true, &global_hooks);
return (char*)print(item, true, &global_configuration);
}
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item)
{
return (char*)print(item, false, &global_hooks);
return (char*)print(item, false, &global_configuration);
}
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt)
@ -1167,7 +1167,7 @@ CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON
return NULL;
}
p.buffer = (unsigned char*)global_hooks.allocate((size_t)prebuffer);
p.buffer = (unsigned char*)global_configuration.allocate((size_t)prebuffer);
if (!p.buffer)
{
return NULL;
@ -1177,11 +1177,11 @@ CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON
p.offset = 0;
p.noalloc = false;
p.format = fmt;
p.hooks = global_hooks;
p.configuration = global_configuration;
if (!print_value(item, &p))
{
global_hooks.deallocate(p.buffer);
global_configuration.deallocate(p.buffer);
return NULL;
}
@ -1202,7 +1202,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, cons
p.offset = 0;
p.noalloc = true;
p.format = format;
p.hooks = global_hooks;
p.configuration = global_configuration;
return print_value(item, &p);
}
@ -1335,7 +1335,7 @@ static cJSON_bool print_value(const cJSON * const item, printbuffer * const outp
{
if (!output_buffer->noalloc)
{
output_buffer->hooks.deallocate(output_buffer->buffer);
output_buffer->configuration.deallocate(output_buffer->buffer);
}
return false;
}
@ -1403,7 +1403,7 @@ static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buf
do
{
/* allocate next item */
cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks));
cJSON *new_item = create_item(&(input_buffer->configuration));
if (new_item == NULL)
{
goto fail; /* allocation failure */
@ -1557,7 +1557,7 @@ static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_bu
do
{
/* allocate next item */
cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks));
cJSON *new_item = create_item(&(input_buffer->configuration));
if (new_item == NULL)
{
goto fail; /* allocation failure */
@ -1858,7 +1858,7 @@ static void suffix_object(cJSON *prev, cJSON *item)
}
/* Utility for handling references. */
static cJSON *create_reference(const cJSON *item, const internal_hooks * const hooks)
static cJSON *create_reference(const cJSON *item, const internal_configuration * const configuration)
{
cJSON *reference = NULL;
if (item == NULL)
@ -1866,7 +1866,7 @@ static cJSON *create_reference(const cJSON *item, const internal_hooks * const h
return NULL;
}
reference = cJSON_New_Item(hooks);
reference = create_item(configuration);
if (reference == NULL)
{
return NULL;
@ -1930,7 +1930,7 @@ static void* cast_away_const(const void* string)
#endif
static cJSON_bool add_item_to_object(cJSON * const object, const char * const string, cJSON * const item, const internal_hooks * const hooks, const cJSON_bool constant_key)
static cJSON_bool add_item_to_object(cJSON * const object, const char * const string, cJSON * const item, const internal_configuration * const configuration, const cJSON_bool constant_key)
{
if ((object == NULL) || (string == NULL) || (item == NULL))
{
@ -1939,7 +1939,7 @@ static cJSON_bool add_item_to_object(cJSON * const object, const char * const st
if (!(item->type & cJSON_StringIsConst) && (item->string != NULL))
{
hooks->deallocate(item->string);
configuration->deallocate(item->string);
}
if (constant_key)
@ -1949,7 +1949,7 @@ static cJSON_bool add_item_to_object(cJSON * const object, const char * const st
}
else
{
char *key = (char*)cJSON_strdup((const unsigned char*)string, hooks);
char *key = (char*)custom_strdup((const unsigned char*)string, configuration);
if (key == NULL)
{
return false;
@ -1964,13 +1964,13 @@ static cJSON_bool add_item_to_object(cJSON * const object, const char * const st
CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item)
{
add_item_to_object(object, string, item, &global_hooks, false);
add_item_to_object(object, string, item, &global_configuration, false);
}
/* Add an item to an object with constant string as key */
CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item)
{
add_item_to_object(object, string, item, &global_hooks, true);
add_item_to_object(object, string, item, &global_configuration, true);
}
CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item)
@ -1980,7 +1980,7 @@ CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item)
return;
}
add_item_to_array(array, create_reference(item, &global_hooks));
add_item_to_array(array, create_reference(item, &global_configuration));
}
CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item)
@ -1990,13 +1990,13 @@ CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *str
return;
}
add_item_to_object(object, string, create_reference(item, &global_hooks), &global_hooks, false);
add_item_to_object(object, string, create_reference(item, &global_configuration), &global_configuration, false);
}
CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name)
{
cJSON *null = cJSON_CreateNull();
if (add_item_to_object(object, name, null, &global_hooks, false))
if (add_item_to_object(object, name, null, &global_configuration, false))
{
return null;
}
@ -2008,7 +2008,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * co
CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name)
{
cJSON *true_item = cJSON_CreateTrue();
if (add_item_to_object(object, name, true_item, &global_hooks, false))
if (add_item_to_object(object, name, true_item, &global_configuration, false))
{
return true_item;
}
@ -2020,7 +2020,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * co
CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name)
{
cJSON *false_item = cJSON_CreateFalse();
if (add_item_to_object(object, name, false_item, &global_hooks, false))
if (add_item_to_object(object, name, false_item, &global_configuration, false))
{
return false_item;
}
@ -2032,7 +2032,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * c
CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean)
{
cJSON *bool_item = cJSON_CreateBool(boolean);
if (add_item_to_object(object, name, bool_item, &global_hooks, false))
if (add_item_to_object(object, name, bool_item, &global_configuration, false))
{
return bool_item;
}
@ -2044,7 +2044,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * co
CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number)
{
cJSON *number_item = cJSON_CreateNumber(number);
if (add_item_to_object(object, name, number_item, &global_hooks, false))
if (add_item_to_object(object, name, number_item, &global_configuration, false))
{
return number_item;
}
@ -2056,7 +2056,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char *
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string)
{
cJSON *string_item = cJSON_CreateString(string);
if (add_item_to_object(object, name, string_item, &global_hooks, false))
if (add_item_to_object(object, name, string_item, &global_configuration, false))
{
return string_item;
}
@ -2068,7 +2068,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char *
CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw)
{
cJSON *raw_item = cJSON_CreateRaw(raw);
if (add_item_to_object(object, name, raw_item, &global_hooks, false))
if (add_item_to_object(object, name, raw_item, &global_configuration, false))
{
return raw_item;
}
@ -2080,7 +2080,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * con
CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name)
{
cJSON *object_item = cJSON_CreateObject();
if (add_item_to_object(object, name, object_item, &global_hooks, false))
if (add_item_to_object(object, name, object_item, &global_configuration, false))
{
return object_item;
}
@ -2092,7 +2092,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char *
CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name)
{
cJSON *array = cJSON_CreateArray();
if (add_item_to_object(object, name, array, &global_hooks, false))
if (add_item_to_object(object, name, array, &global_configuration, false))
{
return array;
}
@ -2257,7 +2257,7 @@ static cJSON_bool replace_item_in_object(cJSON *object, const char *string, cJSO
{
cJSON_free(replacement->string);
}
replacement->string = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks);
replacement->string = (char*)custom_strdup((const unsigned char*)string, &global_configuration);
replacement->type &= ~cJSON_StringIsConst;
cJSON_ReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), replacement);
@ -2278,7 +2278,7 @@ CJSON_PUBLIC(void) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, const c
/* Create basic types: */
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void)
{
cJSON *item = cJSON_New_Item(&global_hooks);
cJSON *item = create_item(&global_configuration);
if(item)
{
item->type = cJSON_NULL;
@ -2289,7 +2289,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void)
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void)
{
cJSON *item = cJSON_New_Item(&global_hooks);
cJSON *item = create_item(&global_configuration);
if(item)
{
item->type = cJSON_True;
@ -2300,7 +2300,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void)
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void)
{
cJSON *item = cJSON_New_Item(&global_hooks);
cJSON *item = create_item(&global_configuration);
if(item)
{
item->type = cJSON_False;
@ -2311,7 +2311,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void)
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean)
{
cJSON *item = cJSON_New_Item(&global_hooks);
cJSON *item = create_item(&global_configuration);
if(item)
{
item->type = boolean ? cJSON_True : cJSON_False;
@ -2322,7 +2322,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean)
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num)
{
cJSON *item = cJSON_New_Item(&global_hooks);
cJSON *item = create_item(&global_configuration);
if(item)
{
item->type = cJSON_Number;
@ -2335,11 +2335,11 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num)
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string)
{
cJSON *item = cJSON_New_Item(&global_hooks);
cJSON *item = create_item(&global_configuration);
if(item)
{
item->type = cJSON_String;
item->valuestring = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks);
item->valuestring = (char*)custom_strdup((const unsigned char*)string, &global_configuration);
if(!item->valuestring)
{
cJSON_Delete(item);
@ -2352,7 +2352,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string)
CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string)
{
cJSON *item = cJSON_New_Item(&global_hooks);
cJSON *item = create_item(&global_configuration);
if (item != NULL)
{
item->type = cJSON_String | cJSON_IsReference;
@ -2364,7 +2364,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string)
CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child)
{
cJSON *item = cJSON_New_Item(&global_hooks);
cJSON *item = create_item(&global_configuration);
if (item != NULL) {
item->type = cJSON_Object | cJSON_IsReference;
item->child = (cJSON*)cast_away_const(child);
@ -2374,7 +2374,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child)
}
CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child) {
cJSON *item = cJSON_New_Item(&global_hooks);
cJSON *item = create_item(&global_configuration);
if (item != NULL) {
item->type = cJSON_Array | cJSON_IsReference;
item->child = (cJSON*)cast_away_const(child);
@ -2385,11 +2385,11 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child) {
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw)
{
cJSON *item = cJSON_New_Item(&global_hooks);
cJSON *item = create_item(&global_configuration);
if(item)
{
item->type = cJSON_Raw;
item->valuestring = (char*)cJSON_strdup((const unsigned char*)raw, &global_hooks);
item->valuestring = (char*)custom_strdup((const unsigned char*)raw, &global_configuration);
if(!item->valuestring)
{
cJSON_Delete(item);
@ -2402,7 +2402,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw)
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void)
{
cJSON *item = cJSON_New_Item(&global_hooks);
cJSON *item = create_item(&global_configuration);
if(item)
{
item->type=cJSON_Array;
@ -2413,7 +2413,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void)
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void)
{
cJSON *item = cJSON_New_Item(&global_hooks);
cJSON *item = create_item(&global_configuration);
if (item)
{
item->type = cJSON_Object;
@ -2580,7 +2580,7 @@ CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse)
goto fail;
}
/* Create new item */
newitem = cJSON_New_Item(&global_hooks);
newitem = create_item(&global_configuration);
if (!newitem)
{
goto fail;
@ -2591,7 +2591,7 @@ CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse)
newitem->valuedouble = item->valuedouble;
if (item->valuestring)
{
newitem->valuestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks);
newitem->valuestring = (char*)custom_strdup((unsigned char*)item->valuestring, &global_configuration);
if (!newitem->valuestring)
{
goto fail;
@ -2599,7 +2599,7 @@ CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse)
}
if (item->string)
{
newitem->string = (item->type&cJSON_StringIsConst) ? item->string : (char*)cJSON_strdup((unsigned char*)item->string, &global_hooks);
newitem->string = (item->type&cJSON_StringIsConst) ? item->string : (char*)custom_strdup((unsigned char*)item->string, &global_configuration);
if (!newitem->string)
{
goto fail;
@ -2935,10 +2935,10 @@ CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * cons
CJSON_PUBLIC(void *) cJSON_malloc(size_t size)
{
return global_hooks.allocate(size);
return global_configuration.allocate(size);
}
CJSON_PUBLIC(void) cJSON_free(void *object)
{
global_hooks.deallocate(object);
global_configuration.deallocate(object);
}

View File

@ -33,11 +33,11 @@ void reset(cJSON *item) {
}
if ((item->valuestring != NULL) && !(item->type & cJSON_IsReference))
{
global_hooks.deallocate(item->valuestring);
global_configuration.deallocate(item->valuestring);
}
if ((item->string != NULL) && !(item->type & cJSON_StringIsConst))
{
global_hooks.deallocate(item->string);
global_configuration.deallocate(item->string);
}
memset(item, 0, sizeof(cJSON));

View File

@ -432,7 +432,7 @@ static void skip_utf8_bom_should_skip_bom(void)
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
buffer.content = string;
buffer.length = sizeof(string);
buffer.hooks = global_hooks;
buffer.configuration = global_configuration;
TEST_ASSERT_TRUE(skip_utf8_bom(&buffer) == &buffer);
TEST_ASSERT_EQUAL_UINT(3U, (unsigned int)buffer.offset);
@ -444,7 +444,7 @@ static void skip_utf8_bom_should_not_skip_bom_if_not_at_beginning(void)
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
buffer.content = string;
buffer.length = sizeof(string);
buffer.hooks = global_hooks;
buffer.configuration = global_configuration;
buffer.offset = 1;
TEST_ASSERT_NULL(skip_utf8_bom(&buffer));

View File

@ -47,7 +47,7 @@ static void assert_not_array(const char *json)
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
buffer.content = (const unsigned char*)json;
buffer.length = strlen(json) + sizeof("");
buffer.hooks = global_hooks;
buffer.configuration = global_configuration;
TEST_ASSERT_FALSE(parse_array(item, &buffer));
assert_is_invalid(item);
@ -58,7 +58,7 @@ static void assert_parse_array(const char *json)
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
buffer.content = (const unsigned char*)json;
buffer.length = strlen(json) + sizeof("");
buffer.hooks = global_hooks;
buffer.configuration = global_configuration;
TEST_ASSERT_TRUE(parse_array(item, &buffer));
assert_is_array(item);

View File

@ -55,7 +55,7 @@ static void assert_not_object(const char *json)
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } };
parsebuffer.content = (const unsigned char*)json;
parsebuffer.length = strlen(json) + sizeof("");
parsebuffer.hooks = global_hooks;
parsebuffer.configuration = global_configuration;
TEST_ASSERT_FALSE(parse_object(item, &parsebuffer));
assert_is_invalid(item);
@ -67,7 +67,7 @@ static void assert_parse_object(const char *json)
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } };
parsebuffer.content = (const unsigned char*)json;
parsebuffer.length = strlen(json) + sizeof("");
parsebuffer.hooks = global_hooks;
parsebuffer.configuration = global_configuration;
TEST_ASSERT_TRUE(parse_object(item, &parsebuffer));
assert_is_object(item);

View File

@ -48,12 +48,12 @@ static void assert_parse_string(const char *string, const char *expected)
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
buffer.content = (const unsigned char*)string;
buffer.length = strlen(string) + sizeof("");
buffer.hooks = global_hooks;
buffer.configuration = global_configuration;
TEST_ASSERT_TRUE_MESSAGE(parse_string(item, &buffer), "Couldn't parse string.");
assert_is_string(item);
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, item->valuestring, "The parsed result isn't as expected.");
global_hooks.deallocate(item->valuestring);
global_configuration.deallocate(item->valuestring);
item->valuestring = NULL;
}
@ -62,7 +62,7 @@ static void assert_not_parse_string(const char * const string)
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
buffer.content = (const unsigned char*)string;
buffer.length = strlen(string) + sizeof("");
buffer.hooks = global_hooks;
buffer.configuration = global_configuration;
TEST_ASSERT_FALSE_MESSAGE(parse_string(item, &buffer), "Malformed string should not be accepted.");
assert_is_invalid(item);

View File

@ -46,7 +46,7 @@ static void assert_parse_value(const char *string, int type)
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
buffer.content = (const unsigned char*) string;
buffer.length = strlen(string) + sizeof("");
buffer.hooks = global_hooks;
buffer.configuration = global_configuration;
TEST_ASSERT_TRUE(parse_value(item, &buffer));
assert_is_value(item, type);

View File

@ -37,21 +37,21 @@ static void assert_print_array(const char * const expected, const char * const i
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } };
parsebuffer.content = (const unsigned char*)input;
parsebuffer.length = strlen(input) + sizeof("");
parsebuffer.hooks = global_hooks;
parsebuffer.configuration = global_configuration;
/* buffer for formatted printing */
formatted_buffer.buffer = printed_formatted;
formatted_buffer.length = sizeof(printed_formatted);
formatted_buffer.offset = 0;
formatted_buffer.noalloc = true;
formatted_buffer.hooks = global_hooks;
formatted_buffer.configuration = global_configuration;
/* buffer for unformatted printing */
unformatted_buffer.buffer = printed_unformatted;
unformatted_buffer.length = sizeof(printed_unformatted);
unformatted_buffer.offset = 0;
unformatted_buffer.noalloc = true;
unformatted_buffer.hooks = global_hooks;
unformatted_buffer.configuration = global_configuration;
memset(item, 0, sizeof(item));
TEST_ASSERT_TRUE_MESSAGE(parse_array(item, &parsebuffer), "Failed to parse array.");

View File

@ -33,7 +33,7 @@ static void assert_print_number(const char *expected, double input)
buffer.length = sizeof(printed);
buffer.offset = 0;
buffer.noalloc = true;
buffer.hooks = global_hooks;
buffer.configuration = global_configuration;
memset(item, 0, sizeof(item));
cJSON_SetNumberValue(item, input);

View File

@ -38,21 +38,21 @@ static void assert_print_object(const char * const expected, const char * const
/* buffer for parsing */
parsebuffer.content = (const unsigned char*)input;
parsebuffer.length = strlen(input) + sizeof("");
parsebuffer.hooks = global_hooks;
parsebuffer.configuration = global_configuration;
/* buffer for formatted printing */
formatted_buffer.buffer = printed_formatted;
formatted_buffer.length = sizeof(printed_formatted);
formatted_buffer.offset = 0;
formatted_buffer.noalloc = true;
formatted_buffer.hooks = global_hooks;
formatted_buffer.configuration = global_configuration;
/* buffer for unformatted printing */
unformatted_buffer.buffer = printed_unformatted;
unformatted_buffer.length = sizeof(printed_unformatted);
unformatted_buffer.offset = 0;
unformatted_buffer.noalloc = true;
unformatted_buffer.hooks = global_hooks;
unformatted_buffer.configuration = global_configuration;
memset(item, 0, sizeof(item));
TEST_ASSERT_TRUE_MESSAGE(parse_object(item, &parsebuffer), "Failed to parse object.");

View File

@ -32,7 +32,7 @@ static void assert_print_string(const char *expected, const char *input)
buffer.length = sizeof(printed);
buffer.offset = 0;
buffer.noalloc = true;
buffer.hooks = global_hooks;
buffer.configuration = global_configuration;
TEST_ASSERT_TRUE_MESSAGE(print_string_ptr((const unsigned char*)input, &buffer), "Failed to print string.");
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed, "The printed string isn't as expected.");

View File

@ -38,11 +38,11 @@ static void assert_print_value(const char *input)
buffer.length = sizeof(printed);
buffer.offset = 0;
buffer.noalloc = true;
buffer.hooks = global_hooks;
buffer.configuration = global_configuration;
parsebuffer.content = (const unsigned char*)input;
parsebuffer.length = strlen(input) + sizeof("");
parsebuffer.hooks = global_hooks;
parsebuffer.configuration = global_configuration;
memset(item, 0, sizeof(item));