2
* Elastic Binary Trees - macros and structures for operations on pointer nodes.
4
* (C) 2002-2009 - Willy Tarreau <w@1wt.eu>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program 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
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29
/* Return the structure of type <type> whose member <member> points to <ptr> */
30
#define ebpt_entry(ptr, type, member) container_of(ptr, type, member)
32
#define EBPT_ROOT EB_ROOT
33
#define EBPT_TREE_HEAD EB_TREE_HEAD
35
/* on *almost* all platforms, a pointer can be cast into a size_t which is unsigned */
37
#define PTR_INT_TYPE size_t
40
typedef PTR_INT_TYPE ptr_t;
42
/* This structure carries a node, a leaf, and a key. It must start with the
43
* eb_node so that it can be cast into an eb_node. We could also have put some
44
* sort of transparent union here to reduce the indirection level, but the fact
45
* is, the end user is not meant to manipulate internals, so this is pointless.
46
* Internally, it is automatically cast as an eb32_node or eb64_node.
49
struct eb_node node; /* the tree node, must be at the beginning */
54
* Exported functions and macros.
55
* Many of them are always inlined because they are extremely small, and
56
* are generally called at most once or twice in a program.
59
/* Return leftmost node in the tree, or NULL if none */
60
static forceinline struct ebpt_node *ebpt_first(struct eb_root *root)
62
return ebpt_entry(eb_first(root), struct ebpt_node, node);
65
/* Return rightmost node in the tree, or NULL if none */
66
static forceinline struct ebpt_node *ebpt_last(struct eb_root *root)
68
return ebpt_entry(eb_last(root), struct ebpt_node, node);
71
/* Return next node in the tree, or NULL if none */
72
static forceinline struct ebpt_node *ebpt_next(struct ebpt_node *ebpt)
74
return ebpt_entry(eb_next(&ebpt->node), struct ebpt_node, node);
77
/* Return previous node in the tree, or NULL if none */
78
static forceinline struct ebpt_node *ebpt_prev(struct ebpt_node *ebpt)
80
return ebpt_entry(eb_prev(&ebpt->node), struct ebpt_node, node);
83
/* Return next node in the tree, skipping duplicates, or NULL if none */
84
static forceinline struct ebpt_node *ebpt_next_unique(struct ebpt_node *ebpt)
86
return ebpt_entry(eb_next_unique(&ebpt->node), struct ebpt_node, node);
89
/* Return previous node in the tree, skipping duplicates, or NULL if none */
90
static forceinline struct ebpt_node *ebpt_prev_unique(struct ebpt_node *ebpt)
92
return ebpt_entry(eb_prev_unique(&ebpt->node), struct ebpt_node, node);
95
/* Delete node from the tree if it was linked in. Mark the node unused. Note
96
* that this function relies on a non-inlined generic function: eb_delete.
98
static forceinline void ebpt_delete(struct ebpt_node *ebpt)
100
eb_delete(&ebpt->node);
104
* The following functions are inlined but derived from the integer versions.
106
static forceinline struct ebpt_node *ebpt_lookup(struct eb_root *root, void *x)
108
if (sizeof(void *) == 4)
109
return (struct ebpt_node *)eb32_lookup(root, (u32)(PTR_INT_TYPE)x);
111
return (struct ebpt_node *)eb64_lookup(root, (u64)(PTR_INT_TYPE)x);
114
static forceinline struct ebpt_node *ebpt_lookup_le(struct eb_root *root, void *x)
116
if (sizeof(void *) == 4)
117
return (struct ebpt_node *)eb32_lookup_le(root, (u32)(PTR_INT_TYPE)x);
119
return (struct ebpt_node *)eb64_lookup_le(root, (u64)(PTR_INT_TYPE)x);
122
static forceinline struct ebpt_node *ebpt_lookup_ge(struct eb_root *root, void *x)
124
if (sizeof(void *) == 4)
125
return (struct ebpt_node *)eb32_lookup_ge(root, (u32)(PTR_INT_TYPE)x);
127
return (struct ebpt_node *)eb64_lookup_ge(root, (u64)(PTR_INT_TYPE)x);
130
static forceinline struct ebpt_node *ebpt_insert(struct eb_root *root, struct ebpt_node *new)
132
if (sizeof(void *) == 4)
133
return (struct ebpt_node *)eb32_insert(root, (struct eb32_node *)new);
135
return (struct ebpt_node *)eb64_insert(root, (struct eb64_node *)new);
139
* The following functions are less likely to be used directly, because
140
* their code is larger. The non-inlined version is preferred.
143
/* Delete node from the tree if it was linked in. Mark the node unused. */
144
static forceinline void __ebpt_delete(struct ebpt_node *ebpt)
146
__eb_delete(&ebpt->node);
149
static forceinline struct ebpt_node *__ebpt_lookup(struct eb_root *root, void *x)
151
if (sizeof(void *) == 4)
152
return (struct ebpt_node *)__eb32_lookup(root, (u32)(PTR_INT_TYPE)x);
154
return (struct ebpt_node *)__eb64_lookup(root, (u64)(PTR_INT_TYPE)x);
157
static forceinline struct ebpt_node *__ebpt_insert(struct eb_root *root, struct ebpt_node *new)
159
if (sizeof(void *) == 4)
160
return (struct ebpt_node *)__eb32_insert(root, (struct eb32_node *)new);
162
return (struct ebpt_node *)__eb64_insert(root, (struct eb64_node *)new);
165
#endif /* _EBPT_TREE_H */