diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index d2ff96b5..99c8a606 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -94,7 +94,7 @@ ZSTD_CCtx* ZSTD_initStaticCCtx(void *workspace, size_t workspaceSize) return NULL; } memset(cctx, 0, sizeof(ZSTD_CCtx)); - cctx->workspace = ws; + ZSTD_cwksp_move(&cctx->workspace, &ws); cctx->staticSize = workspaceSize; /* statically sized space. entropyWorkspace never moves (but prev/next block swap places) */ @@ -3181,7 +3181,7 @@ ZSTD_CDict* ZSTD_createCDict_advanced(const void* dictBuffer, size_t dictSize, cdict = (ZSTD_CDict*)ZSTD_cwksp_reserve_object(&ws, sizeof(ZSTD_CDict)); assert(cdict != NULL); - cdict->workspace = ws; + ZSTD_cwksp_move(&cdict->workspace, &ws); cdict->customMem = customMem; if (ZSTD_isError( ZSTD_initCDict_internal(cdict, dictBuffer, dictSize, @@ -3257,7 +3257,7 @@ const ZSTD_CDict* ZSTD_initStaticCDict( ZSTD_cwksp_init(&ws, workspace, workspaceSize); cdict = (ZSTD_CDict*)ZSTD_cwksp_reserve_object(&ws, sizeof(ZSTD_CDict)); if (cdict == NULL) return NULL; - cdict->workspace = ws; + ZSTD_cwksp_move(&cdict->workspace, &ws); } DEBUGLOG(4, "(workspaceSize < neededSize) : (%u < %u) => %u", diff --git a/lib/compress/zstd_cwksp.c b/lib/compress/zstd_cwksp.c index f8503975..0ff6bec0 100644 --- a/lib/compress/zstd_cwksp.c +++ b/lib/compress/zstd_cwksp.c @@ -176,6 +176,11 @@ void ZSTD_cwksp_free(ZSTD_cwksp* ws, ZSTD_customMem customMem) { ZSTD_cwksp_clear(ws); } +void ZSTD_cwksp_move(ZSTD_cwksp* dst, ZSTD_cwksp* src) { + *dst = *src; + memset(src, 0, sizeof(ZSTD_cwksp)); +} + size_t ZSTD_cwksp_sizeof(const ZSTD_cwksp* ws) { return (BYTE*)ws->workspaceEnd - (BYTE*)ws->workspace; } diff --git a/lib/compress/zstd_cwksp.h b/lib/compress/zstd_cwksp.h index 5abc97be..630ad3ba 100644 --- a/lib/compress/zstd_cwksp.h +++ b/lib/compress/zstd_cwksp.h @@ -177,6 +177,12 @@ size_t ZSTD_cwksp_create(ZSTD_cwksp* ws, size_t size, ZSTD_customMem customMem); void ZSTD_cwksp_free(ZSTD_cwksp* ws, ZSTD_customMem customMem); +/** + * Moves the management of a workspace from one cwksp to another. The src cwksp + * is left in an invalid state (src must be re-init()'ed before its used again). + */ +void ZSTD_cwksp_move(ZSTD_cwksp* dst, ZSTD_cwksp* src); + size_t ZSTD_cwksp_sizeof(const ZSTD_cwksp* ws); int ZSTD_cwksp_reserve_failed(const ZSTD_cwksp* ws);