added recursive free function for structs

This commit is contained in:
overflowerror 2021-05-04 16:44:36 +02:00
parent 834d9a116e
commit 0284446885
3 changed files with 61 additions and 4 deletions

View file

@ -20,6 +20,7 @@ static struct marshaller {
const char* name;
jsonValue_t* (*marshaller)(void*);
void* (*unmarshaller)(jsonValue_t*);
void (*free)(void*);
}* marshallerList = NULL;
static size_t marshallerListLength = 0;
@ -33,7 +34,7 @@ static struct marshaller* findMarshaller(const char* type) {
return NULL;
}
void _registerMarshaller(int namesCount, const char** names, jsonValue_t* (*marshaller)(void*), void* (*unmarshaller)(jsonValue_t*)) {
void _registerMarshaller(int namesCount, const char** names, jsonValue_t* (*marshaller)(void*), void* (*unmarshaller)(jsonValue_t*), void (*structFree)(void*)) {
marshallerList = realloc(marshallerList, (sizeof(struct marshaller)) * (marshallerListLength + namesCount));
if (marshallerList == NULL) {
_marshallPanic(names[0], NULL);
@ -47,7 +48,8 @@ void _registerMarshaller(int namesCount, const char** names, jsonValue_t* (*mars
marshallerList[marshallerListLength++] = (struct marshaller) {
.name = names[i],
.marshaller = marshaller,
.unmarshaller = unmarshaller
.unmarshaller = unmarshaller,
.free = structFree
};
}
}
@ -272,3 +274,26 @@ void* _json_unmarshall(const char* type, const char* json) {
free(value);
return tmp;
}
void _json_free_struct(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
) {
free(value);
} else {
struct marshaller* marshaller = findMarshaller(type);
if (marshaller == NULL) {
_marshallPanic(type, "unknown type");
}
marshaller->free(value);
}
}

View file

@ -7,7 +7,11 @@ char* _json_marshall(const char* type, void* value);
void* _json_unmarshall_value(const char* type, jsonValue_t* value);
void* _json_unmarshall(const char* type, const char* json);
void _json_free_struct(const char* type, void* value);
#define json_marshall(t, v) _json_marshall(# t, (void*) v)
#define json_unmarshall(t, j) (t*) _json_unmarshall(#t, j)
#define json_free_struct(t, v) _json_free_struct(#t, v)
#endif

View file

@ -36,7 +36,7 @@ void generatePreamble(FILE* output, char* files[], int fileno) {
fprintf(output, "\n");
fprintf(output, "extern void _marshallPanic(const char*, const char*);\n");
fprintf(output, "extern void _registerMarshaller(int, const char**, jsonValue_t*(*)(void*), void*(*)(jsonValue_t*));\n");
fprintf(output, "extern void _registerMarshaller(int, const char**, jsonValue_t*(*)(void*), void*(*)(jsonValue_t*), void(*)(void*));\n");
fprintf(output, "\n");
}
@ -138,11 +138,39 @@ char* generateUnmarshallFunction(FILE* output, struct structinfo* info, char* su
return functionName;
}
char* generateFreeFunction(FILE* output, struct structinfo* info, char* suffix) {
#define FREE_FUNCTION_PREFIX "_json_free_value_"
char* functionName = malloc(strlen(FREE_FUNCTION_PREFIX) + strlen(suffix) + 1);
if (functionName == NULL) {
panic("malloc");
}
strcpy(functionName, FREE_FUNCTION_PREFIX);
strcat(functionName, suffix);
fprintf(output, "static void %s(void* _d) {\n", functionName);
fprintf(output, "\tif (_d == NULL)\n");
fprintf(output, "\t\treturn;\n");
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) {
fprintf(output, "\t_json_free_struct(\"%s\", (void*) d->%s);\n", member->type->type, member->name);
}
}
fprintf(output, "\tfree(d);\n");
fprintf(output, "}\n\n");
return functionName;
}
void generateCodeStruct(FILE* output, struct structinfo* info) {
char* suffix = fixStructName(info->names[0]);
char* marshall = generateMarshallFunction(output, info, suffix);
char* unmarshall = generateUnmarshallFunction(output, info, suffix);
char* freeStruct = generateFreeFunction(output, info, suffix);
fprintf(output, "__attribute__((constructor)) static void _register_marshaller_%s_() {\n", suffix);
int namesno = info->names[1] == 0 ? 1 : 2;
@ -150,7 +178,7 @@ void generateCodeStruct(FILE* output, struct structinfo* info) {
fprintf(output, "\ttmp[0] = \"%s\";\n", info->names[0]);
if (namesno > 1)
fprintf(output, "\ttmp[1] = \"%s\";\n", info->names[1]);
fprintf(output, "\t_registerMarshaller(%d, tmp, &%s, &%s);\n", namesno, marshall, unmarshall);
fprintf(output, "\t_registerMarshaller(%d, tmp, &%s, &%s, &%s);\n", namesno, marshall, unmarshall, freeStruct);
fprintf(output, "}\n\n");
free(suffix);