From f9e9f0ebd82fc948450d69f0ba715336b6f416d9 Mon Sep 17 00:00:00 2001 From: Abdelrahman Said Date: Sat, 1 Jul 2023 16:45:31 +0100 Subject: [PATCH] Add function to traverse json tree and free all memory allocations --- compile_commands.json | 20 ++++++------ include/json_entities/json_entities.h | 1 + src/json_entities/json_entities.c | 46 +++++++++++++++++++++++++++ src/main.c | 2 ++ 4 files changed, 59 insertions(+), 10 deletions(-) diff --git a/compile_commands.json b/compile_commands.json index 9228369..939f506 100644 --- a/compile_commands.json +++ b/compile_commands.json @@ -170,12 +170,12 @@ "-x", "c", "-o", - "/tmp/main-5fdd54.o", + "/tmp/main-4e7ed0.o", "src/main.c" ], "directory": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json", "file": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json/src/main.c", - "output": "/tmp/main-5fdd54.o" + "output": "/tmp/main-4e7ed0.o" }, { "arguments": [ @@ -243,12 +243,12 @@ "-x", "c", "-o", - "/tmp/dstring-157144.o", + "/tmp/dstring-762950.o", "src/dstring/dstring.c" ], "directory": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json", "file": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json/src/dstring/dstring.c", - "output": "/tmp/dstring-157144.o" + "output": "/tmp/dstring-762950.o" }, { "arguments": [ @@ -316,12 +316,12 @@ "-x", "c", "-o", - "/tmp/json_entities-f20100.o", + "/tmp/json_entities-5ce443.o", "src/json_entities/json_entities.c" ], "directory": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json", "file": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json/src/json_entities/json_entities.c", - "output": "/tmp/json_entities-f20100.o" + "output": "/tmp/json_entities-5ce443.o" }, { "arguments": [ @@ -389,12 +389,12 @@ "-x", "c", "-o", - "/tmp/lexer-eb6c2d.o", + "/tmp/lexer-6ba5bd.o", "src/lexer/lexer.c" ], "directory": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json", "file": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json/src/lexer/lexer.c", - "output": "/tmp/lexer-eb6c2d.o" + "output": "/tmp/lexer-6ba5bd.o" }, { "arguments": [ @@ -462,11 +462,11 @@ "-x", "c", "-o", - "/tmp/parser-73724a.o", + "/tmp/parser-95ad8d.o", "src/parser/parser.c" ], "directory": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json", "file": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json/src/parser/parser.c", - "output": "/tmp/parser-73724a.o" + "output": "/tmp/parser-95ad8d.o" } ] diff --git a/include/json_entities/json_entities.h b/include/json_entities/json_entities.h index 2841c7e..86cc3d8 100644 --- a/include/json_entities/json_entities.h +++ b/include/json_entities/json_entities.h @@ -65,6 +65,7 @@ struct json_collection { }; void print_json(const jentity_t *entity, u32 indent); +void free_json(jentity_t **entity); jcoll_t *get_collection_from_entity(const jentity_t *entity); jentity_t *create_new_single_entity(const jval_t value, jentity_t *parent); jentity_t *create_new_pair_entity(dstr_t *key, const jval_t value, diff --git a/src/json_entities/json_entities.c b/src/json_entities/json_entities.c index 9b31c4d..9510a85 100644 --- a/src/json_entities/json_entities.c +++ b/src/json_entities/json_entities.c @@ -110,6 +110,52 @@ void print_json(const jentity_t *entity, u32 indent) { } } +void free_json(jentity_t **entity) { + if (!(*entity)) { + return; + } + + jentity_t *entt_ptr = *entity; + + dstr_t *key = NULL; + const jval_t *value = NULL; + + if (entt_ptr->type == JENTITY_SINGLE) { + value = &(entt_ptr->value); + } else { + key = entt_ptr->pair.key; + value = &(entt_ptr->pair.value); + } + + if (key) { + dstr_free(&(entt_ptr->pair.key)); + } + + switch (value->type) { + case JVAL_COLLECTION: + if (value->collection->begin) { + free_json(&(value->collection->begin)); + } + + free(value->collection); + + break; + case JVAL_STRING: + dstr_free(&(entt_ptr->pair.value.string)); + + break; + default: + break; + } + + if (entt_ptr->next) { + free_json(&(entt_ptr->next)); + } + + free(*entity); + *entity = NULL; +} + jcoll_t *get_collection_from_entity(const jentity_t *entity) { return entity->type == JENTITY_SINGLE ? entity->value.collection : entity->pair.value.collection; diff --git a/src/main.c b/src/main.c index 8539a09..13f89fe 100644 --- a/src/main.c +++ b/src/main.c @@ -23,5 +23,7 @@ int main(int argc, char *argv[]) { print_json(root, 2); + free_json(&(root)); + return EXIT_SUCCESS; }