]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Align Alloc Pointer When Transitioning from Buffers to Aligned Allocs
authorW. Felix Handte <w@felixhandte.com>
Tue, 13 Aug 2019 15:57:20 +0000 (11:57 -0400)
committerW. Felix Handte <w@felixhandte.com>
Mon, 9 Sep 2019 17:34:08 +0000 (13:34 -0400)
lib/compress/zstd_compress.c

index f081f8388c7c999f8bff778e8cf9005937658451..78a5794500c01f1399f4f08878f4c472b2fe0822 100644 (file)
@@ -64,8 +64,19 @@ static void* ZSTD_workspace_reserve(ZSTD_CCtx_workspace* ws, size_t bytes, ZSTD_
         bytes, (BYTE *)alloc - (BYTE *)bottom);
     assert(phase >= ws->phase);
     if (phase > ws->phase) {
-        if (ws->phase <= ZSTD_workspace_alloc_buffers) {
-
+        if (ws->phase < ZSTD_workspace_alloc_buffers &&
+                phase >= ZSTD_workspace_alloc_buffers) {
+        }
+        if (ws->phase < ZSTD_workspace_alloc_aligned &&
+                phase >= ZSTD_workspace_alloc_aligned) {
+            /* If unaligned allocations down from a too-large top have left us
+             * unaligned, we need to realign our alloc ptr. Technically, this
+             * can consume space that is unaccounted for in the neededSpace
+             * calculation. However, I believe this can only happen when the
+             * workspace is too large, and specifically when it is too large
+             * by a larger margin than the space that will be consumed. */
+            /* TODO: cleaner, compiler warning friendly way to do this??? */
+            alloc = (BYTE*)alloc - ((size_t)alloc & (sizeof(U32)-1));
         }
         ws->phase = phase;
     }