From d4e81cfe570219b3b1ecea7d3023c1672f21f588 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Thu, 1 Feb 2018 01:20:37 +0100 Subject: [PATCH] cJSON_Delete: Extract delete_item with internal_configuration --- cJSON.c | 62 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/cJSON.c b/cJSON.c index 6db57f0..b1ca035 100644 --- a/cJSON.c +++ b/cJSON.c @@ -218,7 +218,7 @@ static cJSON *create_item(const internal_configuration * const configuration) } /* 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; while (item != NULL) @@ -226,21 +226,27 @@ CJSON_PUBLIC(void) cJSON_Delete(cJSON *item) next = item->next; 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)) { - global_configuration.deallocate(item->valuestring); + configuration->deallocate(item->valuestring); } 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; } } +/* Delete a cJSON structure. */ +CJSON_PUBLIC(void) cJSON_Delete(cJSON *item) +{ + delete_item(item, &global_configuration); +} + static int double_to_saturated_integer(double number) { if (number >= INT_MAX) @@ -1049,7 +1055,7 @@ CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return fail: if (item != NULL) { - cJSON_Delete(item); + delete_item(item, &global_configuration); } if (value != NULL) @@ -1438,7 +1444,7 @@ success: fail: if (head != NULL) { - cJSON_Delete(head); + delete_item(head, &input_buffer->configuration); } return false; @@ -1609,7 +1615,7 @@ success: fail: if (head != NULL) { - cJSON_Delete(head); + delete_item(head, &input_buffer->configuration); } return false; @@ -1992,7 +1998,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * co return null; } - cJSON_Delete(null); + delete_item(null, &global_configuration); return NULL; } @@ -2004,7 +2010,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * co return true_item; } - cJSON_Delete(true_item); + delete_item(true_item, &global_configuration); return NULL; } @@ -2016,7 +2022,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * c return false_item; } - cJSON_Delete(false_item); + delete_item(false_item, &global_configuration); return NULL; } @@ -2028,7 +2034,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * co return bool_item; } - cJSON_Delete(bool_item); + delete_item(bool_item, &global_configuration); return NULL; } @@ -2040,7 +2046,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * return number_item; } - cJSON_Delete(number_item); + delete_item(number_item, &global_configuration); return NULL; } @@ -2052,7 +2058,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * return string_item; } - cJSON_Delete(string_item); + delete_item(string_item, &global_configuration); return NULL; } @@ -2064,7 +2070,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * con return raw_item; } - cJSON_Delete(raw_item); + delete_item(raw_item, &global_configuration); return NULL; } @@ -2076,7 +2082,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * return object_item; } - cJSON_Delete(object_item); + delete_item(object_item, &global_configuration); return NULL; } @@ -2088,7 +2094,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * c return array; } - cJSON_Delete(array); + delete_item(array, &global_configuration); 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_Delete(cJSON_DetachItemFromArray(array, which)); + delete_item(cJSON_DetachItemFromArray(array, which), &global_configuration); } 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_Delete(cJSON_DetachItemFromObject(object, string)); + delete_item(cJSON_DetachItemFromObject(object, string), &global_configuration); } 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. */ @@ -2221,7 +2227,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON item->next = NULL; item->prev = NULL; - cJSON_Delete(item); + delete_item(item, &global_configuration); 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); if(!item->valuestring) { - cJSON_Delete(item); + delete_item(item, &global_configuration); 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); if(!item->valuestring) { - cJSON_Delete(item); + delete_item(item, &global_configuration); return NULL; } } @@ -2432,7 +2438,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count) n = cJSON_CreateNumber(numbers[i]); if (!n) { - cJSON_Delete(a); + delete_item(a, &global_configuration); return NULL; } if(!i) @@ -2468,7 +2474,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count) n = cJSON_CreateNumber((double)numbers[i]); if(!n) { - cJSON_Delete(a); + delete_item(a, &global_configuration); return NULL; } if(!i) @@ -2504,7 +2510,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count) n = cJSON_CreateNumber(numbers[i]); if(!n) { - cJSON_Delete(a); + delete_item(a, &global_configuration); return NULL; } if(!i) @@ -2540,7 +2546,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count) n = cJSON_CreateString(strings[i]); if(!n) { - cJSON_Delete(a); + delete_item(a, &global_configuration); return NULL; } if(!i) @@ -2631,7 +2637,7 @@ CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse) fail: if (newitem != NULL) { - cJSON_Delete(newitem); + delete_item(newitem, &global_configuration); } return NULL;