cJSON_Delete: Extract delete_item with internal_configuration

This commit is contained in:
Max Bruckner 2018-02-01 01:20:37 +01:00
parent 7030dc7c5b
commit d4e81cfe57

62
cJSON.c
View File

@ -218,7 +218,7 @@ static cJSON *create_item(const internal_configuration * const configuration)
} }
/* Delete a cJSON structure. */ /* Delete a cJSON structure. */
CJSON_PUBLIC(void) cJSON_Delete(cJSON *item) static void delete_item(cJSON *item, const internal_configuration * const configuration)
{ {
cJSON *next = NULL; cJSON *next = NULL;
while (item != NULL) while (item != NULL)
@ -226,21 +226,27 @@ CJSON_PUBLIC(void) cJSON_Delete(cJSON *item)
next = item->next; next = item->next;
if (!(item->type & cJSON_IsReference) && (item->child != NULL)) if (!(item->type & cJSON_IsReference) && (item->child != NULL))
{ {
cJSON_Delete(item->child); delete_item(item->child, configuration);
} }
if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL)) if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL))
{ {
global_configuration.deallocate(item->valuestring); configuration->deallocate(item->valuestring);
} }
if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) if (!(item->type & cJSON_StringIsConst) && (item->string != NULL))
{ {
global_configuration.deallocate(item->string); configuration->deallocate(item->string);
} }
global_configuration.deallocate(item); configuration->deallocate(item);
item = next; item = next;
} }
} }
/* Delete a cJSON structure. */
CJSON_PUBLIC(void) cJSON_Delete(cJSON *item)
{
delete_item(item, &global_configuration);
}
static int double_to_saturated_integer(double number) static int double_to_saturated_integer(double number)
{ {
if (number >= INT_MAX) if (number >= INT_MAX)
@ -1049,7 +1055,7 @@ CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return
fail: fail:
if (item != NULL) if (item != NULL)
{ {
cJSON_Delete(item); delete_item(item, &global_configuration);
} }
if (value != NULL) if (value != NULL)
@ -1438,7 +1444,7 @@ success:
fail: fail:
if (head != NULL) if (head != NULL)
{ {
cJSON_Delete(head); delete_item(head, &input_buffer->configuration);
} }
return false; return false;
@ -1609,7 +1615,7 @@ success:
fail: fail:
if (head != NULL) if (head != NULL)
{ {
cJSON_Delete(head); delete_item(head, &input_buffer->configuration);
} }
return false; return false;
@ -1992,7 +1998,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * co
return null; return null;
} }
cJSON_Delete(null); delete_item(null, &global_configuration);
return NULL; return NULL;
} }
@ -2004,7 +2010,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * co
return true_item; return true_item;
} }
cJSON_Delete(true_item); delete_item(true_item, &global_configuration);
return NULL; return NULL;
} }
@ -2016,7 +2022,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * c
return false_item; return false_item;
} }
cJSON_Delete(false_item); delete_item(false_item, &global_configuration);
return NULL; return NULL;
} }
@ -2028,7 +2034,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * co
return bool_item; return bool_item;
} }
cJSON_Delete(bool_item); delete_item(bool_item, &global_configuration);
return NULL; return NULL;
} }
@ -2040,7 +2046,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char *
return number_item; return number_item;
} }
cJSON_Delete(number_item); delete_item(number_item, &global_configuration);
return NULL; return NULL;
} }
@ -2052,7 +2058,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char *
return string_item; return string_item;
} }
cJSON_Delete(string_item); delete_item(string_item, &global_configuration);
return NULL; return NULL;
} }
@ -2064,7 +2070,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * con
return raw_item; return raw_item;
} }
cJSON_Delete(raw_item); delete_item(raw_item, &global_configuration);
return NULL; return NULL;
} }
@ -2076,7 +2082,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char *
return object_item; return object_item;
} }
cJSON_Delete(object_item); delete_item(object_item, &global_configuration);
return NULL; return NULL;
} }
@ -2088,7 +2094,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * c
return array; return array;
} }
cJSON_Delete(array); delete_item(array, &global_configuration);
return NULL; return NULL;
} }
@ -2134,7 +2140,7 @@ CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which)
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which) CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which)
{ {
cJSON_Delete(cJSON_DetachItemFromArray(array, which)); delete_item(cJSON_DetachItemFromArray(array, which), &global_configuration);
} }
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string) CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string)
@ -2153,12 +2159,12 @@ CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, con
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string) CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string)
{ {
cJSON_Delete(cJSON_DetachItemFromObject(object, string)); delete_item(cJSON_DetachItemFromObject(object, string), &global_configuration);
} }
CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string) CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string)
{ {
cJSON_Delete(cJSON_DetachItemFromObjectCaseSensitive(object, string)); delete_item(cJSON_DetachItemFromObjectCaseSensitive(object, string), &global_configuration);
} }
/* Replace array/object items with new ones. */ /* Replace array/object items with new ones. */
@ -2221,7 +2227,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON
item->next = NULL; item->next = NULL;
item->prev = NULL; item->prev = NULL;
cJSON_Delete(item); delete_item(item, &global_configuration);
return true; return true;
} }
@ -2333,7 +2339,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string)
item->valuestring = (char*)custom_strdup((const unsigned char*)string, &global_configuration); item->valuestring = (char*)custom_strdup((const unsigned char*)string, &global_configuration);
if(!item->valuestring) if(!item->valuestring)
{ {
cJSON_Delete(item); delete_item(item, &global_configuration);
return NULL; return NULL;
} }
} }
@ -2383,7 +2389,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw)
item->valuestring = (char*)custom_strdup((const unsigned char*)raw, &global_configuration); item->valuestring = (char*)custom_strdup((const unsigned char*)raw, &global_configuration);
if(!item->valuestring) if(!item->valuestring)
{ {
cJSON_Delete(item); delete_item(item, &global_configuration);
return NULL; return NULL;
} }
} }
@ -2432,7 +2438,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count)
n = cJSON_CreateNumber(numbers[i]); n = cJSON_CreateNumber(numbers[i]);
if (!n) if (!n)
{ {
cJSON_Delete(a); delete_item(a, &global_configuration);
return NULL; return NULL;
} }
if(!i) if(!i)
@ -2468,7 +2474,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count)
n = cJSON_CreateNumber((double)numbers[i]); n = cJSON_CreateNumber((double)numbers[i]);
if(!n) if(!n)
{ {
cJSON_Delete(a); delete_item(a, &global_configuration);
return NULL; return NULL;
} }
if(!i) if(!i)
@ -2504,7 +2510,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count)
n = cJSON_CreateNumber(numbers[i]); n = cJSON_CreateNumber(numbers[i]);
if(!n) if(!n)
{ {
cJSON_Delete(a); delete_item(a, &global_configuration);
return NULL; return NULL;
} }
if(!i) if(!i)
@ -2540,7 +2546,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count)
n = cJSON_CreateString(strings[i]); n = cJSON_CreateString(strings[i]);
if(!n) if(!n)
{ {
cJSON_Delete(a); delete_item(a, &global_configuration);
return NULL; return NULL;
} }
if(!i) if(!i)
@ -2631,7 +2637,7 @@ CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse)
fail: fail:
if (newitem != NULL) if (newitem != NULL)
{ {
cJSON_Delete(newitem); delete_item(newitem, &global_configuration);
} }
return NULL; return NULL;