opaque parameter for custom memory allocation functions

This commit is contained in:
inikep
2016-06-02 13:04:18 +02:00
parent e02bf99fa4
commit 2866951558
7 changed files with 76 additions and 69 deletions
+12 -15
View File
@@ -95,13 +95,12 @@ struct ZBUFF_CCtx_s {
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);
}
@@ -113,8 +112,7 @@ ZBUFF_CCtx* ZBUFF_createCCtx_advanced(ZSTD_customMem 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;
}
@@ -122,11 +120,10 @@ ZBUFF_CCtx* ZBUFF_createCCtx_advanced(ZSTD_customMem customMem)
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;
}
@@ -135,9 +132,9 @@ size_t ZBUFF_freeCCtx(ZBUFF_CCtx* 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;
}
@@ -152,16 +149,16 @@ size_t ZBUFF_compressInit_advanced(ZBUFF_CCtx* zbc,
{ 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);
}
+12 -19
View File
@@ -107,8 +107,7 @@ struct ZSTD_CCtx_s
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;
@@ -123,7 +122,7 @@ struct ZSTD_CCtx_s
ZSTD_CCtx* ZSTD_createCCtx(void)
{
ZSTD_customMem customMem = { NULL, NULL };
ZSTD_customMem const customMem = { NULL, NULL, NULL };
return ZSTD_createCCtx_advanced(customMem);
}
@@ -135,28 +134,24 @@ ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem 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 */
}
@@ -262,8 +257,8 @@ static size_t ZSTD_resetCCtx_advanced (ZSTD_CCtx* zc,
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;
} }
@@ -322,8 +317,7 @@ size_t ZSTD_copyCCtx(ZSTD_CCtx* dstCCtx, const ZSTD_CCtx* srcCCtx)
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 */
@@ -2487,10 +2481,9 @@ size_t ZSTD_compress(void* dst, size_t dstCapacity, const void* src, size_t srcS
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;
}