]> wimlib.net Git - wimlib/blobdiff - src/lzx_compress.c
Get rid of matchfinder_common.h and manual memsets
[wimlib] / src / lzx_compress.c
index 1506b8842ca567533e1fafea263edaaac89fe8ff..5e1be485f3bc9ff0590db093edc8f09599c3f0b5 100644 (file)
 
 /*
  * LZX_BIT_COST is a scaling factor that represents the cost to output one bit.
- * THis makes it possible to consider fractional bit costs.
+ * This makes it possible to consider fractional bit costs.
  *
  * Note: this is only useful as a statistical trick for when the true costs are
  * unknown.  In reality, each token in LZX requires a whole number of bits to
 /*
  * LZX_HASH2_ORDER is the log base 2 of the number of entries in the hash table
  * for finding length 2 matches.  This can be as high as 16 (in which case the
- * hash function is trivial), but using a smaller hash table actually speeds up
+ * hash function is trivial), but using a smaller hash table speeds up
  * compression due to reduced cache pressure.
  */
 #define LZX_HASH2_ORDER                12
@@ -466,8 +466,7 @@ struct lzx_compressor {
                                                    LZX_MAX_MATCH_LEN - 1];
 
                        /* Hash table for finding length 2 matches  */
-                       pos_t hash2_tab[LZX_HASH2_LENGTH]
-                               _aligned_attribute(MATCHFINDER_ALIGNMENT);
+                       pos_t hash2_tab[LZX_HASH2_LENGTH];
 
                        /* Binary trees matchfinder (MUST BE LAST!!!)  */
                        struct bt_matchfinder bt_mf;
@@ -1121,8 +1120,8 @@ lzx_declare_explicit_offset_match(struct lzx_compressor *c, unsigned len, u32 of
                extra_bits = (offset + LZX_OFFSET_ADJUSTMENT) -
                             lzx_offset_slot_base[offset_slot];
 
-               BUILD_BUG_ON(LZX_MAINCODE_MAX_NUM_SYMBOLS > (1 << 10));
-               BUILD_BUG_ON(LZX_LENCODE_NUM_SYMBOLS > (1 << 8));
+               STATIC_ASSERT(LZX_MAINCODE_MAX_NUM_SYMBOLS <= (1 << 10));
+               STATIC_ASSERT(LZX_LENCODE_NUM_SYMBOLS <= (1 << 8));
                *(*next_chosen_item)++ = (struct lzx_item) {
                        .data = (u64)main_symbol |
                                ((u64)len_symbol << 10) |
@@ -1242,7 +1241,7 @@ lzx_find_min_cost_path(struct lzx_compressor * const restrict c,
         * it is no longer needed.  */
        struct lzx_lru_queue queues[512];
 
-       BUILD_BUG_ON(ARRAY_LEN(queues) < LZX_MAX_MATCH_LEN + 1);
+       STATIC_ASSERT(ARRAY_LEN(queues) >= LZX_MAX_MATCH_LEN + 1);
 #define QUEUE(in) (queues[(uintptr_t)(in) % ARRAY_LEN(queues)])
 
        /* Initially, the cost to reach each node is "infinity".  */
@@ -1294,7 +1293,7 @@ lzx_find_min_cost_path(struct lzx_compressor * const restrict c,
                        matchptr = in_next - lzx_lru_queue_R0(QUEUE(in_next));
                        if (load_u16_unaligned(matchptr) != load_u16_unaligned(in_next))
                                goto R0_done;
-                       BUILD_BUG_ON(LZX_MIN_MATCH_LEN != 2);
+                       STATIC_ASSERT(LZX_MIN_MATCH_LEN == 2);
                        do {
                                u32 cost = cur_node->cost +
                                           c->costs.match_cost[0][
@@ -1479,7 +1478,7 @@ lzx_set_default_costs(struct lzx_compressor *c, const u8 *block, u32 block_size)
        unsigned num_used_bytes;
 
        /* The costs below are hard coded to use a scaling factor of 16.  */
-       BUILD_BUG_ON(LZX_BIT_COST != 16);
+       STATIC_ASSERT(LZX_BIT_COST == 16);
 
        /*
         * Heuristics:
@@ -1600,7 +1599,7 @@ lzx_compress_near_optimal(struct lzx_compressor *c,
        struct lzx_lru_queue queue;
 
        bt_matchfinder_init(&c->bt_mf);
-       matchfinder_init(c->hash2_tab, LZX_HASH2_LENGTH);
+       memset(c->hash2_tab, 0, sizeof(c->hash2_tab));
        next_hash = bt_matchfinder_hash_3_bytes(in_next);
        lzx_lru_queue_init(&queue);
 
@@ -1624,13 +1623,11 @@ lzx_compress_near_optimal(struct lzx_compressor *c,
                                max_len = in_end - in_next;
                                nice_len = min(max_len, nice_len);
 
-                               /* This extra check is needed to ensure that
-                                * reading the next 3 bytes when looking for a
-                                * length 2 match is valid.  In addition, we
-                                * cannot allow ourselves to find a length 2
-                                * match of the very last two bytes with the
-                                * very first two bytes, since such a match has
-                                * an offset too large to be represented.  */
+                               /* This extra check is needed to ensure that we
+                                * never output a length 2 match of the very
+                                * last two bytes with the very first two bytes,
+                                * since such a match has an offset too large to
+                                * be represented.  */
                                if (unlikely(max_len < 3)) {
                                        in_next++;
                                        cache_ptr->length = 0;
@@ -1645,7 +1642,7 @@ lzx_compress_near_optimal(struct lzx_compressor *c,
                        hash2 = lz_hash_2_bytes(in_next, LZX_HASH2_ORDER);
                        cur_match = c->hash2_tab[hash2];
                        c->hash2_tab[hash2] = in_next - in_begin;
-                       if (matchfinder_node_valid(cur_match) &&
+                       if (cur_match != 0 &&
                            (LZX_HASH2_ORDER == 16 ||
                             load_u16_unaligned(&in_begin[cur_match]) ==
                             load_u16_unaligned(in_next)))
@@ -1736,7 +1733,7 @@ lzx_find_longest_repeat_offset_match(const u8 * const in_next,
                                     struct lzx_lru_queue queue,
                                     unsigned *rep_max_idx_ret)
 {
-       BUILD_BUG_ON(LZX_NUM_RECENT_OFFSETS != 3);
+       STATIC_ASSERT(LZX_NUM_RECENT_OFFSETS == 3);
        LZX_ASSERT(bytes_remaining >= 2);
 
        const unsigned max_len = min(bytes_remaining, LZX_MAX_MATCH_LEN);
@@ -2039,9 +2036,7 @@ lzx_create_compressor(size_t max_bufsize, unsigned compression_level,
        if (window_order == 0)
                return WIMLIB_ERR_INVALID_PARAM;
 
-       c = ALIGNED_MALLOC(lzx_get_compressor_size(max_bufsize,
-                                                  compression_level),
-                          MATCHFINDER_ALIGNMENT);
+       c = MALLOC(lzx_get_compressor_size(max_bufsize, compression_level));
        if (!c)
                goto oom0;
 
@@ -2116,14 +2111,14 @@ lzx_create_compressor(size_t max_bufsize, unsigned compression_level,
        return 0;
 
 oom1:
-       ALIGNED_FREE(c);
+       FREE(c);
 oom0:
        return WIMLIB_ERR_NOMEM;
 }
 
 static size_t
-lzx_compress(const void *in, size_t in_nbytes,
-            void *out, size_t out_nbytes_avail, void *_c)
+lzx_compress(const void *restrict in, size_t in_nbytes,
+            void *restrict out, size_t out_nbytes_avail, void *restrict _c)
 {
        struct lzx_compressor *c = _c;
        struct lzx_output_bitstream os;
@@ -2165,7 +2160,7 @@ lzx_free_compressor(void *_c)
 
        if (!c->destructive)
                FREE(c->in_buffer);
-       ALIGNED_FREE(c);
+       FREE(c);
 }
 
 const struct compressor_ops lzx_compressor_ops = {