blob: 0ab057b140173e2c0289afdc586f0a225cb4fcd2 [file] [log] [blame]
Willy Tarreauca308392017-11-05 13:31:29 +01001/*
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 Tarreau0cb98b22017-11-20 21:11:12 +010031#ifndef _EB32TREE_H
Willy Tarreauca308392017-11-05 13:31:29 +010032typedef unsigned int u32;
33typedef signed int s32;
Willy Tarreau0cb98b22017-11-20 21:11:12 +010034#endif
Willy Tarreauca308392017-11-05 13:31:29 +010035
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 Tarreauebf4ba42020-02-22 15:55:33 +010041 * 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 Tarreauca308392017-11-05 13:31:29 +010044 */
45struct eb32sc_node {
46 struct eb_node node; /* the tree node, must be at the beginning */
Willy Tarreauebf4ba42020-02-22 15:55:33 +010047 MAYBE_ALIGN(sizeof(u32));
Willy Tarreau319f0782018-10-21 06:52:11 +020048 u32 key;
Willy Tarreauebf4ba42020-02-22 15:55:33 +010049 ALWAYS_ALIGN(sizeof(void*));
Willy Tarreauca308392017-11-05 13:31:29 +010050 unsigned long node_s; /* visibility of this node's branches */
51 unsigned long leaf_s; /* visibility of this node's leaf */
Willy Tarreauebf4ba42020-02-22 15:55:33 +010052} ALIGNED(sizeof(void*));
Willy Tarreauca308392017-11-05 13:31:29 +010053
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 */
64REGPRM2 struct eb32sc_node *eb32sc_lookup_ge(struct eb_root *root, u32 x, unsigned long scope);
Willy Tarreau8878b6c2017-11-05 21:23:21 +010065REGPRM2 struct eb32sc_node *eb32sc_lookup_ge_or_first(struct eb_root *root, u32 x, unsigned long scope);
Willy Tarreauca308392017-11-05 13:31:29 +010066REGPRM2 struct eb32sc_node *eb32sc_insert(struct eb_root *root, struct eb32sc_node *new, unsigned long scope);
67void eb32sc_delete(struct eb32sc_node *node);
68
Willy Tarreaud1d55ac2017-11-05 14:33:01 +010069/* 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 Tarreauca308392017-11-05 13:31:29 +010073 */
Willy Tarreaud1d55ac2017-11-05 14:33:01 +010074static inline struct eb32sc_node *eb32sc_walk_down_left(eb_troot_t *start, unsigned long scope)
Willy Tarreauca308392017-11-05 13:31:29 +010075{
Willy Tarreaud1d55ac2017-11-05 14:33:01 +010076 struct eb_root *root;
77 struct eb_node *node;
Willy Tarreau52743302017-11-13 18:55:44 +010078 struct eb32sc_node *eb32;
Willy Tarreaud1d55ac2017-11-05 14:33:01 +010079
80 if (unlikely(!start))
81 return NULL;
82
Willy Tarreau52743302017-11-13 18:55:44 +010083 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 Tarreaud1d55ac2017-11-05 14:33:01 +0100102
Willy Tarreau52743302017-11-13 18:55:44 +0100103 /* 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 Tarreaud1d55ac2017-11-05 14:33:01 +0100109
Willy Tarreau52743302017-11-13 18:55:44 +0100110 /* 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 Tarreauca308392017-11-05 13:31:29 +0100116}
117
Willy Tarreauf6ac3652017-11-13 19:13:06 +0100118/* Return next node in the tree, starting with tagged parent <start>, or NULL if none */
119static inline struct eb32sc_node *eb32sc_next_with_parent(eb_troot_t *start, unsigned long scope)
Willy Tarreauca308392017-11-05 13:31:29 +0100120{
Willy Tarreauf6ac3652017-11-13 19:13:06 +0100121 while (eb_gettag(start) != EB_LEFT)
Willy Tarreau52743302017-11-13 18:55:44 +0100122 /* Walking up from right branch, so we cannot be below root */
Willy Tarreauf6ac3652017-11-13 19:13:06 +0100123 start = (eb_root_to_node(eb_untag(start, EB_RGHT)))->node_p;
Willy Tarreauca308392017-11-05 13:31:29 +0100124
Willy Tarreau52743302017-11-13 18:55:44 +0100125 /* Note that <t> cannot be NULL at this stage */
Willy Tarreauf6ac3652017-11-13 19:13:06 +0100126 start = (eb_untag(start, EB_LEFT))->b[EB_RGHT];
127 if (eb_clrtag(start) == NULL)
Willy Tarreau52743302017-11-13 18:55:44 +0100128 return NULL;
Willy Tarreauca308392017-11-05 13:31:29 +0100129
Willy Tarreauf6ac3652017-11-13 19:13:06 +0100130 return eb32sc_walk_down_left(start, scope);
131}
132
133/* Return next node in the tree, or NULL if none */
134static 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 Tarreauca308392017-11-05 13:31:29 +0100137}
138
139/* Return leftmost node in the tree, or NULL if none */
140static inline struct eb32sc_node *eb32sc_first(struct eb_root *root, unsigned long scope)
141{
Willy Tarreaud1d55ac2017-11-05 14:33:01 +0100142 return eb32sc_walk_down_left(root->b[0], scope);
Willy Tarreauca308392017-11-05 13:31:29 +0100143}
144
145#endif /* _EB32SC_TREE_H */