blob: df97353a4105ae3e52bb5c1732ab599bdcf6d51e [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);
58REGPRM2 struct eb32sc_node *eb32sc_insert(struct eb_root *root, struct eb32sc_node *new, unsigned long scope);
59void eb32sc_delete(struct eb32sc_node *node);
60
61/* Walks down starting at root pointer <start>, and always walking on side
62 * <side>. It either returns the node hosting the first leaf on that side,
63 * or NULL if no leaf is found. <start> may either be NULL or a branch pointer.
64 * The pointer to the leaf (or NULL) is returned.
65 */
66static inline struct eb32sc_node *eb32sc_walk_down(eb_troot_t *start, unsigned int side, unsigned long scope)
67{
68 /* A NULL pointer on an empty tree root will be returned as-is */
69 while (eb_gettag(start) == EB_NODE)
70 start = (eb_untag(start, EB_NODE))->b[side];
71 /* NULL is left untouched (root==eb_node, EB_LEAF==0) */
72 return eb32sc_entry(eb_root_to_node(eb_untag(start, EB_LEAF)), struct eb32sc_node, node);
73}
74
75/* Return next node in the tree, or NULL if none */
76static inline struct eb32sc_node *eb32sc_next(struct eb32sc_node *eb32, unsigned long scope)
77{
78 struct eb_node *node = &eb32->node;
79 eb_troot_t *t = node->leaf_p;
80
81 while (eb_gettag(t) != EB_LEFT)
82 /* Walking up from right branch, so we cannot be below root */
83 t = (eb_root_to_node(eb_untag(t, EB_RGHT)))->node_p;
84
85 /* Note that <t> cannot be NULL at this stage */
86 t = (eb_untag(t, EB_LEFT))->b[EB_RGHT];
87 if (eb_clrtag(t) == NULL)
88 return NULL;
89
90 return eb32sc_walk_down(t, EB_LEFT, scope);
91}
92
93/* Return leftmost node in the tree, or NULL if none */
94static inline struct eb32sc_node *eb32sc_first(struct eb_root *root, unsigned long scope)
95{
96 return eb32sc_walk_down(root->b[0], EB_LEFT, scope);
97}
98
99#endif /* _EB32SC_TREE_H */