From 65057cf009a749e66b045b0a709f5e95c6c8abd1 Mon Sep 17 00:00:00 2001 From: "W. Felix Handte" Date: Wed, 14 Aug 2019 14:44:17 -0400 Subject: [PATCH] Rewrite ZSTD_initStaticCCtx to Alloc CCtx in Workspace --- lib/compress/zstd_compress.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index a8bf0d927..f27698c0d 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -278,12 +278,19 @@ ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem) ZSTD_CCtx* ZSTD_initStaticCCtx(void *workspace, size_t workspaceSize) { - ZSTD_CCtx* const cctx = (ZSTD_CCtx*) workspace; + ZSTD_CCtx_workspace tmpWorkspace; + ZSTD_CCtx* cctx; if (workspaceSize <= sizeof(ZSTD_CCtx)) return NULL; /* minimum size */ if ((size_t)workspace & 7) return NULL; /* must be 8-aligned */ + ZSTD_workspace_init(&tmpWorkspace, workspace, workspaceSize); + + cctx = (ZSTD_CCtx*)ZSTD_workspace_reserve_object(&tmpWorkspace, sizeof(ZSTD_CCtx)); + if (cctx == NULL) { + return NULL; + } memset(cctx, 0, sizeof(ZSTD_CCtx)); + cctx->workspace = tmpWorkspace; cctx->staticSize = workspaceSize; - ZSTD_workspace_init(&cctx->workspace, (void*)(cctx+1), workspaceSize - sizeof(ZSTD_CCtx)); /* statically sized space. entropyWorkspace never moves (but prev/next block swap places) */ if (!ZSTD_workspace_check_available(&cctx->workspace, HUF_WORKSPACE_SIZE + 2 * sizeof(ZSTD_compressedBlockState_t))) return NULL;