}
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);
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;i<dlen;i++) {
const ez_blob_desc *d = &desc[i+1];
printf("}\n");
if (!doos) {
- fputs("\n", stdout);
- printf("void *%1$s_decode(const ez_blob *blob) {\n"
- "\tvoid *p = calloc(1, %2$u);\n"
+ printf("struct %1$s;\n"
+ "void *%1$s_decode(const ez_blob *blob) {\n"
+ "\tvoid *p = malloc(%2$u);\n"
"\t%1$s_decode_raw(blob, p);\n"
"\treturn p;\n"
"}\n",
}
}
+static void export_free(const ez_blob_desc *desc, const char *name) {
+ printf("void %1$s_free_raw(void * __restrict p) {\n", name);
+ for (int i=0,dlen=desc->bd_length;i<dlen;i++) {
+ const ez_blob_desc *d = &desc[i+1];
+ switch (d->bd_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;
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;
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);
export_decode(desc, xname, 0);
if (gen & 8)
export_decode(desc, xname, 1);
+ if (gen & 16)
+ export_free(desc, xname);
}
}