added new free function for arrays and updated codegen to free arrays

This commit is contained in:
overflowerror 2021-05-06 14:51:44 +02:00
parent 0bff23e15e
commit 0b14898cee
4 changed files with 40 additions and 1 deletions

View file

@ -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";

View file

@ -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");

View file

@ -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);
}
}

View file

@ -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