Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:11

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  * Copyright (c) 2015 embedded brains GmbH & Co. KG
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 #ifndef _LINUX_RBTREE_H
0029 #define _LINUX_RBTREE_H
0030 
0031 #include <rtems/score/rbtree.h>
0032 
0033 #ifdef __cplusplus
0034 extern "C" {
0035 #endif
0036 
0037 #define rb_node RBTree_Node
0038 
0039 #define rb_left Node.rbe_left
0040 
0041 #define rb_right Node.rbe_right
0042 
0043 /*
0044  * Getting rid of this placeholder structure is a bit difficult.  The use of
0045  * this placeholder struct may lead to bugs with link-time optimization due to
0046  * a strict aliasing violation.
0047  *
0048  * A common use of this API is a direct access of the rb_node member to get the
0049  * root node of the tree. So, this cannot be changed.
0050  *
0051  * The red-black tree implementation is provided by <sys/tree.h> and we have
0052  *
0053  * struct RBTree_Control {
0054  *   struct RBTree_Node *rbh_root;
0055  * };
0056  *
0057  * The member name rbh_root is fixed by the <sys/tree.h> API.  To use
0058  * RBTree_Control directly we would need two defines:
0059  *
0060  * #define rb_root RBTree_Control
0061  * #define rb_node rbh_root
0062  *
0063  * We already have an rb_node define to RBTree_Node, see above.
0064  */
0065 struct rb_root {
0066   RBTree_Node *rb_node;
0067 };
0068 
0069 RTEMS_STATIC_ASSERT(
0070   sizeof( struct rb_root ) == sizeof( RBTree_Control ),
0071   rb_root_size
0072 );
0073 
0074 RTEMS_STATIC_ASSERT(
0075   offsetof( struct rb_root, rb_node ) == offsetof( RBTree_Control, rbh_root ),
0076   rb_root_node
0077 );
0078 
0079 #undef RTEMS_RB_ROOT
0080 #define RTEMS_RB_ROOT ( (struct rb_root) { NULL } )
0081 
0082 #define rb_entry( p, container, field ) RTEMS_CONTAINER_OF( p, container, field )
0083 
0084 static inline void rb_insert_color( struct rb_node *node, struct rb_root *root)
0085 {
0086   _RBTree_Insert_color( (RBTree_Control *) root, node );
0087 }
0088 
0089 static inline void rb_erase( struct rb_node *node, struct rb_root *root )
0090 {
0091   _RBTree_Extract( (RBTree_Control *) root, node );
0092 }
0093 
0094 static inline struct rb_node *rb_next( struct rb_node *node )
0095 {
0096   return _RBTree_Successor( node );
0097 }
0098 
0099 static inline struct rb_node *rb_prev( struct rb_node *node )
0100 {
0101   return _RBTree_Predecessor( node );
0102 }
0103 
0104 static inline struct rb_node *rb_first( struct rb_root *root )
0105 {
0106   return _RBTree_Minimum( (RBTree_Control *) root );
0107 }
0108 
0109 static inline struct rb_node *rb_last( struct rb_root *root )
0110 {
0111   return _RBTree_Maximum( (RBTree_Control *) root );
0112 }
0113 
0114 static inline void rb_replace_node(
0115   struct rb_node *victim,
0116   struct rb_node *replacement,
0117   struct rb_root *root
0118 )
0119 {
0120   _RBTree_Replace_node(
0121     (RBTree_Control *) root,
0122     victim,
0123     replacement
0124   );
0125 }
0126 
0127 static inline void rb_link_node(
0128   struct rb_node *node,
0129   struct rb_node *parent,
0130   struct rb_node **link
0131 )
0132 {
0133   _RBTree_Initialize_node( node );
0134   _RBTree_Add_child( node, parent, link );
0135 }
0136 
0137 static inline struct rb_node *rb_parent( struct rb_node *node )
0138 {
0139   return _RBTree_Parent( node );
0140 }
0141 
0142 #define rbtree_postorder_for_each_entry_safe( node, next, root, field ) \
0143   for ( \
0144     node = _RBTree_Postorder_first( \
0145       (RBTree_Control *) root, \
0146       offsetof( __typeof__( *node ), field ) \
0147     ); \
0148     node != NULL && ( \
0149       next = _RBTree_Postorder_next( \
0150         &node->field, \
0151         offsetof( __typeof__( *node ), field ) \
0152       ), \
0153       node != NULL \
0154     ); \
0155     node = next \
0156   )
0157 
0158 #ifdef __cplusplus
0159 }
0160 #endif
0161 
0162 #endif /* _LINUX_RBTREE_H */