Preallocate workSpace for cctx and combine create and init for cstream

This commit is contained in:
Nick Terrell 2017-04-03 15:23:09 -07:00
parent 21b6c53b9f
commit 59aadc85dc
5 changed files with 53 additions and 43 deletions

View File

@ -256,9 +256,8 @@ size_t ZSTD_DStreamWorkspaceBound(size_t maxWindowSize);
typedef struct ZSTD_CStream_s ZSTD_CStream;
/*===== ZSTD_CStream management functions =====*/
ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream(void* workspace, size_t workspaceSize);
ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, ZSTD_parameters params, unsigned long long pledgedSrcSize);
ZSTDLIB_API size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, ZSTD_parameters params, const ZSTD_CDict* cdict, unsigned long long pledgedSrcSize);
ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream(ZSTD_parameters params, unsigned long long pledgedSrcSize, void* workspace, size_t workspaceSize);
ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream_usingCDict(ZSTD_parameters params, const ZSTD_CDict* cdict, unsigned long long pledgedSrcSize, void* workspace, size_t workspaceSize);
/*===== Streaming compression functions =====*/
ZSTDLIB_API size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize); /**< re-use compression parameters from previous init; skip dictionary loading stage; zcs must be init at least once before. note: pledgedSrcSize must be correct, a size of 0 means unknown. for a frame size of 0 use initCStream_advanced */
@ -295,9 +294,8 @@ ZSTDLIB_API size_t ZSTD_CStreamOutSize(void); /**< recommended size for output
typedef struct ZSTD_DStream_s ZSTD_DStream;
/*===== ZSTD_DStream management functions =====*/
ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream(void* workspace, size_t workspaceSize);
ZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds, size_t maxWindowSize);
ZSTDLIB_API size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, size_t maxWindowSize, const ZSTD_DDict* ddict);
ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream(size_t maxWindowSize, void* workspace, size_t workspaceSize);
ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream_usingDDict(size_t maxWindowSize, const ZSTD_DDict* ddict, void* workspace, size_t workspaceSize);
/*===== Streaming decompression functions =====*/
ZSTDLIB_API size_t ZSTD_resetDStream(ZSTD_DStream* zds); /**< re-use decompression parameters from previous init; saves dictionary loading */

View File

@ -48,6 +48,12 @@ ZSTD_customMem ZSTD_initStack(void* workspace, size_t workspaceSize) {
return stackMem;
}
void* ZSTD_stackAllocAll(void* opaque, size_t* size) {
ZSTD_stack* stack = (ZSTD_stack*)opaque;
*size = stack->end - ZSTD_PTR_ALIGN(stack->ptr);
return stack_push(stack, *size);
}
void* ZSTD_stackAlloc(void* opaque, size_t size) {
ZSTD_stack* stack = (ZSTD_stack*)opaque;
return stack_push(stack, size);

View File

@ -115,7 +115,11 @@ static ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem)
ZSTD_CCtx* ZSTD_createCCtx(void* workspace, size_t workspaceSize)
{
ZSTD_customMem const stackMem = ZSTD_initStack(workspace, workspaceSize);
return ZSTD_createCCtx_advanced(stackMem);
ZSTD_CCtx* cctx = ZSTD_createCCtx_advanced(stackMem);
if (cctx) {
cctx->workSpace = ZSTD_stackAllocAll(cctx->customMem.opaque, &cctx->workSpaceSize);
}
return cctx;
}
size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx)
@ -2917,12 +2921,6 @@ ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem)
return zcs;
}
ZSTD_CStream* ZSTD_createCStream(void* workspace, size_t workspaceSize)
{
ZSTD_customMem const stackMem = ZSTD_initStack(workspace, workspaceSize);
return ZSTD_createCStream_advanced(stackMem);
}
size_t ZSTD_freeCStream(ZSTD_CStream* zcs)
{
if (zcs==NULL) return 0; /* support free on NULL */
@ -3006,18 +3004,25 @@ static size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs,
return ZSTD_resetCStream_internal(zcs, pledgedSrcSize);
}
/* note : cdict must outlive compression session */
size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, ZSTD_parameters params, const ZSTD_CDict* cdict, unsigned long long pledgedSrcSize)
ZSTD_CStream* ZSTD_createCStream(ZSTD_parameters params, unsigned long long pledgedSrcSize, void* workspace, size_t workspaceSize)
{
size_t const initError = ZSTD_initCStream_advanced(zcs, NULL, 0, params, pledgedSrcSize);
zcs->cdict = cdict;
zcs->cctx->dictID = params.fParams.noDictIDFlag ? 0 : cdict->refContext->dictID;
return initError;
ZSTD_customMem const stackMem = ZSTD_initStack(workspace, workspaceSize);
ZSTD_CStream* const zcs = ZSTD_createCStream_advanced(stackMem);
if (zcs) {
size_t const code = ZSTD_initCStream_advanced(zcs, NULL, 0, params, pledgedSrcSize);
if (ZSTD_isError(code)) { return NULL; }
}
return zcs;
}
size_t ZSTD_initCStream(ZSTD_CStream* zcs, ZSTD_parameters params, unsigned long long pledgedSrcSize)
ZSTD_CStream* ZSTD_createCStream_usingCDict(ZSTD_parameters params, const ZSTD_CDict* cdict, unsigned long long pledgedSrcSize, void* workspace, size_t workspaceSize)
{
return ZSTD_initCStream_advanced(zcs, NULL, 0, params, pledgedSrcSize);
ZSTD_CStream* const zcs = ZSTD_createCStream(params, pledgedSrcSize, workspace, workspaceSize);
if (zcs) {
zcs->cdict = cdict;
zcs->cctx->dictID = params.fParams.noDictIDFlag ? 0 : cdict->refContext->dictID;
}
return zcs;
}
/*====== Compression ======*/

View File

@ -2098,10 +2098,30 @@ static ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem)
return zds;
}
ZSTD_DStream* ZSTD_createDStream(void* workspace, size_t workspaceSize)
ZSTD_DStream* ZSTD_createDStream(size_t maxWindowSize, void* workspace, size_t workspaceSize)
{
ZSTD_customMem const stackMem = ZSTD_initStack(workspace, workspaceSize);
return ZSTD_createDStream_advanced(stackMem);
ZSTD_DStream* zds = ZSTD_createDStream_advanced(stackMem);
if (!zds) { return NULL; }
zds->maxWindowSize = maxWindowSize;
zds->stage = zdss_loadHeader;
zds->lhSize = zds->inPos = zds->outStart = zds->outEnd = 0;
ZSTD_freeDDict(zds->ddictLocal);
zds->ddictLocal = NULL;
zds->ddict = zds->ddictLocal;
zds->legacyVersion = 0;
zds->hostageByte = 0;
return zds;
}
ZSTD_DStream* ZSTD_createDStream_usingDDict(size_t maxWindowSize, const ZSTD_DDict* ddict, void* workspace, size_t workspaceSize)
{
ZSTD_DStream* zds = ZSTD_createDStream(maxWindowSize, workspace, workspaceSize);
if (zds) {
zds->ddict = ddict;
}
return zds;
}
size_t ZSTD_freeDStream(ZSTD_DStream* zds)
@ -2127,26 +2147,6 @@ size_t ZSTD_freeDStream(ZSTD_DStream* zds)
size_t ZSTD_DStreamInSize(void) { return ZSTD_BLOCKSIZE_ABSOLUTEMAX + ZSTD_blockHeaderSize; }
size_t ZSTD_DStreamOutSize(void) { return ZSTD_BLOCKSIZE_ABSOLUTEMAX; }
size_t ZSTD_initDStream(ZSTD_DStream* zds, size_t maxWindowSize)
{
zds->maxWindowSize = maxWindowSize;
zds->stage = zdss_loadHeader;
zds->lhSize = zds->inPos = zds->outStart = zds->outEnd = 0;
ZSTD_freeDDict(zds->ddictLocal);
zds->ddictLocal = NULL;
zds->ddict = zds->ddictLocal;
zds->legacyVersion = 0;
zds->hostageByte = 0;
return ZSTD_frameHeaderSize_prefix;
}
size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, size_t maxWindowSize, const ZSTD_DDict* ddict) /**< note : ddict will just be referenced, and must outlive decompression session */
{
size_t const initResult = ZSTD_initDStream(zds, maxWindowSize);
zds->ddict = ddict;
return initResult;
}
size_t ZSTD_resetDStream(ZSTD_DStream* zds)
{
zds->stage = zdss_loadHeader;

View File

@ -229,6 +229,7 @@ typedef struct {
ZSTD_customMem ZSTD_initStack(void* workspace, size_t workspaceSize);
void* ZSTD_stackAllocAll(void* opaque, size_t* size);
void* ZSTD_stackAlloc(void* opaque, size_t size);
void ZSTD_stackFree(void* opaque, void* address);