1 /*
2 * Elastic Binary Trees - macros and structures for operations on 32bit nodes.
3 * Version 6.0.6 with backports from v7-dev
4 * (C) 2002-2017 - Willy Tarreau <w@1wt.eu>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation, version 2.1
9 * exclusively.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #ifndef _EB32SCTREE_H
22 #define _EB32SCTREE_H
23
24 #include "ebtree.h"
25
26
27 /* Return the structure of type <type> whose member <member> points to <ptr> */
28 #define eb32sc_entry(ptr, type, member) container_of(ptr, type, member)
29
30 /* These types may sometimes already be defined */
31 #ifndef _EB32TREE_H
32 typedef unsigned int u32;
33 typedef signed int s32;
34 #endif
35
36 /* This structure carries a node, a leaf, a scope, and a key. It must start
37 * with the eb_node so that it can be cast into an eb_node. We could also
38 * have put some sort of transparent union here to reduce the indirection
39 * level, but the fact is, the end user is not meant to manipulate internals,
40 * so this is pointless.
41 * In case sizeof(void*)>=sizeof(long), we know there will be some padding after
42 * the leaf if it's unaligned. In this case we force the alignment on void* so
43 * that we prefer to have the padding before for more efficient accesses.
44 */
45 struct eb32sc_node {
46 struct eb_node node; /* the tree node, must be at the beginning */
47 MAYBE_ALIGN(sizeof(u32));
48 u32 key;
49 ALWAYS_ALIGN(sizeof(void*));
50 unsigned long node_s; /* visibility of this node's branches */
51 unsigned long leaf_s; /* visibility of this node's leaf */
52 } ALIGNED(sizeof(void*));
53
54 /*
55 * Exported functions and macros.
56 * Many of them are always inlined because they are extremely small, and
57 * are generally called at most once or twice in a program.
58 */
59
60 /*
61 * The following functions are not inlined by default. They are declared
62 * in eb32sctree.c, which simply relies on their inline version.
63 */
64 REGPRM2 struct eb32sc_node *eb32sc_lookup_ge(struct eb_root *root, u32 x, unsigned long scope);
65 REGPRM2 struct eb32sc_node *eb32sc_lookup_ge_or_first(struct eb_root *root, u32 x, unsigned long scope);
66 REGPRM2 struct eb32sc_node *eb32sc_insert(struct eb_root *root, struct eb32sc_node *new, unsigned long scope);
67 void eb32sc_delete(struct eb32sc_node *node);
68
69 /* Walks down left starting at root pointer <start>, and follow the leftmost
70 * branch whose scope matches <scope>. It either returns the node hosting the
71 * first leaf on that side, or NULL if no leaf is found. <start> may either be
72 * NULL or a branch pointer. The pointer to the leaf (or NULL) is returned.
73 */
eb32sc_walk_down_left(eb_troot_t * start,unsigned long scope)74 static inline struct eb32sc_node *eb32sc_walk_down_left(eb_troot_t *start, unsigned long scope)
75 {
76 struct eb_root *root;
77 struct eb_node *node;
78 struct eb32sc_node *eb32;
79
80 if (unlikely(!start))
81 return NULL;
82
83 while (1) {
84 if (eb_gettag(start) == EB_NODE) {
85 root = eb_untag(start, EB_NODE);
86 node = eb_root_to_node(root);
87 eb32 = container_of(node, struct eb32sc_node, node);
88 if (eb32->node_s & scope) {
89 start = node->branches.b[EB_LEFT];
90 continue;
91 }
92 start = node->node_p;
93 }
94 else {
95 root = eb_untag(start, EB_LEAF);
96 node = eb_root_to_node(root);
97 eb32 = container_of(node, struct eb32sc_node, node);
98 if (eb32->leaf_s & scope)
99 return eb32;
100 start = node->leaf_p;
101 }
102
103 /* here we're on a node that doesn't match the scope. We have
104 * to walk to the closest right location.
105 */
106 while (eb_gettag(start) != EB_LEFT)
107 /* Walking up from right branch, so we cannot be below root */
108 start = (eb_root_to_node(eb_untag(start, EB_RGHT)))->node_p;
109
110 /* Note that <start> cannot be NULL at this stage */
111 root = eb_untag(start, EB_LEFT);
112 start = root->b[EB_RGHT];
113 if (eb_clrtag(start) == NULL)
114 return NULL;
115 }
116 }
117
118 /* Return next node in the tree, starting with tagged parent <start>, or NULL if none */
eb32sc_next_with_parent(eb_troot_t * start,unsigned long scope)119 static inline struct eb32sc_node *eb32sc_next_with_parent(eb_troot_t *start, unsigned long scope)
120 {
121 while (eb_gettag(start) != EB_LEFT)
122 /* Walking up from right branch, so we cannot be below root */
123 start = (eb_root_to_node(eb_untag(start, EB_RGHT)))->node_p;
124
125 /* Note that <t> cannot be NULL at this stage */
126 start = (eb_untag(start, EB_LEFT))->b[EB_RGHT];
127 if (eb_clrtag(start) == NULL)
128 return NULL;
129
130 return eb32sc_walk_down_left(start, scope);
131 }
132
133 /* Return next node in the tree, or NULL if none */
eb32sc_next(struct eb32sc_node * eb32,unsigned long scope)134 static inline struct eb32sc_node *eb32sc_next(struct eb32sc_node *eb32, unsigned long scope)
135 {
136 return eb32sc_next_with_parent(eb32->node.leaf_p, scope);
137 }
138
139 /* Return leftmost node in the tree, or NULL if none */
eb32sc_first(struct eb_root * root,unsigned long scope)140 static inline struct eb32sc_node *eb32sc_first(struct eb_root *root, unsigned long scope)
141 {
142 return eb32sc_walk_down_left(root->b[0], scope);
143 }
144
145 #endif /* _EB32SC_TREE_H */
146