blob: d0dc31b71a7900a86c597d8e3d8f42379f8a6eb3 [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;
71
72 if (unlikely(!start))
73 return NULL;
74
75 while (eb_gettag(start) == EB_NODE) {
76 root = eb_untag(start, EB_NODE);
77 node = eb_root_to_node(root);
78
79 start = node->branches.b[EB_LEFT];
80 if (!(container_of(node, struct eb32sc_node, node)->node_s & scope))
81 start = node->branches.b[EB_RGHT];
82 }
83
84 /* now we have a leaf */
85 node = eb_root_to_node(eb_untag(start, EB_LEAF));
86 if (!(eb32sc_entry(node, struct eb32sc_node, node)->leaf_s & scope))
87 return NULL;
88
89 return eb32sc_entry(node, struct eb32sc_node, node);
Willy Tarreauca308392017-11-05 13:31:29 +010090}
91
92/* Return next node in the tree, or NULL if none */
93static inline struct eb32sc_node *eb32sc_next(struct eb32sc_node *eb32, unsigned long scope)
94{
Willy Tarreaud1d55ac2017-11-05 14:33:01 +010095 struct eb_root *root;
Willy Tarreauca308392017-11-05 13:31:29 +010096 struct eb_node *node = &eb32->node;
97 eb_troot_t *t = node->leaf_p;
98
Willy Tarreaud1d55ac2017-11-05 14:33:01 +010099 while (1) {
100 while (eb_gettag(t) != EB_LEFT)
101 /* Walking up from right branch, so we cannot be below root */
102 t = (eb_root_to_node(eb_untag(t, EB_RGHT)))->node_p;
Willy Tarreauca308392017-11-05 13:31:29 +0100103
Willy Tarreaud1d55ac2017-11-05 14:33:01 +0100104 /* Note that <t> cannot be NULL at this stage */
105 root = eb_untag(t, EB_LEFT);
106 t = root->b[EB_RGHT];
107 if (eb_clrtag(t) == NULL)
108 return NULL;
Willy Tarreauca308392017-11-05 13:31:29 +0100109
Willy Tarreaud1d55ac2017-11-05 14:33:01 +0100110 /* we can't be below the root here */
111 eb32 = eb32sc_walk_down_left(t, scope);
112 if (eb32)
113 return eb32;
114 /* not found below, this means we have to go up */
115 t = eb_root_to_node(root)->node_p;
116 }
Willy Tarreauca308392017-11-05 13:31:29 +0100117}
118
119/* Return leftmost node in the tree, or NULL if none */
120static inline struct eb32sc_node *eb32sc_first(struct eb_root *root, unsigned long scope)
121{
Willy Tarreaud1d55ac2017-11-05 14:33:01 +0100122 return eb32sc_walk_down_left(root->b[0], scope);
Willy Tarreauca308392017-11-05 13:31:29 +0100123}
124
125#endif /* _EB32SC_TREE_H */