changeset 1747:5e928618fdc8

Change DMGrowBuf API somewhat and implement more copy operations.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 11 Jun 2018 13:57:07 +0300
parents dd57dd9430cb
children 9c4b3fecc510
files src/dmgrowbuf.c src/dmgrowbuf.h tools/64vw.c tools/gfxconv.c tools/lib64fmts.c tools/lib64gfx.c
diffstat 6 files changed, 63 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/src/dmgrowbuf.c	Sun Jun 10 20:01:03 2018 +0300
+++ b/src/dmgrowbuf.c	Mon Jun 11 13:57:07 2018 +0300
@@ -42,7 +42,6 @@
     buf->offs = 0;
     buf->size = initial;
     buf->mingrow = mingrow;
-    buf->allocated = FALSE;
     buf->backwards = FALSE;
     buf->is_const  = FALSE;
 
@@ -54,33 +53,6 @@
 }
 
 
-int dmGrowBufNew(DMGrowBuf **pbuf, const size_t initial, const size_t mingrow)
-{
-    int res;
-    if (pbuf == NULL)
-        return DMERR_NULLPTR;
-
-    DM_DBG("dmGrowBufNew(%p, %" DM_PRIu_SIZE_T ", %" DM_PRIu_SIZE_T ")\n",
-        pbuf, initial, mingrow);
-
-    if ((*pbuf = dmMalloc0(sizeof(DMGrowBuf))) == NULL)
-        return DMERR_MALLOC;
-
-    if ((res = dmGrowBufAlloc(*pbuf, initial, mingrow)) != DMERR_OK)
-    {
-        // The "allocated" flag has not yet been set
-        dmGrowBufFree(*pbuf);
-
-        // .. thus free the allocated struct here
-        dmFreeR(pbuf);
-        return res;
-    }
-
-    (*pbuf)->allocated = TRUE;
-    return res;
-}
-
-
 void dmGrowBufFree(DMGrowBuf *buf)
 {
     DM_DBG("dmGrowBufFree(%p)\n", buf);
@@ -88,20 +60,48 @@
     {
         DM_DBG(
             "      buf->data       = %p\n"
-            "      buf->allocated  = %s\n"
             "      buf->is_const   = %s\n",
             buf->data,
-            buf->allocated ? "YES" : "NO",
             buf->is_const ? "YES" : "NO");
 
         if (buf->is_const)
             return;
 
         dmFreeR(&buf->data);
+    }
+}
 
-        if (buf->allocated)
-            dmFree(buf);
+
+DMGrowBuf * dmGrowBufCopy(DMGrowBuf *dst, const DMGrowBuf *src, const size_t enlarge)
+{
+    if (dst == NULL)
+        return NULL;
+
+    DM_DBG("dmGrowBufCopy(dst=%p, src=%p, enlarge=" DM_PRIu_SIZE_T "):\n"
+        "    data=%p, size=%" DM_PRIu_SIZE_T ", len=%" DM_PRIu_SIZE_T ", offs=%" DM_PRIu_SIZE_T "\n",
+        dst, src, enlarge, src->data, src->size, src->len, src->offs);
+
+    // Copy the struct here
+    memcpy(dst, src, sizeof(DMGrowBuf));
+    dst->size += enlarge;
+
+    // Allocate new memory for the data
+    if ((dst->data = dmMalloc(dst->size)) == NULL)
+    {
+        // If that fails, clear the struct
+        memset(dst, 0, sizeof(DMGrowBuf));
+        return NULL;
     }
+
+    // Copy data
+    memcpy(dst->data, src->data, src->size);
+    if (enlarge > 0)
+        memset(dst->data + src->size, 0, enlarge);
+    
+    // And reset some struct information
+    dst->is_const = FALSE;
+
+    return dst;
 }
 
 
@@ -121,12 +121,12 @@
 }
 
 
-DMGrowBuf * dmGrowBufCreateFrom(DMGrowBuf *dst, Uint8 *data, size_t len)
+DMGrowBuf * dmGrowBufConstCreateFrom(DMGrowBuf *dst, Uint8 *data, size_t len)
 {
     if (dmGrowBufInit(dst) != DMERR_OK)
         return NULL;
 
-    DM_DBG("dmGrowBufCreateFrom(dst=%p, data=%p, len=%" DM_PRIu_SIZE_T ")\n",
+    DM_DBG("dmGrowBufConstCreateFrom(dst=%p, data=%p, len=%" DM_PRIu_SIZE_T ")\n",
         dst, data, len);
 
     dst->data = data;
@@ -137,6 +137,20 @@
 }
 
 
+DMGrowBuf * dmGrowBufConstCopyOffs(DMGrowBuf *dst, const DMGrowBuf *src, const size_t offs)
+{
+    return dmGrowBufConstCreateFrom(dst, src->data + offs, src->len - offs);
+}
+
+
+DMGrowBuf * dmGrowBufCopyOffs(DMGrowBuf *dst, const DMGrowBuf *src, const size_t offs, const size_t enlarge)
+{
+    DMGrowBuf tmp;
+    return dmGrowBufCopy(dst,
+        dmGrowBufConstCreateFrom(&tmp, src->data + offs, src->len - offs), enlarge);
+}
+
+
 static BOOL dmGrowBufRealloc(DMGrowBuf *buf, const size_t nsize, const BOOL clear)
 {
     DM_DBG("dmGrowBufRealloc(%p):\n"
--- a/src/dmgrowbuf.h	Sun Jun 10 20:01:03 2018 +0300
+++ b/src/dmgrowbuf.h	Mon Jun 11 13:57:07 2018 +0300
@@ -28,7 +28,6 @@
         mingrow;    // Minimum amount of bytes the allocation size grows by
 
     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
         is_const;   // TRUE will cause any reallocs etc. modifications to fail
@@ -37,15 +36,13 @@
 
 int    dmGrowBufInit(DMGrowBuf *buf);
 int    dmGrowBufAlloc(DMGrowBuf *buf, const size_t initial, const size_t mingrow);
-int    dmGrowBufNew(DMGrowBuf **pbuf, const size_t initial, const size_t mingrow);
 void   dmGrowBufFree(DMGrowBuf *buf);
 
+DMGrowBuf * dmGrowBufCopy(DMGrowBuf *dst, const DMGrowBuf *src, const size_t enlarge);
+DMGrowBuf * dmGrowBufCopyOffs(DMGrowBuf *dst, const DMGrowBuf *src, const size_t offs, const size_t enlarge);
 DMGrowBuf * dmGrowBufConstCopy(DMGrowBuf *dst, const DMGrowBuf *src);
-DMGrowBuf * dmGrowBufCreateFrom(DMGrowBuf *buf, Uint8 *data, size_t len);
-static inline DMGrowBuf * dmGrowBufCreateFromOffs(DMGrowBuf *dst, const DMGrowBuf *src, const size_t offs)
-{
-    return dmGrowBufCreateFrom(dst, src->data + offs, src->len - offs);
-}
+DMGrowBuf * dmGrowBufConstCopyOffs(DMGrowBuf *dst, const DMGrowBuf *src, const size_t offs);
+DMGrowBuf * dmGrowBufConstCreateFrom(DMGrowBuf *buf, Uint8 *data, size_t len);
 
 
 BOOL   dmGrowBufGrow(DMGrowBuf *buf, const size_t amount);
--- a/tools/64vw.c	Sun Jun 10 20:01:03 2018 +0300
+++ b/tools/64vw.c	Mon Jun 11 13:57:07 2018 +0300
@@ -197,7 +197,7 @@
     if (optProbeOnly)
         ret = dmC64ProbeBMP(dataBuf, dataSize, fmt) != DM_PROBE_SCORE_FALSE ? DMERR_OK : DMERR_NOT_SUPPORTED;
     else
-        ret = dmC64DecodeBMP(cimage, dmGrowBufCreateFrom(&tmp, dataBuf, dataSize), 0, 2, fmt, forced);
+        ret = dmC64DecodeBMP(cimage, dmGrowBufConstCreateFrom(&tmp, dataBuf, dataSize), 0, 2, fmt, forced);
 
 exit:
     dmFree(dataBuf);
--- a/tools/gfxconv.c	Sun Jun 10 20:01:03 2018 +0300
+++ b/tools/gfxconv.c	Mon Jun 11 13:57:07 2018 +0300
@@ -1709,7 +1709,10 @@
                 forced->name, forced->type, forced->fext);
         }
 
-        res = dmC64DecodeBMP(&inC64Image, dmGrowBufCreateFrom(&tbuf, dataBuf, dataSize), 0, 2, &inC64Fmt, forced);
+        res = dmC64DecodeBMP(&inC64Image,
+            dmGrowBufConstCreateFrom(&tbuf, dataBuf, dataSize),
+            0, 2, &inC64Fmt, forced);
+
         if (forced == NULL && inC64Fmt != NULL && res == DMERR_OK)
         {
             dmMsg(1, "Probed '%s' format image, type %d, %s\n",
--- a/tools/lib64fmts.c	Sun Jun 10 20:01:03 2018 +0300
+++ b/tools/lib64fmts.c	Mon Jun 11 13:57:07 2018 +0300
@@ -98,7 +98,7 @@
     cfg.rleMarkerB   = buf->data[0x0d];
 
     if ((res = dmDecodeGenericRLEAlloc(&mem,
-        dmGrowBufCreateFromOffs(&tmp, buf, 0x0e), &cfg)) != DMERR_OK)
+        dmGrowBufConstCopyOffs(&tmp, buf, 0x0e), &cfg)) != DMERR_OK)
         goto out;
 
     res = dmC64DecodeGenericBMP(img, &mem, fmt);
@@ -201,7 +201,7 @@
     cfg.rleMarkerW   = buf->data[9];
 
     if ((res = dmDecodeGenericRLEAlloc(&mem,
-        dmGrowBufCreateFromOffs(&tmp, buf, 10), &cfg)) != DMERR_OK)
+        dmGrowBufConstCopyOffs(&tmp, buf, 10), &cfg)) != DMERR_OK)
         goto out;
 
     res = dmC64DecodeGenericBMP(img, &mem, fmt);
@@ -583,7 +583,7 @@
     }
     else
     {
-        res = dmC64DecodeGenericBMP(img, dmGrowBufCreateFromOffs(&tmp, buf, FUNPAINT2_HEADER_SIZE), fmt);
+        res = dmC64DecodeGenericBMP(img, dmGrowBufConstCopyOffs(&tmp, buf, FUNPAINT2_HEADER_SIZE), fmt);
     }
 
     return res;
@@ -806,7 +806,7 @@
     cfg.rleMarkerB   = buf->data[0];
 
     if ((res = dmDecodeGenericRLEAlloc(
-        &mem, dmGrowBufCreateFromOffs(&tmp, buf, 1), &cfg)) == DMERR_OK)
+        &mem, dmGrowBufConstCopyOffs(&tmp, buf, 1), &cfg)) == DMERR_OK)
         res = dmC64DecodeGenericBMP(img, &mem, fmt);
 
     dmGrowBufFree(&mem);
--- a/tools/lib64gfx.c	Sun Jun 10 20:01:03 2018 +0300
+++ b/tools/lib64gfx.c	Mon Jun 11 13:57:07 2018 +0300
@@ -1373,7 +1373,7 @@
         if (probeOffs >= buf->len)
             return DMERR_OUT_OF_DATA;
 
-        dmGrowBufCreateFromOffs(&tmp, buf, probeOffs);
+        dmGrowBufConstCopyOffs(&tmp, buf, probeOffs);
         if (dmC64ProbeBMP(tmp.data, tmp.len, fmt) == DM_PROBE_SCORE_FALSE)
             return DMERR_NOT_SUPPORTED;
     }
@@ -1392,7 +1392,7 @@
     if ((*img = dmC64ImageAlloc(*fmt)) == NULL)
         return DMERR_MALLOC;
 
-    dmGrowBufCreateFromOffs(&tmp, buf, loadOffs);
+    dmGrowBufConstCopyOffs(&tmp, buf, loadOffs);
 
     // Decode the bitmap to memory layout
     if ((*fmt)->decode != NULL)