blob: be469baa66bbcb80dbf8e6b591b296768694a412 [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 */
31typedef unsigned int u32;
32typedef signed int s32;
33
34/* This structure carries a node, a leaf, a scope, and a key. It must start
35 * with the eb_node so that it can be cast into an eb_node. We could also
36 * have put some sort of transparent union here to reduce the indirection
37 * level, but the fact is, the end user is not meant to manipulate internals,
38 * so this is pointless.
39 */
40struct eb32sc_node {
41 struct eb_node node; /* the tree node, must be at the beginning */
42 unsigned long node_s; /* visibility of this node's branches */
43 unsigned long leaf_s; /* visibility of this node's leaf */
44 u32 key;
45};
46
47/*
48 * Exported functions and macros.
49 * Many of them are always inlined because they are extremely small, and
50 * are generally called at most once or twice in a program.
51 */
52
53/*
54 * The following functions are not inlined by default. They are declared
55 * in eb32sctree.c, which simply relies on their inline version.
56 */
57REGPRM2 struct eb32sc_node *eb32sc_lookup_ge(struct eb_root *root, u32 x, unsigned long scope);
Willy Tarreau8878b6c2017-11-05 21:23:21 +010058REGPRM2 struct eb32sc_node *eb32sc_lookup_ge_or_first(struct eb_root *root, u32 x, unsigned long scope);
Willy Tarreauca308392017-11-05 13:31:29 +010059REGPRM2 struct eb32sc_node *eb32sc_insert(struct eb_root *root, struct eb32sc_node *new, unsigned long scope);
60void eb32sc_delete(struct eb32sc_node *node);
61
Willy Tarreaud1d55ac2017-11-05 14:33:01 +010062/* Walks down left starting at root pointer <start>, and follow the leftmost
63 * branch whose scope matches <scope>. It either returns the node hosting the
64 * first leaf on that side, or NULL if no leaf is found. <start> may either be
65 * NULL or a branch pointer. The pointer to the leaf (or NULL) is returned.
Willy Tarreauca308392017-11-05 13:31:29 +010066 */
Willy Tarreaud1d55ac2017-11-05 14:33:01 +010067static inline struct eb32sc_node *eb32sc_walk_down_left(eb_troot_t *start, unsigned long scope)
Willy Tarreauca308392017-11-05 13:31:29 +010068{
Willy Tarreaud1d55ac2017-11-05 14:33:01 +010069 struct eb_root *root;
70 struct eb_node *node;
Willy Tarreau52743302017-11-13 18:55:44 +010071 struct eb32sc_node *eb32;
Willy Tarreaud1d55ac2017-11-05 14:33:01 +010072
73 if (unlikely(!start))
74 return NULL;
75
Willy Tarreau52743302017-11-13 18:55:44 +010076 while (1) {
77 if (eb_gettag(start) == EB_NODE) {
78 root = eb_untag(start, EB_NODE);
79 node = eb_root_to_node(root);
80 eb32 = container_of(node, struct eb32sc_node, node);
81 if (eb32->node_s & scope) {
82 start = node->branches.b[EB_LEFT];
83 continue;
84 }
85 start = node->node_p;
86 }
87 else {
88 root = eb_untag(start, EB_LEAF);
89 node = eb_root_to_node(root);
90 eb32 = container_of(node, struct eb32sc_node, node);
91 if (eb32->leaf_s & scope)
92 return eb32;
93 start = node->leaf_p;
94 }
Willy Tarreaud1d55ac2017-11-05 14:33:01 +010095
Willy Tarreau52743302017-11-13 18:55:44 +010096 /* here we're on a node that doesn't match the scope. We have
97 * to walk to the closest right location.
98 */
99 while (eb_gettag(start) != EB_LEFT)
100 /* Walking up from right branch, so we cannot be below root */
101 start = (eb_root_to_node(eb_untag(start, EB_RGHT)))->node_p;
Willy Tarreaud1d55ac2017-11-05 14:33:01 +0100102
Willy Tarreau52743302017-11-13 18:55:44 +0100103 /* Note that <start> cannot be NULL at this stage */
104 root = eb_untag(start, EB_LEFT);
105 start = root->b[EB_RGHT];
106 if (eb_clrtag(start) == NULL)
107 return NULL;
108 }
Willy Tarreauca308392017-11-05 13:31:29 +0100109}
110
Willy Tarreauf6ac3652017-11-13 19:13:06 +0100111/* Return next node in the tree, starting with tagged parent <start>, or NULL if none */
112static inline struct eb32sc_node *eb32sc_next_with_parent(eb_troot_t *start, unsigned long scope)
Willy Tarreauca308392017-11-05 13:31:29 +0100113{
Willy Tarreauf6ac3652017-11-13 19:13:06 +0100114 while (eb_gettag(start) != EB_LEFT)
Willy Tarreau52743302017-11-13 18:55:44 +0100115 /* Walking up from right branch, so we cannot be below root */
Willy Tarreauf6ac3652017-11-13 19:13:06 +0100116 start = (eb_root_to_node(eb_untag(start, EB_RGHT)))->node_p;
Willy Tarreauca308392017-11-05 13:31:29 +0100117
Willy Tarreau52743302017-11-13 18:55:44 +0100118 /* Note that <t> cannot be NULL at this stage */
Willy Tarreauf6ac3652017-11-13 19:13:06 +0100119 start = (eb_untag(start, EB_LEFT))->b[EB_RGHT];
120 if (eb_clrtag(start) == NULL)
Willy Tarreau52743302017-11-13 18:55:44 +0100121 return NULL;
Willy Tarreauca308392017-11-05 13:31:29 +0100122
Willy Tarreauf6ac3652017-11-13 19:13:06 +0100123 return eb32sc_walk_down_left(start, scope);
124}
125
126/* Return next node in the tree, or NULL if none */
127static inline struct eb32sc_node *eb32sc_next(struct eb32sc_node *eb32, unsigned long scope)
128{
129 return eb32sc_next_with_parent(eb32->node.leaf_p, scope);
Willy Tarreauca308392017-11-05 13:31:29 +0100130}
131
132/* Return leftmost node in the tree, or NULL if none */
133static inline struct eb32sc_node *eb32sc_first(struct eb_root *root, unsigned long scope)
134{
Willy Tarreaud1d55ac2017-11-05 14:33:01 +0100135 return eb32sc_walk_down_left(root->b[0], scope);
Willy Tarreauca308392017-11-05 13:31:29 +0100136}
137
138#endif /* _EB32SC_TREE_H */