]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Use local block_start and window variables in FLUSH_BLOCK.
authorHans Kristian Rosbach <hk-git@circlestorm.org>
Mon, 11 May 2026 18:01:26 +0000 (20:01 +0200)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Tue, 19 May 2026 20:43:47 +0000 (22:43 +0200)
Also ensure all deflate methods use local window variable.
deflate_medium now passes local window to its static functions.

deflate_fast.c
deflate_huff.c
deflate_medium.c
deflate_p.h
deflate_rle.c
deflate_slow.c
deflate_stored.c

index 22311d73dcdc9e49e8ddc211782cad205dfad558..2744e3247792fa89820c7bc6f313697c37fc2fe5 100644 (file)
@@ -98,15 +98,15 @@ Z_INTERNAL block_state deflate_fast(deflate_state *s, int flush) {
             s->strstart++;
         }
         if (UNLIKELY(bflush))
-            FLUSH_BLOCK(s, 0);
+            FLUSH_BLOCK(s, window, 0);
     }
     s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
 
     if (UNLIKELY(flush == Z_FINISH)) {
-        FLUSH_BLOCK(s, 1);
+        FLUSH_BLOCK(s, window, 1);
         return finish_done;
     }
     if (UNLIKELY(s->sym_next))
-        FLUSH_BLOCK(s, 0);
+        FLUSH_BLOCK(s, window, 0);
     return block_done;
 }
index d5a234b114a42a499de910ef4de3cdeec5ba7cc6..3f7bfef2c680c949519ba64eca288ea4daebad2c 100644 (file)
@@ -14,6 +14,7 @@
  * (It will be regenerated if this run of deflate switches away from Huffman.)
  */
 Z_INTERNAL block_state deflate_huff(deflate_state *s, int flush) {
+    unsigned char *window = s->window;
     int bflush = 0;         /* set if current block must be flushed */
 
     for (;;) {
@@ -28,18 +29,18 @@ Z_INTERNAL block_state deflate_huff(deflate_state *s, int flush) {
         }
 
         /* Output a literal byte */
-        bflush = zng_tr_tally_lit(s, s->window[s->strstart]);
+        bflush = zng_tr_tally_lit(s, window[s->strstart]);
         s->lookahead--;
         s->strstart++;
         if (bflush)
-            FLUSH_BLOCK(s, 0);
+            FLUSH_BLOCK(s, window, 0);
     }
     s->insert = 0;
     if (flush == Z_FINISH) {
-        FLUSH_BLOCK(s, 1);
+        FLUSH_BLOCK(s, window, 1);
         return finish_done;
     }
     if (s->sym_next)
-        FLUSH_BLOCK(s, 0);
+        FLUSH_BLOCK(s, window, 0);
     return block_done;
 }
index 860834d1fceb93317b6db922f842d62c1cc92b04..02fb6e048b32cc15ff8085d6d34cbbe5e55b4507 100644 (file)
@@ -20,7 +20,7 @@ struct match {
     uint16_t orgstart;
 };
 
-static int emit_match(deflate_state *s, struct match match) {
+static int emit_match(deflate_state *s, unsigned char *window, struct match match) {
     int bflush = 0;
     uint32_t match_len = match.match_length;
 
@@ -30,7 +30,7 @@ static int emit_match(deflate_state *s, struct match match) {
     /* matches that are not long enough we need to emit as literals */
     if (match_len < WANT_MIN_MATCH) {
         while (match_len) {
-            bflush += zng_tr_tally_lit(s, s->window[match.strstart]);
+            bflush += zng_tr_tally_lit(s, window[match.strstart]);
             match_len--;
             match.strstart++;
         }
@@ -125,8 +125,7 @@ Z_FORCEINLINE static struct match find_best_match(deflate_state *s, uint32_t has
  * - (current->match_length - 1) <= next->match_start
  * - (current->match_length - 1) <= next->strstart
  */
-static void fizzle_matches(deflate_state *s, struct match *current, struct match *next) {
-    unsigned char *window = s->window;
+static void fizzle_matches(deflate_state *s, unsigned char *window, struct match *current, struct match *next) {
     unsigned char *match, *orig;
     struct match c, n;
     int changed = 0;
@@ -180,6 +179,7 @@ Z_INTERNAL block_state deflate_medium(deflate_state *s, int flush) {
     /* Align the first struct to start on a new cacheline, this allows us to fit both structs in one cacheline */
     ALIGNED_(16) struct match current_match = {0};
                  struct match next_match = {0};
+    unsigned char *window = s->window;
 
     /* For levels below 5, don't check the next position for a better match */
     int early_exit = s->level < 5;
@@ -235,7 +235,7 @@ Z_INTERNAL block_state deflate_medium(deflate_state *s, int flush) {
                      && next_match.match_length >= WANT_MIN_MATCH
                      && tmp_cmatch_len_sub <= next_match.match_start
                      && tmp_cmatch_len_sub <= next_match.strstart) {
-                fizzle_matches(s, &current_match, &next_match);
+                fizzle_matches(s, window, &current_match, &next_match);
             }
 
             s->strstart = current_match.strstart;
@@ -244,21 +244,21 @@ Z_INTERNAL block_state deflate_medium(deflate_state *s, int flush) {
         }
 
         /* now emit the current match */
-        bflush = emit_match(s, current_match);
+        bflush = emit_match(s, window, current_match);
 
         /* move the "cursor" forward */
         s->strstart += current_match.match_length;
 
         if (UNLIKELY(bflush))
-            FLUSH_BLOCK(s, 0);
+            FLUSH_BLOCK(s, window, 0);
     }
     s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
     if (flush == Z_FINISH) {
-        FLUSH_BLOCK(s, 1);
+        FLUSH_BLOCK(s, window, 1);
         return finish_done;
     }
     if (UNLIKELY(s->sym_next))
-        FLUSH_BLOCK(s, 0);
+        FLUSH_BLOCK(s, window, 0);
 
     return block_done;
 }
index f60970bab37666b82f00fb182e39169a1b93ba1d..6085e62c9b667c96975a082e58cd9221bd271bcd 100644 (file)
@@ -185,19 +185,20 @@ Z_FORCEINLINE static unsigned read_buf(PREFIX3(stream) *strm, unsigned char *buf
  * Flush the current block, with given end-of-file flag.
  * IN assertion: strstart is set to the end of the current match.
  */
-#define FLUSH_BLOCK_ONLY(s, last) { \
-    zng_tr_flush_block(s, (s->block_start >= 0 ? \
-                   &s->window[(unsigned)s->block_start] : \
+#define FLUSH_BLOCK_ONLY(s, window, last) { \
+    int block_start = s->block_start; \
+    zng_tr_flush_block(s, (block_start >= 0 ? \
+                   &window[(unsigned)block_start] : \
                    NULL), \
-                   (uint32_t)((int)s->strstart - s->block_start), \
+                   (uint32_t)((int)s->strstart - block_start), \
                    (last)); \
     s->block_start = (int)s->strstart; \
     PREFIX(flush_pending)(s->strm); \
 }
 
 /* Same but force premature exit if necessary. */
-#define FLUSH_BLOCK(s, last) { \
-    FLUSH_BLOCK_ONLY(s, last); \
+#define FLUSH_BLOCK(s, window, last) { \
+    FLUSH_BLOCK_ONLY(s, window, last); \
     if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
 }
 
index 058908c53332b09471137edf73487e321fffba63..b6696f5b48e91d8ce28363a552c5da13e1fc3fa2 100644 (file)
@@ -22,8 +22,9 @@
  * deflate switches away from Z_RLE.)
  */
 Z_INTERNAL block_state deflate_rle(deflate_state *s, int flush) {
-    int bflush = 0;                 /* set if current block must be flushed */
+    unsigned char *window = s->window;
     unsigned char *scan;            /* scan goes up to strend for length of run */
+    int bflush = 0;                 /* set if current block must be flushed */
     uint32_t match_len = 0;
 
     for (;;) {
@@ -41,12 +42,12 @@ Z_INTERNAL block_state deflate_rle(deflate_state *s, int flush) {
 
         /* See how many times the previous byte repeats */
         if (s->lookahead >= STD_MIN_MATCH && s->strstart > 0) {
-            scan = s->window + s->strstart - 1;
+            scan = window + s->strstart - 1;
             if (scan[0] == scan[1] && scan[1] == scan[2]) {
                 match_len = compare256_rle(scan, scan+3)+2;
                 match_len = MIN(match_len, s->lookahead);
             }
-            Assert(scan+match_len <= s->window + s->window_size - 1, "wild scan");
+            Assert(scan+match_len <= window + s->window_size - 1, "wild scan");
         }
 
         /* Emit match if have run of STD_MIN_MATCH or longer, else emit literal */
@@ -61,19 +62,19 @@ Z_INTERNAL block_state deflate_rle(deflate_state *s, int flush) {
             match_len = 0;
         } else {
             /* No match, output a literal byte */
-            bflush = zng_tr_tally_lit(s, s->window[s->strstart]);
+            bflush = zng_tr_tally_lit(s, window[s->strstart]);
             s->lookahead--;
             s->strstart++;
         }
         if (bflush)
-            FLUSH_BLOCK(s, 0);
+            FLUSH_BLOCK(s, window, 0);
     }
     s->insert = 0;
     if (flush == Z_FINISH) {
-        FLUSH_BLOCK(s, 1);
+        FLUSH_BLOCK(s, window, 1);
         return finish_done;
     }
     if (s->sym_next)
-        FLUSH_BLOCK(s, 0);
+        FLUSH_BLOCK(s, window, 0);
     return block_done;
 }
index e524ec1e3733f8aea4cfb3041cf9e2635ffc2d80..11384a9a243c41c7a3e7fcf7fac8b75c6e46b428 100644 (file)
@@ -110,7 +110,7 @@ Z_INTERNAL block_state deflate_slow(deflate_state *s, int flush) {
             s->strstart += mov_fwd + 1;
 
             if (UNLIKELY(bflush))
-                FLUSH_BLOCK(s, 0);
+                FLUSH_BLOCK(s, window, 0);
 
         } else if (s->match_available) {
             /* If there was no match at the previous position, output a
@@ -119,7 +119,7 @@ Z_INTERNAL block_state deflate_slow(deflate_state *s, int flush) {
              */
             bflush = zng_tr_tally_lit(s, window[s->strstart-1]);
             if (UNLIKELY(bflush))
-                FLUSH_BLOCK_ONLY(s, 0);
+                FLUSH_BLOCK_ONLY(s, window, 0);
             s->prev_length = match_len;
             s->strstart++;
             s->lookahead--;
@@ -142,10 +142,10 @@ Z_INTERNAL block_state deflate_slow(deflate_state *s, int flush) {
     }
     s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
     if (UNLIKELY(flush == Z_FINISH)) {
-        FLUSH_BLOCK(s, 1);
+        FLUSH_BLOCK(s, window, 1);
         return finish_done;
     }
     if (UNLIKELY(s->sym_next))
-        FLUSH_BLOCK(s, 0);
+        FLUSH_BLOCK(s, window, 0);
     return block_done;
 }
index f912f631b2d897ecb3cd9b46893eb982065ca9de..9c9e3fecb86be7a614763e5e3430f2fc1016249f 100644 (file)
@@ -25,6 +25,7 @@
  * maximizes the opportunities to have a single copy from next_in to next_out.
  */
 Z_INTERNAL block_state deflate_stored(deflate_state *s, int flush) {
+    unsigned char *window = s->window;
     /* Smallest worthy block size when not flushing or finishing. By default
      * this is 32K. This can be as small as 507 bytes for memLevel == 1. For
      * large input and output buffers, the stored block size will be larger.
@@ -83,7 +84,7 @@ Z_INTERNAL block_state deflate_stored(deflate_state *s, int flush) {
         /* Copy uncompressed bytes from the window to next_out. */
         if (left) {
             left = MIN(left, len);
-            memcpy(s->strm->next_out, s->window + s->block_start, left);
+            memcpy(s->strm->next_out, window + s->block_start, left);
             s->strm->next_out += left;
             s->strm->avail_out -= left;
             s->strm->total_out += left;
@@ -115,19 +116,19 @@ Z_INTERNAL block_state deflate_stored(deflate_state *s, int flush) {
          */
         if (used >= w_size) {    /* supplant the previous history */
             s->matches = 2;         /* clear hash */
-            memcpy(s->window, s->strm->next_in - w_size, w_size);
+            memcpy(window, s->strm->next_in - w_size, w_size);
             s->strstart = w_size;
             s->insert = s->strstart;
         } else {
             if (s->window_size - s->strstart <= used) {
                 /* Slide the window down. */
                 s->strstart -= w_size;
-                memcpy(s->window, s->window + w_size, s->strstart);
+                memcpy(window, window + w_size, s->strstart);
                 if (s->matches < 2)
                     s->matches++;   /* add a pending slide_hash() */
                 s->insert = MIN(s->insert, s->strstart);
             }
-            memcpy(s->window + s->strstart, s->strm->next_in - used, used);
+            memcpy(window + s->strstart, s->strm->next_in - used, used);
             s->strstart += used;
             s->insert += MIN(used, w_size - s->insert);
         }
@@ -149,7 +150,7 @@ Z_INTERNAL block_state deflate_stored(deflate_state *s, int flush) {
         /* Slide the window down. */
         s->block_start -= (int)w_size;
         s->strstart -= w_size;
-        memcpy(s->window, s->window + w_size, s->strstart);
+        memcpy(window, window + w_size, s->strstart);
         if (s->matches < 2)
             s->matches++;           /* add a pending slide_hash() */
         have += w_size;          /* more space now */
@@ -158,7 +159,7 @@ Z_INTERNAL block_state deflate_stored(deflate_state *s, int flush) {
 
     have = MIN(have, s->strm->avail_in);
     if (have) {
-        read_buf(s->strm, s->window + s->strstart, have);
+        read_buf(s->strm, window + s->strstart, have);
         s->strstart += have;
         s->insert += MIN(have, w_size - s->insert);
     }
@@ -177,7 +178,7 @@ Z_INTERNAL block_state deflate_stored(deflate_state *s, int flush) {
     if (left >= min_block || ((left || flush == Z_FINISH) && flush != Z_NO_FLUSH && s->strm->avail_in == 0 && left <= have)) {
         len = MIN(left, have);
         last = flush == Z_FINISH && s->strm->avail_in == 0 && len == left ? 1 : 0;
-        zng_tr_stored_block(s, s->window + s->block_start, len, last);
+        zng_tr_stored_block(s, window + s->block_start, len, last);
         s->block_start += (int)len;
         PREFIX(flush_pending)(s->strm);
     }