changeset 694:0fc5ddaccc57

Implement fclose() in mem ioctx, with an context flag for freeing the memory on fclose().
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 09 Mar 2020 18:55:54 +0200
parents a622d21833e1
children a04b8fe158b9
files th_ioctx.h th_ioctx_mem.c
diffstat 2 files changed, 9 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/th_ioctx.h	Mon Mar 09 18:54:37 2020 +0200
+++ b/th_ioctx.h	Mon Mar 09 18:55:54 2020 +0200
@@ -49,6 +49,7 @@
     size_t minAlloc;    ///< Minimum allocation increase (0 = default)
     off_t  memOffset;   ///< Current offset in data
     uint8_t *memData;   ///< Pointer to data
+    BOOL   memFree;     ///< TRUE = free the allocated memory on thfclose(), FALSE = no
     BOOL   memWrite;    ///< TRUE = memory can be written to / expanded etc. FALSE = no
 
     // Message functions
--- a/th_ioctx_mem.c	Mon Mar 09 18:54:37 2020 +0200
+++ b/th_ioctx_mem.c	Mon Mar 09 18:55:54 2020 +0200
@@ -43,6 +43,13 @@
 }
 
 
+static void th_mem_fclose(th_ioctx *ctx)
+{
+    if (ctx->memFree)
+        th_free_r(&ctx->memData);
+}
+
+
 static BOOL th_mem_realloc(th_ioctx *ctx, const size_t newSize)
 {
     size_t grow;
@@ -222,7 +229,7 @@
     "MemIO",
 
     th_mem_fopen,
-    NULL,
+    th_mem_fclose,
 
     th_mem_freset,
     th_mem_ferror,