blob: 5ace6628d9cb7d1edb9768add1d4817d554a4b17 [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
Willy Tarreauca308392017-11-05 13:31:29 +010030/*
31 * Exported functions and macros.
32 * Many of them are always inlined because they are extremely small, and
33 * are generally called at most once or twice in a program.
34 */
35
36/*
37 * The following functions are not inlined by default. They are declared
38 * in eb32sctree.c, which simply relies on their inline version.
39 */
Willy Tarreau03e78532020-02-25 07:38:05 +010040struct eb32sc_node *eb32sc_lookup_ge(struct eb_root *root, u32 x, unsigned long scope);
41struct eb32sc_node *eb32sc_lookup_ge_or_first(struct eb_root *root, u32 x, unsigned long scope);
42struct eb32sc_node *eb32sc_insert(struct eb_root *root, struct eb32sc_node *new, unsigned long scope);
Willy Tarreauca308392017-11-05 13:31:29 +010043void eb32sc_delete(struct eb32sc_node *node);
44
Willy Tarreaud1d55ac2017-11-05 14:33:01 +010045/* Walks down left starting at root pointer <start>, and follow the leftmost
46 * branch whose scope matches <scope>. It either returns the node hosting the
47 * first leaf on that side, or NULL if no leaf is found. <start> may either be
48 * NULL or a branch pointer. The pointer to the leaf (or NULL) is returned.
Willy Tarreauca308392017-11-05 13:31:29 +010049 */
Willy Tarreaud1d55ac2017-11-05 14:33:01 +010050static inline struct eb32sc_node *eb32sc_walk_down_left(eb_troot_t *start, unsigned long scope)
Willy Tarreauca308392017-11-05 13:31:29 +010051{
Willy Tarreaud1d55ac2017-11-05 14:33:01 +010052 struct eb_root *root;
53 struct eb_node *node;
Willy Tarreau52743302017-11-13 18:55:44 +010054 struct eb32sc_node *eb32;
Willy Tarreaud1d55ac2017-11-05 14:33:01 +010055
56 if (unlikely(!start))
57 return NULL;
58
Willy Tarreau52743302017-11-13 18:55:44 +010059 while (1) {
60 if (eb_gettag(start) == EB_NODE) {
61 root = eb_untag(start, EB_NODE);
62 node = eb_root_to_node(root);
63 eb32 = container_of(node, struct eb32sc_node, node);
64 if (eb32->node_s & scope) {
65 start = node->branches.b[EB_LEFT];
66 continue;
67 }
68 start = node->node_p;
69 }
70 else {
71 root = eb_untag(start, EB_LEAF);
72 node = eb_root_to_node(root);
73 eb32 = container_of(node, struct eb32sc_node, node);
74 if (eb32->leaf_s & scope)
75 return eb32;
76 start = node->leaf_p;
77 }
Willy Tarreaud1d55ac2017-11-05 14:33:01 +010078
Willy Tarreau52743302017-11-13 18:55:44 +010079 /* here we're on a node that doesn't match the scope. We have
80 * to walk to the closest right location.
81 */
82 while (eb_gettag(start) != EB_LEFT)
83 /* Walking up from right branch, so we cannot be below root */
84 start = (eb_root_to_node(eb_untag(start, EB_RGHT)))->node_p;
Willy Tarreaud1d55ac2017-11-05 14:33:01 +010085
Willy Tarreau52743302017-11-13 18:55:44 +010086 /* Note that <start> cannot be NULL at this stage */
87 root = eb_untag(start, EB_LEFT);
88 start = root->b[EB_RGHT];
89 if (eb_clrtag(start) == NULL)
90 return NULL;
91 }
Willy Tarreauca308392017-11-05 13:31:29 +010092}
93
Willy Tarreauf6ac3652017-11-13 19:13:06 +010094/* Return next node in the tree, starting with tagged parent <start>, or NULL if none */
95static inline struct eb32sc_node *eb32sc_next_with_parent(eb_troot_t *start, unsigned long scope)
Willy Tarreauca308392017-11-05 13:31:29 +010096{
Willy Tarreauf6ac3652017-11-13 19:13:06 +010097 while (eb_gettag(start) != EB_LEFT)
Willy Tarreau52743302017-11-13 18:55:44 +010098 /* Walking up from right branch, so we cannot be below root */
Willy Tarreauf6ac3652017-11-13 19:13:06 +010099 start = (eb_root_to_node(eb_untag(start, EB_RGHT)))->node_p;
Willy Tarreauca308392017-11-05 13:31:29 +0100100
Willy Tarreau52743302017-11-13 18:55:44 +0100101 /* Note that <t> cannot be NULL at this stage */
Willy Tarreauf6ac3652017-11-13 19:13:06 +0100102 start = (eb_untag(start, EB_LEFT))->b[EB_RGHT];
103 if (eb_clrtag(start) == NULL)
Willy Tarreau52743302017-11-13 18:55:44 +0100104 return NULL;
Willy Tarreauca308392017-11-05 13:31:29 +0100105
Willy Tarreauf6ac3652017-11-13 19:13:06 +0100106 return eb32sc_walk_down_left(start, scope);
107}
108
109/* Return next node in the tree, or NULL if none */
110static inline struct eb32sc_node *eb32sc_next(struct eb32sc_node *eb32, unsigned long scope)
111{
112 return eb32sc_next_with_parent(eb32->node.leaf_p, scope);
Willy Tarreauca308392017-11-05 13:31:29 +0100113}
114
115/* Return leftmost node in the tree, or NULL if none */
116static inline struct eb32sc_node *eb32sc_first(struct eb_root *root, unsigned long scope)
117{
Willy Tarreaud1d55ac2017-11-05 14:33:01 +0100118 return eb32sc_walk_down_left(root->b[0], scope);
Willy Tarreauca308392017-11-05 13:31:29 +0100119}
120
121#endif /* _EB32SC_TREE_H */