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

@ -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;
}