32
32
REGPRM3 struct ebpt_node *ebim_lookup(struct eb_root *root, const void *x, unsigned int len);
33
33
REGPRM3 struct ebpt_node *ebim_insert(struct eb_root *root, struct ebpt_node *new, unsigned int len);
35
/* Find the first occurence of a key of <len> bytes in the tree <root>.
36
* If none can be found, return NULL.
35
/* Find the first occurence of a key of a least <len> bytes matching <x> in the
36
* tree <root>. The caller is responsible for ensuring that <len> will not exceed
37
* the common parts between the tree's keys and <x>. In case of multiple matches,
38
* the leftmost node is returned. This means that this function can be used to
39
* lookup string keys by prefix if all keys in the tree are zero-terminated. If
40
* no match is found, NULL is returned. Returns first node if <len> is zero.
38
42
static forceinline struct ebpt_node *
39
43
__ebim_lookup(struct eb_root *root, const void *x, unsigned int len)
41
45
struct ebpt_node *node;
46
50
troot = root->b[EB_LEFT];
47
51
if (unlikely(troot == NULL))
54
if (unlikely(len == 0))
52
if ((eb_gettag(troot) == EB_LEAF)) {
59
if (eb_gettag(troot) == EB_LEAF) {
53
60
node = container_of(eb_untag(troot, EB_LEAF),
54
61
struct ebpt_node, node.branches);
55
if (memcmp(node->key, x, len) == 0)
62
if (memcmp(node->key + pos, x, len) != 0)
60
67
node = container_of(eb_untag(troot, EB_NODE),
61
68
struct ebpt_node, node.branches);
66
73
* value, and we walk down left, or it's a different
67
74
* one and we don't have our key.
69
if (memcmp(node->key, x, len) != 0)
72
troot = node->node.branches.b[EB_LEFT];
73
while (eb_gettag(troot) != EB_LEAF)
74
troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
75
node = container_of(eb_untag(troot, EB_LEAF),
76
struct ebpt_node, node.branches);
80
/* OK, normal data node, let's walk down */
81
bit = equal_bits(x, node->key, bit, node_bit);
83
return NULL; /* no more common bits */
85
troot = node->node.branches.b[(((unsigned char*)x)[node_bit >> 3] >>
86
(~node_bit & 7)) & 1];
76
if (memcmp(node->key + pos, x, len) != 0)
82
/* OK, normal data node, let's walk down. We check if all full
83
* bytes are equal, and we start from the last one we did not
84
* completely check. We stop as soon as we reach the last byte,
85
* because we must decide to go left/right or abort.
87
node_bit = ~node_bit + (pos << 3) + 8; // = (pos<<3) + (7 - node_bit)
89
/* This surprizing construction gives better performance
90
* because gcc does not try to reorder the loop. Tested to
91
* be fine with 2.95 to 4.2.
94
if (*(unsigned char*)(node->key + pos++) ^ *(unsigned char*)(x++))
95
goto ret_null; /* more than one full byte is different */
97
goto walk_left; /* return first node if all bytes matched */
104
/* here we know that only the last byte differs, so node_bit < 8.
105
* We have 2 possibilities :
106
* - more than the last bit differs => return NULL
107
* - walk down on side = (x[pos] >> node_bit) & 1
109
side = *(unsigned char *)x >> node_bit;
110
if (((*(unsigned char*)(node->key + pos) >> node_bit) ^ side) > 1)
113
troot = node->node.branches.b[side];
116
troot = node->node.branches.b[EB_LEFT];
118
while (eb_gettag(troot) != EB_LEAF)
119
troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
120
node = container_of(eb_untag(troot, EB_LEAF),
121
struct ebpt_node, node.branches);
90
128
/* Insert ebpt_node <new> into subtree starting at node root <root>.