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 */ |
Willy Tarreau | 0cb98b2 | 2017-11-20 21:11:12 +0100 | [diff] [blame] | 31 | #ifndef _EB32TREE_H |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 32 | typedef unsigned int u32; |
| 33 | typedef signed int s32; |
Willy Tarreau | 0cb98b2 | 2017-11-20 21:11:12 +0100 | [diff] [blame] | 34 | #endif |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 35 | |
| 36 | /* This structure carries a node, a leaf, a scope, and a key. It must start |
| 37 | * with the eb_node so that it can be cast into an eb_node. We could also |
| 38 | * have put some sort of transparent union here to reduce the indirection |
| 39 | * level, but the fact is, the end user is not meant to manipulate internals, |
| 40 | * so this is pointless. |
| 41 | */ |
| 42 | struct eb32sc_node { |
| 43 | struct eb_node node; /* the tree node, must be at the beginning */ |
Willy Tarreau | 319f078 | 2018-10-21 06:52:11 +0200 | [diff] [blame] | 44 | u32 key; |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 45 | unsigned long node_s; /* visibility of this node's branches */ |
| 46 | unsigned long leaf_s; /* visibility of this node's leaf */ |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | /* |
| 50 | * Exported functions and macros. |
| 51 | * Many of them are always inlined because they are extremely small, and |
| 52 | * are generally called at most once or twice in a program. |
| 53 | */ |
| 54 | |
| 55 | /* |
| 56 | * The following functions are not inlined by default. They are declared |
| 57 | * in eb32sctree.c, which simply relies on their inline version. |
| 58 | */ |
| 59 | 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] | 60 | 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] | 61 | REGPRM2 struct eb32sc_node *eb32sc_insert(struct eb_root *root, struct eb32sc_node *new, unsigned long scope); |
| 62 | void eb32sc_delete(struct eb32sc_node *node); |
| 63 | |
Willy Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 64 | /* Walks down left starting at root pointer <start>, and follow the leftmost |
| 65 | * branch whose scope matches <scope>. It either returns the node hosting the |
| 66 | * first leaf on that side, or NULL if no leaf is found. <start> may either be |
| 67 | * 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] | 68 | */ |
Willy Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 69 | 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] | 70 | { |
Willy Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 71 | struct eb_root *root; |
| 72 | struct eb_node *node; |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 73 | struct eb32sc_node *eb32; |
Willy Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 74 | |
| 75 | if (unlikely(!start)) |
| 76 | return NULL; |
| 77 | |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 78 | while (1) { |
| 79 | if (eb_gettag(start) == EB_NODE) { |
| 80 | root = eb_untag(start, EB_NODE); |
| 81 | node = eb_root_to_node(root); |
| 82 | eb32 = container_of(node, struct eb32sc_node, node); |
| 83 | if (eb32->node_s & scope) { |
| 84 | start = node->branches.b[EB_LEFT]; |
| 85 | continue; |
| 86 | } |
| 87 | start = node->node_p; |
| 88 | } |
| 89 | else { |
| 90 | root = eb_untag(start, EB_LEAF); |
| 91 | node = eb_root_to_node(root); |
| 92 | eb32 = container_of(node, struct eb32sc_node, node); |
| 93 | if (eb32->leaf_s & scope) |
| 94 | return eb32; |
| 95 | start = node->leaf_p; |
| 96 | } |
Willy Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 97 | |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 98 | /* here we're on a node that doesn't match the scope. We have |
| 99 | * to walk to the closest right location. |
| 100 | */ |
| 101 | while (eb_gettag(start) != EB_LEFT) |
| 102 | /* Walking up from right branch, so we cannot be below root */ |
| 103 | start = (eb_root_to_node(eb_untag(start, EB_RGHT)))->node_p; |
Willy Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 104 | |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 105 | /* Note that <start> cannot be NULL at this stage */ |
| 106 | root = eb_untag(start, EB_LEFT); |
| 107 | start = root->b[EB_RGHT]; |
| 108 | if (eb_clrtag(start) == NULL) |
| 109 | return NULL; |
| 110 | } |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Willy Tarreau | f6ac365 | 2017-11-13 19:13:06 +0100 | [diff] [blame] | 113 | /* Return next node in the tree, starting with tagged parent <start>, or NULL if none */ |
| 114 | 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] | 115 | { |
Willy Tarreau | f6ac365 | 2017-11-13 19:13:06 +0100 | [diff] [blame] | 116 | while (eb_gettag(start) != EB_LEFT) |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 117 | /* Walking up from right branch, so we cannot be below root */ |
Willy Tarreau | f6ac365 | 2017-11-13 19:13:06 +0100 | [diff] [blame] | 118 | start = (eb_root_to_node(eb_untag(start, EB_RGHT)))->node_p; |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 119 | |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 120 | /* Note that <t> cannot be NULL at this stage */ |
Willy Tarreau | f6ac365 | 2017-11-13 19:13:06 +0100 | [diff] [blame] | 121 | start = (eb_untag(start, EB_LEFT))->b[EB_RGHT]; |
| 122 | if (eb_clrtag(start) == NULL) |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 123 | return NULL; |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 124 | |
Willy Tarreau | f6ac365 | 2017-11-13 19:13:06 +0100 | [diff] [blame] | 125 | return eb32sc_walk_down_left(start, scope); |
| 126 | } |
| 127 | |
| 128 | /* Return next node in the tree, or NULL if none */ |
| 129 | static inline struct eb32sc_node *eb32sc_next(struct eb32sc_node *eb32, unsigned long scope) |
| 130 | { |
| 131 | return eb32sc_next_with_parent(eb32->node.leaf_p, scope); |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /* Return leftmost node in the tree, or NULL if none */ |
| 135 | static inline struct eb32sc_node *eb32sc_first(struct eb_root *root, unsigned long scope) |
| 136 | { |
Willy Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 137 | return eb32sc_walk_down_left(root->b[0], scope); |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | #endif /* _EB32SC_TREE_H */ |