RTEMS 6.1-rc4
Loading...
Searching...
No Matches
rbtree.h
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/*
4 * Copyright (c) 2015 embedded brains GmbH & Co. KG
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef _LINUX_RBTREE_H
29#define _LINUX_RBTREE_H
30
31#include <rtems/score/rbtree.h>
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37#define rb_node RBTree_Node
38
39#define rb_left Node.rbe_left
40
41#define rb_right Node.rbe_right
42
43/*
44 * Getting rid of this placeholder structure is a bit difficult. The use of
45 * this placeholder struct may lead to bugs with link-time optimization due to
46 * a strict aliasing violation.
47 *
48 * A common use of this API is a direct access of the rb_node member to get the
49 * root node of the tree. So, this cannot be changed.
50 *
51 * The red-black tree implementation is provided by <sys/tree.h> and we have
52 *
53 * struct RBTree_Control {
54 * struct RBTree_Node *rbh_root;
55 * };
56 *
57 * The member name rbh_root is fixed by the <sys/tree.h> API. To use
58 * RBTree_Control directly we would need two defines:
59 *
60 * #define rb_root RBTree_Control
61 * #define rb_node rbh_root
62 *
63 * We already have an rb_node define to RBTree_Node, see above.
64 */
65struct rb_root {
66 RBTree_Node *rb_node;
67};
68
70 sizeof( struct rb_root ) == sizeof( RBTree_Control ),
71 rb_root_size
72);
73
75 offsetof( struct rb_root, rb_node ) == offsetof( RBTree_Control, rbh_root ),
76 rb_root_node
77);
78
79#undef RTEMS_RB_ROOT
80#define RTEMS_RB_ROOT ( (struct rb_root) { NULL } )
81
82#define rb_entry( p, container, field ) RTEMS_CONTAINER_OF( p, container, field )
83
84static inline void rb_insert_color( struct rb_node *node, struct rb_root *root)
85{
86 _RBTree_Insert_color( (RBTree_Control *) root, node );
87}
88
89static inline void rb_erase( struct rb_node *node, struct rb_root *root )
90{
91 _RBTree_Extract( (RBTree_Control *) root, node );
92}
93
94static inline struct rb_node *rb_next( struct rb_node *node )
95{
96 return _RBTree_Successor( node );
97}
98
99static inline struct rb_node *rb_prev( struct rb_node *node )
100{
101 return _RBTree_Predecessor( node );
102}
103
104static inline struct rb_node *rb_first( struct rb_root *root )
105{
106 return _RBTree_Minimum( (RBTree_Control *) root );
107}
108
109static inline struct rb_node *rb_last( struct rb_root *root )
110{
111 return _RBTree_Maximum( (RBTree_Control *) root );
112}
113
114static inline void rb_replace_node(
115 struct rb_node *victim,
116 struct rb_node *replacement,
117 struct rb_root *root
118)
119{
121 (RBTree_Control *) root,
122 victim,
123 replacement
124 );
125}
126
127static inline void rb_link_node(
128 struct rb_node *node,
129 struct rb_node *parent,
130 struct rb_node **link
131)
132{
133 _RBTree_Initialize_node( node );
134 _RBTree_Add_child( node, parent, link );
135}
136
137static inline struct rb_node *rb_parent( struct rb_node *node )
138{
139 return _RBTree_Parent( node );
140}
141
142#define rbtree_postorder_for_each_entry_safe( node, next, root, field ) \
143 for ( \
144 node = _RBTree_Postorder_first( \
145 (RBTree_Control *) root, \
146 offsetof( __typeof__( *node ), field ) \
147 ); \
148 node != NULL && ( \
149 next = _RBTree_Postorder_next( \
150 &node->field, \
151 offsetof( __typeof__( *node ), field ) \
152 ), \
153 node != NULL \
154 ); \
155 node = next \
156 )
157
158#ifdef __cplusplus
159}
160#endif
161
162#endif /* _LINUX_RBTREE_H */
#define RTEMS_STATIC_ASSERT(_cond, _msg)
It is defined if a static analysis run is performed.
Definition: basedefs.h:841
RBTree_Node * _RBTree_Predecessor(const RBTree_Node *node)
Returns the predecessor of a node.
Definition: rbtreeprev.c:46
void _RBTree_Extract(RBTree_Control *the_rbtree, RBTree_Node *the_node)
Extracts (removes) the node from the red-black tree.
Definition: rbtreeextract.c:63
RBTree_Node * _RBTree_Maximum(const RBTree_Control *the_rbtree)
Returns the maximum node of the red-black tree.
Definition: rbtreemax.c:43
RBTree_Node * _RBTree_Successor(const RBTree_Node *node)
Returns the successor of a node.
Definition: rbtreenext.c:47
void _RBTree_Insert_color(RBTree_Control *the_rbtree, RBTree_Node *the_node)
Rebalances the red-black tree after insertion of the node.
void _RBTree_Replace_node(RBTree_Control *the_rbtree, RBTree_Node *victim, RBTree_Node *replacement)
Replaces a node in the red-black tree without a rebalance.
Definition: rbtreereplace.c:43
RBTree_Node * _RBTree_Minimum(const RBTree_Control *the_rbtree)
Returns the minimum node of the red-black tree.
Definition: rbtreemin.c:43
This header file provides interfaces of the Red-Black Tree Handler which are used by the implementati...
Red-black tree node.
Definition: rbtree.h:73
Definition: rbtree.h:65