Optimize memory layout

This commit is contained in:
Justine Tunney 2022-09-12 04:19:32 -07:00
parent 0305194d98
commit b69f3d2488
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
41 changed files with 383 additions and 347 deletions

View file

@ -1,9 +1,10 @@
#ifndef NSYNC_ATOMIC_H_
#define NSYNC_ATOMIC_H_
#include "libc/atomic.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
typedef uint32_t nsync_atomic_uint32_;
typedef atomic_uint_fast32_t nsync_atomic_uint32_;
#define NSYNC_ATOMIC_UINT32_INIT_ 0
#define NSYNC_ATOMIC_UINT32_LOAD_(p) (*(p))

View file

@ -55,4 +55,3 @@ $(THIRD_PARTY_NSYNC_OBJS): third_party/nsync/nsync.mk
.PHONY: o/$(MODE)/third_party/nsync
o/$(MODE)/third_party/nsync: $(THIRD_PARTY_NSYNC_CHECKS)

View file

@ -27,6 +27,8 @@
#include "libc/sysv/consts/prot.h"
#include "libc/fmt/fmt.h"
#include "libc/stdio/stdio.h"
#include "libc/runtime/sysconf.h"
#include "libc/runtime/sysconf.h"
#include "libc/runtime/runtime.h"
#ifndef UTIL /* This module contains no code for Zip Utilities */
@ -537,17 +539,17 @@ struct zlist far *z; /* zip entry to compress */
if (window != NULL)
free(window); /* window can't be a mapped file here */
window_size = (ulg)q + MIN_LOOKAHEAD;
remain = window_size & (FRAMESIZE-1);
remain = window_size & (sysconf(_SC_PAGESIZE)-1);
/* If we can't touch the page beyond the end of file, we must
* allocate an extra page.
*/
if (remain > MIN_LOOKAHEAD) {
window = (uch*)mmap(0, window_size, PROT_READ, MAP_PRIVATE, ifile, 0);
} else {
window = (uch*)valloc(window_size - remain + FRAMESIZE);
window = (uch*)pvalloc(window_size - remain + sysconf(_SC_PAGESIZE));
if (window != NULL) {
window = (uch*)mmap((char*)window, window_size - remain, PROT_READ,
MAP_PRIVATE | MAP_FIXED, ifile, 0);
MAP_PRIVATE | MAP_FIXED, ifile, 0);
} else {
window = (uch*)(-1);
}

View file

@ -5,8 +5,9 @@
Use of this source code is governed by the BSD-style licenses that can
be found in the third_party/zlib/LICENSE file.
*/
#include "libc/intrin/bits.h"
#include "libc/dce.h"
#include "libc/intrin/bits.h"
#include "libc/intrin/kprintf.h"
#include "libc/macros.internal.h"
#include "libc/mem/mem.h"
#include "libc/nexgen32e/x86feature.h"
@ -119,7 +120,7 @@ Invented 1990 Phillip Walter Katz\"");
(uint64_t)((long)s->strstart - s->block_start), (last)); \
s->block_start = s->strstart; \
flush_pending(s->strm); \
Tracev((stderr, "[FLUSH]")); \
Tracev(("[FLUSH]")); \
}
/**
@ -1275,9 +1276,17 @@ static uInt longest_match(struct DeflateState *s, IPos cur_match) {
* are always equal when the other bytes match, given that
* the hash keys are equal and that HASH_BITS >= 8.
*/
Assert(*scan == *match || scan[1] != match[1], "match[2]??");
scan += 2, match++;
Assert(*scan == *match, "match[2]?");
if (1 /* !s->chromium_zlib_hash */) {
Assert(*scan == *match, "match[2]?");
} else {
/* When using CRC hashing, scan[2] and match[2] may mismatch, but in
* that case at least one of the other hashed bytes will mismatch
* also. Bytes 0 and 1 were already checked above, and we know there
* are at least four bytes to check otherwise the mismatch would have
* been found by the scan_end comparison above, so: */
Assert(*scan == *match || scan[1] != match[1], "match[2]??");
}
/* We check for insufficient lookahead only every 8th comparison;
* the 256th check will be made at strstart+258.
*/
@ -1362,17 +1371,17 @@ static uInt longest_match(struct DeflateState *s, IPos cur_match) {
static void check_match(struct DeflateState *s, IPos start, IPos match,
int length) {
/* check that the match is indeed a match */
if (zmemcmp(s->window + match, s->window + start, length) != EQUAL) {
fprintf(stderr, " start %u, match %u, length %d\n", start, match, length);
if (memcmp(s->window + match, s->window + start, length) != EQUAL) {
kprintf(" start %u, match %u, length %d\n", start, match, length);
do {
fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
kprintf("%c%c", s->window[match++], s->window[start++]);
} while (--length != 0);
z_error("invalid match");
z_error(__FILE__, __LINE__, "invalid match");
}
if (z_verbose > 1) {
fprintf(stderr, "\\[%d,%d]", start - match, length);
kprintf("\\[%d,%d]", start - match, length);
do {
putc(s->window[start++], stderr);
kprintf("%c", s->window[start++]);
} while (--length != 0);
}
}
@ -1640,7 +1649,7 @@ static block_state deflate_fast(struct DeflateState *s, int flush) {
}
} else {
/* No match, output a literal byte */
Tracevv((stderr, "%c", s->window[s->strstart]));
Tracevv(("%c", s->window[s->strstart]));
_tr_tally_lit(s, s->window[s->strstart], bflush);
s->lookahead--;
s->strstart++;
@ -1755,7 +1764,7 @@ static block_state deflate_slow(struct DeflateState *s, int flush) {
* single literal. If there was a match but the current match
* is longer, truncate the previous match to a single literal.
*/
Tracevv((stderr, "%c", s->window[s->strstart - 1]));
Tracevv(("%c", s->window[s->strstart - 1]));
_tr_tally_lit(s, s->window[s->strstart - 1], bflush);
if (bflush) {
FLUSH_BLOCK_ONLY(s, 0);
@ -1774,7 +1783,7 @@ static block_state deflate_slow(struct DeflateState *s, int flush) {
}
Assert(flush != Z_NO_FLUSH, "no flush?");
if (s->match_available) {
Tracevv((stderr, "%c", s->window[s->strstart - 1]));
Tracevv(("%c", s->window[s->strstart - 1]));
_tr_tally_lit(s, s->window[s->strstart - 1], bflush);
s->match_available = 0;
}
@ -1839,7 +1848,7 @@ static block_state deflate_rle(struct DeflateState *s, int flush) {
s->match_length = 0;
} else {
/* No match, output a literal byte */
Tracevv((stderr, "%c", s->window[s->strstart]));
Tracevv(("%c", s->window[s->strstart]));
_tr_tally_lit(s, s->window[s->strstart], bflush);
s->lookahead--;
s->strstart++;
@ -1873,7 +1882,7 @@ static block_state deflate_huff(struct DeflateState *s, int flush) {
}
/* Output a literal byte */
s->match_length = 0;
Tracevv((stderr, "%c", s->window[s->strstart]));
Tracevv(("%c", s->window[s->strstart]));
_tr_tally_lit(s, s->window[s->strstart], bflush);
s->lookahead--;
s->strstart++;

View file

@ -280,9 +280,6 @@ void _tr_stored_block(struct DeflateState *s, charf *buf, uint64_t stored_len,
#define d_code(dist) \
((dist) < 256 ? kZlibDistCode[dist] : kZlibDistCode[256 + ((dist) >> 7)])
#ifndef ZLIB_DEBUG
/* Inline versions of _tr_tally for speed: */
extern const ct_data kZlibStaticDtree[D_CODES] hidden;
extern const ct_data kZlibStaticLtree[L_CODES + 2] hidden;
extern const int kZlibBaseDist[D_CODES] hidden;
@ -290,6 +287,9 @@ extern const int kZlibBaseLength[LENGTH_CODES] hidden;
extern const uint8_t kZlibDistCode[DIST_CODE_LEN] hidden;
extern const uint8_t kZlibLengthCode[MAX_MATCH - MIN_MATCH + 1] hidden;
#ifndef ZLIB_DEBUG
/* Inline versions of _tr_tally for speed: */
#define _tr_tally_lit(s, c, flush) \
{ \
uint8_t cc = (c); \

View file

@ -46,7 +46,7 @@ int inflateBackInit(z_streamp strm, int windowBits, unsigned char *window) {
}
state = (struct InflateState *)ZALLOC(strm, 1, sizeof(struct InflateState));
if (state == Z_NULL) return Z_MEM_ERROR;
Tracev((stderr, "inflate: allocated\n"));
Tracev(("inflate: allocated\n"));
strm->state = (struct DeflateState *)state;
state->dmax = 32768U;
state->wbits = (uInt)windowBits;
@ -54,6 +54,7 @@ int inflateBackInit(z_streamp strm, int windowBits, unsigned char *window) {
state->window = window;
state->wnext = 0;
state->whave = 0;
state->sane = 0;
return Z_OK;
}
@ -97,7 +98,7 @@ static void fixedtables(struct InflateState *state) {
}
state->lencode = lenfix;
state->distcode = distfix;
#else /* !BUILDFIXED */
#else /* !BUILDFIXED */
state->lencode = kZlibLenfix;
state->distcode = kZlibDistfix;
#endif /* BUILDFIXED */
@ -277,18 +278,18 @@ int inflateBack(z_streamp strm, in_func in, void *in_desc, out_func out,
DROPBITS(1);
switch (BITS(2)) {
case 0: /* stored block */
Tracev((stderr, "inflate: stored block%s\n",
Tracev(("inflate: stored block%s\n",
state->last ? " (last)" : ""));
state->mode = STORED;
break;
case 1: /* fixed block */
fixedtables(state);
Tracev((stderr, "inflate: fixed codes block%s\n",
Tracev(("inflate: fixed codes block%s\n",
state->last ? " (last)" : ""));
state->mode = LEN; /* decode codes */
break;
case 2: /* dynamic block */
Tracev((stderr, "inflate: dynamic codes block%s\n",
Tracev(("inflate: dynamic codes block%s\n",
state->last ? " (last)" : ""));
state->mode = TABLE;
break;
@ -309,7 +310,7 @@ int inflateBack(z_streamp strm, in_func in, void *in_desc, out_func out,
break;
}
state->length = (unsigned)hold & 0xffff;
Tracev((stderr, "inflate: stored length %u\n", state->length));
Tracev(("inflate: stored length %u\n", state->length));
INITBITS();
/* copy stored block from input to output */
@ -326,7 +327,7 @@ int inflateBack(z_streamp strm, in_func in, void *in_desc, out_func out,
put += copy;
state->length -= copy;
}
Tracev((stderr, "inflate: stored end\n"));
Tracev(("inflate: stored end\n"));
state->mode = TYPE;
break;
@ -346,7 +347,7 @@ int inflateBack(z_streamp strm, in_func in, void *in_desc, out_func out,
break;
}
#endif
Tracev((stderr, "inflate: table sizes ok\n"));
Tracev(("inflate: table sizes ok\n"));
/* get code length code lengths (not a typo) */
state->have = 0;
@ -366,7 +367,7 @@ int inflateBack(z_streamp strm, in_func in, void *in_desc, out_func out,
state->mode = BAD;
break;
}
Tracev((stderr, "inflate: code lengths ok\n"));
Tracev(("inflate: code lengths ok\n"));
/* get length and distance code code lengths */
state->have = 0;
@ -445,7 +446,7 @@ int inflateBack(z_streamp strm, in_func in, void *in_desc, out_func out,
state->mode = BAD;
break;
}
Tracev((stderr, "inflate: codes ok\n"));
Tracev(("inflate: codes ok\n"));
state->mode = LEN;
case LEN:
@ -479,8 +480,7 @@ int inflateBack(z_streamp strm, in_func in, void *in_desc, out_func out,
/* process literal */
if (here.op == 0) {
Tracevv((stderr,
here.val >= 0x20 && here.val < 0x7f
Tracevv((here.val >= 0x20 && here.val < 0x7f
? "inflate: literal '%c'\n"
: "inflate: literal 0x%02x\n",
here.val));
@ -493,7 +493,7 @@ int inflateBack(z_streamp strm, in_func in, void *in_desc, out_func out,
/* process end of block */
if (here.op & 32) {
Tracevv((stderr, "inflate: end of block\n"));
Tracevv(("inflate: end of block\n"));
state->mode = TYPE;
break;
}
@ -512,7 +512,7 @@ int inflateBack(z_streamp strm, in_func in, void *in_desc, out_func out,
state->length += BITS(state->extra);
DROPBITS(state->extra);
}
Tracevv((stderr, "inflate: length %u\n", state->length));
Tracevv(("inflate: length %u\n", state->length));
/* get distance code */
for (;;) {
@ -551,7 +551,7 @@ int inflateBack(z_streamp strm, in_func in, void *in_desc, out_func out,
state->mode = BAD;
break;
}
Tracevv((stderr, "inflate: distance %u\n", state->offset));
Tracevv(("inflate: distance %u\n", state->offset));
/* copy match from window to output */
do {
@ -574,25 +574,27 @@ int inflateBack(z_streamp strm, in_func in, void *in_desc, out_func out,
break;
case DONE:
/* inflate stream terminated properly -- write leftover output */
/* inflate stream terminated properly */
ret = Z_STREAM_END;
if (left < state->wsize) {
if (out(out_desc, state->window, state->wsize - left))
ret = Z_BUF_ERROR;
}
goto inf_leave;
case BAD:
ret = Z_DATA_ERROR;
goto inf_leave;
default: /* can't happen, but makes compilers happy */
default:
/* can't happen, but makes compilers happy */
ret = Z_STREAM_ERROR;
goto inf_leave;
}
/* Return unused input */
/* Write leftover output and return unused input */
inf_leave:
if (left < state->wsize) {
if (out(out_desc, state->window, state->wsize - left) &&
ret == Z_STREAM_END)
ret = Z_BUF_ERROR;
}
strm->next_in = next;
strm->avail_in = have;
return ret;
@ -603,6 +605,6 @@ int inflateBackEnd(z_streamp strm) {
return Z_STREAM_ERROR;
ZFREE(strm, strm->state);
strm->state = Z_NULL;
Tracev((stderr, "inflate: end\n"));
Tracev(("inflate: end\n"));
return Z_OK;
}

View file

@ -122,8 +122,7 @@ void inflate_fast(z_streamp strm, unsigned start) {
bits -= op;
op = (unsigned)(here.op);
if (op == 0) { /* literal */
Tracevv((stderr,
here.val >= 0x20 && here.val < 0x7f
Tracevv((here.val >= 0x20 && here.val < 0x7f
? "inflate: literal '%c'\n"
: "inflate: literal 0x%02x\n",
here.val));
@ -140,7 +139,7 @@ void inflate_fast(z_streamp strm, unsigned start) {
hold >>= op;
bits -= op;
}
Tracevv((stderr, "inflate: length %u\n", len));
Tracevv(("inflate: length %u\n", len));
if (bits < 15) {
hold += (unsigned long)(*in++) << bits;
bits += 8;
@ -174,7 +173,7 @@ void inflate_fast(z_streamp strm, unsigned start) {
#endif
hold >>= op;
bits -= op;
Tracevv((stderr, "inflate: distance %u\n", dist));
Tracevv(("inflate: distance %u\n", dist));
op = (unsigned)(out - beg); /* max distance in output */
if (dist > op) { /* see if copy from window */
op = dist - op; /* distance back in window */
@ -277,7 +276,7 @@ void inflate_fast(z_streamp strm, unsigned start) {
here = lcode[here.val + (hold & ((1U << op) - 1))];
goto dolen;
} else if (op & 32) { /* end-of-block */
Tracevv((stderr, "inflate: end of block\n"));
Tracevv(("inflate: end of block\n"));
state->mode = TYPE;
break;
} else {

View file

@ -147,8 +147,7 @@ void inflate_fast_chunk(z_streamp strm, unsigned start) {
bits -= op;
op = (unsigned)(here.op);
if (op == 0) { /* literal */
Tracevv((stderr,
here.val >= 0x20 && here.val < 0x7f
Tracevv((here.val >= 0x20 && here.val < 0x7f
? "inflate: literal '%c'\n"
: "inflate: literal 0x%02x\n",
here.val));
@ -166,7 +165,7 @@ void inflate_fast_chunk(z_streamp strm, unsigned start) {
hold >>= op;
bits -= op;
}
Tracevv((stderr, "inflate: length %u\n", len));
Tracevv(("inflate: length %u\n", len));
if (bits < 15) {
hold |= READ64LE(in) << bits;
in += 6;
@ -196,7 +195,7 @@ void inflate_fast_chunk(z_streamp strm, unsigned start) {
#endif
hold >>= op;
bits -= op;
Tracevv((stderr, "inflate: distance %u\n", dist));
Tracevv(("inflate: distance %u\n", dist));
op = (unsigned)(out - beg); /* max distance in output */
if (dist > op) { /* see if copy from window */
op = dist - op; /* distance back in window */
@ -283,7 +282,7 @@ void inflate_fast_chunk(z_streamp strm, unsigned start) {
here = lcode[here.val + (hold & ((1U << op) - 1))];
goto dolen;
} else if (op & 32) { /* end-of-block */
Tracevv((stderr, "inflate: end of block\n"));
Tracevv(("inflate: end of block\n"));
state->mode = TYPE;
break;
} else {

View file

@ -147,7 +147,7 @@ int inflateResetKeep(z_streamp strm) {
state->lencode = state->distcode = state->next = state->codes;
state->sane = 1;
state->back = -1;
Tracev((stderr, "inflate: reset\n"));
Tracev(("inflate: reset\n"));
return Z_OK;
}
@ -207,7 +207,7 @@ int inflateInit2(z_streamp strm, int windowBits) {
}
state = (struct InflateState *)ZALLOC(strm, 1, sizeof(struct InflateState));
if (state == Z_NULL) return Z_MEM_ERROR;
Tracev((stderr, "inflate: allocated\n"));
Tracev(("inflate: allocated\n"));
strm->state = (struct DeflateState *)state;
state->strm = strm;
state->window = Z_NULL;
@ -661,7 +661,7 @@ int inflate(z_streamp strm, int flush) {
break;
}
state->dmax = 1U << len;
Tracev((stderr, "inflate: zlib header ok\n"));
Tracev(("inflate: zlib header ok\n"));
strm->adler = state->check = adler32(0L, Z_NULL, 0);
state->mode = hold & 0x200 ? DICTID : TYPE;
INITBITS();
@ -718,8 +718,9 @@ int inflate(z_streamp strm, int flush) {
copy = state->length;
if (copy > have) copy = have;
if (copy) {
if (state->head != Z_NULL && state->head->extra != Z_NULL) {
len = state->head->extra_len - state->length;
if (state->head != Z_NULL && state->head->extra != Z_NULL &&
(len = state->head->extra_len - state->length) <
state->head->extra_max) {
memcpy(state->head->extra + len, next,
len + copy > state->head->extra_max
? state->head->extra_max - len
@ -735,6 +736,7 @@ int inflate(z_streamp strm, int flush) {
}
state->length = 0;
state->mode = NAME;
/* fallthrough */
case NAME:
if (state->flags & 0x0800) {
if (have == 0) goto inf_leave;
@ -815,13 +817,13 @@ int inflate(z_streamp strm, int flush) {
DROPBITS(1);
switch (BITS(2)) {
case 0: /* stored block */
Tracev((stderr, "inflate: stored block%s\n",
Tracev(("inflate: stored block%s\n",
state->last ? " (last)" : ""));
state->mode = STORED;
break;
case 1: /* fixed block */
fixedtables(state);
Tracev((stderr, "inflate: fixed codes block%s\n",
Tracev(("inflate: fixed codes block%s\n",
state->last ? " (last)" : ""));
state->mode = LEN_; /* decode codes */
if (flush == Z_TREES) {
@ -830,7 +832,7 @@ int inflate(z_streamp strm, int flush) {
}
break;
case 2: /* dynamic block */
Tracev((stderr, "inflate: dynamic codes block%s\n",
Tracev(("inflate: dynamic codes block%s\n",
state->last ? " (last)" : ""));
state->mode = TABLE;
break;
@ -849,7 +851,7 @@ int inflate(z_streamp strm, int flush) {
break;
}
state->length = (unsigned)hold & 0xffff;
Tracev((stderr, "inflate: stored length %u\n", state->length));
Tracev(("inflate: stored length %u\n", state->length));
INITBITS();
state->mode = COPY_;
if (flush == Z_TREES) goto inf_leave;
@ -869,7 +871,7 @@ int inflate(z_streamp strm, int flush) {
state->length -= copy;
break;
}
Tracev((stderr, "inflate: stored end\n"));
Tracev(("inflate: stored end\n"));
state->mode = TYPE;
break;
case TABLE:
@ -887,7 +889,7 @@ int inflate(z_streamp strm, int flush) {
break;
}
#endif
Tracev((stderr, "inflate: table sizes ok\n"));
Tracev(("inflate: table sizes ok\n"));
state->have = 0;
state->mode = LENLENS;
case LENLENS:
@ -910,7 +912,7 @@ int inflate(z_streamp strm, int flush) {
state->mode = BAD;
break;
}
Tracev((stderr, "inflate: code lengths ok\n"));
Tracev(("inflate: code lengths ok\n"));
state->have = 0;
state->mode = CODELENS;
case CODELENS:
@ -989,7 +991,7 @@ int inflate(z_streamp strm, int flush) {
state->mode = BAD;
break;
}
Tracev((stderr, "inflate: codes ok\n"));
Tracev(("inflate: codes ok\n"));
state->mode = LEN_;
if (flush == Z_TREES) goto inf_leave;
case LEN_:
@ -1023,8 +1025,7 @@ int inflate(z_streamp strm, int flush) {
state->back += here.bits;
state->length = (unsigned)here.val;
if ((int)(here.op) == 0) {
Tracevv((stderr,
here.val >= 0x20 && here.val < 0x7f
Tracevv((here.val >= 0x20 && here.val < 0x7f
? "inflate: literal '%c'\n"
: "inflate: literal 0x%02x\n",
here.val));
@ -1032,7 +1033,7 @@ int inflate(z_streamp strm, int flush) {
break;
}
if (here.op & 32) {
Tracevv((stderr, "inflate: end of block\n"));
Tracevv(("inflate: end of block\n"));
state->back = -1;
state->mode = TYPE;
break;
@ -1051,7 +1052,7 @@ int inflate(z_streamp strm, int flush) {
DROPBITS(state->extra);
state->back += state->extra;
}
Tracevv((stderr, "inflate: length %u\n", state->length));
Tracevv(("inflate: length %u\n", state->length));
state->was = state->length;
state->mode = DIST;
case DIST:
@ -1095,7 +1096,7 @@ int inflate(z_streamp strm, int flush) {
break;
}
#endif
Tracevv((stderr, "inflate: distance %u\n", state->offset));
Tracevv(("inflate: distance %u\n", state->offset));
state->mode = MATCH;
case MATCH:
if (left == 0) goto inf_leave;
@ -1109,7 +1110,7 @@ int inflate(z_streamp strm, int flush) {
break;
}
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
Trace((stderr, "inflate.c too far\n"));
Trace(("inflate.c too far\n"));
copy -= state->whave;
if (copy > state->length) copy = state->length;
if (copy > left) copy = left;
@ -1166,7 +1167,7 @@ int inflate(z_streamp strm, int flush) {
break;
}
INITBITS();
Tracev((stderr, "inflate: check matches trailer\n"));
Tracev(("inflate: check matches trailer\n"));
}
#ifdef GUNZIP
state->mode = LENGTH;
@ -1179,7 +1180,7 @@ int inflate(z_streamp strm, int flush) {
break;
}
INITBITS();
Tracev((stderr, "inflate: length matches trailer\n"));
Tracev(("inflate: length matches trailer\n"));
}
#endif
state->mode = DONE;
@ -1245,7 +1246,7 @@ int inflateEnd(z_streamp strm) {
if (state->window != Z_NULL) ZFREE(strm, state->window);
ZFREE(strm, strm->state);
strm->state = Z_NULL;
Tracev((stderr, "inflate: end\n"));
Tracev(("inflate: end\n"));
return Z_OK;
}
@ -1293,7 +1294,7 @@ int inflateSetDictionary(z_streamp strm, const Bytef *dictionary,
return Z_MEM_ERROR;
}
state->havedict = 1;
Tracev((stderr, "inflate: dictionary set\n"));
Tracev(("inflate: dictionary set\n"));
return Z_OK;
}

View file

@ -27,7 +27,7 @@ static const uint16_t kZlibDeflateLbase[31] = {
/* Length codes 257..285 extra */
static const uint16_t kZlibDeflateLext[31] = {
16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 77, 202};
19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 76, 202};
/* Distance codes 0..29 base */
static const uint16_t kZlibDeflateDbase[32] = {

View file

@ -5,6 +5,7 @@
Use of this source code is governed by the BSD-style licenses that can
be found in the third_party/zlib/LICENSE file.
*/
#include "libc/intrin/kprintf.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "third_party/zlib/deflate.internal.h"
@ -128,10 +129,10 @@ static void gen_trees_header(void);
/* Send a code of the given tree. c and tree must not have side effects */
#else /* !ZLIB_DEBUG */
#define send_code(s, c, tree) \
{ \
if (z_verbose > 2) fprintf(stderr, "\ncd %3d ", (c)); \
send_bits(s, tree[c].Code, tree[c].Len); \
#define send_code(s, c, tree) \
{ \
if (z_verbose > 2) kprintf("\ncd %3d ", (c)); \
send_bits(s, tree[c].Code, tree[c].Len); \
}
#endif
@ -151,7 +152,7 @@ static void gen_trees_header(void);
*/
#ifdef ZLIB_DEBUG
static void send_bits(struct DeflateState *s, int value, int length) {
Tracevv((stderr, " l %2d v %4x ", length, value));
Tracevv((" l %2d v %4x ", length, value));
Assert(length > 0 && length <= 15, "invalid length");
s->bits_sent += (uint64_t)length;
/* If not enough room in bi_buf, use (valid) bits from bi_buf and
@ -466,7 +467,7 @@ static void gen_bitlen(struct DeflateState *s, tree_desc *desc) {
}
if (overflow == 0) return;
Tracev((stderr, "\nbit length overflow\n"));
Tracev(("\nbit length overflow\n"));
/* This happens for example on obj2 and pic of the Calgary corpus */
/* Find the first bit length which could increase: */
@ -493,7 +494,7 @@ static void gen_bitlen(struct DeflateState *s, tree_desc *desc) {
m = s->heap[--h];
if (m > max_code) continue;
if ((unsigned)tree[m].Len != (unsigned)bits) {
Tracev((stderr, "code %d bits %d->%d\n", m, tree[m].Len, bits));
Tracev(("code %d bits %d->%d\n", m, tree[m].Len, bits));
s->opt_len += ((uint64_t)bits - tree[m].Len) * tree[m].Freq;
tree[m].Len = (uint16_t)bits;
}
@ -531,7 +532,7 @@ static void gen_codes(ct_data *tree, int max_code, uint16_t *bl_count) {
*/
Assert(code + bl_count[MAX_BITS] - 1 == (1 << MAX_BITS) - 1,
"inconsistent bit counts");
Tracev((stderr, "\ngen_codes: max_code %d ", max_code));
Tracev(("\ngen_codes: max_code %d ", max_code));
for (n = 0; n <= max_code; n++) {
int len = tree[n].Len;
@ -540,8 +541,8 @@ static void gen_codes(ct_data *tree, int max_code, uint16_t *bl_count) {
tree[n].Code = (uint16_t)bi_reverse(next_code[len]++, len);
Tracecv(tree != kZlibStaticLtree,
(stderr, "\nn %3d %c l %2d c %4x (%x) ", n, (isgraph(n) ? n : ' '),
len, tree[n].Code, next_code[len] - 1));
("\nn %3d %c l %2d c %4x (%x) ", n, (isgraph(n) ? n : ' '), len,
tree[n].Code, next_code[len] - 1));
}
}
@ -614,8 +615,8 @@ static void build_tree(struct DeflateState *s, tree_desc *desc) {
tree[n].Dad = tree[m].Dad = (uint16_t)node;
#ifdef DUMP_BL_TREE
if (tree == s->bl_tree) {
fprintf(stderr, "\nnode %d(%d), sons %d(%d) %d(%d)", node,
tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
kprintf("\nnode %d(%d), sons %d(%d) %d(%d)", node, tree[node].Freq, n,
tree[n].Freq, m, tree[m].Freq);
}
#endif
/* and insert the new node in the heap */
@ -765,7 +766,7 @@ static int build_bl_tree(struct DeflateState *s) {
}
/* Update opt_len to include the bit length tree and counts */
s->opt_len += 3 * ((uint64_t)max_blindex + 1) + 5 + 5 + 4;
Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld", s->opt_len, s->static_len));
Tracev(("\ndyn trees: dyn %ld, stat %ld", s->opt_len, s->static_len));
return max_blindex;
}
@ -782,21 +783,21 @@ static void send_all_trees(struct DeflateState *s, int lcodes, int dcodes,
Assert(lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
Assert(lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
"too many codes");
Tracev((stderr, "\nbl counts: "));
Tracev(("\nbl counts: "));
send_bits(s, lcodes - 257, 5); /* not +255 as stated in appnote.txt */
send_bits(s, dcodes - 1, 5);
send_bits(s, blcodes - 4, 4); /* not -3 as stated in appnote.txt */
for (rank = 0; rank < blcodes; rank++) {
Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
Tracev(("\nbl code %2d ", bl_order[rank]));
send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
}
Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
Tracev(("\nbl tree: sent %ld", s->bits_sent));
send_tree(s, (ct_data *)s->dyn_ltree, lcodes - 1); /* literal tree */
Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
Tracev(("\nlit tree: sent %ld", s->bits_sent));
send_tree(s, (ct_data *)s->dyn_dtree, dcodes - 1); /* distance tree */
Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
Tracev(("\ndist tree: sent %ld", s->bits_sent));
}
/**
@ -860,12 +861,10 @@ void _tr_flush_block(struct DeflateState *s, charf *buf, uint64_t stored_len,
/* Construct the literal and distance trees */
build_tree(s, (tree_desc *)(&(s->l_desc)));
Tracev(
(stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len, s->static_len));
Tracev(("\nlit data: dyn %ld, stat %ld", s->opt_len, s->static_len));
build_tree(s, (tree_desc *)(&(s->d_desc)));
Tracev(
(stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len, s->static_len));
Tracev(("\ndist data: dyn %ld, stat %ld", s->opt_len, s->static_len));
/* At this point, opt_len and static_len are the total bit lengths of
* the compressed block data, excluding the tree representations.
*/
@ -879,7 +878,7 @@ void _tr_flush_block(struct DeflateState *s, charf *buf, uint64_t stored_len,
opt_lenb = (s->opt_len + 3 + 7) >> 3;
static_lenb = (s->static_len + 3 + 7) >> 3;
Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ", opt_lenb,
Tracev(("\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ", opt_lenb,
s->opt_len, static_lenb, s->static_len, stored_len,
s->sym_next / 3));
@ -937,7 +936,7 @@ void _tr_flush_block(struct DeflateState *s, charf *buf, uint64_t stored_len,
s->compressed_len += 7; /* align on byte boundary */
#endif
}
Tracev((stderr, "\ncomprlen %lu(%lu) ", s->compressed_len >> 3,
Tracev(("\ncomprlen %lu(%lu) ", s->compressed_len >> 3,
s->compressed_len - 7 * last));
}
@ -987,7 +986,7 @@ static void compress_block(struct DeflateState *s, const ct_data *ltree,
lc = s->sym_buf[sx++];
if (dist == 0) {
send_code(s, lc, ltree); /* send a literal byte */
Tracecv(isgraph(lc), (stderr, " '%c' ", lc));
Tracecv(isgraph(lc), (" '%c' ", lc));
} else {
/* Here, lc is the match length - MIN_MATCH */
code = kZlibLengthCode[lc];

View file

@ -7,6 +7,10 @@
#define DEF_MEM_LEVEL 8
#define MAX_WBITS 15 /* 32K LZ77 window */
#ifdef MODE_DBG
#define ZLIB_DEBUG
#endif
#if !(__ASSEMBLER__ + __LINKER__ + 0)
typedef unsigned char Byte;

View file

@ -5,6 +5,9 @@
Use of this source code is governed by the BSD-style licenses that can
be found in the third_party/zlib/LICENSE file.
*/
#include "libc/intrin/kprintf.h"
#include "libc/intrin/weaken.h"
#include "libc/log/log.h"
#include "libc/mem/mem.h"
#include "third_party/zlib/zutil.internal.h"
@ -117,9 +120,10 @@ uLong zlibCompileFlags() {
#endif
int z_verbose hidden = verbose;
void z_error(char *m) {
fprintf(stderr, "%s\n", m);
exit(1);
void z_error(const char *file, int line, char *m) {
kprintf("%s:%d: zlib panic: %s\n", file, line, m);
if (weaken(__die)) weaken(__die)();
_Exit(1);
}
#endif

View file

@ -1,5 +1,6 @@
#ifndef ZUTIL_H
#define ZUTIL_H
#include "libc/intrin/kprintf.h"
#include "third_party/zlib/zlib.h"
/* default windowBits for decompression. MAX_WBITS is for compression only */
@ -46,32 +47,33 @@ extern const char *const z_errmsg[10] hidden; /* indexed by 2-zlib_error */
/* Diagnostic functions */
#ifdef ZLIB_DEBUG
#include "libc/stdio/stdio.h"
extern int z_verbose hidden;
extern void z_error(char *m) hidden;
#define Assert(cond, msg) \
{ \
if (!(cond)) z_error(msg); \
extern void z_error(const char *, int, char *) hidden;
#define Assert(cond, msg) \
{ \
if (!(cond)) { \
z_error(__FILE__, __LINE__, msg); \
} \
}
#define Trace(x) \
{ \
if (z_verbose >= 0) fprintf x; \
if (z_verbose >= 0) kprintf x; \
}
#define Tracev(x) \
{ \
if (z_verbose > 0) fprintf x; \
if (z_verbose > 0) kprintf x; \
}
#define Tracevv(x) \
{ \
if (z_verbose > 1) fprintf x; \
if (z_verbose > 1) kprintf x; \
}
#define Tracec(c, x) \
{ \
if (z_verbose > 0 && (c)) fprintf x; \
if (z_verbose > 0 && (c)) kprintf x; \
}
#define Tracecv(c, x) \
{ \
if (z_verbose > 1 && (c)) fprintf x; \
if (z_verbose > 1 && (c)) kprintf x; \
}
#else
#define Assert(cond, msg)