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