Skip to content

Commit

Permalink
Use fixed size array in good_for_rle
Browse files Browse the repository at this point in the history
OptimizeHuffmanForRle is only called from TryOptimizeHuffmanForRle
with fixed length values of ZOPFLI_NUM_LL and ZOPFLI_NUM_D. Rather
than fighting with compilers about warnings in the malloc call, use
a fixed size array the size of the larger of these.

Fixes google#150
  • Loading branch information
jibsen committed Jun 14, 2019
1 parent ef109dd commit 088a2be
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/zopfli/deflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ more efficiently. length contains the size of the histogram.
void OptimizeHuffmanForRle(int length, size_t* counts) {
int i, k, stride;
size_t symbol, sum, limit;
int* good_for_rle;
int good_for_rle[ZOPFLI_NUM_LL];

/* 1) We don't want to touch the trailing zeros. We may break the
rules of the format by adding more data in the distance codes. */
Expand All @@ -449,7 +449,7 @@ void OptimizeHuffmanForRle(int length, size_t* counts) {
}
/* 2) Let's mark all population counts that already can be encoded
with an rle code.*/
good_for_rle = (int*)malloc((unsigned)length * sizeof(int));
assert(length > 0 && length <= ZOPFLI_NUM_LL);
for (i = 0; i < length; ++i) good_for_rle[i] = 0;

/* Let's not spoil any of the existing good rle codes.
Expand Down Expand Up @@ -513,8 +513,6 @@ void OptimizeHuffmanForRle(int length, size_t* counts) {
sum += counts[i];
}
}

free(good_for_rle);
}

/*
Expand Down

0 comments on commit 088a2be

Please sign in to comment.