# HG changeset patch # User Matti Hamalainen # Date 1528200255 -10800 # Node ID a2e65aa475546c38bde138f098e58513de6dd565 # Parent f71cd6691e05e6469298ea193e49b1408bb5c084 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also DMGrowBuf::is_const flag, that marks a buffer "unmodifiable", though obviously that is not strictly enforced. diff -r f71cd6691e05 -r a2e65aa47554 src/dmgrowbuf.c --- 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)) diff -r f71cd6691e05 -r a2e65aa47554 src/dmgrowbuf.h --- 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);