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. |
Willy Tarreau | 41136de | 2020-02-22 15:55:33 +0100 | [diff] [blame] | 41 | * In case sizeof(void*)>=sizeof(long), we know there will be some padding after |
| 42 | * the leaf if it's unaligned. In this case we force the alignment on void* so |
| 43 | * that we prefer to have the padding before for more efficient accesses. |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 44 | */ |
| 45 | struct eb32sc_node { |
| 46 | struct eb_node node; /* the tree node, must be at the beginning */ |
Willy Tarreau | 41136de | 2020-02-22 15:55:33 +0100 | [diff] [blame] | 47 | MAYBE_ALIGN(sizeof(u32)); |
Willy Tarreau | 319f078 | 2018-10-21 06:52:11 +0200 | [diff] [blame] | 48 | u32 key; |
Willy Tarreau | 41136de | 2020-02-22 15:55:33 +0100 | [diff] [blame] | 49 | ALWAYS_ALIGN(sizeof(void*)); |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 50 | unsigned long node_s; /* visibility of this node's branches */ |
| 51 | unsigned long leaf_s; /* visibility of this node's leaf */ |
Willy Tarreau | 41136de | 2020-02-22 15:55:33 +0100 | [diff] [blame] | 52 | } ALIGNED(sizeof(void*)); |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | * Exported functions and macros. |
| 56 | * Many of them are always inlined because they are extremely small, and |
| 57 | * are generally called at most once or twice in a program. |
| 58 | */ |
| 59 | |
| 60 | /* |
| 61 | * The following functions are not inlined by default. They are declared |
| 62 | * in eb32sctree.c, which simply relies on their inline version. |
| 63 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 64 | struct eb32sc_node *eb32sc_lookup_ge(struct eb_root *root, u32 x, unsigned long scope); |
| 65 | struct eb32sc_node *eb32sc_lookup_ge_or_first(struct eb_root *root, u32 x, unsigned long scope); |
| 66 | 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] | 67 | void eb32sc_delete(struct eb32sc_node *node); |
| 68 | |
Willy Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 69 | /* Walks down left starting at root pointer <start>, and follow the leftmost |
| 70 | * branch whose scope matches <scope>. It either returns the node hosting the |
| 71 | * first leaf on that side, or NULL if no leaf is found. <start> may either be |
| 72 | * 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] | 73 | */ |
Willy Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 74 | 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] | 75 | { |
Willy Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 76 | struct eb_root *root; |
| 77 | struct eb_node *node; |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 78 | struct eb32sc_node *eb32; |
Willy Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 79 | |
| 80 | if (unlikely(!start)) |
| 81 | return NULL; |
| 82 | |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 83 | while (1) { |
| 84 | if (eb_gettag(start) == EB_NODE) { |
| 85 | root = eb_untag(start, EB_NODE); |
| 86 | node = eb_root_to_node(root); |
| 87 | eb32 = container_of(node, struct eb32sc_node, node); |
| 88 | if (eb32->node_s & scope) { |
| 89 | start = node->branches.b[EB_LEFT]; |
| 90 | continue; |
| 91 | } |
| 92 | start = node->node_p; |
| 93 | } |
| 94 | else { |
| 95 | root = eb_untag(start, EB_LEAF); |
| 96 | node = eb_root_to_node(root); |
| 97 | eb32 = container_of(node, struct eb32sc_node, node); |
| 98 | if (eb32->leaf_s & scope) |
| 99 | return eb32; |
| 100 | start = node->leaf_p; |
| 101 | } |
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 | /* here we're on a node that doesn't match the scope. We have |
| 104 | * to walk to the closest right location. |
| 105 | */ |
| 106 | while (eb_gettag(start) != EB_LEFT) |
| 107 | /* Walking up from right branch, so we cannot be below root */ |
| 108 | start = (eb_root_to_node(eb_untag(start, EB_RGHT)))->node_p; |
Willy Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 109 | |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 110 | /* Note that <start> cannot be NULL at this stage */ |
| 111 | root = eb_untag(start, EB_LEFT); |
| 112 | start = root->b[EB_RGHT]; |
| 113 | if (eb_clrtag(start) == NULL) |
| 114 | return NULL; |
| 115 | } |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Willy Tarreau | f6ac365 | 2017-11-13 19:13:06 +0100 | [diff] [blame] | 118 | /* Return next node in the tree, starting with tagged parent <start>, or NULL if none */ |
| 119 | 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] | 120 | { |
Willy Tarreau | f6ac365 | 2017-11-13 19:13:06 +0100 | [diff] [blame] | 121 | while (eb_gettag(start) != EB_LEFT) |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 122 | /* Walking up from right branch, so we cannot be below root */ |
Willy Tarreau | f6ac365 | 2017-11-13 19:13:06 +0100 | [diff] [blame] | 123 | start = (eb_root_to_node(eb_untag(start, EB_RGHT)))->node_p; |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 124 | |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 125 | /* Note that <t> cannot be NULL at this stage */ |
Willy Tarreau | f6ac365 | 2017-11-13 19:13:06 +0100 | [diff] [blame] | 126 | start = (eb_untag(start, EB_LEFT))->b[EB_RGHT]; |
| 127 | if (eb_clrtag(start) == NULL) |
Willy Tarreau | 5274330 | 2017-11-13 18:55:44 +0100 | [diff] [blame] | 128 | return NULL; |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 129 | |
Willy Tarreau | f6ac365 | 2017-11-13 19:13:06 +0100 | [diff] [blame] | 130 | return eb32sc_walk_down_left(start, scope); |
| 131 | } |
| 132 | |
| 133 | /* Return next node in the tree, or NULL if none */ |
| 134 | static inline struct eb32sc_node *eb32sc_next(struct eb32sc_node *eb32, unsigned long scope) |
| 135 | { |
| 136 | return eb32sc_next_with_parent(eb32->node.leaf_p, scope); |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /* Return leftmost node in the tree, or NULL if none */ |
| 140 | static inline struct eb32sc_node *eb32sc_first(struct eb_root *root, unsigned long scope) |
| 141 | { |
Willy Tarreau | d1d55ac | 2017-11-05 14:33:01 +0100 | [diff] [blame] | 142 | return eb32sc_walk_down_left(root->b[0], scope); |
Willy Tarreau | ca30839 | 2017-11-05 13:31:29 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | #endif /* _EB32SC_TREE_H */ |