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 | |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 30 | /* |
| 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 Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 40 | struct eb32sc_node *eb32sc_lookup_ge(struct eb_root *root, u32 x, unsigned long scope); |
| 41 | struct eb32sc_node *eb32sc_lookup_ge_or_first(struct eb_root *root, u32 x, unsigned long scope); |
| 42 | struct eb32sc_node *eb32sc_insert(struct eb_root *root, struct eb32sc_node *new, unsigned long scope); |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 43 | void eb32sc_delete(struct eb32sc_node *node); |
| 44 | |
Willy Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 45 | /* 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 Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 49 | */ |
Willy Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 50 | 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] | 51 | { |
Willy Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 52 | struct eb_root *root; |
| 53 | struct eb_node *node; |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 54 | struct eb32sc_node *eb32; |
Willy Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 55 | |
| 56 | if (unlikely(!start)) |
| 57 | return NULL; |
| 58 | |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 59 | 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 Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 78 | |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 79 | /* 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 Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 85 | |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 86 | /* 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 Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Willy Tarreau | f6ac365 | 2017-11-13 19:13:06 +0100 | [diff] [blame] | 94 | /* Return next node in the tree, starting with tagged parent <start>, or NULL if none */ |
| 95 | 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] | 96 | { |
Willy Tarreau | f6ac365 | 2017-11-13 19:13:06 +0100 | [diff] [blame] | 97 | while (eb_gettag(start) != EB_LEFT) |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 98 | /* Walking up from right branch, so we cannot be below root */ |
Willy Tarreau | f6ac365 | 2017-11-13 19:13:06 +0100 | [diff] [blame] | 99 | start = (eb_root_to_node(eb_untag(start, EB_RGHT)))->node_p; |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 100 | |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 101 | /* Note that <t> cannot be NULL at this stage */ |
Willy Tarreau | f6ac365 | 2017-11-13 19:13:06 +0100 | [diff] [blame] | 102 | start = (eb_untag(start, EB_LEFT))->b[EB_RGHT]; |
| 103 | if (eb_clrtag(start) == NULL) |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 104 | return NULL; |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 105 | |
Willy Tarreau | f6ac365 | 2017-11-13 19:13:06 +0100 | [diff] [blame] | 106 | return eb32sc_walk_down_left(start, scope); |
| 107 | } |
| 108 | |
| 109 | /* Return next node in the tree, or NULL if none */ |
| 110 | static 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 Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* Return leftmost node in the tree, or NULL if none */ |
| 116 | static inline struct eb32sc_node *eb32sc_first(struct eb_root *root, unsigned long scope) |
| 117 | { |
Willy Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 118 | return eb32sc_walk_down_left(root->b[0], scope); |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | #endif /* _EB32SC_TREE_H */ |