Mercurial > hg > dmlib
diff src/dmgrowbuf.c @ 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 | e40227e994e2 |
children | 22b6fa1a2ee4 |
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"