mirror of
https://github.com/facebook/zstd.git
synced 2025-08-01 09:47:01 +03:00
zlibWrapper: added support for custom memory allocation functions
This commit is contained in:
@ -78,13 +78,13 @@ void* ZSTD_defaultAllocFunction(void* opaque, size_t size)
|
|||||||
{
|
{
|
||||||
void* address = malloc(size);
|
void* address = malloc(size);
|
||||||
(void)opaque;
|
(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;
|
return address;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ZSTD_defaultFreeFunction(void* opaque, void* address)
|
void ZSTD_defaultFreeFunction(void* opaque, void* address)
|
||||||
{
|
{
|
||||||
(void)opaque;
|
(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);
|
free(address);
|
||||||
}
|
}
|
||||||
|
@ -258,6 +258,6 @@ int ZSTD_isSkipFrame(ZSTD_DCtx* dctx);
|
|||||||
/* custom memory allocation functions */
|
/* custom memory allocation functions */
|
||||||
void* ZSTD_defaultAllocFunction(void* opaque, size_t size);
|
void* ZSTD_defaultAllocFunction(void* opaque, size_t size);
|
||||||
void ZSTD_defaultFreeFunction(void* opaque, void* address);
|
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 */
|
#endif /* ZSTD_CCOMMON_H_MODULE */
|
||||||
|
@ -133,14 +133,14 @@ void* ZBUFF_allocFunction(void* opaque, size_t size)
|
|||||||
{
|
{
|
||||||
(void)opaque;
|
(void)opaque;
|
||||||
void* address = malloc(size);
|
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;
|
return address;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ZBUFF_freeFunction(void* opaque, void* address)
|
void ZBUFF_freeFunction(void* opaque, void* address)
|
||||||
{
|
{
|
||||||
(void)opaque;
|
(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);
|
free(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,11 +62,14 @@ void *myalloc(q, n, m)
|
|||||||
unsigned n, m;
|
unsigned n, m;
|
||||||
{
|
{
|
||||||
q = Z_NULL;
|
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)
|
void myfree(void *q, void *p)
|
||||||
{
|
{
|
||||||
|
/* printf("myfree %p\n", p); */
|
||||||
q = Z_NULL;
|
q = Z_NULL;
|
||||||
free(p);
|
free(p);
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,7 @@
|
|||||||
#include "zstd.h"
|
#include "zstd.h"
|
||||||
#include "zstd_static.h" /* ZSTD_MAGICNUMBER */
|
#include "zstd_static.h" /* ZSTD_MAGICNUMBER */
|
||||||
#include "zbuff.h"
|
#include "zbuff.h"
|
||||||
|
#include "zbuff_static.h" /* ZBUFF_createCCtx_advanced */
|
||||||
|
|
||||||
|
|
||||||
#define Z_INFLATE_SYNC 8
|
#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(); }
|
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 *** */
|
/* *** Compression *** */
|
||||||
|
|
||||||
@ -82,14 +99,21 @@ typedef struct {
|
|||||||
ZBUFF_CCtx* zbc;
|
ZBUFF_CCtx* zbc;
|
||||||
size_t bytesLeft;
|
size_t bytesLeft;
|
||||||
int compressionLevel;
|
int compressionLevel;
|
||||||
|
z_stream allocFunc; /* copy of zalloc, zfree, opaque */
|
||||||
} ZWRAP_CCtx;
|
} ZWRAP_CCtx;
|
||||||
|
|
||||||
|
|
||||||
ZWRAP_CCtx* ZWRAP_createCCtx()
|
ZWRAP_CCtx* ZWRAP_createCCtx(z_streamp strm)
|
||||||
{
|
{
|
||||||
ZWRAP_CCtx* zwc = (ZWRAP_CCtx*)malloc(sizeof(ZWRAP_CCtx));
|
ZWRAP_CCtx* zwc = (ZWRAP_CCtx*)malloc(sizeof(ZWRAP_CCtx));
|
||||||
if (zwc==NULL) return NULL;
|
if (zwc==NULL) return NULL;
|
||||||
memset(zwc, 0, sizeof(*zwc));
|
memset(zwc, 0, sizeof(*zwc));
|
||||||
|
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();
|
zwc->zbc = ZBUFF_createCCtx();
|
||||||
return zwc;
|
return zwc;
|
||||||
}
|
}
|
||||||
@ -115,7 +139,7 @@ ZEXTERN int ZEXPORT z_deflateInit_ OF((z_streamp strm, int level,
|
|||||||
}
|
}
|
||||||
|
|
||||||
LOG_WRAPPER("- deflateInit level=%d\n", level);
|
LOG_WRAPPER("- deflateInit level=%d\n", level);
|
||||||
zwc = ZWRAP_createCCtx();
|
zwc = ZWRAP_createCCtx(strm);
|
||||||
if (zwc == NULL) return Z_MEM_ERROR;
|
if (zwc == NULL) return Z_MEM_ERROR;
|
||||||
|
|
||||||
if (level == Z_DEFAULT_COMPRESSION)
|
if (level == Z_DEFAULT_COMPRESSION)
|
||||||
@ -265,14 +289,16 @@ typedef struct {
|
|||||||
int stream_size;
|
int stream_size;
|
||||||
char *version;
|
char *version;
|
||||||
int windowBits;
|
int windowBits;
|
||||||
|
z_stream allocFunc; /* copy of zalloc, zfree, opaque */
|
||||||
} ZWRAP_DCtx;
|
} ZWRAP_DCtx;
|
||||||
|
|
||||||
|
|
||||||
ZWRAP_DCtx* ZWRAP_createDCtx(void)
|
ZWRAP_DCtx* ZWRAP_createDCtx(z_streamp strm)
|
||||||
{
|
{
|
||||||
ZWRAP_DCtx* zwd = (ZWRAP_DCtx*)malloc(sizeof(ZWRAP_DCtx));
|
ZWRAP_DCtx* zwd = (ZWRAP_DCtx*)malloc(sizeof(ZWRAP_DCtx));
|
||||||
if (zwd==NULL) return NULL;
|
if (zwd==NULL) return NULL;
|
||||||
memset(zwd, 0, sizeof(*zwd));
|
memset(zwd, 0, sizeof(*zwd));
|
||||||
|
memcpy(&zwd->allocFunc, strm, sizeof(z_stream));
|
||||||
return zwd;
|
return zwd;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -290,7 +316,7 @@ size_t ZWRAP_freeDCtx(ZWRAP_DCtx* zwd)
|
|||||||
ZEXTERN int ZEXPORT z_inflateInit_ OF((z_streamp strm,
|
ZEXTERN int ZEXPORT z_inflateInit_ OF((z_streamp strm,
|
||||||
const char *version, int stream_size))
|
const char *version, int stream_size))
|
||||||
{
|
{
|
||||||
ZWRAP_DCtx* zwd = ZWRAP_createDCtx();
|
ZWRAP_DCtx* zwd = ZWRAP_createDCtx(strm);
|
||||||
LOG_WRAPPER("- inflateInit\n");
|
LOG_WRAPPER("- inflateInit\n");
|
||||||
if (zwd == NULL) return Z_MEM_ERROR;
|
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);
|
return inflate(strm, flush);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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();
|
zwd->zbd = ZBUFF_createDCtx();
|
||||||
|
|
||||||
{ size_t const errorCode = ZBUFF_decompressInit(zwd->zbd);
|
{ size_t const errorCode = ZBUFF_decompressInit(zwd->zbd);
|
||||||
if (ZSTD_isError(errorCode)) return Z_MEM_ERROR; }
|
if (ZSTD_isError(errorCode)) return Z_MEM_ERROR; }
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user