blob: 51a2664edeb17d94cb25c60b3e785a5f817d1569 [file] [log] [blame]
Willy Tarreauca308392017-11-05 13:31:29 +01001/*
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 */
Willy Tarreau0cb98b22017-11-20 21:11:12 +010031#ifndef _EB32TREE_H
Willy Tarreauca308392017-11-05 13:31:29 +010032typedef unsigned int u32;
33typedef signed int s32;
Willy Tarreau0cb98b22017-11-20 21:11:12 +010034#endif
Willy Tarreauca308392017-11-05 13:31:29 +010035
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 */
42struct eb32sc_node {
43 struct eb_node node; /* the tree node, must be at the beginning */
Willy Tarreau319f0782018-10-21 06:52:11 +020044 u32 key;
Willy Tarreauca308392017-11-05 13:31:29 +010045 unsigned long node_s; /* visibility of this node's branches */
46 unsigned long leaf_s; /* visibility of this node's leaf */
Willy Tarreauca308392017-11-05 13:31:29 +010047};
48
49/*
50 * Exported functions and macros.
51 * Many of them are always inlined because they are extremely small, and
52 * are generally called at most once or twice in a program.
53 */
54
55/*
56 * The following functions are not inlined by default. They are declared
57 * in eb32sctree.c, which simply relies on their inline version.
58 */
59REGPRM2 struct eb32sc_node *eb32sc_lookup_ge(struct eb_root *root, u32 x, unsigned long scope);
Willy Tarreau8878b6c2017-11-05 21:23:21 +010060REGPRM2 struct eb32sc_node *eb32sc_lookup_ge_or_first(struct eb_root *root, u32 x, unsigned long scope);
Willy Tarreauca308392017-11-05 13:31:29 +010061REGPRM2 struct eb32sc_node *eb32sc_insert(struct eb_root *root, struct eb32sc_node *new, unsigned long scope);
62void eb32sc_delete(struct eb32sc_node *node);
63
Willy Tarreaud1d55ac2017-11-05 14:33:01 +010064/* Walks down left starting at root pointer <start>, and follow the leftmost
65 * branch whose scope matches <scope>. It either returns the node hosting the
66 * first leaf on that side, or NULL if no leaf is found. <start> may either be
67 * NULL or a branch pointer. The pointer to the leaf (or NULL) is returned.
Willy Tarreauca308392017-11-05 13:31:29 +010068 */
Willy Tarreaud1d55ac2017-11-05 14:33:01 +010069static inline struct eb32sc_node *eb32sc_walk_down_left(eb_troot_t *start, unsigned long scope)
Willy Tarreauca308392017-11-05 13:31:29 +010070{
Willy Tarreaud1d55ac2017-11-05 14:33:01 +010071 struct eb_root *root;
72 struct eb_node *node;
Willy Tarreau52743302017-11-13 18:55:44 +010073 struct eb32sc_node *eb32;
Willy Tarreaud1d55ac2017-11-05 14:33:01 +010074
75 if (unlikely(!start))
76 return NULL;
77
Willy Tarreau52743302017-11-13 18:55:44 +010078 while (1) {
79 if (eb_gettag(start) == EB_NODE) {
80 root = eb_untag(start, EB_NODE);
81 node = eb_root_to_node(root);
82 eb32 = container_of(node, struct eb32sc_node, node);
83 if (eb32->node_s & scope) {
84 start = node->branches.b[EB_LEFT];
85 continue;
86 }
87 start = node->node_p;
88 }
89 else {
90 root = eb_untag(start, EB_LEAF);
91 node = eb_root_to_node(root);
92 eb32 = container_of(node, struct eb32sc_node, node);
93 if (eb32->leaf_s & scope)
94 return eb32;
95 start = node->leaf_p;
96 }
Willy Tarreaud1d55ac2017-11-05 14:33:01 +010097
Willy Tarreau52743302017-11-13 18:55:44 +010098 /* here we're on a node that doesn't match the scope. We have
99 * to walk to the closest right location.
100 */
101 while (eb_gettag(start) != EB_LEFT)
102 /* Walking up from right branch, so we cannot be below root */
103 start = (eb_root_to_node(eb_untag(start, EB_RGHT)))->node_p;
Willy Tarreaud1d55ac2017-11-05 14:33:01 +0100104
Willy Tarreau52743302017-11-13 18:55:44 +0100105 /* Note that <start> cannot be NULL at this stage */
106 root = eb_untag(start, EB_LEFT);
107 start = root->b[EB_RGHT];
108 if (eb_clrtag(start) == NULL)
109 return NULL;
110 }
Willy Tarreauca308392017-11-05 13:31:29 +0100111}
112
Willy Tarreauf6ac3652017-11-13 19:13:06 +0100113/* Return next node in the tree, starting with tagged parent <start>, or NULL if none */
114static inline struct eb32sc_node *eb32sc_next_with_parent(eb_troot_t *start, unsigned long scope)
Willy Tarreauca308392017-11-05 13:31:29 +0100115{
Willy Tarreauf6ac3652017-11-13 19:13:06 +0100116 while (eb_gettag(start) != EB_LEFT)
Willy Tarreau52743302017-11-13 18:55:44 +0100117 /* Walking up from right branch, so we cannot be below root */
Willy Tarreauf6ac3652017-11-13 19:13:06 +0100118 start = (eb_root_to_node(eb_untag(start, EB_RGHT)))->node_p;
Willy Tarreauca308392017-11-05 13:31:29 +0100119
Willy Tarreau52743302017-11-13 18:55:44 +0100120 /* Note that <t> cannot be NULL at this stage */
Willy Tarreauf6ac3652017-11-13 19:13:06 +0100121 start = (eb_untag(start, EB_LEFT))->b[EB_RGHT];
122 if (eb_clrtag(start) == NULL)
Willy Tarreau52743302017-11-13 18:55:44 +0100123 return NULL;
Willy Tarreauca308392017-11-05 13:31:29 +0100124
Willy Tarreauf6ac3652017-11-13 19:13:06 +0100125 return eb32sc_walk_down_left(start, scope);
126}
127
128/* Return next node in the tree, or NULL if none */
129static inline struct eb32sc_node *eb32sc_next(struct eb32sc_node *eb32, unsigned long scope)
130{
131 return eb32sc_next_with_parent(eb32->node.leaf_p, scope);
Willy Tarreauca308392017-11-05 13:31:29 +0100132}
133
134/* Return leftmost node in the tree, or NULL if none */
135static inline struct eb32sc_node *eb32sc_first(struct eb_root *root, unsigned long scope)
136{
Willy Tarreaud1d55ac2017-11-05 14:33:01 +0100137 return eb32sc_walk_down_left(root->b[0], scope);
Willy Tarreauca308392017-11-05 13:31:29 +0100138}
139
140#endif /* _EB32SC_TREE_H */