changeset 1700:a2e65aa47554

Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also DMGrowBuf::is_const flag, that marks a buffer "unmodifiable", though obviously that is not strictly enforced.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 05 Jun 2018 15:04:15 +0300
parents f71cd6691e05
children 31a1f710e342
files src/dmgrowbuf.c src/dmgrowbuf.h
diffstat 2 files changed, 41 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/dmgrowbuf.c	Tue Jun 05 15:02:56 2018 +0300
+++ b/src/dmgrowbuf.c	Tue Jun 05 15:04:15 2018 +0300
@@ -87,7 +87,13 @@
         DM_DBG(
             "      buf->data       = %p\n"
             "      buf->allocated  = %s\n",
-            buf->data, buf->allocated ? "YES" : "NO");
+            "      buf->is_const   = %s\n",
+            buf->data,
+            buf->allocated ? "YES" : "NO",
+            buf->is_const ? "YES" : "NO");
+
+        if (buf->is_const)
+            return;
 
         dmFreeR(&buf->data);
 
@@ -97,6 +103,26 @@
 }
 
 
+DMGrowBuf * dmGrowBufConstCopy(DMGrowBuf *dst, const DMGrowBuf *src)
+{
+    memcpy(dst, src, sizeof(DMGrowBuf));
+    dst->is_const = TRUE;
+    return dst;
+}
+
+
+DMGrowBuf * dmGrowBufCreateFrom(DMGrowBuf *dst, Uint8 *data, size_t len)
+{
+    dmGrowBufInit(dst);
+
+    dst->data = data;
+    dst->len = dst->size = len;
+    dst->is_const = TRUE;
+
+    return dst;
+}
+
+
 static BOOL dmGrowBufRealloc(DMGrowBuf *buf, const size_t nsize, const BOOL clear)
 {
     DM_DBG("dmGrowBufRealloc(%p):\n"
@@ -104,6 +130,9 @@
         ", nstack=%d, offs=%" DM_PRIu_SIZE_T "\n",
         buf, buf->size, nsize, buf->nstack, buf->offs);
 
+    if (buf->is_const)
+        return FALSE;
+
     // Can't be smaller than current size!
     if (nsize < buf->size)
         return FALSE;
@@ -144,6 +173,9 @@
 {
     size_t grow = (amount > buf->mingrow) ? amount : buf->mingrow;
 
+    if (buf->is_const)
+        return FALSE;
+
     if (buf->data == NULL ||
         (buf->backwards && amount >= buf->offs) ||
         (!buf->backwards && buf->offs + amount >= buf->size))
@@ -163,6 +195,9 @@
 //
 BOOL dmGrowBufCheckGrow(DMGrowBuf *buf, const size_t nsize)
 {
+    if (buf->is_const)
+        return FALSE;
+
     if (buf->data == NULL || nsize > buf->size)
     {
         if (!dmGrowBufRealloc(buf, nsize + buf->mingrow, TRUE))
--- a/src/dmgrowbuf.h	Tue Jun 05 15:02:56 2018 +0300
+++ b/src/dmgrowbuf.h	Tue Jun 05 15:04:15 2018 +0300
@@ -30,7 +30,8 @@
     BOOL
         allocated,  // TRUE if this structure itself has been allocated and can be freed, FALSE if static
         backwards,  // TRUE if the buffer grows backwards (e.g. "offs" moves backwards)
-        literal;    // TRUE if dmGrowBufPut*() functions stores data "literally" in backwards mode
+        literal,    // TRUE if dmGrowBufPut*() functions stores data "literally" in backwards mode
+        is_const;   // TRUE will cause any reallocs etc. modifications to fail
 } DMGrowBuf;
 
 
@@ -39,6 +40,9 @@
 int    dmGrowBufNew(DMGrowBuf **pbuf, const size_t initial, const size_t mingrow);
 void   dmGrowBufFree(DMGrowBuf *buf);
 
+DMGrowBuf * dmGrowBufConstCopy(DMGrowBuf *dst, const DMGrowBuf *src);
+DMGrowBuf * dmGrowBufCreateFrom(DMGrowBuf *buf, Uint8 *data, size_t len);
+
 BOOL   dmGrowBufGrow(DMGrowBuf *buf, const size_t amount);
 BOOL   dmGrowBufCheckGrow(DMGrowBuf *buf, const size_t nsize);