Willy Tarreau | 9b7a617 | 2021-10-06 17:55:45 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Elastic Binary Trees - types |
| 3 | * Version 6.0.6 |
| 4 | * (C) 2002-2011 - 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 _EBTREE_T_H |
| 22 | #define _EBTREE_T_H |
| 23 | |
| 24 | #include <haproxy/api-t.h> |
| 25 | |
| 26 | /* |
| 27 | * generic types for ebtree |
| 28 | */ |
| 29 | |
| 30 | /* Number of bits per node, and number of leaves per node */ |
| 31 | #define EB_NODE_BITS 1 |
| 32 | #define EB_NODE_BRANCHES (1 << EB_NODE_BITS) |
| 33 | #define EB_NODE_BRANCH_MASK (EB_NODE_BRANCHES - 1) |
| 34 | |
| 35 | /* Be careful not to tweak those values. The walking code is optimized for NULL |
| 36 | * detection on the assumption that the following values are intact. |
| 37 | */ |
| 38 | #define EB_LEFT 0 |
| 39 | #define EB_RGHT 1 |
| 40 | #define EB_LEAF 0 |
| 41 | #define EB_NODE 1 |
| 42 | |
| 43 | /* Tags to set in root->b[EB_RGHT] : |
| 44 | * - EB_NORMAL is a normal tree which stores duplicate keys. |
| 45 | * - EB_UNIQUE is a tree which stores unique keys. |
| 46 | */ |
| 47 | #define EB_NORMAL 0 |
| 48 | #define EB_UNIQUE 1 |
| 49 | |
| 50 | /* This is the same as an eb_node pointer, except that the lower bit embeds |
| 51 | * a tag. See eb_dotag()/eb_untag()/eb_gettag(). This tag has two meanings : |
| 52 | * - 0=left, 1=right to designate the parent's branch for leaf_p/node_p |
| 53 | * - 0=link, 1=leaf to designate the branch's type for branch[] |
| 54 | */ |
| 55 | typedef void eb_troot_t; |
| 56 | |
| 57 | /* The eb_root connects the node which contains it, to two nodes below it, one |
| 58 | * of which may be the same node. At the top of the tree, we use an eb_root |
| 59 | * too, which always has its right branch NULL (+/1 low-order bits). |
| 60 | */ |
| 61 | struct eb_root { |
| 62 | eb_troot_t *b[EB_NODE_BRANCHES]; /* left and right branches */ |
| 63 | }; |
| 64 | |
| 65 | /* The eb_node contains the two parts, one for the leaf, which always exists, |
| 66 | * and one for the node, which remains unused in the very first node inserted |
| 67 | * into the tree. This structure is 20 bytes per node on 32-bit machines. Do |
| 68 | * not change the order, benchmarks have shown that it's optimal this way. |
| 69 | * Note: be careful about this struct's alignment if it gets included into |
| 70 | * another struct and some atomic ops are expected on the keys or the node. |
| 71 | */ |
| 72 | struct eb_node { |
| 73 | struct eb_root branches; /* branches, must be at the beginning */ |
| 74 | eb_troot_t *node_p; /* link node's parent */ |
| 75 | eb_troot_t *leaf_p; /* leaf node's parent */ |
| 76 | short int bit; /* link's bit position. */ |
| 77 | short unsigned int pfx; /* data prefix length, always related to leaf */ |
| 78 | } __attribute__((packed)); |
| 79 | |
| 80 | |
| 81 | /* The root of a tree is an eb_root initialized with both pointers NULL. |
| 82 | * During its life, only the left pointer will change. The right one will |
| 83 | * always remain NULL, which is the way we detect it. |
| 84 | */ |
| 85 | #define EB_ROOT \ |
| 86 | (struct eb_root) { \ |
| 87 | .b = {[0] = NULL, [1] = NULL }, \ |
| 88 | } |
| 89 | |
| 90 | #define EB_ROOT_UNIQUE \ |
| 91 | (struct eb_root) { \ |
| 92 | .b = {[0] = NULL, [1] = (void *)1 }, \ |
| 93 | } |
| 94 | |
| 95 | #define EB_TREE_HEAD(name) \ |
| 96 | struct eb_root name = EB_ROOT |
| 97 | |
| 98 | |
| 99 | /* |
| 100 | * types for eb32tree |
| 101 | */ |
| 102 | |
| 103 | #define EB32_ROOT EB_ROOT |
| 104 | #define EB32_TREE_HEAD EB_TREE_HEAD |
| 105 | |
| 106 | /* These types may sometimes already be defined */ |
| 107 | typedef unsigned int u32; |
| 108 | typedef signed int s32; |
| 109 | |
| 110 | /* This structure carries a node, a leaf, and a key. It must start with the |
| 111 | * eb_node so that it can be cast into an eb_node. We could also have put some |
| 112 | * sort of transparent union here to reduce the indirection level, but the fact |
| 113 | * is, the end user is not meant to manipulate internals, so this is pointless. |
| 114 | */ |
| 115 | struct eb32_node { |
| 116 | struct eb_node node; /* the tree node, must be at the beginning */ |
| 117 | MAYBE_ALIGN(sizeof(u32)); |
| 118 | u32 key; |
| 119 | } ALIGNED(sizeof(void*)); |
| 120 | |
| 121 | /* This structure carries a node, a leaf, a scope, and a key. It must start |
| 122 | * with the eb_node so that it can be cast into an eb_node. We could also |
| 123 | * have put some sort of transparent union here to reduce the indirection |
| 124 | * level, but the fact is, the end user is not meant to manipulate internals, |
| 125 | * so this is pointless. |
| 126 | * In case sizeof(void*)>=sizeof(long), we know there will be some padding after |
| 127 | * the leaf if it's unaligned. In this case we force the alignment on void* so |
| 128 | * that we prefer to have the padding before for more efficient accesses. |
| 129 | */ |
| 130 | struct eb32sc_node { |
| 131 | struct eb_node node; /* the tree node, must be at the beginning */ |
| 132 | MAYBE_ALIGN(sizeof(u32)); |
| 133 | u32 key; |
| 134 | ALWAYS_ALIGN(sizeof(void*)); |
| 135 | unsigned long node_s; /* visibility of this node's branches */ |
| 136 | unsigned long leaf_s; /* visibility of this node's leaf */ |
| 137 | } ALIGNED(sizeof(void*)); |
| 138 | |
| 139 | /* |
| 140 | * types for eb64tree |
| 141 | */ |
| 142 | |
| 143 | #define EB64_ROOT EB_ROOT |
| 144 | #define EB64_TREE_HEAD EB_TREE_HEAD |
| 145 | |
| 146 | /* These types may sometimes already be defined */ |
| 147 | typedef unsigned long long u64; |
| 148 | typedef signed long long s64; |
| 149 | |
| 150 | /* This structure carries a node, a leaf, and a key. It must start with the |
| 151 | * eb_node so that it can be cast into an eb_node. We could also have put some |
| 152 | * sort of transparent union here to reduce the indirection level, but the fact |
| 153 | * is, the end user is not meant to manipulate internals, so this is pointless. |
| 154 | * In case sizeof(void*)>=sizeof(u64), we know there will be some padding after |
| 155 | * the key if it's unaligned. In this case we force the alignment on void* so |
| 156 | * that we prefer to have the padding before for more efficient accesses. |
| 157 | */ |
| 158 | struct eb64_node { |
| 159 | struct eb_node node; /* the tree node, must be at the beginning */ |
| 160 | MAYBE_ALIGN(sizeof(u64)); |
| 161 | ALWAYS_ALIGN(sizeof(void*)); |
| 162 | u64 key; |
| 163 | } ALIGNED(sizeof(void*)); |
| 164 | |
| 165 | #define EBPT_ROOT EB_ROOT |
| 166 | #define EBPT_TREE_HEAD EB_TREE_HEAD |
| 167 | |
| 168 | /* on *almost* all platforms, a pointer can be cast into a size_t which is unsigned */ |
| 169 | #ifndef PTR_INT_TYPE |
| 170 | #define PTR_INT_TYPE size_t |
| 171 | #endif |
| 172 | |
| 173 | /* |
| 174 | * types for ebpttree |
| 175 | */ |
| 176 | |
| 177 | typedef PTR_INT_TYPE ptr_t; |
| 178 | |
| 179 | /* This structure carries a node, a leaf, and a key. It must start with the |
| 180 | * eb_node so that it can be cast into an eb_node. We could also have put some |
| 181 | * sort of transparent union here to reduce the indirection level, but the fact |
| 182 | * is, the end user is not meant to manipulate internals, so this is pointless. |
| 183 | * Internally, it is automatically cast as an eb32_node or eb64_node. |
| 184 | * We always align the key since the struct itself will be padded to the same |
| 185 | * size anyway. |
| 186 | */ |
| 187 | struct ebpt_node { |
| 188 | struct eb_node node; /* the tree node, must be at the beginning */ |
| 189 | ALWAYS_ALIGN(sizeof(void*)); |
| 190 | void *key; |
| 191 | } ALIGNED(sizeof(void*)); |
| 192 | |
| 193 | /* |
| 194 | * types for ebmbtree |
| 195 | */ |
| 196 | |
| 197 | #define EBMB_ROOT EB_ROOT |
| 198 | #define EBMB_TREE_HEAD EB_TREE_HEAD |
| 199 | |
| 200 | /* This structure carries a node, a leaf, and a key. It must start with the |
| 201 | * eb_node so that it can be cast into an eb_node. We could also have put some |
| 202 | * sort of transparent union here to reduce the indirection level, but the fact |
| 203 | * is, the end user is not meant to manipulate internals, so this is pointless. |
| 204 | * The 'node.bit' value here works differently from scalar types, as it contains |
| 205 | * the number of identical bits between the two branches. |
| 206 | * Note that we take a great care of making sure the key is located exactly at |
| 207 | * the end of the struct even if that involves holes before it, so that it |
| 208 | * always aliases any external key a user would append after. This is why the |
| 209 | * key uses the same alignment as the struct. |
| 210 | */ |
| 211 | struct ebmb_node { |
| 212 | struct eb_node node; /* the tree node, must be at the beginning */ |
| 213 | ALWAYS_ALIGN(sizeof(void*)); |
| 214 | unsigned char key[0]; /* the key, its size depends on the application */ |
| 215 | } ALIGNED(sizeof(void*)); |
| 216 | |
| 217 | #endif /* _EB_TREE_T_H */ |