]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Hide Workspace Movement Behind Helper Function
authorW. Felix Handte <w@felixhandte.com>
Tue, 3 Sep 2019 16:48:45 +0000 (12:48 -0400)
committerW. Felix Handte <w@felixhandte.com>
Mon, 9 Sep 2019 17:34:08 +0000 (13:34 -0400)
lib/compress/zstd_compress.c
lib/compress/zstd_cwksp.c
lib/compress/zstd_cwksp.h

index d2ff96b56f2bb1ebee29f1aadfa3c446c02f67df..99c8a60654dd3c1a061502098be7b131bea0d94f 100644 (file)
@@ -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",
index f850397557d0c9c3f658e1888e3de4f6115201c5..0ff6bec04fbf3cfe8bcb201a4c8dde6c1842578a 100644 (file)
@@ -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;
 }
index 5abc97be7abc2dce7f879e438a686c2e183db423..630ad3ba56680f469e3017864338db9dce0b2505 100644 (file)
@@ -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);