]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
added ZBUFF_createCCtx_advanced and ZBUFF_createDCtx_advanced
authorinikep <inikep@gmail.com>
Mon, 23 May 2016 15:04:23 +0000 (17:04 +0200)
committerinikep <inikep@gmail.com>
Mon, 23 May 2016 15:04:23 +0000 (17:04 +0200)
lib/common/zbuff.h
lib/compress/zbuff_compress.c
lib/compress/zstd_compress.c
lib/decompress/zbuff_decompress.c
lib/decompress/zstd_decompress.c

index 3caa39caf27291953612aa3b153676faa71d9cea..4d7f12a1fd13769fa321d7c5ddb26b40883932c1 100644 (file)
@@ -38,7 +38,8 @@ extern "C" {
 /* *************************************
 *  Dependencies
 ***************************************/
-#include <stddef.h>   /* size_t */
+#include <stddef.h>      /* size_t */
+#include "zstd_static.h" /* ZSTD_customMem */
 
 
 /* ***************************************************************
@@ -60,6 +61,7 @@ extern "C" {
 ***************************************/
 typedef struct ZBUFF_CCtx_s ZBUFF_CCtx;
 ZSTDLIB_API ZBUFF_CCtx* ZBUFF_createCCtx(void);
+ZSTDLIB_API ZBUFF_CCtx* ZBUFF_createCCtx_advanced(ZSTD_customMem customMem);
 ZSTDLIB_API size_t      ZBUFF_freeCCtx(ZBUFF_CCtx* cctx);
 
 ZSTDLIB_API size_t ZBUFF_compressInit(ZBUFF_CCtx* cctx, int compressionLevel);
@@ -112,6 +114,7 @@ ZSTDLIB_API size_t ZBUFF_compressEnd(ZBUFF_CCtx* cctx, void* dst, size_t* dstCap
 
 typedef struct ZBUFF_DCtx_s ZBUFF_DCtx;
 ZSTDLIB_API ZBUFF_DCtx* ZBUFF_createDCtx(void);
+ZSTDLIB_API ZBUFF_DCtx* ZBUFF_createDCtx_advanced(ZSTD_customMem customMem);
 ZSTDLIB_API size_t      ZBUFF_freeDCtx(ZBUFF_DCtx* dctx);
 
 ZSTDLIB_API size_t ZBUFF_decompressInit(ZBUFF_DCtx* dctx);
index 260aca0870ef9ce77ab066002f6ff9f668d7f157..cabf447a475617301c3a64e211ec52cc05979d36 100644 (file)
@@ -95,14 +95,39 @@ struct ZBUFF_CCtx_s {
     size_t outBuffContentSize;
     size_t outBuffFlushedSize;
     ZBUFF_cStage stage;
+    ZSTD_allocFunction customAlloc;
+    ZSTD_freeFunction customFree;
 };   /* typedef'd tp ZBUFF_CCtx within "zstd_buffered.h" */
 
 ZBUFF_CCtx* ZBUFF_createCCtx(void)
 {
-    ZBUFF_CCtx* zbc = (ZBUFF_CCtx*)malloc(sizeof(ZBUFF_CCtx));
+    ZSTD_customMem customMem = { NULL, NULL };
+    return ZBUFF_createCCtx_advanced(customMem);
+}
+
+ZBUFF_CCtx* ZBUFF_createCCtx_advanced(ZSTD_customMem customMem)
+{
+    ZBUFF_CCtx* zbc;
+
+    if (!customMem.customAlloc && !customMem.customFree)
+    {
+        zbc = (ZBUFF_CCtx*)calloc(1, sizeof(ZBUFF_CCtx));
+        if (zbc==NULL) return NULL;
+        zbc->customAlloc = malloc;
+        zbc->customFree = free;
+        zbc->zc = ZSTD_createCCtx();
+        return zbc;
+    }
+
+    if (!customMem.customAlloc || !customMem.customFree)
+        return NULL;
+
+    zbc = (ZBUFF_CCtx*)customMem.customAlloc(sizeof(ZBUFF_CCtx));
     if (zbc==NULL) return NULL;
-    memset(zbc, 0, sizeof(*zbc));
-    zbc->zc = ZSTD_createCCtx();
+    memset(zbc, 0, sizeof(ZBUFF_CCtx));
+    zbc->customAlloc = customMem.customAlloc; 
+    zbc->customFree = customMem.customFree;
+    zbc->zc = ZSTD_createCCtx_advanced(customMem);
     return zbc;
 }
 
@@ -110,9 +135,9 @@ size_t ZBUFF_freeCCtx(ZBUFF_CCtx* zbc)
 {
     if (zbc==NULL) return 0;   /* support free on NULL */
     ZSTD_freeCCtx(zbc->zc);
-    free(zbc->inBuff);
-    free(zbc->outBuff);
-    free(zbc);
+    zbc->customFree(zbc->inBuff);
+    zbc->customFree(zbc->outBuff);
+    zbc->customFree(zbc);
     return 0;
 }
 
@@ -127,16 +152,16 @@ size_t ZBUFF_compressInit_advanced(ZBUFF_CCtx* zbc,
     {   size_t const neededInBuffSize = (size_t)1 << params.cParams.windowLog;
         if (zbc->inBuffSize < neededInBuffSize) {
             zbc->inBuffSize = neededInBuffSize;
-            free(zbc->inBuff);   /* should not be necessary */
-            zbc->inBuff = (char*)malloc(neededInBuffSize);
+            zbc->customFree(zbc->inBuff);   /* should not be necessary */
+            zbc->inBuff = (char*)zbc->customAlloc(neededInBuffSize);
             if (zbc->inBuff == NULL) return ERROR(memory_allocation);
         }
         zbc->blockSize = MIN(ZSTD_BLOCKSIZE_MAX, neededInBuffSize/2);
     }
     if (zbc->outBuffSize < ZSTD_compressBound(zbc->blockSize)+1) {
         zbc->outBuffSize = ZSTD_compressBound(zbc->blockSize)+1;
-        free(zbc->outBuff);   /* should not be necessary */
-        zbc->outBuff = (char*)malloc(zbc->outBuffSize);
+        zbc->customFree(zbc->outBuff);   /* should not be necessary */
+        zbc->outBuff = (char*)zbc->customAlloc(zbc->outBuffSize);
         if (zbc->outBuff == NULL) return ERROR(memory_allocation);
     }
 
index 966d9acbdf2caa35bb6fad030ea46fe1d2a2393c..bca725f545ea081ec9ff565794a4eb813aa650e1 100644 (file)
@@ -128,7 +128,7 @@ ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem)
 {
     ZSTD_CCtx* ctx;
 
-    if (!customMem.customAlloc || !customMem.customFree)
+    if (!customMem.customAlloc && !customMem.customFree)
     {
         ctx = (ZSTD_CCtx*) calloc(1, sizeof(ZSTD_CCtx));
         if (!ctx) return NULL;
@@ -138,6 +138,9 @@ ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem)
         return ctx;
     }
 
+    if (!customMem.customAlloc || !customMem.customFree)
+        return NULL;
+
     ctx = (ZSTD_CCtx*) customMem.customAlloc(sizeof(ZSTD_CCtx));
     if (!ctx) return NULL;
 
index 858b442f72edebb1b368164740fc74f47b88868d..d4ca550a7bd2d9efd8743cd58ef26c77cf99609f 100644 (file)
@@ -82,15 +82,41 @@ struct ZBUFF_DCtx_s {
     size_t blockSize;
     BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX];
     size_t lhSize;
+    ZSTD_allocFunction customAlloc;
+    ZSTD_freeFunction customFree;
 };   /* typedef'd to ZBUFF_DCtx within "zstd_buffered.h" */
 
 
 ZBUFF_DCtx* ZBUFF_createDCtx(void)
 {
-    ZBUFF_DCtx* zbd = (ZBUFF_DCtx*)malloc(sizeof(ZBUFF_DCtx));
+    ZSTD_customMem customMem = { NULL, NULL };
+    return ZBUFF_createDCtx_advanced(customMem);
+}
+
+ZBUFF_DCtx* ZBUFF_createDCtx_advanced(ZSTD_customMem customMem)
+{
+    ZBUFF_DCtx* zbd;
+
+    if (!customMem.customAlloc && !customMem.customFree)
+    {
+        zbd = (ZBUFF_DCtx*)calloc(1, sizeof(ZBUFF_DCtx));
+        if (zbd==NULL) return NULL;
+        zbd->customAlloc = malloc;
+        zbd->customFree = free;
+        zbd->zd = ZSTD_createDCtx();
+        zbd->stage = ZBUFFds_init;
+        return zbd;
+    }
+
+    if (!customMem.customAlloc || !customMem.customFree)
+        return NULL;
+
+    zbd = (ZBUFF_DCtx*)customMem.customAlloc(sizeof(ZBUFF_DCtx));
     if (zbd==NULL) return NULL;
-    memset(zbd, 0, sizeof(*zbd));
-    zbd->zd = ZSTD_createDCtx();
+    memset(zbd, 0, sizeof(ZBUFF_DCtx));
+    zbd->customAlloc = customMem.customAlloc; 
+    zbd->customFree = customMem.customFree;
+    zbd->zd = ZSTD_createDCtx_advanced(customMem);
     zbd->stage = ZBUFFds_init;
     return zbd;
 }
@@ -99,9 +125,9 @@ size_t ZBUFF_freeDCtx(ZBUFF_DCtx* zbd)
 {
     if (zbd==NULL) return 0;   /* support free on null */
     ZSTD_freeDCtx(zbd->zd);
-    free(zbd->inBuff);
-    free(zbd->outBuff);
-    free(zbd);
+    zbd->customFree(zbd->inBuff);
+    zbd->customFree(zbd->outBuff);
+    zbd->customFree(zbd);
     return 0;
 }
 
@@ -170,16 +196,16 @@ size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbd,
             {   size_t const blockSize = MIN(1 << zbd->fParams.windowLog, ZSTD_BLOCKSIZE_MAX);
                 zbd->blockSize = blockSize;
                 if (zbd->inBuffSize < blockSize) {
-                    free(zbd->inBuff);
+                    zbd->customFree(zbd->inBuff);
                     zbd->inBuffSize = blockSize;
-                    zbd->inBuff = (char*)malloc(blockSize);
+                    zbd->inBuff = (char*)zbd->customAlloc(blockSize);
                     if (zbd->inBuff == NULL) return ERROR(memory_allocation);
                 }
                 {   size_t const neededOutSize = ((size_t)1 << zbd->fParams.windowLog) + blockSize;
                     if (zbd->outBuffSize < neededOutSize) {
-                        free(zbd->outBuff);
+                        zbd->customFree(zbd->outBuff);
                         zbd->outBuffSize = neededOutSize;
-                        zbd->outBuff = (char*)malloc(neededOutSize);
+                        zbd->outBuff = (char*)zbd->customAlloc(neededOutSize);
                         if (zbd->outBuff == NULL) return ERROR(memory_allocation);
             }   }   }
             zbd->stage = ZBUFFds_read;
index a3eee709ecb0284ab162914b1a1bdfa5dfe72e74..b670c548cccbea7f3199f8b3b9235121ce00daaf 100644 (file)
@@ -153,7 +153,7 @@ ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem)
 {
     ZSTD_DCtx* dctx;
 
-    if (!customMem.customAlloc || !customMem.customFree)
+    if (!customMem.customAlloc && !customMem.customFree)
     {
         dctx = (ZSTD_DCtx*) malloc(sizeof(ZSTD_DCtx));
         if (!dctx) return NULL;
@@ -164,6 +164,9 @@ ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem)
         return dctx;
     }
 
+    if (!customMem.customAlloc || !customMem.customFree)
+        return NULL;
+
     dctx = (ZSTD_DCtx*) customMem.customAlloc(sizeof(ZSTD_DCtx));
     if (!dctx) return NULL;
     dctx->customAlloc = customMem.customAlloc;