From 46364ca2b0c15413e83a0bf20568675d4fe6095b Mon Sep 17 00:00:00 2001 From: Not Zed Date: Wed, 9 Jun 2021 09:26:53 +0930 Subject: [PATCH] Add TRANSIENTP element for automatic free of temporary storage. Clear blob contents on free. --- ez-blob-compiler.c | 7 +++++++ ez-blob.c | 4 +++- ez-blob.h | 3 +++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/ez-blob-compiler.c b/ez-blob-compiler.c index cb9a41f..bf9b1f9 100644 --- a/ez-blob-compiler.c +++ b/ez-blob-compiler.c @@ -75,6 +75,8 @@ void export_size(const ez_blob_desc *desc, const char *name) { case EZ_BLOB_INT8 | EZ_BLOB_CSTRING: strings = 1; break; + case EZ_BLOB_TRANSIENTP: + break; default: fprintf(stderr, "Unsupported type: %d\n", d->bd_type); abort(); @@ -93,6 +95,7 @@ void export_size(const ez_blob_desc *desc, const char *name) { case EZ_BLOB_INT64: case EZ_BLOB_FLOAT32: case EZ_BLOB_FLOAT64: + case EZ_BLOB_TRANSIENTP: break; case EZ_BLOB_INT8 | EZ_BLOB_CSTRING: if (doinline) { @@ -156,6 +159,8 @@ void export_encode(const ez_blob_desc *desc, const char *name) { printf("\tb = string_encode(b, *(const char **)(p + %d));\n", d->bd_offset); } break; + case EZ_BLOB_TRANSIENTP: + break; default: fprintf(stderr, "Unsupported type: %d\n", d->bd_type); abort(); @@ -219,6 +224,8 @@ void export_decode(const ez_blob_desc *desc, const char *name, int doos) { printf("\tb = string_decode(b, (char **)(p + %d));\n", d->bd_offset); } break; + case EZ_BLOB_TRANSIENTP: + break; default: fprintf(stderr, "Unsupported type: %d\n", d->bd_type); abort(); diff --git a/ez-blob.c b/ez-blob.c index 683ac4e..4a563b6 100644 --- a/ez-blob.c +++ b/ez-blob.c @@ -32,7 +32,7 @@ void ez_blob_free_raw(const ez_blob_desc *desc, void *p) { void *v = p + d->bd_offset; int st = d->bd_type & EZ_BLOB_STORAGE; int dt = d->bd_type & EZ_BLOB_TYPE; - + switch (st) { case EZ_BLOB_SINGLE: if (dt == EZ_BLOB_STRUCT) @@ -47,6 +47,7 @@ void ez_blob_free_raw(const ez_blob_desc *desc, void *p) { } break; case EZ_BLOB_CSTRING: + case EZ_BLOB_TRANSIENTP: free(((char **)v)[0]); break; case EZ_BLOB_VECTOR: @@ -80,6 +81,7 @@ void ez_blob_free_raw(const ez_blob_desc *desc, void *p) { break; } } + memset(p, 0, desc->bd_offset); } void ez_blob_free(const ez_blob_desc *d, void *p) { diff --git a/ez-blob.h b/ez-blob.h index dcb4eeb..ce17272 100644 --- a/ez-blob.h +++ b/ez-blob.h @@ -81,6 +81,7 @@ typedef enum ez_blob_desc_type { EZ_BLOB_SINGLE = 0x00, // the value at bd_offset EZ_BLOB_SINGLEP = 0x10, // pointer to value at bd_offset EZ_BLOB_CSTRING = 0x20, // 0-terminated string pointer + EZ_BLOB_TRANSIENTP = 0x30, // transient pointer which needs to be free()'d on free EZ_BLOB_VECTOR = 0x40, // ez_blob at bd_offset EZ_BLOB_VECTORP = 0x50, // pointer to ez_blob at bd_offset @@ -134,6 +135,8 @@ typedef struct ez_blob { #define EZ_BLOB_STRING(s, id, f) { EZ_BLOB_INT8 | EZ_BLOB_CSTRING, id, offsetof(s, f), #f } #define EZ_BLOB_STRINGN(s, id, f) { EZ_BLOB_INT8 | EZ_BLOB_CSTRING | EZ_BLOB_ISNULLABLE, id, offsetof(s, f), #f } +#define EZ_BLOB_TRANSIENTP(s, id, f) { EZ_BLOB_TRANSIENTP, id, offsetof(s, f), #f } + #define EZ_BLOB_STRUCT(s, id, f, other) { EZ_BLOB_STRUCT | EZ_BLOB_SINGLE, id, offsetof(s, f), #f, .bd_table = other } #define EZ_BLOB_STRUCTP(s, id, f, other) { EZ_BLOB_STRUCT | EZ_BLOB_SINGLEP, id, offsetof(s, f), #f, .bd_table = other } #define EZ_BLOB_STRUCTPN(s, id, f, other) { EZ_BLOB_STRUCT | EZ_BLOB_SINGLEP | EZ_BLOB_ISNULLABLE, id, offsetof(s, f), #f, .bd_table = other } -- 2.39.2