mirror of
https://github.com/facebook/zstd.git
synced 2025-07-30 22:23:13 +03:00
zlibWrapper: ZWRAP_createCCtx and ZWRAP_freeCCtx use custom memory allocation functions
This commit is contained in:
@ -259,5 +259,5 @@ int ZSTD_isSkipFrame(ZSTD_DCtx* dctx);
|
|||||||
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);
|
||||||
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 */
|
||||||
|
@ -38,6 +38,7 @@
|
|||||||
#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 */
|
#include "zbuff_static.h" /* ZBUFF_createCCtx_advanced */
|
||||||
|
#include "zstd_internal.h" /* defaultCustomMem */
|
||||||
|
|
||||||
|
|
||||||
#define Z_INFLATE_SYNC 8
|
#define Z_INFLATE_SYNC 8
|
||||||
@ -47,8 +48,6 @@
|
|||||||
#define LOG_WRAPPER(...) // printf(__VA_ARGS__)
|
#define LOG_WRAPPER(...) // printf(__VA_ARGS__)
|
||||||
|
|
||||||
|
|
||||||
#define MIN(a,b) ((a)<(b)?(a):(b))
|
|
||||||
|
|
||||||
#define FINISH_WITH_ERR(msg) { \
|
#define FINISH_WITH_ERR(msg) { \
|
||||||
fprintf(stderr, "ERROR: %s\n", msg); \
|
fprintf(stderr, "ERROR: %s\n", msg); \
|
||||||
return Z_MEM_ERROR; \
|
return Z_MEM_ERROR; \
|
||||||
@ -99,22 +98,31 @@ typedef struct {
|
|||||||
ZBUFF_CCtx* zbc;
|
ZBUFF_CCtx* zbc;
|
||||||
size_t bytesLeft;
|
size_t bytesLeft;
|
||||||
int compressionLevel;
|
int compressionLevel;
|
||||||
|
ZSTD_customMem customMem;
|
||||||
z_stream allocFunc; /* copy of zalloc, zfree, opaque */
|
z_stream allocFunc; /* copy of zalloc, zfree, opaque */
|
||||||
} ZWRAP_CCtx;
|
} ZWRAP_CCtx;
|
||||||
|
|
||||||
|
|
||||||
ZWRAP_CCtx* ZWRAP_createCCtx(z_streamp strm)
|
ZWRAP_CCtx* ZWRAP_createCCtx(z_streamp strm)
|
||||||
{
|
{
|
||||||
ZWRAP_CCtx* zwc = (ZWRAP_CCtx*)malloc(sizeof(ZWRAP_CCtx));
|
ZWRAP_CCtx* zwc;
|
||||||
if (zwc==NULL) return NULL;
|
|
||||||
memset(zwc, 0, sizeof(*zwc));
|
|
||||||
if (strm->zalloc && strm->zfree) {
|
if (strm->zalloc && strm->zfree) {
|
||||||
ZSTD_customMem ZWRAP_customMem = { ZWRAP_allocFunction, ZWRAP_freeFunction, &zwc->allocFunc };
|
zwc = (ZWRAP_CCtx*)strm->zalloc(strm->opaque, 1, sizeof(ZWRAP_CCtx));
|
||||||
memcpy(&zwc->allocFunc, strm, sizeof(z_stream));
|
if (zwc==NULL) return NULL;
|
||||||
zwc->zbc = ZBUFF_createCCtx_advanced(ZWRAP_customMem);
|
memset(zwc, 0, sizeof(ZWRAP_CCtx));
|
||||||
|
memcpy(&zwc->allocFunc, strm, sizeof(z_stream));
|
||||||
|
{ ZSTD_customMem ZWRAP_customMem = { ZWRAP_allocFunction, ZWRAP_freeFunction, &zwc->allocFunc };
|
||||||
|
memcpy(&zwc->customMem, &ZWRAP_customMem, sizeof(ZSTD_customMem));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
zwc = (ZWRAP_CCtx*)defaultCustomMem.customAlloc(defaultCustomMem.opaque, sizeof(ZWRAP_CCtx));
|
||||||
|
if (zwc==NULL) return NULL;
|
||||||
|
memset(zwc, 0, sizeof(ZWRAP_CCtx));
|
||||||
|
memcpy(&zwc->customMem, &defaultCustomMem, sizeof(ZSTD_customMem));
|
||||||
}
|
}
|
||||||
else
|
|
||||||
zwc->zbc = ZBUFF_createCCtx();
|
zwc->zbc = ZBUFF_createCCtx_advanced(zwc->customMem);
|
||||||
return zwc;
|
return zwc;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,7 +131,7 @@ size_t ZWRAP_freeCCtx(ZWRAP_CCtx* zwc)
|
|||||||
{
|
{
|
||||||
if (zwc==NULL) return 0; /* support free on NULL */
|
if (zwc==NULL) return 0; /* support free on NULL */
|
||||||
ZBUFF_freeCCtx(zwc->zbc);
|
ZBUFF_freeCCtx(zwc->zbc);
|
||||||
free(zwc);
|
zwc->customMem.customFree(zwc->customMem.opaque, zwc);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user