Use tighter bound on max number of nodes

This commit is contained in:
Eric Biggers 2016-05-23 19:55:06 -05:00
parent 7bfba00a19
commit 4d81654402

View File

@ -52,7 +52,8 @@
/* /*
* The minimum and maximum block lengths, in bytes of source data, which the * The minimum and maximum block lengths, in bytes of source data, which the
* parsing algorithms may choose. * parsing algorithms may choose. Caveat: due to implementation details, the
* actual maximum will be slightly higher than the number defined below.
*/ */
#define MIN_BLOCK_LENGTH 10000 #define MIN_BLOCK_LENGTH 10000
#define MAX_BLOCK_LENGTH 300000 #define MAX_BLOCK_LENGTH 300000
@ -396,10 +397,19 @@ struct deflate_compressor {
MAX_MATCHES_PER_POS + MAX_MATCHES_PER_POS +
DEFLATE_MAX_MATCH_LEN - 1]; DEFLATE_MAX_MATCH_LEN - 1];
/* Array of structures, one per position, for running /*
* the minimum-cost path algorithm. */ * Array of nodes, one per position, for running the
struct deflate_optimum_node optimum[MAX_BLOCK_LENGTH + * minimum-cost path algorithm.
3 * DEFLATE_MAX_MATCH_LEN]; *
* This array must be large enough to accommodate the
* worst-case number of nodes, which occurs if we find a
* match of length DEFLATE_MAX_MATCH_LEN at position
* MAX_BLOCK_LENGTH - 1, producing a block of length
* MAX_BLOCK_LENGTH - 1 + DEFLATE_MAX_MATCH_LEN. Add
* one for the end-of-block node.
*/
struct deflate_optimum_node optimum[MAX_BLOCK_LENGTH - 1 +
DEFLATE_MAX_MATCH_LEN + 1];
/* The current cost model being used. */ /* The current cost model being used. */
struct deflate_costs costs; struct deflate_costs costs;
@ -2301,12 +2311,13 @@ deflate_optimize_and_write_block(struct deflate_compressor *c,
{ {
struct deflate_optimum_node * const end_node = c->optimum + block_length; struct deflate_optimum_node * const end_node = c->optimum + block_length;
unsigned num_passes_remaining = c->num_optim_passes; unsigned num_passes_remaining = c->num_optim_passes;
unsigned i; u32 i;
/* Force the block to really end at 'end_node', even if some matches /* Force the block to really end at 'end_node', even if some matches
* extend beyond it. */ * extend beyond it. */
for (i = 1; i < DEFLATE_MAX_MATCH_LEN; i++) for (i = block_length; i <= MIN(block_length - 1 + DEFLATE_MAX_MATCH_LEN,
end_node[i].cost_to_end = 0x80000000; ARRAY_LEN(c->optimum) - 1); i++)
c->optimum[i].cost_to_end = 0x80000000;
do { do {
/* /*