]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
zlibWrapper: added support for custom memory allocation functions
authorinikep <inikep@gmail.com>
Thu, 2 Jun 2016 14:52:36 +0000 (16:52 +0200)
committerinikep <inikep@gmail.com>
Thu, 2 Jun 2016 14:52:36 +0000 (16:52 +0200)
lib/common/zstd_common.c
lib/common/zstd_internal.h
programs/zbufftest.c
zlibWrapper/examples/example.c
zlibWrapper/zstd_zlibwrapper.c

index 262eeb232bcebbc96626ef21a426767a415c4bbe..5d3db0ea023333ee60eb9957324b50f8100acc24 100644 (file)
@@ -78,13 +78,13 @@ void* ZSTD_defaultAllocFunction(void* opaque, size_t size)
 {
     void* address = malloc(size);
     (void)opaque;
-    /* DISPLAYLEVEL(4, "alloc %p, %d opaque=%d \n", address, (int)size, (int)opaque); */
+    /* printf("alloc %p, %d opaque=%p \n", address, (int)size, opaque); */
     return address;
 }
 
 void ZSTD_defaultFreeFunction(void* opaque, void* address)
 {
     (void)opaque;
-    /* if (address) DISPLAYLEVEL(4, "free %p opaque=%d \n", address, (int)opaque); */
+    /* if (address) printf("free %p opaque=%p \n", address, opaque); */
     free(address);
 }
index 8c647b511497b2e47fb6a1a48444eb5f2fbdaa80..230ce79fcd7e3f3cd5405efe5de7519743b0e055 100644 (file)
@@ -258,6 +258,6 @@ int ZSTD_isSkipFrame(ZSTD_DCtx* dctx);
 /* custom memory allocation functions */
 void* ZSTD_defaultAllocFunction(void* opaque, size_t size);
 void ZSTD_defaultFreeFunction(void* opaque, void* address);
-MEM_STATIC ZSTD_customMem const defaultCustomMem = { ZSTD_defaultAllocFunction, ZSTD_defaultFreeFunction, NULL };
+static ZSTD_customMem const defaultCustomMem = { ZSTD_defaultAllocFunction, ZSTD_defaultFreeFunction, NULL };
 
 #endif   /* ZSTD_CCOMMON_H_MODULE */
index f57c4cdf9a4c1892d0daf65e2cbd747273f18e70..18382abf9313dbb001dd801538ce990b56299174 100644 (file)
@@ -133,14 +133,14 @@ void* ZBUFF_allocFunction(void* opaque, size_t size)
 {
     (void)opaque;
     void* address = malloc(size);
-    /* DISPLAYLEVEL(4, "alloc %p, %d opaque=%d \n", address, (int)size, (int)opaque); */
+    /* DISPLAYLEVEL(4, "alloc %p, %d opaque=%p \n", address, (int)size, opaque); */
     return address;
 }
 
 void ZBUFF_freeFunction(void* opaque, void* address)
 {
     (void)opaque;
-    /* if (address) DISPLAYLEVEL(4, "free %p opaque=%d \n", address, (int)opaque); */
+    /* if (address) DISPLAYLEVEL(4, "free %p opaque=%p \n", address, opaque); */
     free(address);
 }
 
index fe6c80ac19162bc56e2d19ff0dab4bc68c0520d7..3fea9d2ad72ccfe85d315b691b371f7a9c2b45c8 100644 (file)
@@ -62,11 +62,14 @@ void *myalloc(q, n, m)
     unsigned n, m;
 {
     q = Z_NULL;
-    return calloc(n, m);
+    void *buf = calloc(n, m);
+  /*  printf("myalloc %p n=%d m=%d\n", buf, n, m); */
+    return buf;
 }
 
 void myfree(void *q, void *p)
 {
+  /*  printf("myfree %p\n", p); */
     q = Z_NULL;
     free(p);
 }
index ff85a8613697c3a701e73bc5f3a5df0b8a83de06..26f173b131b10a3ffb96cec41ad39adcade7588a 100644 (file)
@@ -37,6 +37,7 @@
 #include "zstd.h"
 #include "zstd_static.h"      /* ZSTD_MAGICNUMBER */
 #include "zbuff.h"
+#include "zbuff_static.h"      /* ZBUFF_createCCtx_advanced */
 
 
 #define Z_INFLATE_SYNC              8
@@ -75,6 +76,22 @@ const char * zstdVersion() { return ZSTD_VERSION_STRING; }
 ZEXTERN const char * ZEXPORT z_zlibVersion OF((void)) { return zlibVersion();  }
 
 
+void* ZWRAP_allocFunction(void* opaque, size_t size)
+{
+    z_streamp strm = (z_streamp) opaque;
+    void* address = strm->zalloc(strm->opaque, 1, size);
+  /*  printf("ZWRAP alloc %p, %d \n", address, (int)size); */
+    return address;
+}
+
+void ZWRAP_freeFunction(void* opaque, void* address)
+{
+    z_streamp strm = (z_streamp) opaque;
+    strm->zfree(strm->opaque, address);
+   /* if (address) printf("ZWRAP free %p \n", address); */
+}
+
+
 
 /* *** Compression *** */
 
@@ -82,15 +99,22 @@ typedef struct {
     ZBUFF_CCtx* zbc;
     size_t bytesLeft;
     int compressionLevel;
+    z_stream allocFunc; /* copy of zalloc, zfree, opaque */
 } ZWRAP_CCtx;
 
 
-ZWRAP_CCtx* ZWRAP_createCCtx()
+ZWRAP_CCtx* ZWRAP_createCCtx(z_streamp strm)
 {
     ZWRAP_CCtx* zwc = (ZWRAP_CCtx*)malloc(sizeof(ZWRAP_CCtx));
     if (zwc==NULL) return NULL;
     memset(zwc, 0, sizeof(*zwc));
-    zwc->zbc = ZBUFF_createCCtx();
+    if (strm->zalloc && strm->zfree) {
+       ZSTD_customMem ZWRAP_customMem = { ZWRAP_allocFunction, ZWRAP_freeFunction, &zwc->allocFunc };
+       memcpy(&zwc->allocFunc, strm, sizeof(z_stream));
+       zwc->zbc = ZBUFF_createCCtx_advanced(ZWRAP_customMem);
+    }
+    else
+       zwc->zbc = ZBUFF_createCCtx();
     return zwc;
 }
 
@@ -115,7 +139,7 @@ ZEXTERN int ZEXPORT z_deflateInit_ OF((z_streamp strm, int level,
     }
 
     LOG_WRAPPER("- deflateInit level=%d\n", level);
-    zwc = ZWRAP_createCCtx();
+    zwc = ZWRAP_createCCtx(strm);
     if (zwc == NULL) return Z_MEM_ERROR;
 
     if (level == Z_DEFAULT_COMPRESSION)
@@ -265,14 +289,16 @@ typedef struct {
     int stream_size;
     char *version;
     int windowBits;
+    z_stream allocFunc; /* copy of zalloc, zfree, opaque */
 } ZWRAP_DCtx;
 
 
-ZWRAP_DCtx* ZWRAP_createDCtx(void)
+ZWRAP_DCtx* ZWRAP_createDCtx(z_streamp strm)
 {
     ZWRAP_DCtx* zwd = (ZWRAP_DCtx*)malloc(sizeof(ZWRAP_DCtx));
     if (zwd==NULL) return NULL;
     memset(zwd, 0, sizeof(*zwd));
+    memcpy(&zwd->allocFunc, strm, sizeof(z_stream));
     return zwd;
 }
 
@@ -290,7 +316,7 @@ size_t ZWRAP_freeDCtx(ZWRAP_DCtx* zwd)
 ZEXTERN int ZEXPORT z_inflateInit_ OF((z_streamp strm,
                                      const char *version, int stream_size))
 {
-    ZWRAP_DCtx* zwd = ZWRAP_createDCtx();
+    ZWRAP_DCtx* zwd = ZWRAP_createDCtx(strm);
     LOG_WRAPPER("- inflateInit\n");
     if (zwd == NULL) return Z_MEM_ERROR;
 
@@ -402,7 +428,12 @@ ZEXTERN int ZEXPORT z_inflate OF((z_streamp strm, int flush))
                 return inflate(strm, flush);
             }
 
-            zwd->zbd = ZBUFF_createDCtx();
+            if (zwd->allocFunc.zalloc && zwd->allocFunc.zfree) {
+               ZSTD_customMem ZWRAP_customMem = { ZWRAP_allocFunction, ZWRAP_freeFunction, &zwd->allocFunc };
+               zwd->zbd = ZBUFF_createDCtx_advanced(ZWRAP_customMem);
+            } else
+               zwd->zbd = ZBUFF_createDCtx();
+
             { size_t const errorCode = ZBUFF_decompressInit(zwd->zbd);
               if (ZSTD_isError(errorCode)) return Z_MEM_ERROR; }