From 0b14898cee9928d255ce2529cdf6acd8e55001f2 Mon Sep 17 00:00:00 2001 From: overflowerror Date: Thu, 6 May 2021 14:51:44 +0200 Subject: [PATCH] added new free function for arrays and updated codegen to free arrays --- demo/marshaller.c | 1 + marshaller/codegen.c | 6 +++++- src/marshaller.c | 31 +++++++++++++++++++++++++++++++ src/marshaller.h | 3 +++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/demo/marshaller.c b/demo/marshaller.c index 4a76607..3f07322 100644 --- a/demo/marshaller.c +++ b/demo/marshaller.c @@ -41,6 +41,7 @@ int main() { free(post->content); post->content = "Just do it."; + json_free_prim_array(post->user.aliases); char** aliases = alloca(sizeof(char*) * 3); aliases[0] = "overflowerror"; aliases[1] = "overflow"; diff --git a/marshaller/codegen.c b/marshaller/codegen.c index ddc23b5..8b35df4 100644 --- a/marshaller/codegen.c +++ b/marshaller/codegen.c @@ -172,7 +172,9 @@ char* generateFreeFunction(FILE* output, struct structinfo* info, char* suffix) fprintf(output, "\t%s* d = (%s*) _d;\n", info->names[0], info->names[0]); for (size_t i = 0; i < info->memberno; i++) { struct memberinfo* member = info->members[i]; - if (member->type->isPointer || strcmp(member->type->type, "string") == 0) { + if (member->type->isArray) { + fprintf(output, "\t_json_free_array(\"%s\", (void**) d->%s);\n", member->type->type, member->name); + } else if (member->type->isPointer || strcmp(member->type->type, "string") == 0) { fprintf(output, "\t_json_free_struct(\"%s\", (void*) d->%s, true);\n", member->type->type, member->name); } else if (!(strcmp(member->type->type, "char") == 0 || strcmp(member->type->type, "short") == 0 || @@ -184,6 +186,8 @@ char* generateFreeFunction(FILE* output, struct structinfo* info, char* suffix) strcmp(member->type->type, "bool") == 0 )) { fprintf(output, "\t_json_free_struct(\"%s\", (void*) &(d->%s), false);\n", member->type->type, member->name); + } else { + // will be freed with this } } fprintf(output, "\tif (this)\n"); diff --git a/src/marshaller.c b/src/marshaller.c index 4721233..a7a8a9a 100644 --- a/src/marshaller.c +++ b/src/marshaller.c @@ -372,3 +372,34 @@ void _json_free_struct(const char* type, void* value, bool this) { marshaller->free(value, this); } } + +void _json_free_array(const char* type, void** value) { + if (value == NULL) { + return; + } else if (strcmp(type, "char") == 0 || + strcmp(type, "short") == 0 || + strcmp(type, "int") == 0 || + strcmp(type, "long") == 0 || + strcmp(type, "long long") == 0 || + strcmp(type, "float") == 0 || + strcmp(type, "double") == 0 || + strcmp(type, "bool") == 0 || + strcmp(type, "string") == 0 + ) { + for (size_t i = 0; value[i] != NULL; i++) { + free(value[i]); + } + free(value); + } else { + struct marshaller* marshaller = findMarshaller(type); + if (marshaller == NULL) { + _marshallPanic(type, "unknown type"); + } + + for (size_t i = 0; value[i] != NULL; i++) { + marshaller->free(value[i], true); + } + + free(value); + } +} diff --git a/src/marshaller.h b/src/marshaller.h index a6f5ebf..7bf9127 100644 --- a/src/marshaller.h +++ b/src/marshaller.h @@ -14,6 +14,7 @@ void* _json_unmarshall(const char* type, const char* json); void* _json_unmarshall_array(const char* type, const char* json); void _json_free_struct(const char* type, void* value, bool this); +void _json_free_array(const char* type, void** value); #define json_marshall(t, v) _json_marshall(# t, (void*) v) #define json_marshall_array(t, v) _json_marshall_array(# t, (void*) v) @@ -21,5 +22,7 @@ void _json_free_struct(const char* type, void* value, bool this); #define json_unmarshall_array(t, j) (t*) _json_unmarshall_array(#t, j) #define json_free_struct(t, v) _json_free_struct(#t, v, true) +#define json_free_array(t, v) _json_free_array(#t, (void**) v) +#define json_free_prim_array(v) _json_free_array("int", (void**) v) #endif