[rtems commit] rbtree: Remove superfluous NULL pointer checks

Sebastian Huber sebh at rtems.org
Tue Jul 22 15:42:46 UTC 2014


Module:    rtems
Branch:    master
Commit:    d7a946937807076a84a4f07b4536e3eca30727d2
Changeset: http://git.rtems.org/rtems/commit/?id=d7a946937807076a84a4f07b4536e3eca30727d2

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Mon Jul 21 18:16:47 2014 +0200

rbtree: Remove superfluous NULL pointer checks

---

 cpukit/score/include/rtems/score/rbtree.h |   40 ++++++++++++++++------------
 cpukit/score/src/rbtree.c                 |    4 ---
 cpukit/score/src/rbtreeextract.c          |   11 --------
 cpukit/score/src/rbtreeinsert.c           |    4 ---
 testsuites/sptests/sprbtree01/init.c      |    3 --
 5 files changed, 23 insertions(+), 39 deletions(-)

diff --git a/cpukit/score/include/rtems/score/rbtree.h b/cpukit/score/include/rtems/score/rbtree.h
index a219a6e..7c88326 100644
--- a/cpukit/score/include/rtems/score/rbtree.h
+++ b/cpukit/score/include/rtems/score/rbtree.h
@@ -229,10 +229,9 @@ RBTree_Node *_RBTree_Find(
  * @param[in] is_unique If true, then reject nodes with a duplicate key, else
  *   otherwise.
  *
- *  @retval 0 Successfully inserted.
- *  @retval -1 NULL @a the_node.
- *  @retval RBTree_Node* if one with equal value to @a the_node 's key exists
- *          in an unique @a the_rbtree.
+ * @retval NULL Successfully inserted.
+ * @retval existing_node This is a unique insert and there exists a node with
+ *   an equal key in the tree already.
  */
 RBTree_Node *_RBTree_Insert(
   RBTree_Control *the_rbtree,
@@ -481,25 +480,32 @@ RTEMS_INLINE_ROUTINE RBTree_Node *_RBTree_Successor(
 }
 
 /**
- * @brief Get the first node.
+ * @brief Gets a node with an extremal key value.
  *
- * This function removes the minimum or maximum node from the_rbtree and
- * returns a pointer to that node.
+ * This function extracts a node with the minimum or maximum key value from
+ * tree and returns a pointer to that node if it exists.  In case multiple
+ * nodes with an extremal key value exist, then they are extracted in FIFO
+ * order.
  *
- * @param[in] the_rbtree is the rbtree to attempt to get the min node from.
- * @param[in] dir specifies whether to get minimum (0) or maximum (1)
- *
- * @return This method returns the min or max node on the rbtree, or NULL.
+ * @param[in] the_rbtree The red-black tree control.
+ * @param[in] dir Specifies whether to get a node with the minimum (RBT_LEFT)
+ *   or maximum (RBT_RIGHT) key value.
  *
- * @note This routine may return NULL if the RBTree is empty.
+ * @retval NULL The tree is empty.
+ * @retval extremal_node A node with the minimal or maximal key value on the
+ *   tree.
  */
 RTEMS_INLINE_ROUTINE RBTree_Node *_RBTree_Get(
-    RBTree_Control *the_rbtree,
-    RBTree_Direction dir
-    )
+  RBTree_Control *the_rbtree,
+  RBTree_Direction dir
+)
 {
-  RBTree_Node *the_node = the_rbtree->first[dir];
-  _RBTree_Extract(the_rbtree, the_node);
+  RBTree_Node *the_node = the_rbtree->first[ dir ];
+
+  if ( the_node != NULL ) {
+    _RBTree_Extract( the_rbtree, the_node );
+  }
+
   return the_node;
 }
 
diff --git a/cpukit/score/src/rbtree.c b/cpukit/score/src/rbtree.c
index d5f758c..064cc0c 100644
--- a/cpukit/score/src/rbtree.c
+++ b/cpukit/score/src/rbtree.c
@@ -34,10 +34,6 @@ void _RBTree_Initialize(
   size_t       count;
   RBTree_Node *next;
 
-  /* TODO: Error message? */
-  if ( !the_rbtree )
-    return;
-
   /* could do sanity checks here */
   _RBTree_Initialize_empty( the_rbtree );
 
diff --git a/cpukit/score/src/rbtreeextract.c b/cpukit/score/src/rbtreeextract.c
index e4278a4..0ea805b 100644
--- a/cpukit/score/src/rbtreeextract.c
+++ b/cpukit/score/src/rbtreeextract.c
@@ -11,7 +11,6 @@
 #endif
 
 #include <rtems/score/rbtreeimpl.h>
-#include <rtems/score/isr.h>
 
 /** @brief  Validate and fix-up tree properties after deleting a node
  *
@@ -91,13 +90,6 @@ static void _RBTree_Extract_validate( RBTree_Node *the_node )
     the_node->color = RBT_BLACK;
 }
 
-/** @brief Extract a Node (unprotected)
- *
- *  This routine extracts (removes) @a the_node from @a the_rbtree.
- *
- *  @note It does NOT disable interrupts to ensure the atomicity
- *        of the extract operation.
- */
 void _RBTree_Extract(
   RBTree_Control *the_rbtree,
   RBTree_Node    *the_node
@@ -107,9 +99,6 @@ void _RBTree_Extract(
   RBTree_Color     victim_color;
   RBTree_Direction dir;
 
-  if ( !the_node )
-    return;
-
   /* check if min needs to be updated */
   if ( the_node == the_rbtree->first[ RBT_LEFT ] ) {
     RBTree_Node *next;
diff --git a/cpukit/score/src/rbtreeinsert.c b/cpukit/score/src/rbtreeinsert.c
index c77c574..b31c8e7 100644
--- a/cpukit/score/src/rbtreeinsert.c
+++ b/cpukit/score/src/rbtreeinsert.c
@@ -11,7 +11,6 @@
 #endif
 
 #include <rtems/score/rbtreeimpl.h>
-#include <rtems/score/isr.h>
 
 /** @brief Validate and fix-up tree properties for a new insert/colored node
  *
@@ -67,9 +66,6 @@ RBTree_Node *_RBTree_Insert(
   bool            is_unique
 )
 {
-  if ( !the_node )
-    return (RBTree_Node *) -1;
-
   RBTree_Node *iter_node = the_rbtree->root;
 
   if ( !iter_node ) { /* special case: first node inserted */
diff --git a/testsuites/sptests/sprbtree01/init.c b/testsuites/sptests/sprbtree01/init.c
index acf7f68..ca79095 100644
--- a/testsuites/sptests/sprbtree01/init.c
+++ b/testsuites/sptests/sprbtree01/init.c
@@ -149,9 +149,6 @@ rtems_task Init(
   rb_insert_unique( &rbtree1, &node1.Node );
   rb_insert_unique( &rbtree1, &node2.Node );
 
-  p = rb_insert_unique( &rbtree1, NULL );
-  if (p != (void *)(-1))
-    puts( "INIT - FAILED NULL NODE INSERT" );
 
   _RBTree_Rotate(NULL, RBT_LEFT);
   i = (node1.Node.parent == &node2.Node);



More information about the vc mailing list