From: Hans Kristian Rosbach Date: Mon, 11 May 2026 18:01:26 +0000 (+0200) Subject: Use local block_start and window variables in FLUSH_BLOCK. X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=caffccf15c7505c19b2bf7d50ff163c52e0fe386;p=thirdparty%2Fzlib-ng.git Use local block_start and window variables in FLUSH_BLOCK. Also ensure all deflate methods use local window variable. deflate_medium now passes local window to its static functions. --- diff --git a/deflate_fast.c b/deflate_fast.c index 22311d73d..2744e3247 100644 --- a/deflate_fast.c +++ b/deflate_fast.c @@ -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; } diff --git a/deflate_huff.c b/deflate_huff.c index d5a234b11..3f7bfef2c 100644 --- a/deflate_huff.c +++ b/deflate_huff.c @@ -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; } diff --git a/deflate_medium.c b/deflate_medium.c index 860834d1f..02fb6e048 100644 --- a/deflate_medium.c +++ b/deflate_medium.c @@ -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, ¤t_match, &next_match); + fizzle_matches(s, window, ¤t_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; } diff --git a/deflate_p.h b/deflate_p.h index f60970bab..6085e62c9 100644 --- a/deflate_p.h +++ b/deflate_p.h @@ -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; \ } diff --git a/deflate_rle.c b/deflate_rle.c index 058908c53..b6696f5b4 100644 --- a/deflate_rle.c +++ b/deflate_rle.c @@ -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; } diff --git a/deflate_slow.c b/deflate_slow.c index e524ec1e3..11384a9a2 100644 --- a/deflate_slow.c +++ b/deflate_slow.c @@ -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; } diff --git a/deflate_stored.c b/deflate_stored.c index f912f631b..9c9e3fecb 100644 --- a/deflate_stored.c +++ b/deflate_stored.c @@ -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); }