public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
From: Sebastian Huber <sebastian.huber@embedded-brains.de>
To: newlib@sourceware.org
Subject: [PATCH 05/11] Remove from RB_REMOVE_COLOR some null checks
Date: Mon, 19 Oct 2020 18:05:18 +0200	[thread overview]
Message-ID: <20201019160522.15408-6-sebastian.huber@embedded-brains.de> (raw)
In-Reply-To: <20201019160522.15408-1-sebastian.huber@embedded-brains.de>

From: dougm <dougm@FreeBSD.org>

where the pointer checked is provably never null. Restructure the surrounding
code just enough to make the non-nullness obvious.

Reviewed by:	markj
Tested by:	pho
Differential Revision:	https://reviews.freebsd.org/D25089
---
 newlib/libc/include/sys/tree.h | 46 +++++++++++++++-------------------
 1 file changed, 20 insertions(+), 26 deletions(-)

diff --git a/newlib/libc/include/sys/tree.h b/newlib/libc/include/sys/tree.h
index d5adfdd4e..372c001b9 100644
--- a/newlib/libc/include/sys/tree.h
+++ b/newlib/libc/include/sys/tree.h
@@ -493,26 +493,23 @@ name##_RB_REMOVE_COLOR(struct name *head, struct type *parent)		\
 				RB_ROTATE_LEFT(head, parent, tmp, field);\
 				tmp = RB_RIGHT(parent, field);		\
 			}						\
-			if (!RB_ISRED(RB_LEFT(tmp, field), field) &&	\
-			    !RB_ISRED(RB_RIGHT(tmp, field), field)) {	\
-				RB_COLOR(tmp, field) = RB_RED;		\
-				elm = parent;				\
-				parent = RB_PARENT(elm, field);		\
-				continue;				\
-			}						\
-			if (!RB_ISRED(RB_RIGHT(tmp, field), field)) {	\
+			if (RB_ISRED(RB_LEFT(tmp, field), field)) {	\
 				struct type *oleft;			\
-				if ((oleft = RB_LEFT(tmp, field))	\
-				    != NULL)				\
-					RB_COLOR(oleft, field) = RB_BLACK; \
+				oleft = RB_LEFT(tmp, field);		\
+				RB_COLOR(oleft, field) = RB_BLACK;	\
 				RB_COLOR(tmp, field) = RB_RED;		\
 				RB_ROTATE_RIGHT(head, tmp, oleft, field); \
 				tmp = RB_RIGHT(parent, field);		\
+			} else if (!RB_ISRED(RB_RIGHT(tmp, field), field)) { \
+				RB_COLOR(tmp, field) = RB_RED;		\
+				elm = parent;				\
+				parent = RB_PARENT(elm, field);		\
+				continue;				\
 			}						\
+			if (RB_ISRED(RB_RIGHT(tmp, field), field))	\
+				RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK; \
 			RB_COLOR(tmp, field) = RB_COLOR(parent, field);	\
 			RB_COLOR(parent, field) = RB_BLACK;		\
-			if (RB_RIGHT(tmp, field))			\
-				RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK; \
 			RB_ROTATE_LEFT(head, parent, tmp, field);	\
 			elm = RB_ROOT(head);				\
 			break;						\
@@ -523,26 +520,23 @@ name##_RB_REMOVE_COLOR(struct name *head, struct type *parent)		\
 				RB_ROTATE_RIGHT(head, parent, tmp, field);\
 				tmp = RB_LEFT(parent, field);		\
 			}						\
-			if (!RB_ISRED(RB_LEFT(tmp, field), field) &&	\
-			    !RB_ISRED(RB_RIGHT(tmp, field), field)) {	\
-				RB_COLOR(tmp, field) = RB_RED;		\
-				elm = parent;				\
-				parent = RB_PARENT(elm, field);		\
-				continue;				\
-			}						\
-			if (!RB_ISRED(RB_LEFT(tmp, field), field)) {	\
+			if (RB_ISRED(RB_RIGHT(tmp, field), field)) {	\
 				struct type *oright;			\
-				if ((oright = RB_RIGHT(tmp, field))	\
-				    != NULL)				\
-					RB_COLOR(oright, field) = RB_BLACK; \
+				oright = RB_RIGHT(tmp, field);		\
+				RB_COLOR(oright, field) = RB_BLACK;	\
 				RB_COLOR(tmp, field) = RB_RED;		\
 				RB_ROTATE_LEFT(head, tmp, oright, field); \
 				tmp = RB_LEFT(parent, field);		\
+			} else if (!RB_ISRED(RB_LEFT(tmp, field), field)) { \
+				RB_COLOR(tmp, field) = RB_RED;		\
+				elm = parent;				\
+				parent = RB_PARENT(elm, field);		\
+				continue;				\
 			}						\
+			if (RB_ISRED(RB_LEFT(tmp, field), field))	\
+				RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK; \
 			RB_COLOR(tmp, field) = RB_COLOR(parent, field);	\
 			RB_COLOR(parent, field) = RB_BLACK;		\
-			if (RB_LEFT(tmp, field))			\
-				RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK; \
 			RB_ROTATE_RIGHT(head, parent, tmp, field);	\
 			elm = RB_ROOT(head);				\
 			break;						\
-- 
2.26.2


  parent reply	other threads:[~2020-10-19 16:05 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-19 16:05 [PATCH 00/11] Synchronize <sys/tree.h> with FreeBSD Sebastian Huber
2020-10-19 16:05 ` [PATCH 01/11] Add RB_REINSERT(3), a low overhead alternative to Sebastian Huber
2020-10-19 16:05 ` [PATCH 02/11] Correct the use of RB_AUGMENT in the RB_TREE Sebastian Huber
2020-10-19 16:05 ` [PATCH 03/11] For the case when RB_REMOVE requires a nontrivial Sebastian Huber
2020-10-19 16:05 ` [PATCH 04/11] RB_REMOVE invokes RB_REMOVE_COLOR either when Sebastian Huber
2020-10-19 16:05 ` Sebastian Huber [this message]
2020-10-19 16:05 ` [PATCH 06/11] To reduce the size of an rb_node, drop the color Sebastian Huber
2020-10-19 16:05 ` [PATCH 07/11] Restore an RB_COLOR macro, for the benefit of Sebastian Huber
2020-10-19 16:05 ` [PATCH 08/11] Fixup r361997 by balancing parens. Duh Sebastian Huber
2020-10-19 16:05 ` [PATCH 09/11] Linuxkpi uses the rb-tree structures Sebastian Huber
2020-10-26  9:52 ` [PATCH 00/11] Synchronize <sys/tree.h> with FreeBSD Corinna Vinschen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201019160522.15408-6-sebastian.huber@embedded-brains.de \
    --to=sebastian.huber@embedded-brains.de \
    --cc=newlib@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).