|
|
9142b8 |
From 8ef5456338a947944cc03b95c22c837af5884ddc Mon Sep 17 00:00:00 2001
|
|
|
9142b8 |
From: Eugene Syromyatnikov <evgsyr@gmail.com>
|
|
|
9142b8 |
Date: Wed, 18 Aug 2021 21:51:22 +0200
|
|
|
9142b8 |
Subject: [PATCH 149/150] trie: use BIT* and MASK* macros
|
|
|
9142b8 |
|
|
|
9142b8 |
This makes reading the code a bit easier. It also solves some issues
|
|
|
9142b8 |
where there is a hypothertical possibility of having bit shifts of size
|
|
|
9142b8 |
64, by virtue of using the *_SAFE macros (that should silence some
|
|
|
9142b8 |
reported "left shifting by more than 63 bits has undefined behavior"
|
|
|
9142b8 |
covscan issues).
|
|
|
9142b8 |
|
|
|
9142b8 |
* src/trie.c (trie_create): Use BIT32, MASK64.
|
|
|
9142b8 |
(trie_create_data_block): Use BIT32, change iterator variable type
|
|
|
9142b8 |
to size_t.
|
|
|
9142b8 |
(trie_get_node): Use BIT64, MASK64.
|
|
|
9142b8 |
(trie_data_block_calc_pos): Use BIT32, MASK64, MASK64_SAFE.
|
|
|
9142b8 |
(trie_iterate_keys_node): Use BIT64, MASK64, MASK64_SAFE.
|
|
|
9142b8 |
(trie_free_node): Use BIT64.
|
|
|
9142b8 |
---
|
|
|
9142b8 |
src/trie.c | 34 +++++++++++++++++-----------------
|
|
|
9142b8 |
1 file changed, 17 insertions(+), 17 deletions(-)
|
|
|
9142b8 |
|
|
|
9142b8 |
diff --git a/src/trie.c b/src/trie.c
|
|
|
9142b8 |
index 586ff25..0a231e4 100644
|
|
|
9142b8 |
--- a/src/trie.c
|
|
|
9142b8 |
+++ b/src/trie.c
|
|
|
9142b8 |
@@ -15,6 +15,7 @@
|
|
|
9142b8 |
#include <stdio.h>
|
|
|
9142b8 |
|
|
|
9142b8 |
#include "trie.h"
|
|
|
9142b8 |
+#include "macros.h"
|
|
|
9142b8 |
#include "xmalloc.h"
|
|
|
9142b8 |
|
|
|
9142b8 |
static const uint8_t ptr_sz_lg = (sizeof(void *) == 8 ? 6 : 5);
|
|
|
9142b8 |
@@ -87,7 +88,7 @@ trie_create(uint8_t key_size, uint8_t item_size_lg, uint8_t node_key_bits,
|
|
|
9142b8 |
/ t->node_key_bits;
|
|
|
9142b8 |
|
|
|
9142b8 |
if (item_size_lg != 6)
|
|
|
9142b8 |
- t->empty_value &= (((uint64_t) 1 << (1 << t->item_size_lg)) - 1);
|
|
|
9142b8 |
+ t->empty_value &= MASK64(BIT32(t->item_size_lg));
|
|
|
9142b8 |
|
|
|
9142b8 |
return t;
|
|
|
9142b8 |
}
|
|
|
9142b8 |
@@ -96,8 +97,8 @@ static void *
|
|
|
9142b8 |
trie_create_data_block(struct trie *t)
|
|
|
9142b8 |
{
|
|
|
9142b8 |
uint64_t fill_value = t->empty_value;
|
|
|
9142b8 |
- for (int i = 1; i < 1 << (6 - t->item_size_lg); i++) {
|
|
|
9142b8 |
- fill_value <<= (1 << t->item_size_lg);
|
|
|
9142b8 |
+ for (size_t i = 1; i < BIT32(6 - t->item_size_lg); i++) {
|
|
|
9142b8 |
+ fill_value <<= BIT32(t->item_size_lg);
|
|
|
9142b8 |
fill_value |= t->empty_value;
|
|
|
9142b8 |
}
|
|
|
9142b8 |
|
|
|
9142b8 |
@@ -105,7 +106,7 @@ trie_create_data_block(struct trie *t)
|
|
|
9142b8 |
if (sz < 6)
|
|
|
9142b8 |
sz = 6;
|
|
|
9142b8 |
|
|
|
9142b8 |
- size_t count = 1 << (sz - 6);
|
|
|
9142b8 |
+ size_t count = BIT32(sz - 6);
|
|
|
9142b8 |
uint64_t *data_block = xcalloc(count, 8);
|
|
|
9142b8 |
|
|
|
9142b8 |
for (size_t i = 0; i < count; i++)
|
|
|
9142b8 |
@@ -119,7 +120,7 @@ trie_get_node(struct trie *t, uint64_t key, bool auto_create)
|
|
|
9142b8 |
{
|
|
|
9142b8 |
void **cur_node = &(t->data);
|
|
|
9142b8 |
|
|
|
9142b8 |
- if (t->key_size < 64 && key > (uint64_t) 1 << t->key_size)
|
|
|
9142b8 |
+ if (t->key_size < 64 && key > MASK64(t->key_size))
|
|
|
9142b8 |
return NULL;
|
|
|
9142b8 |
|
|
|
9142b8 |
for (uint8_t cur_depth = 0; cur_depth <= t->max_depth; cur_depth++) {
|
|
|
9142b8 |
@@ -133,13 +134,13 @@ trie_get_node(struct trie *t, uint64_t key, bool auto_create)
|
|
|
9142b8 |
if (cur_depth == t->max_depth)
|
|
|
9142b8 |
*cur_node = trie_create_data_block(t);
|
|
|
9142b8 |
else
|
|
|
9142b8 |
- *cur_node = xcalloc(1 << sz, 1);
|
|
|
9142b8 |
+ *cur_node = xcalloc(BIT64(sz), 1);
|
|
|
9142b8 |
}
|
|
|
9142b8 |
|
|
|
9142b8 |
if (cur_depth == t->max_depth)
|
|
|
9142b8 |
break;
|
|
|
9142b8 |
|
|
|
9142b8 |
- size_t pos = (key >> offs) & ((1 << (sz - ptr_sz_lg)) - 1);
|
|
|
9142b8 |
+ size_t pos = (key >> offs) & MASK64(sz - ptr_sz_lg);
|
|
|
9142b8 |
cur_node = (((void **) (*cur_node)) + pos);
|
|
|
9142b8 |
}
|
|
|
9142b8 |
|
|
|
9142b8 |
@@ -152,7 +153,7 @@ trie_data_block_calc_pos(struct trie *t, uint64_t key,
|
|
|
9142b8 |
{
|
|
|
9142b8 |
uint64_t key_mask;
|
|
|
9142b8 |
|
|
|
9142b8 |
- key_mask = (1 << t->data_block_key_bits) - 1;
|
|
|
9142b8 |
+ key_mask = MASK64(t->data_block_key_bits);
|
|
|
9142b8 |
*pos = (key & key_mask) >> (6 - t->item_size_lg);
|
|
|
9142b8 |
|
|
|
9142b8 |
if (t->item_size_lg == 6) {
|
|
|
9142b8 |
@@ -161,10 +162,10 @@ trie_data_block_calc_pos(struct trie *t, uint64_t key,
|
|
|
9142b8 |
return;
|
|
|
9142b8 |
}
|
|
|
9142b8 |
|
|
|
9142b8 |
- key_mask = (1 << (6 - t->item_size_lg)) - 1;
|
|
|
9142b8 |
- *offs = (key & key_mask) * (1 << t->item_size_lg);
|
|
|
9142b8 |
+ key_mask = MASK64(6 - t->item_size_lg);
|
|
|
9142b8 |
+ *offs = (key & key_mask) << t->item_size_lg;
|
|
|
9142b8 |
|
|
|
9142b8 |
- *mask = (((uint64_t) 1 << (1 << t->item_size_lg)) - 1) << *offs;
|
|
|
9142b8 |
+ *mask = MASK64_SAFE(BIT32(t->item_size_lg)) << *offs;
|
|
|
9142b8 |
}
|
|
|
9142b8 |
|
|
|
9142b8 |
bool
|
|
|
9142b8 |
@@ -211,7 +212,7 @@ trie_iterate_keys_node(struct trie *t,
|
|
|
9142b8 |
return 0;
|
|
|
9142b8 |
|
|
|
9142b8 |
if (t->key_size < 64) {
|
|
|
9142b8 |
- uint64_t key_max = ((uint64_t) 1 << t->key_size) - 1;
|
|
|
9142b8 |
+ uint64_t key_max = MASK64(t->key_size);
|
|
|
9142b8 |
if (end > key_max)
|
|
|
9142b8 |
end = key_max;
|
|
|
9142b8 |
}
|
|
|
9142b8 |
@@ -228,15 +229,14 @@ trie_iterate_keys_node(struct trie *t,
|
|
|
9142b8 |
t->key_size :
|
|
|
9142b8 |
trie_get_node_bit_offs(t, depth - 1);
|
|
|
9142b8 |
|
|
|
9142b8 |
- uint64_t first_key_in_node = start &
|
|
|
9142b8 |
- (uint64_t) -1 << parent_node_bit_off;
|
|
|
9142b8 |
+ uint64_t first_key_in_node = start & ~MASK64_SAFE(parent_node_bit_off);
|
|
|
9142b8 |
|
|
|
9142b8 |
uint8_t node_bit_off = trie_get_node_bit_offs(t, depth);
|
|
|
9142b8 |
uint8_t node_key_bits = parent_node_bit_off - node_bit_off;
|
|
|
9142b8 |
- uint64_t mask = ((uint64_t) 1 << (node_key_bits)) - 1;
|
|
|
9142b8 |
+ uint64_t mask = MASK64_SAFE(node_key_bits);
|
|
|
9142b8 |
uint64_t start_index = (start >> node_bit_off) & mask;
|
|
|
9142b8 |
uint64_t end_index = (end >> node_bit_off) & mask;
|
|
|
9142b8 |
- uint64_t child_key_count = (uint64_t) 1 << node_bit_off;
|
|
|
9142b8 |
+ uint64_t child_key_count = BIT64(node_bit_off);
|
|
|
9142b8 |
|
|
|
9142b8 |
uint64_t count = 0;
|
|
|
9142b8 |
|
|
|
9142b8 |
@@ -274,7 +274,7 @@ trie_free_node(struct trie *t, void *node, uint8_t depth)
|
|
|
9142b8 |
if (depth >= t->max_depth)
|
|
|
9142b8 |
goto free_node;
|
|
|
9142b8 |
|
|
|
9142b8 |
- size_t sz = 1 << (trie_get_node_size(t, depth) - ptr_sz_lg);
|
|
|
9142b8 |
+ size_t sz = BIT64(trie_get_node_size(t, depth) - ptr_sz_lg);
|
|
|
9142b8 |
for (size_t i = 0; i < sz; i++)
|
|
|
9142b8 |
trie_free_node(t, ((void **) node)[i], depth + 1);
|
|
|
9142b8 |
|
|
|
9142b8 |
--
|
|
|
9142b8 |
2.1.4
|
|
|
9142b8 |
|