From ff9114aee3123a4e80effe848776b746c3e4cc91 Mon Sep 17 00:00:00 2001 From: inikep Date: Thu, 2 Jun 2016 16:52:36 +0200 Subject: [PATCH] zlibWrapper: added support for custom memory allocation functions --- lib/common/zstd_common.c | 4 ++-- lib/common/zstd_internal.h | 2 +- programs/zbufftest.c | 4 ++-- zlibWrapper/examples/example.c | 5 +++- zlibWrapper/zstd_zlibwrapper.c | 43 +++++++++++++++++++++++++++++----- 5 files changed, 46 insertions(+), 12 deletions(-) diff --git a/lib/common/zstd_common.c b/lib/common/zstd_common.c index 262eeb232..5d3db0ea0 100644 --- a/lib/common/zstd_common.c +++ b/lib/common/zstd_common.c @@ -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); } diff --git a/lib/common/zstd_internal.h b/lib/common/zstd_internal.h index 8c647b511..230ce79fc 100644 --- a/lib/common/zstd_internal.h +++ b/lib/common/zstd_internal.h @@ -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 */ diff --git a/programs/zbufftest.c b/programs/zbufftest.c index f57c4cdf9..18382abf9 100644 --- a/programs/zbufftest.c +++ b/programs/zbufftest.c @@ -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); } diff --git a/zlibWrapper/examples/example.c b/zlibWrapper/examples/example.c index fe6c80ac1..3fea9d2ad 100644 --- a/zlibWrapper/examples/example.c +++ b/zlibWrapper/examples/example.c @@ -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); } diff --git a/zlibWrapper/zstd_zlibwrapper.c b/zlibWrapper/zstd_zlibwrapper.c index ff85a8613..26f173b13 100644 --- a/zlibWrapper/zstd_zlibwrapper.c +++ b/zlibWrapper/zstd_zlibwrapper.c @@ -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; }