]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
added ZSTD_createDCtx_advanced
authorinikep <inikep@gmail.com>
Mon, 23 May 2016 14:24:52 +0000 (16:24 +0200)
committerinikep <inikep@gmail.com>
Mon, 23 May 2016 14:24:52 +0000 (16:24 +0200)
lib/common/zstd_static.h
lib/compress/zstd_compress.c
lib/decompress/zstd_decompress.c

index 912eb13351c93c968d829214df30c07628ddae05..e0f50cf406300ee771a1f772298ff73713b573ae 100644 (file)
@@ -103,9 +103,12 @@ typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; }
 *  Advanced functions
 ***************************************/
 /*! ZSTD_createCCtx_advanced() :
- *  Create ZSTD context using external alloc and free functions */ 
+ *  Create a ZSTD compression context using external alloc and free functions */ 
 ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem);
 
+/*! ZSTD_createDCtx_advanced() :
+ *  Create a ZSTD decompression context using external alloc and free functions */ 
+ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem);
 
 ZSTDLIB_API unsigned ZSTD_maxCLevel (void);
 
index 00106f5fe687fb6ed309112893865b81b037c0df..966d9acbdf2caa35bb6fad030ea46fe1d2a2393c 100644 (file)
@@ -120,20 +120,25 @@ struct ZSTD_CCtx_s
 
 ZSTD_CCtx* ZSTD_createCCtx(void)
 {
-    ZSTD_CCtx* ctx = (ZSTD_CCtx*) calloc(1, sizeof(ZSTD_CCtx));
-    if (!ctx) return NULL;
-
-    ctx->customAlloc = malloc;
-    ctx->customFree = free;
-    return ctx;
+    ZSTD_customMem customMem = { NULL, NULL };
+    return ZSTD_createCCtx_advanced(customMem);
 }
 
 ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem)
 {
+    ZSTD_CCtx* ctx;
+
     if (!customMem.customAlloc || !customMem.customFree)
-        return ZSTD_createCCtx();
+    {
+        ctx = (ZSTD_CCtx*) calloc(1, sizeof(ZSTD_CCtx));
+        if (!ctx) return NULL;
+
+        ctx->customAlloc = malloc;
+        ctx->customFree = free;
+        return ctx;
+    }
 
-    ZSTD_CCtx* ctx = (ZSTD_CCtx*) customMem.customAlloc(sizeof(ZSTD_CCtx));
+    ctx = (ZSTD_CCtx*) customMem.customAlloc(sizeof(ZSTD_CCtx));
     if (!ctx) return NULL;
 
     memset(ctx, 0, sizeof(ZSTD_CCtx));
@@ -307,7 +312,9 @@ size_t ZSTD_copyCCtx(ZSTD_CCtx* dstCCtx, const ZSTD_CCtx* srcCCtx)
 {
     if (srcCCtx->stage!=1) return ERROR(stage_wrong);
 
-    dstCCtx->hashLog3 = srcCCtx->hashLog3; /* must be before ZSTD_resetCCtx_advanced */
+    dstCCtx->hashLog3    = srcCCtx->hashLog3; /* must be before ZSTD_resetCCtx_advanced */
+    dstCCtx->customAlloc = srcCCtx->customAlloc;
+    dstCCtx->customFree  = srcCCtx->customFree;
     ZSTD_resetCCtx_advanced(dstCCtx, srcCCtx->params, 0);
     dstCCtx->params.fParams.contentSizeFlag = 0;   /* content size different from the one set during srcCCtx init */
 
index 177e9c8a210e60a4c67e91d9c02dbd4cd90163ec..a3eee709ecb0284ab162914b1a1bdfa5dfe72e74 100644 (file)
@@ -116,6 +116,8 @@ struct ZSTD_DCtx_s
     size_t expected;
     size_t headerSize;
     ZSTD_frameParams fParams;
+    ZSTD_allocFunction customAlloc;
+    ZSTD_freeFunction customFree;
     blockType_t bType;   /* used in ZSTD_decompressContinue(), to transfer blockType between header decoding and block decoding stages */
     ZSTD_dStage stage;
     U32 flagRepeatTable;
@@ -143,15 +145,38 @@ size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx)
 
 ZSTD_DCtx* ZSTD_createDCtx(void)
 {
-    ZSTD_DCtx* dctx = (ZSTD_DCtx*)malloc(sizeof(ZSTD_DCtx));
-    if (dctx==NULL) return NULL;
+    ZSTD_customMem customMem = { NULL, NULL };
+    return ZSTD_createDCtx_advanced(customMem);
+}
+
+ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem)
+{
+    ZSTD_DCtx* dctx;
+
+    if (!customMem.customAlloc || !customMem.customFree)
+    {
+        dctx = (ZSTD_DCtx*) malloc(sizeof(ZSTD_DCtx));
+        if (!dctx) return NULL;
+        dctx->customAlloc = malloc;
+        dctx->customFree = free;
+
+        ZSTD_decompressBegin(dctx);
+        return dctx;
+    }
+
+    dctx = (ZSTD_DCtx*) customMem.customAlloc(sizeof(ZSTD_DCtx));
+    if (!dctx) return NULL;
+    dctx->customAlloc = customMem.customAlloc; 
+    dctx->customFree = customMem.customFree;
+
     ZSTD_decompressBegin(dctx);
     return dctx;
 }
 
+
 size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx)
 {
-    free(dctx);
+    dctx->customFree(dctx);
     return 0;   /* reserved as a potential error code in the future */
 }