Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 1 | /* |
| 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 */ |
| 31 | typedef unsigned int u32; |
| 32 | typedef 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 | */ |
| 40 | struct 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 | */ |
| 57 | REGPRM2 struct eb32sc_node *eb32sc_lookup_ge(struct eb_root *root, u32 x, unsigned long scope); |
Willy Tarreau | 8878b6c | 2017-11-05 21:23:21 +0100 | [diff] [blame] | 58 | REGPRM2 struct eb32sc_node *eb32sc_lookup_ge_or_first(struct eb_root *root, u32 x, unsigned long scope); |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 59 | REGPRM2 struct eb32sc_node *eb32sc_insert(struct eb_root *root, struct eb32sc_node *new, unsigned long scope); |
| 60 | void eb32sc_delete(struct eb32sc_node *node); |
| 61 | |
Willy Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 62 | /* 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 Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 66 | */ |
Willy Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 67 | static inline struct eb32sc_node *eb32sc_walk_down_left(eb_troot_t *start, unsigned long scope) |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 68 | { |
Willy Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 69 | struct eb_root *root; |
| 70 | struct eb_node *node; |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 71 | struct eb32sc_node *eb32; |
Willy Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 72 | |
| 73 | if (unlikely(!start)) |
| 74 | return NULL; |
| 75 | |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 76 | 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 Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 95 | |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 96 | /* 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 Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 102 | |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 103 | /* 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 Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Willy Tarreau | f6ac365 | 2017-11-13 19:13:06 +0100 | [diff] [blame] | 111 | /* Return next node in the tree, starting with tagged parent <start>, or NULL if none */ |
| 112 | static inline struct eb32sc_node *eb32sc_next_with_parent(eb_troot_t *start, unsigned long scope) |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 113 | { |
Willy Tarreau | f6ac365 | 2017-11-13 19:13:06 +0100 | [diff] [blame] | 114 | while (eb_gettag(start) != EB_LEFT) |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 115 | /* Walking up from right branch, so we cannot be below root */ |
Willy Tarreau | f6ac365 | 2017-11-13 19:13:06 +0100 | [diff] [blame] | 116 | start = (eb_root_to_node(eb_untag(start, EB_RGHT)))->node_p; |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 117 | |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 118 | /* Note that <t> cannot be NULL at this stage */ |
Willy Tarreau | f6ac365 | 2017-11-13 19:13:06 +0100 | [diff] [blame] | 119 | start = (eb_untag(start, EB_LEFT))->b[EB_RGHT]; |
| 120 | if (eb_clrtag(start) == NULL) |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 121 | return NULL; |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 122 | |
Willy Tarreau | f6ac365 | 2017-11-13 19:13:06 +0100 | [diff] [blame] | 123 | return eb32sc_walk_down_left(start, scope); |
| 124 | } |
| 125 | |
| 126 | /* Return next node in the tree, or NULL if none */ |
| 127 | static 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 Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /* Return leftmost node in the tree, or NULL if none */ |
| 133 | static inline struct eb32sc_node *eb32sc_first(struct eb_root *root, unsigned long scope) |
| 134 | { |
Willy Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 135 | return eb32sc_walk_down_left(root->b[0], scope); |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | #endif /* _EB32SC_TREE_H */ |