X-Git-Url: https://wimlib.net/git/?p=wimlib;a=blobdiff_plain;f=src%2Flzms-compress.c;h=2c9356d9b1a538006a7fc9dcae90404bc2c271a2;hp=6f17f5db1e3f56a82044eae0d86e4c814ea48522;hb=ee4fcdd5c4924803ae67a09fecac7d6b4b8ead6e;hpb=5c3a7d924621f76d54a1a45d9ecf376159a7eaf7 diff --git a/src/lzms-compress.c b/src/lzms-compress.c index 6f17f5db..2c9356d9 100644 --- a/src/lzms-compress.c +++ b/src/lzms-compress.c @@ -32,6 +32,7 @@ #endif #include "wimlib.h" +#include "wimlib/assert.h" #include "wimlib/compiler.h" #include "wimlib/compressor_ops.h" #include "wimlib/compress_common.h" @@ -123,7 +124,7 @@ struct lzms_range_encoder { * lzms_range_encoder_raw. */ struct lzms_range_encoder_raw *rc; - /* Bits recently encoded by this range encoder. This are used as in + /* Bits recently encoded by this range encoder. This is used as an * index into @prob_entries. */ u32 state; @@ -162,7 +163,7 @@ struct lzms_huffman_encoder { u8 lens[LZMS_MAX_NUM_SYMS]; /* The codeword of each symbol in the Huffman code. */ - u16 codewords[LZMS_MAX_NUM_SYMS]; + u32 codewords[LZMS_MAX_NUM_SYMS]; }; /* State of the LZMS compressor. */ @@ -533,7 +534,7 @@ lzms_encode_lz_match(struct lzms_compressor *ctx, u32 length, u32 offset) /* Main bit: 1 = a match, not a literal. */ lzms_range_encode_bit(&ctx->main_range_encoder, 1); - /* Match bit: 0 = a LZ match, not a delta match. */ + /* Match bit: 0 = an LZ match, not a delta match. */ lzms_range_encode_bit(&ctx->match_range_encoder, 0); /* Determine if the offset can be represented as a recent offset. */ @@ -665,17 +666,9 @@ lzms_do_init_rc_costs(void) static void lzms_init_rc_costs(void) { - static bool done = false; - static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; - - if (unlikely(!done)) { - pthread_mutex_lock(&mutex); - if (!done) { - lzms_do_init_rc_costs(); - done = true; - } - pthread_mutex_unlock(&mutex); - } + static pthread_once_t once = PTHREAD_ONCE_INIT; + + pthread_once(&once, lzms_do_init_rc_costs); } /* @@ -1179,6 +1172,9 @@ static const struct wimlib_lzms_compressor_params lzms_default = { .optim_array_length = 1024, }; +static bool +lzms_params_valid(const struct wimlib_compressor_params_header *); + static const struct wimlib_lzms_compressor_params * lzms_get_params(const struct wimlib_compressor_params_header *_params) { @@ -1188,6 +1184,8 @@ lzms_get_params(const struct wimlib_compressor_params_header *_params) if (params == NULL) params = &lzms_default; + LZMS_ASSERT(lzms_params_valid(¶ms->hdr)); + return params; } @@ -1221,7 +1219,7 @@ lzms_create_compressor(size_t max_block_size, if (!lz_sarray_init(&ctx->lz_sarray, max_block_size, params->min_match_length, - params->max_match_length, + min(params->max_match_length, LZ_SARRAY_LEN_MAX), params->max_search_depth, params->max_matches_per_pos)) goto oom;