RTEMS  5.1
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 #define rb_node RBTree_Node
21 
22 #define rb_left Node.rbe_left
23 
24 #define rb_right Node.rbe_right
25 
26 /*
27  * Getting rid of this placeholder structure is a bit difficult. The use of
28  * this placeholder struct may lead to bugs with link-time optimization due to
29  * a strict aliasing violation.
30  *
31  * A common use of this API is a direct access of the rb_node member to get the
32  * root node of the tree. So, this cannot be changed.
33  *
34  * The red-black tree implementation is provided by <sys/tree.h> and we have
35  *
36  * struct RBTree_Control {
37  * struct RBTree_Node *rbh_root;
38  * };
39  *
40  * The member name rbh_root is fixed by the <sys/tree.h> API. To use
41  * RBTree_Control directly we would need two defines:
42  *
43  * #define rb_root RBTree_Control
44  * #define rb_node rbh_root
45  *
46  * We already have an rb_node define to RBTree_Node, see above.
47  */
48 struct rb_root {
49  RBTree_Node *rb_node;
50 };
51 
52 RTEMS_STATIC_ASSERT(
53  sizeof( struct rb_root ) == sizeof( RBTree_Control ),
54  rb_root_size
55 );
56 
57 RTEMS_STATIC_ASSERT(
58  offsetof( struct rb_root, rb_node ) == offsetof( RBTree_Control, rbh_root ),
59  rb_root_node
60 );
61 
62 #undef RB_ROOT
63 #define RB_ROOT ( (struct rb_root) { NULL } )
64 
65 #define rb_entry( p, container, field ) RTEMS_CONTAINER_OF( p, container, field )
66 
67 static inline void rb_insert_color( struct rb_node *node, struct rb_root *root)
68 {
69  _RBTree_Insert_color( (RBTree_Control *) root, node );
70 }
71 
72 static inline void rb_erase( struct rb_node *node, struct rb_root *root )
73 {
74  _RBTree_Extract( (RBTree_Control *) root, node );
75 }
76 
77 static inline struct rb_node *rb_next( struct rb_node *node )
78 {
79  return _RBTree_Successor( node );
80 }
81 
82 static inline struct rb_node *rb_prev( struct rb_node *node )
83 {
84  return _RBTree_Predecessor( node );
85 }
86 
87 static inline struct rb_node *rb_first( struct rb_root *root )
88 {
89  return _RBTree_Minimum( (RBTree_Control *) root );
90 }
91 
92 static inline struct rb_node *rb_last( struct rb_root *root )
93 {
94  return _RBTree_Maximum( (RBTree_Control *) root );
95 }
96 
97 static inline void rb_replace_node(
98  struct rb_node *victim,
99  struct rb_node *replacement,
100  struct rb_root *root
101 )
102 {
104  (RBTree_Control *) root,
105  victim,
106  replacement
107  );
108 }
109 
110 static inline void rb_link_node(
111  struct rb_node *node,
112  struct rb_node *parent,
113  struct rb_node **link
114 )
115 {
116  _RBTree_Initialize_node( node );
117  _RBTree_Add_child( node, parent, link );
118 }
119 
120 static inline struct rb_node *rb_parent( struct rb_node *node )
121 {
122  return _RBTree_Parent( node );
123 }
124 
125 #define rbtree_postorder_for_each_entry_safe( node, next, root, field ) \
126  for ( \
127  node = _RBTree_Postorder_first( \
128  (RBTree_Control *) root, \
129  offsetof( __typeof__( *node ), field ) \
130  ); \
131  node != NULL && ( \
132  next = _RBTree_Postorder_next( \
133  &node->field, \
134  offsetof( __typeof__( *node ), field ) \
135  ), \
136  node != NULL \
137  ); \
138  node = next \
139  )
140 
141 #endif /* _LINUX_RBTREE_H */
RTEMS_INLINE_ROUTINE void _RBTree_Initialize_node(RBTree_Node *the_node)
Initializes a red-black tree node.
Definition: rbtree.h:129
RBTree_Node * _RBTree_Predecessor(const RBTree_Node *node)
Returns the predecessor of a node.
Definition: rbtreenext.c:51
Definition: rbtree.h:48
Red-black tree node.
Definition: rbtree.h:55
RBTree_Node * _RBTree_Minimum(const RBTree_Control *the_rbtree)
Returns the minimum node of the red-black tree.
Definition: rbtreenext.c:36
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
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_Successor(const RBTree_Node *node)
Returns the successor of a node.
Definition: rbtreenext.c:46
Constants and Structures Associated with the Red-Black Tree Handler.
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
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
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