/*-*************************************
* Dependencies
***************************************/
+#include <stdlib.h> /* malloc */
#include "error_private.h"
#include "zstd_static.h" /* declaration of ZSTD_isError, ZSTD_getErrorName, ZSTD_getErrorCode */
#include "zbuff.h" /* declaration of ZBUFF_isError, ZBUFF_getErrorName */
unsigned ZBUFF_isError(size_t errorCode) { return ERR_isError(errorCode); }
const char* ZBUFF_getErrorName(size_t errorCode) { return ERR_getErrorName(errorCode); }
+
+
+
+void* ZSTD_defaultAllocFunction(void* opaque, size_t size)
+{
+ (void)opaque;
+ void* address = malloc(size);
+ /* DISPLAYLEVEL(4, "alloc %p, %d opaque=%d \n", address, (int)size, (int)opaque); */
+ return address;
+}
+
+void ZSTD_defaultFreeFunction(void* opaque, void* address)
+{
+ (void)opaque;
+ /* if (address) DISPLAYLEVEL(4, "free %p opaque=%d \n", address, (int)opaque); */
+ free(address);
+}
ZSTD_frameParameters fParams;
} ZSTD_parameters;
-typedef void* (*ZSTD_allocFunction) (size_t size);
-typedef void (*ZSTD_freeFunction) (void* address);
-typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; } ZSTD_customMem;
+/* custom memory allocation functions */
+typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size);
+typedef void (*ZSTD_freeFunction) (void* opaque, void* address);
+typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem;
+
+void* ZSTD_defaultAllocFunction(void* opaque, size_t size);
+void ZSTD_defaultFreeFunction(void* opaque, void* address);
+static ZSTD_customMem const defaultCustomMem = { ZSTD_defaultAllocFunction, ZSTD_defaultFreeFunction, NULL };
/*-*************************************
size_t outBuffContentSize;
size_t outBuffFlushedSize;
ZBUFF_cStage stage;
- ZSTD_allocFunction customAlloc;
- ZSTD_freeFunction customFree;
+ ZSTD_customMem customMem;
}; /* typedef'd tp ZBUFF_CCtx within "zstd_buffered.h" */
ZBUFF_CCtx* ZBUFF_createCCtx(void)
{
- ZSTD_customMem customMem = { NULL, NULL };
+ ZSTD_customMem const customMem = { NULL, NULL, NULL };
return ZBUFF_createCCtx_advanced(customMem);
}
{
zbc = (ZBUFF_CCtx*)calloc(1, sizeof(ZBUFF_CCtx));
if (zbc==NULL) return NULL;
- zbc->customAlloc = malloc;
- zbc->customFree = free;
+ memcpy(&zbc->customMem, &defaultCustomMem, sizeof(ZSTD_customMem));
zbc->zc = ZSTD_createCCtx();
return zbc;
}
if (!customMem.customAlloc || !customMem.customFree)
return NULL;
- zbc = (ZBUFF_CCtx*)customMem.customAlloc(sizeof(ZBUFF_CCtx));
+ zbc = (ZBUFF_CCtx*)customMem.customAlloc(customMem.opaque, sizeof(ZBUFF_CCtx));
if (zbc==NULL) return NULL;
memset(zbc, 0, sizeof(ZBUFF_CCtx));
- zbc->customAlloc = customMem.customAlloc;
- zbc->customFree = customMem.customFree;
+ memcpy(&zbc->customMem, &customMem, sizeof(ZSTD_customMem));
zbc->zc = ZSTD_createCCtx_advanced(customMem);
return zbc;
}
{
if (zbc==NULL) return 0; /* support free on NULL */
ZSTD_freeCCtx(zbc->zc);
- zbc->customFree(zbc->inBuff);
- zbc->customFree(zbc->outBuff);
- zbc->customFree(zbc);
+ zbc->customMem.customFree(zbc->customMem.opaque, zbc->inBuff);
+ zbc->customMem.customFree(zbc->customMem.opaque, zbc->outBuff);
+ zbc->customMem.customFree(zbc->customMem.opaque, zbc);
return 0;
}
{ size_t const neededInBuffSize = (size_t)1 << params.cParams.windowLog;
if (zbc->inBuffSize < neededInBuffSize) {
zbc->inBuffSize = neededInBuffSize;
- zbc->customFree(zbc->inBuff); /* should not be necessary */
- zbc->inBuff = (char*)zbc->customAlloc(neededInBuffSize);
+ zbc->customMem.customFree(zbc->customMem.opaque, zbc->inBuff); /* should not be necessary */
+ zbc->inBuff = (char*)zbc->customMem.customAlloc(zbc->customMem.opaque, neededInBuffSize);
if (zbc->inBuff == NULL) return ERROR(memory_allocation);
}
zbc->blockSize = MIN(ZSTD_BLOCKSIZE_MAX, neededInBuffSize/2);
}
if (zbc->outBuffSize < ZSTD_compressBound(zbc->blockSize)+1) {
zbc->outBuffSize = ZSTD_compressBound(zbc->blockSize)+1;
- zbc->customFree(zbc->outBuff); /* should not be necessary */
- zbc->outBuff = (char*)zbc->customAlloc(zbc->outBuffSize);
+ zbc->customMem.customFree(zbc->customMem.opaque, zbc->outBuff); /* should not be necessary */
+ zbc->outBuff = (char*)zbc->customMem.customAlloc(zbc->customMem.opaque, zbc->outBuffSize);
if (zbc->outBuff == NULL) return ERROR(memory_allocation);
}
size_t workSpaceSize;
size_t blockSize;
XXH64_state_t xxhState;
- ZSTD_allocFunction customAlloc;
- ZSTD_freeFunction customFree;
+ ZSTD_customMem customMem;
seqStore_t seqStore; /* sequences storage ptrs */
U32* hashTable;
ZSTD_CCtx* ZSTD_createCCtx(void)
{
- ZSTD_customMem customMem = { NULL, NULL };
+ ZSTD_customMem const customMem = { NULL, NULL, NULL };
return ZSTD_createCCtx_advanced(customMem);
}
{
ctx = (ZSTD_CCtx*) calloc(1, sizeof(ZSTD_CCtx));
if (!ctx) return NULL;
-
- ctx->customAlloc = malloc;
- ctx->customFree = free;
+ memcpy(&ctx->customMem, &defaultCustomMem, sizeof(ZSTD_customMem));
return ctx;
}
if (!customMem.customAlloc || !customMem.customFree)
return NULL;
- ctx = (ZSTD_CCtx*) customMem.customAlloc(sizeof(ZSTD_CCtx));
+ ctx = (ZSTD_CCtx*) customMem.customAlloc(customMem.opaque, sizeof(ZSTD_CCtx));
if (!ctx) return NULL;
-
memset(ctx, 0, sizeof(ZSTD_CCtx));
- ctx->customAlloc = customMem.customAlloc;
- ctx->customFree = customMem.customFree;
+ memcpy(&ctx->customMem, &customMem, sizeof(ZSTD_customMem));
return ctx;
}
size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx)
{
- cctx->customFree(cctx->workSpace);
- cctx->customFree(cctx);
+ cctx->customMem.customFree(cctx->customMem.opaque, cctx->workSpace);
+ cctx->customMem.customFree(cctx->customMem.opaque, cctx);
return 0; /* reserved as a potential error code in the future */
}
size_t const neededSpace = tableSpace + (256*sizeof(U32)) /* huffTable */ + tokenSpace
+ ((params.cParams.strategy == ZSTD_btopt) ? optSpace : 0);
if (zc->workSpaceSize < neededSpace) {
- zc->customFree(zc->workSpace);
- zc->workSpace = zc->customAlloc(neededSpace);
+ zc->customMem.customFree(zc->customMem.opaque, zc->workSpace);
+ zc->workSpace = zc->customMem.customAlloc(zc->customMem.opaque, neededSpace);
if (zc->workSpace == NULL) return ERROR(memory_allocation);
zc->workSpaceSize = neededSpace;
} }
if (srcCCtx->stage!=1) return ERROR(stage_wrong);
dstCCtx->hashLog3 = srcCCtx->hashLog3; /* must be before ZSTD_resetCCtx_advanced */
- dstCCtx->customAlloc = srcCCtx->customAlloc;
- dstCCtx->customFree = srcCCtx->customFree;
+ memcpy(&dstCCtx->customMem, &srcCCtx->customMem, sizeof(ZSTD_customMem));
ZSTD_resetCCtx_advanced(dstCCtx, srcCCtx->params, 0);
dstCCtx->params.fParams.contentSizeFlag = 0; /* content size different from the one set during srcCCtx init */
size_t result;
ZSTD_CCtx ctxBody;
memset(&ctxBody, 0, sizeof(ctxBody));
- ctxBody.customAlloc = malloc;
- ctxBody.customFree = free;
+ memcpy(&ctxBody.customMem, &defaultCustomMem, sizeof(ZSTD_customMem));
result = ZSTD_compressCCtx(&ctxBody, dst, dstCapacity, src, srcSize, compressionLevel);
- ctxBody.customFree(ctxBody.workSpace); /* can't free ctxBody, since it's on stack; just free heap content */
+ ctxBody.customMem.customFree(ctxBody.customMem.opaque, ctxBody.workSpace); /* can't free ctxBody, since it's on stack; just free heap content */
return result;
}
size_t blockSize;
BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX];
size_t lhSize;
- ZSTD_allocFunction customAlloc;
- ZSTD_freeFunction customFree;
+ ZSTD_customMem customMem;
}; /* typedef'd to ZBUFF_DCtx within "zstd_buffered.h" */
ZBUFF_DCtx* ZBUFF_createDCtx(void)
{
- ZSTD_customMem customMem = { NULL, NULL };
+ ZSTD_customMem const customMem = { NULL, NULL, NULL };
return ZBUFF_createDCtx_advanced(customMem);
}
{
zbd = (ZBUFF_DCtx*)calloc(1, sizeof(ZBUFF_DCtx));
if (zbd==NULL) return NULL;
- zbd->customAlloc = malloc;
- zbd->customFree = free;
+ memcpy(&zbd->customMem, &defaultCustomMem, sizeof(ZSTD_customMem));
zbd->zd = ZSTD_createDCtx();
zbd->stage = ZBUFFds_init;
return zbd;
if (!customMem.customAlloc || !customMem.customFree)
return NULL;
- zbd = (ZBUFF_DCtx*)customMem.customAlloc(sizeof(ZBUFF_DCtx));
+ zbd = (ZBUFF_DCtx*)customMem.customAlloc(customMem.opaque, sizeof(ZBUFF_DCtx));
if (zbd==NULL) return NULL;
memset(zbd, 0, sizeof(ZBUFF_DCtx));
- zbd->customAlloc = customMem.customAlloc;
- zbd->customFree = customMem.customFree;
+ memcpy(&zbd->customMem, &customMem, sizeof(ZSTD_customMem));
zbd->zd = ZSTD_createDCtx_advanced(customMem);
zbd->stage = ZBUFFds_init;
return zbd;
{
if (zbd==NULL) return 0; /* support free on null */
ZSTD_freeDCtx(zbd->zd);
- zbd->customFree(zbd->inBuff);
- zbd->customFree(zbd->outBuff);
- zbd->customFree(zbd);
+ zbd->customMem.customFree(zbd->customMem.opaque, zbd->inBuff);
+ zbd->customMem.customFree(zbd->customMem.opaque, zbd->outBuff);
+ zbd->customMem.customFree(zbd->customMem.opaque, zbd);
return 0;
}
{ size_t const blockSize = MIN(1 << zbd->fParams.windowLog, ZSTD_BLOCKSIZE_MAX);
zbd->blockSize = blockSize;
if (zbd->inBuffSize < blockSize) {
- zbd->customFree(zbd->inBuff);
+ zbd->customMem.customFree(zbd->customMem.opaque, zbd->inBuff);
zbd->inBuffSize = blockSize;
- zbd->inBuff = (char*)zbd->customAlloc(blockSize);
+ zbd->inBuff = (char*)zbd->customMem.customAlloc(zbd->customMem.opaque, blockSize);
if (zbd->inBuff == NULL) return ERROR(memory_allocation);
}
{ size_t const neededOutSize = ((size_t)1 << zbd->fParams.windowLog) + blockSize;
if (zbd->outBuffSize < neededOutSize) {
- zbd->customFree(zbd->outBuff);
+ zbd->customMem.customFree(zbd->customMem.opaque, zbd->outBuff);
zbd->outBuffSize = neededOutSize;
- zbd->outBuff = (char*)zbd->customAlloc(neededOutSize);
+ zbd->outBuff = (char*)zbd->customMem.customAlloc(zbd->customMem.opaque, neededOutSize);
if (zbd->outBuff == NULL) return ERROR(memory_allocation);
} } }
zbd->stage = ZBUFFds_read;
size_t headerSize;
ZSTD_frameParams fParams;
XXH64_state_t xxhState;
- ZSTD_allocFunction customAlloc;
- ZSTD_freeFunction customFree;
+ ZSTD_customMem customMem;
blockType_t bType; /* used in ZSTD_decompressContinue(), to transfer blockType between header decoding and block decoding stages */
ZSTD_dStage stage;
U32 dictID;
if (!customMem.customAlloc && !customMem.customFree) {
dctx = (ZSTD_DCtx*) malloc(sizeof(ZSTD_DCtx));
if (!dctx) return NULL;
- dctx->customAlloc = malloc;
- dctx->customFree = free;
-
+ memcpy(&dctx->customMem, &defaultCustomMem, sizeof(ZSTD_customMem));
ZSTD_decompressBegin(dctx);
return dctx;
}
if (!customMem.customAlloc || !customMem.customFree)
return NULL;
- dctx = (ZSTD_DCtx*) customMem.customAlloc(sizeof(ZSTD_DCtx));
+ dctx = (ZSTD_DCtx*) customMem.customAlloc(customMem.opaque, sizeof(ZSTD_DCtx));
if (!dctx) return NULL;
- dctx->customAlloc = customMem.customAlloc;
- dctx->customFree = customMem.customFree;
-
+ memcpy(&dctx->customMem, &customMem, sizeof(ZSTD_customMem));
ZSTD_decompressBegin(dctx);
return dctx;
}
ZSTD_DCtx* ZSTD_createDCtx(void)
{
- ZSTD_customMem const customMem = { NULL, NULL };
+ ZSTD_customMem const customMem = { NULL, NULL, NULL };
return ZSTD_createDCtx_advanced(customMem);
}
size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx)
{
- dctx->customFree(dctx);
+ dctx->customMem.customFree(dctx->customMem.opaque, dctx);
return 0; /* reserved as a potential error code in the future */
}
}
*/
-void* ZBUFF_allocFunction(size_t size)
+void* ZBUFF_allocFunction(void* opaque, size_t size)
{
+ (void)opaque;
void* address = malloc(size);
- /* DISPLAYLEVEL(4, "alloc %p, %d \n", address, (int)size); */
+ /* DISPLAYLEVEL(4, "alloc %p, %d opaque=%d \n", address, (int)size, (int)opaque); */
return address;
}
-void ZBUFF_freeFunction(void* address)
+void ZBUFF_freeFunction(void* opaque, void* address)
{
- /* if (address) DISPLAYLEVEL(4, "free %p \n", address); */
+ (void)opaque;
+ /* if (address) DISPLAYLEVEL(4, "free %p opaque=%d \n", address, (int)opaque); */
free(address);
}
int result=0;
U32 mainPause = 0;
const char* programName = argv[0];
- ZSTD_customMem customMem = { ZBUFF_allocFunction, ZBUFF_freeFunction };
- ZSTD_customMem customNULL = { NULL, NULL };
+ ZSTD_customMem customMem = { ZBUFF_allocFunction, ZBUFF_freeFunction, (void*)777 };
+ ZSTD_customMem customNULL = { NULL, NULL, NULL };
/* Check command line */
for(argNb=1; argNb<argc; argNb++) {