From 7062a6c280ccc8a2d92fd9c5a73f16cfba19cbc9 Mon Sep 17 00:00:00 2001 From: Not Zed Date: Thu, 11 Jan 2024 17:08:37 +1030 Subject: [PATCH] Add a *_free() function generator. Plus some small fixes. --- ez-blob-compiler.c | 55 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/ez-blob-compiler.c b/ez-blob-compiler.c index 56f023c..d86969b 100644 --- a/ez-blob-compiler.c +++ b/ez-blob-compiler.c @@ -171,7 +171,6 @@ static void export_encode(const ez_blob_desc *desc, const char *name) { } static void export_decode(const ez_blob_desc *desc, const char *name, int doos) { - fputs("\n", stdout); if (doos) { printf("void *%s_decode_os(const ez_blob * __restrict blob, struct obstack * __restrict os) {\n", name); printf("\tvoid *p = obstack_alloc(os, %u);\n", desc->bd_offset); @@ -181,8 +180,7 @@ static void export_decode(const ez_blob_desc *desc, const char *name, int doos) printf("\tconst char * __restrict b = blob->eb_data;\n"); printf("\tsize_t len;\n"); printf("\tchar **sp;\n"); - if (doos) - printf("\tmemset(p, 0, %u);\n", desc->bd_offset); + printf("\tmemset(p, 0, %u);\n", desc->bd_offset); for (int i=0,dlen=desc->bd_length;ibd_length;ibd_type) { + case EZ_BLOB_INT8: + case EZ_BLOB_INT16: + case EZ_BLOB_INT32: + case EZ_BLOB_FLOAT32: + case EZ_BLOB_INT64: + case EZ_BLOB_FLOAT64: + break; + case EZ_BLOB_INT8 | EZ_BLOB_CSTRING: + case EZ_BLOB_TRANSIENTP: + printf("\tfree(*((char **)(p + %d)));\n", d->bd_offset); + break; + default: + fprintf(stderr, "Unsupported type: %d\n", d->bd_type); + abort(); + } + } + printf("}\n"); + printf("void %1$s_free(struct %1$s * __restrict p) {\n" + "\tif (p) {\n" + "\t\t%1$s_free_raw(p);\n" + "\t\tfree(p);\n" + "\t}\n" + "}\n" , name); +} + int main(int argc, char **argv) { int gen = 0; int header = 0; @@ -281,12 +309,17 @@ int main(int argc, char **argv) { dogen |= 4; else if (strcmp(s, "decode_obstack") == 0) dogen |= 8; + else if (strcmp(s, "free") == 0) + dogen |= 16; } if (dogen) gen = dogen; i += 1; } else if (strcmp(cmd, "--header") == 0 || strcmp(cmd, "-h") == 0) { header = 1; + } else if (strcmp(cmd, "-i") == 0) { + printf("#include \"%s\"\n", argv[i+1]); + i += 1; } else { file = cmd; tables = i + 1; @@ -373,12 +406,14 @@ int main(int argc, char **argv) { if (gen & 2) printf("void %s_encode_raw(const void *p, ez_blob *blob);\n", xname); if (gen & 4) { - printf("void %s_decode_raw(const ez_blob *blob, void *p);\n", xname); - if ((gen & 8) == 0) - printf("void *%s_decode(const ez_blob *blob);\n", xname); + printf("void %1$s_decode_raw(const ez_blob *blob, void *p);\n" + "void *%1$s_decode(const ez_blob *blob);\n", xname); } if (gen & 8) printf("void *%s_decode_os(const ez_blob *blob, struct obstack *os);\n", xname); + if (gen & 16) + printf("struct %1$s;\n" + "void %1$s_free(struct %1$s *p);\n", xname); } else { if (gen & 1) export_size(desc, xname); @@ -388,6 +423,8 @@ int main(int argc, char **argv) { export_decode(desc, xname, 0); if (gen & 8) export_decode(desc, xname, 1); + if (gen & 16) + export_free(desc, xname); } } -- 2.39.5