RTEMS 5.2
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
rbtree.h
1/*
2 * Copyright (c) 2015 embedded brains GmbH. All rights reserved.
3 *
4 * embedded brains GmbH
5 * Dornierstr. 4
6 * 82178 Puchheim
7 * Germany
8 * <rtems@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.org/license/LICENSE.
13 */
14
15#ifndef _LINUX_RBTREE_H
16#define _LINUX_RBTREE_H
17
18#include <rtems/score/rbtree.h>
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24#define rb_node RBTree_Node
25
26#define rb_left Node.rbe_left
27
28#define rb_right Node.rbe_right
29
30/*
31 * Getting rid of this placeholder structure is a bit difficult. The use of
32 * this placeholder struct may lead to bugs with link-time optimization due to
33 * a strict aliasing violation.
34 *
35 * A common use of this API is a direct access of the rb_node member to get the
36 * root node of the tree. So, this cannot be changed.
37 *
38 * The red-black tree implementation is provided by <sys/tree.h> and we have
39 *
40 * struct RBTree_Control {
41 * struct RBTree_Node *rbh_root;
42 * };
43 *
44 * The member name rbh_root is fixed by the <sys/tree.h> API. To use
45 * RBTree_Control directly we would need two defines:
46 *
47 * #define rb_root RBTree_Control
48 * #define rb_node rbh_root
49 *
50 * We already have an rb_node define to RBTree_Node, see above.
51 */
52struct rb_root {
53 RBTree_Node *rb_node;
54};
55
56RTEMS_STATIC_ASSERT(
57 sizeof( struct rb_root ) == sizeof( RBTree_Control ),
58 rb_root_size
59);
60
61RTEMS_STATIC_ASSERT(
62 offsetof( struct rb_root, rb_node ) == offsetof( RBTree_Control, rbh_root ),
63 rb_root_node
64);
65
66#undef RB_ROOT
67#define RB_ROOT ( (struct rb_root) { NULL } )
68
69#define rb_entry( p, container, field ) RTEMS_CONTAINER_OF( p, container, field )
70
71static inline void rb_insert_color( struct rb_node *node, struct rb_root *root)
72{
73 _RBTree_Insert_color( (RBTree_Control *) root, node );
74}
75
76static inline void rb_erase( struct rb_node *node, struct rb_root *root )
77{
78 _RBTree_Extract( (RBTree_Control *) root, node );
79}
80
81static inline struct rb_node *rb_next( struct rb_node *node )
82{
83 return _RBTree_Successor( node );
84}
85
86static inline struct rb_node *rb_prev( struct rb_node *node )
87{
88 return _RBTree_Predecessor( node );
89}
90
91static inline struct rb_node *rb_first( struct rb_root *root )
92{
93 return _RBTree_Minimum( (RBTree_Control *) root );
94}
95
96static inline struct rb_node *rb_last( struct rb_root *root )
97{
98 return _RBTree_Maximum( (RBTree_Control *) root );
99}
100
101static inline void rb_replace_node(
102 struct rb_node *victim,
103 struct rb_node *replacement,
104 struct rb_root *root
105)
106{
108 (RBTree_Control *) root,
109 victim,
110 replacement
111 );
112}
113
114static inline void rb_link_node(
115 struct rb_node *node,
116 struct rb_node *parent,
117 struct rb_node **link
118)
119{
121 _RBTree_Add_child( node, parent, link );
122}
123
124static inline struct rb_node *rb_parent( struct rb_node *node )
125{
126 return _RBTree_Parent( node );
127}
128
129#define rbtree_postorder_for_each_entry_safe( node, next, root, field ) \
130 for ( \
131 node = _RBTree_Postorder_first( \
132 (RBTree_Control *) root, \
133 offsetof( __typeof__( *node ), field ) \
134 ); \
135 node != NULL && ( \
136 next = _RBTree_Postorder_next( \
137 &node->field, \
138 offsetof( __typeof__( *node ), field ) \
139 ), \
140 node != NULL \
141 ); \
142 node = next \
143 )
144
145#ifdef __cplusplus
146}
147#endif
148
149#endif /* _LINUX_RBTREE_H */
RBTree_Node * _RBTree_Predecessor(const RBTree_Node *node)
Returns the predecessor of a node.
Definition: rbtreenext.c:51
RTEMS_INLINE_ROUTINE void _RBTree_Add_child(RBTree_Node *child, RBTree_Node *parent, RBTree_Node **link)
Adds a child node to a parent node.
Definition: rbtree.h:145
RTEMS_INLINE_ROUTINE RBTree_Node * _RBTree_Parent(const RBTree_Node *the_node)
Returns a pointer to the parent of this node.
Definition: rbtree.h:295
void _RBTree_Extract(RBTree_Control *the_rbtree, RBTree_Node *the_node)
Extracts (removes) the node from the red-black tree.
Definition: rbtreeextract.c:35
RBTree_Node * _RBTree_Maximum(const RBTree_Control *the_rbtree)
Returns the maximum node of the red-black tree.
Definition: rbtreenext.c:41
RBTree_Node * _RBTree_Successor(const RBTree_Node *node)
Returns the successor of a node.
Definition: rbtreenext.c:46
void _RBTree_Insert_color(RBTree_Control *the_rbtree, RBTree_Node *the_node)
Rebalances the red-black tree after insertion of the node.
Definition: rbtreeinsert.c:17
RTEMS_INLINE_ROUTINE void _RBTree_Initialize_node(RBTree_Node *the_node)
Initializes a red-black tree node.
Definition: rbtree.h:129
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:29
RBTree_Node * _RBTree_Minimum(const RBTree_Control *the_rbtree)
Returns the minimum node of the red-black tree.
Definition: rbtreenext.c:36
Constants and Structures Associated with the Red-Black Tree Handler.
Red-black tree node.
Definition: rbtree.h:55
Definition: rbtree.h:52