]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Pull Phase Advance Logic Out into Internal Function
authorW. Felix Handte <w@felixhandte.com>
Tue, 3 Sep 2019 16:59:10 +0000 (12:59 -0400)
committerW. Felix Handte <w@felixhandte.com>
Mon, 9 Sep 2019 17:34:08 +0000 (13:34 -0400)
lib/compress/zstd_cwksp.c

index 0ff6bec04fbf3cfe8bcb201a4c8dde6c1842578a..36ba4bbe0d39b2a5e7a104ace813230f96204aa0 100644 (file)
@@ -23,15 +23,8 @@ size_t ZSTD_cwksp_align(size_t size, size_t const align) {
     return (size + mask) & ~mask;
 }
 
-/**
- * Internal function, use wrappers instead.
- */
-static void* ZSTD_cwksp_reserve_internal(ZSTD_cwksp* ws, size_t bytes, ZSTD_cwksp_alloc_phase_e phase) {
-    /* TODO(felixh): alignment */
-    void* alloc = (BYTE *)ws->allocStart - bytes;
-    void* bottom = ws->tableEnd;
-    DEBUGLOG(4, "cwksp: reserving align %zd bytes, %zd bytes remaining",
-        bytes, (BYTE *)alloc - (BYTE *)bottom);
+static void ZSTD_cwksp_internal_advance_phase(
+        ZSTD_cwksp* ws, ZSTD_cwksp_alloc_phase_e phase) {
     assert(phase >= ws->phase);
     if (phase > ws->phase) {
         if (ws->phase < ZSTD_cwksp_alloc_buffers &&
@@ -46,10 +39,23 @@ static void* ZSTD_cwksp_reserve_internal(ZSTD_cwksp* ws, size_t bytes, ZSTD_cwks
              * 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->allocStart = (BYTE*)ws->allocStart - ((size_t)ws->allocStart & (sizeof(U32)-1));
         }
         ws->phase = phase;
     }
+}
+
+/**
+ * Internal function, use wrappers instead.
+ */
+static void* ZSTD_cwksp_reserve_internal(ZSTD_cwksp* ws, size_t bytes, ZSTD_cwksp_alloc_phase_e phase) {
+    /* TODO(felixh): alignment */
+    void* alloc;
+    void* bottom = ws->tableEnd;
+    ZSTD_cwksp_internal_advance_phase(ws, phase);
+    alloc = (BYTE *)ws->allocStart - bytes;
+    DEBUGLOG(4, "cwksp: reserving align %zd bytes, %zd bytes remaining",
+        bytes, ZSTD_cwksp_available_space(ws) - bytes);
     assert(alloc >= bottom);
     if (alloc < bottom) {
         ws->allocFailed = 1;
@@ -87,13 +93,7 @@ void* ZSTD_cwksp_reserve_table(ZSTD_cwksp* ws, size_t bytes) {
     DEBUGLOG(4, "cwksp: reserving table %zd bytes, %zd bytes remaining",
         bytes, ZSTD_cwksp_available_space(ws) - bytes);
     assert((bytes & (sizeof(U32)-1)) == 0);
-    assert(phase >= ws->phase);
-    if (phase > ws->phase) {
-        if (ws->phase <= ZSTD_cwksp_alloc_buffers) {
-
-        }
-        ws->phase = phase;
-    }
+    ZSTD_cwksp_internal_advance_phase(ws, phase);
     assert(end <= top);
     if (end > top) {
         DEBUGLOG(4, "cwksp: object alloc failed!");
@@ -116,6 +116,7 @@ void* ZSTD_cwksp_reserve_object(ZSTD_cwksp* ws, size_t bytes) {
         bytes, roundedBytes, ZSTD_cwksp_available_space(ws) - roundedBytes);
     assert(((size_t)start & (sizeof(void*)-1)) == 0);
     assert((bytes & (sizeof(void*)-1)) == 0);
+    /* we must be in the first phase, no advance is possible */
     if (ws->phase != ZSTD_cwksp_alloc_objects || end > ws->workspaceEnd) {
         DEBUGLOG(4, "cwksp: object alloc failed!");
         ws->allocFailed = 1;