From e6edad8f46e1edd4eaa51f291dc0b62fe314ee13 Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Fri, 19 Jan 2018 19:50:28 +0100 Subject: [PATCH] Clean up OptVar handling. --- src/compress_lzma.cpp | 18 +++++++++++------- src/conf.h | 27 +++++++++++++++------------ 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/compress_lzma.cpp b/src/compress_lzma.cpp index e33fc93d..fc540f17 100644 --- a/src/compress_lzma.cpp +++ b/src/compress_lzma.cpp @@ -43,8 +43,6 @@ void lzma_compress_config_t::reset() { - mem_clear(this, sizeof(*this)); - pos_bits.reset(); lit_pos_bits.reset(); lit_context_bits.reset(); @@ -111,11 +109,11 @@ static int prepare(lzma_compress_result_t *res, res->num_fast_bytes = 64; // 5 .. 273 res->match_finder_cycles = 0; // UPX overrides - res->pos_bits = lzma_compress_config_t::pos_bits_t::default_value_c; - res->lit_pos_bits = lzma_compress_config_t::lit_pos_bits_t::default_value_c; - res->lit_context_bits = lzma_compress_config_t::lit_context_bits_t::default_value_c; - res->dict_size = lzma_compress_config_t::dict_size_t::default_value_c; - res->num_fast_bytes = lzma_compress_config_t::num_fast_bytes_t::default_value_c; + res->pos_bits = lzma_compress_config_t::pos_bits_t::default_value; + res->lit_pos_bits = lzma_compress_config_t::lit_pos_bits_t::default_value; + res->lit_context_bits = lzma_compress_config_t::lit_context_bits_t::default_value; + res->dict_size = lzma_compress_config_t::dict_size_t::default_value; + res->num_fast_bytes = lzma_compress_config_t::num_fast_bytes_t::default_value; // method overrides if (method >= 0x100) { res->pos_bits = (method >> 16) & 15; @@ -201,6 +199,12 @@ static int prepare(lzma_compress_result_t *res, } } + lzma_compress_config_t::pos_bits_t::assertValue(res->pos_bits); + lzma_compress_config_t::lit_pos_bits_t::assertValue(res->lit_pos_bits); + lzma_compress_config_t::lit_context_bits_t::assertValue(res->lit_context_bits); + lzma_compress_config_t::dict_size_t::assertValue(res->dict_size); + lzma_compress_config_t::num_fast_bytes_t::assertValue(res->num_fast_bytes); + res->num_probs = 1846 + (768 << (res->lit_context_bits + res->lit_pos_bits)); //printf("\nlzma_compress config: %u %u %u %u %u\n", res->pos_bits, res->lit_pos_bits, res->lit_context_bits, res->dict_size, res->num_probs); return 0; diff --git a/src/conf.h b/src/conf.h index 2e1aaf47..fd21a326 100644 --- a/src/conf.h +++ b/src/conf.h @@ -480,24 +480,27 @@ struct upx_callback_t // compression - config_t **************************************************************************/ -template +template struct OptVar { typedef T value_type; - static const T default_value_c = default_value; - static const T min_value_c = min_value; - static const T max_value_c = max_value; + static const T default_value = default_value_; + static const T min_value = min_value_; + static const T max_value = max_value_; - void assertValue() const { + static void assertValue(const T &v) { // info: this generates annoying warnings "unsigned >= 0 is always true" - //assert(v >= min_value_c); - assert(v == min_value_c || v >= min_value_c + 1); - assert(v <= max_value_c); + //assert(v >= min_value); + assert(v == min_value || v >= min_value + 1); + assert(v <= max_value); + } + void assertValue() const { + assertValue(v); } OptVar() : v(default_value), is_set(0) { } - OptVar& operator= (const T other) { - v = other; is_set += 1; + OptVar& operator= (const T &other) { + v = other; is_set = 1; assertValue(); return *this; } @@ -513,10 +516,10 @@ struct OptVar // optional assignments template inline void oassign(OptVar &self, const OptVar &other) { - if (other.is_set) { self.v = other.v; self.is_set += 1; } + if (other.is_set) { self.v = other.v; self.is_set = 1; } } template -inline void oassign(unsigned &v, const OptVar &other) { +inline void oassign(T &v, const OptVar &other) { if (other.is_set) { v = other.v; } }