+/*=**************************************************************
+* Custom allocator
+****************************************************************/
+/* default uses stdlib */
void* ZSTD_defaultAllocFunction(void* opaque, size_t size)
{
void* address = malloc(size);
(void)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) printf("free %p opaque=%p \n", address, opaque); */
free(address);
}
+
+void* ZSTD_malloc(size_t size, ZSTD_customMem customMem)
+{
+ return customMem.customAlloc(customMem.opaque, size);
+}
+
+void ZSTD_free(void* ptr, ZSTD_customMem customMem)
+{
+ if (ptr!=NULL)
+ customMem.customFree(customMem.opaque, ptr);
+}
void* ZSTD_defaultAllocFunction(void* opaque, size_t size);
void ZSTD_defaultFreeFunction(void* opaque, void* address);
static const ZSTD_customMem defaultCustomMem = { ZSTD_defaultAllocFunction, ZSTD_defaultFreeFunction, NULL };
+void* ZSTD_malloc(size_t size, ZSTD_customMem customMem);
+void ZSTD_free(void* ptr, ZSTD_customMem customMem);
+
/*====== common function ======*/
{
ZSTD_CCtx* cctx;
- if (!customMem.customAlloc && !customMem.customFree)
- customMem = defaultCustomMem;
+ if (!customMem.customAlloc && !customMem.customFree) customMem = defaultCustomMem;
+ if (!customMem.customAlloc || !customMem.customFree) return NULL;
- if (!customMem.customAlloc || !customMem.customFree)
- return NULL;
-
- cctx = (ZSTD_CCtx*) customMem.customAlloc(customMem.opaque, sizeof(ZSTD_CCtx));
+ cctx = (ZSTD_CCtx*) ZSTD_malloc(sizeof(ZSTD_CCtx), customMem);
if (!cctx) return NULL;
memset(cctx, 0, sizeof(ZSTD_CCtx));
memcpy(&(cctx->customMem), &customMem, sizeof(ZSTD_customMem));
size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx)
{
if (cctx==NULL) return 0; /* support free on NULL */
- if (cctx->workSpace) cctx->customMem.customFree(cctx->customMem.opaque, cctx->workSpace);
- cctx->customMem.customFree(cctx->customMem.opaque, cctx);
+ ZSTD_free(cctx->workSpace, cctx->customMem);
+ ZSTD_free(cctx, cctx->customMem);
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->customMem.customFree(zc->customMem.opaque, zc->workSpace);
- zc->workSpace = zc->customMem.customAlloc(zc->customMem.opaque, neededSpace);
+ ZSTD_free(zc->workSpace, zc->customMem);
+ zc->workSpace = ZSTD_malloc(neededSpace, zc->customMem);
if (zc->workSpace == NULL) return ERROR(memory_allocation);
zc->workSpaceSize = neededSpace;
} }
memset(&ctxBody, 0, sizeof(ctxBody));
memcpy(&ctxBody.customMem, &defaultCustomMem, sizeof(ZSTD_customMem));
result = ZSTD_compressCCtx(&ctxBody, dst, dstCapacity, src, srcSize, compressionLevel);
- ctxBody.customMem.customFree(ctxBody.customMem.opaque, ctxBody.workSpace); /* can't free ctxBody, since it's on stack; just free heap content */
+ ZSTD_free(ctxBody.workSpace, defaultCustomMem); /* can't free ctxBody itself, as it's on stack; free only heap content */
return result;
}
ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize, ZSTD_parameters params, ZSTD_customMem customMem)
{
- if (!customMem.customAlloc && !customMem.customFree)
- customMem = defaultCustomMem;
-
- if (!customMem.customAlloc || !customMem.customFree) /* can't have 1/2 custom alloc/free as NULL */
- return NULL;
+ if (!customMem.customAlloc && !customMem.customFree) customMem = defaultCustomMem;
+ if (!customMem.customAlloc || !customMem.customFree) return NULL;
- { ZSTD_CDict* const cdict = (ZSTD_CDict*) customMem.customAlloc(customMem.opaque, sizeof(*cdict));
- void* const dictContent = customMem.customAlloc(customMem.opaque, dictSize);
+ { ZSTD_CDict* const cdict = (ZSTD_CDict*) ZSTD_malloc(sizeof(ZSTD_CDict), customMem);
+ void* const dictContent = ZSTD_malloc(dictSize, customMem);
ZSTD_CCtx* const cctx = ZSTD_createCCtx_advanced(customMem);
if (!dictContent || !cdict || !cctx) {
- customMem.customFree(customMem.opaque, dictContent);
- customMem.customFree(customMem.opaque, cdict);
- customMem.customFree(customMem.opaque, cctx);
+ ZSTD_free(dictContent, customMem);
+ ZSTD_free(cdict, customMem);
+ ZSTD_free(cctx, customMem);
return NULL;
}
memcpy(dictContent, dict, dictSize);
{ size_t const errorCode = ZSTD_compressBegin_advanced(cctx, dictContent, dictSize, params, 0);
if (ZSTD_isError(errorCode)) {
- customMem.customFree(customMem.opaque, dictContent);
- customMem.customFree(customMem.opaque, cdict);
- customMem.customFree(customMem.opaque, cctx);
+ ZSTD_free(dictContent, customMem);
+ ZSTD_free(cdict, customMem);
+ ZSTD_free(cctx, customMem);
return NULL;
} }
size_t ZSTD_freeCDict(ZSTD_CDict* cdict)
{
- ZSTD_freeFunction const cFree = cdict->refContext->customMem.customFree;
- void* const opaque = cdict->refContext->customMem.opaque;
- ZSTD_freeCCtx(cdict->refContext);
- cFree(opaque, cdict->dictContent);
- cFree(opaque, cdict);
- return 0;
+ if (cdict==NULL) return 0; /* support free on NULL */
+ { ZSTD_customMem cMem = cdict->refContext->customMem;
+ ZSTD_freeCCtx(cdict->refContext);
+ ZSTD_free(cdict->dictContent, cMem);
+ ZSTD_free(cdict, cMem);
+ return 0;
+ }
}
/*! ZSTD_compress_usingCDict() :
{
ZSTD_CStream* zcs;
- if (!customMem.customAlloc && !customMem.customFree)
- customMem = defaultCustomMem;
+ if (!customMem.customAlloc && !customMem.customFree) customMem = defaultCustomMem;
+ if (!customMem.customAlloc || !customMem.customFree) return NULL;
- if (!customMem.customAlloc || !customMem.customFree)
- return NULL;
-
- zcs = (ZSTD_CStream*)customMem.customAlloc(customMem.opaque, sizeof(ZSTD_CStream));
+ zcs = (ZSTD_CStream*)ZSTD_malloc(sizeof(ZSTD_CStream), customMem);
if (zcs==NULL) return NULL;
memset(zcs, 0, sizeof(ZSTD_CStream));
memcpy(&zcs->customMem, &customMem, sizeof(ZSTD_customMem));
size_t ZSTD_freeCStream(ZSTD_CStream* zcs)
{
if (zcs==NULL) return 0; /* support free on NULL */
- ZSTD_freeCCtx(zcs->zc);
- if (zcs->inBuff) zcs->customMem.customFree(zcs->customMem.opaque, zcs->inBuff);
- if (zcs->outBuff) zcs->customMem.customFree(zcs->customMem.opaque, zcs->outBuff);
- zcs->customMem.customFree(zcs->customMem.opaque, zcs);
- return 0;
+ { ZSTD_customMem const cMem = zcs->customMem;
+ ZSTD_freeCCtx(zcs->zc);
+ ZSTD_free(zcs->inBuff, cMem);
+ ZSTD_free(zcs->outBuff, cMem);
+ ZSTD_free(zcs, cMem);
+ return 0;
+ }
}
{ size_t const neededInBuffSize = (size_t)1 << params.cParams.windowLog;
if (zcs->inBuffSize < neededInBuffSize) {
zcs->inBuffSize = neededInBuffSize;
- zcs->customMem.customFree(zcs->customMem.opaque, zcs->inBuff); /* should not be necessary */
- zcs->inBuff = (char*)zcs->customMem.customAlloc(zcs->customMem.opaque, neededInBuffSize);
+ ZSTD_free(zcs->inBuff, zcs->customMem); /* should not be necessary */
+ zcs->inBuff = (char*) ZSTD_malloc(neededInBuffSize, zcs->customMem);
if (zcs->inBuff == NULL) return ERROR(memory_allocation);
}
zcs->blockSize = MIN(ZSTD_BLOCKSIZE_ABSOLUTEMAX, neededInBuffSize);
}
if (zcs->outBuffSize < ZSTD_compressBound(zcs->blockSize)+1) {
zcs->outBuffSize = ZSTD_compressBound(zcs->blockSize)+1;
- zcs->customMem.customFree(zcs->customMem.opaque, zcs->outBuff); /* should not be necessary */
- zcs->outBuff = (char*)zcs->customMem.customAlloc(zcs->customMem.opaque, zcs->outBuffSize);
+ ZSTD_free(zcs->outBuff, zcs->customMem); /* should not be necessary */
+ zcs->outBuff = (char*) ZSTD_malloc(zcs->outBuffSize, zcs->customMem);
if (zcs->outBuff == NULL) return ERROR(memory_allocation);
}
if (!customMem.customAlloc && !customMem.customFree) customMem = defaultCustomMem;
if (!customMem.customAlloc || !customMem.customFree) return NULL;
- dctx = (ZSTD_DCtx*) customMem.customAlloc(customMem.opaque, sizeof(ZSTD_DCtx));
+ dctx = (ZSTD_DCtx*) ZSTD_malloc(sizeof(ZSTD_DCtx), customMem);
if (!dctx) return NULL;
memcpy(&dctx->customMem, &customMem, sizeof(customMem));
ZSTD_decompressBegin(dctx);
size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx)
{
if (dctx==NULL) return 0; /* support free on NULL */
- dctx->customMem.customFree(dctx->customMem.opaque, dctx);
+ ZSTD_free(dctx, dctx->customMem);
return 0; /* reserved as a potential error code in the future */
}
ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize, ZSTD_customMem customMem)
{
- if (!customMem.customAlloc && !customMem.customFree)
- customMem = defaultCustomMem;
-
- if (!customMem.customAlloc || !customMem.customFree)
- return NULL;
+ if (!customMem.customAlloc && !customMem.customFree) customMem = defaultCustomMem;
+ if (!customMem.customAlloc || !customMem.customFree) return NULL;
- { ZSTD_DDict* const ddict = (ZSTD_DDict*) customMem.customAlloc(customMem.opaque, sizeof(*ddict));
- void* const dictContent = customMem.customAlloc(customMem.opaque, dictSize);
+ { ZSTD_DDict* const ddict = (ZSTD_DDict*) ZSTD_malloc(sizeof(ZSTD_DDict), customMem);
+ void* const dictContent = ZSTD_malloc(dictSize, customMem);
ZSTD_DCtx* const dctx = ZSTD_createDCtx_advanced(customMem);
if (!dictContent || !ddict || !dctx) {
- customMem.customFree(customMem.opaque, dictContent);
- customMem.customFree(customMem.opaque, ddict);
- customMem.customFree(customMem.opaque, dctx);
+ ZSTD_free(dictContent, customMem);
+ ZSTD_free(ddict, customMem);
+ ZSTD_free(dctx, customMem);
return NULL;
}
memcpy(dictContent, dict, dictSize);
{ size_t const errorCode = ZSTD_decompressBegin_usingDict(dctx, dictContent, dictSize);
if (ZSTD_isError(errorCode)) {
- customMem.customFree(customMem.opaque, dictContent);
- customMem.customFree(customMem.opaque, ddict);
- customMem.customFree(customMem.opaque, dctx);
+ ZSTD_free(dictContent, customMem);
+ ZSTD_free(ddict, customMem);
+ ZSTD_free(dctx, customMem);
return NULL;
} }
size_t ZSTD_freeDDict(ZSTD_DDict* ddict)
{
- ZSTD_freeFunction const cFree = ddict->refContext->customMem.customFree;
- void* const opaque = ddict->refContext->customMem.opaque;
- ZSTD_freeDCtx(ddict->refContext);
- cFree(opaque, ddict->dict);
- cFree(opaque, ddict);
- return 0;
+ if (ddict==NULL) return 0; /* support free on NULL */
+ { ZSTD_customMem const cMem = ddict->refContext->customMem;
+ ZSTD_freeDCtx(ddict->refContext);
+ ZSTD_free(ddict->dict, cMem);
+ ZSTD_free(ddict, cMem);
+ return 0;
+ }
}
/*! ZSTD_decompress_usingDDict() :
{
ZSTD_DStream* zds;
- if (!customMem.customAlloc && !customMem.customFree)
- customMem = defaultCustomMem;
-
- if (!customMem.customAlloc || !customMem.customFree)
- return NULL;
+ if (!customMem.customAlloc && !customMem.customFree) customMem = defaultCustomMem;
+ if (!customMem.customAlloc || !customMem.customFree) return NULL;
- zds = (ZSTD_DStream*)customMem.customAlloc(customMem.opaque, sizeof(ZSTD_DStream));
+ zds = (ZSTD_DStream*) ZSTD_malloc(sizeof(ZSTD_DStream), customMem);
if (zds==NULL) return NULL;
memset(zds, 0, sizeof(ZSTD_DStream));
memcpy(&zds->customMem, &customMem, sizeof(ZSTD_customMem));
size_t ZSTD_freeDStream(ZSTD_DStream* zds)
{
if (zds==NULL) return 0; /* support free on null */
- ZSTD_freeDCtx(zds->zd);
- if (zds->inBuff) zds->customMem.customFree(zds->customMem.opaque, zds->inBuff);
- if (zds->outBuff) zds->customMem.customFree(zds->customMem.opaque, zds->outBuff);
- if (zds->dictContent) zds->customMem.customFree(zds->customMem.opaque, zds->dictContent);
+ { ZSTD_customMem const cMem = zds->customMem;
+ ZSTD_freeDCtx(zds->zd);
+ ZSTD_free(zds->inBuff, cMem);
+ ZSTD_free(zds->outBuff, cMem);
+ ZSTD_free(zds->dictContent, cMem);
#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
- if (zds->legacyContext) {
- ZSTD_freeLegacyStreamContext(zds->legacyContext, zds->previousLegacyVersion);
- zds->legacyContext = NULL;
- }
+ if (zds->legacyContext)
+ ZSTD_freeLegacyStreamContext(zds->legacyContext, zds->previousLegacyVersion);
#endif
- zds->customMem.customFree(zds->customMem.opaque, zds);
- return 0;
+ ZSTD_free(zds, cMem);
+ return 0;
+ }
}
zds->lhSize = zds->inPos = zds->outStart = zds->outEnd = 0;
if ((dict != zds->dictSource) | (dictSize != zds->dictSize)) { /* new dictionary */
if (dictSize > zds->dictSize) {
- if (zds->dictContent) zds->customMem.customFree(zds->customMem.opaque, zds->dictContent);
- zds->dictContent = zds->customMem.customAlloc(zds->customMem.opaque, dictSize);
+ ZSTD_free(zds->dictContent, zds->customMem);
+ zds->dictContent = ZSTD_malloc(dictSize, zds->customMem);
if (zds->dictContent == NULL) return ERROR(memory_allocation);
}
memcpy(zds->dictContent, dict, dictSize);
size_t const neededOutSize = zds->fParams.windowSize + blockSize;
zds->blockSize = blockSize;
if (zds->inBuffSize < blockSize) {
- zds->customMem.customFree(zds->customMem.opaque, zds->inBuff);
+ ZSTD_free(zds->inBuff, zds->customMem);
zds->inBuffSize = blockSize;
- zds->inBuff = (char*)zds->customMem.customAlloc(zds->customMem.opaque, blockSize);
+ zds->inBuff = (char*)ZSTD_malloc(blockSize, zds->customMem);
if (zds->inBuff == NULL) return ERROR(memory_allocation);
}
if (zds->outBuffSize < neededOutSize) {
- zds->customMem.customFree(zds->customMem.opaque, zds->outBuff);
+ ZSTD_free(zds->outBuff, zds->customMem);
zds->outBuffSize = neededOutSize;
- zds->outBuff = (char*)zds->customMem.customAlloc(zds->customMem.opaque, neededOutSize);
+ zds->outBuff = (char*)ZSTD_malloc(neededOutSize, zds->customMem);
if (zds->outBuff == NULL) return ERROR(memory_allocation);
} }
zds->stage = zdss_read;