From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dedi548.your-server.de (dedi548.your-server.de [85.10.215.148]) by sourceware.org (Postfix) with ESMTPS id A5F8B388EC1D for ; Mon, 19 Oct 2020 16:05:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org A5F8B388EC1D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embedded-brains.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=sebastian.huber@embedded-brains.de Received: from sslproxy02.your-server.de ([78.47.166.47]) by dedi548.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92.3) (envelope-from ) id 1kUXf6-0004Oq-Mh for newlib@sourceware.org; Mon, 19 Oct 2020 18:05:28 +0200 Received: from [82.100.198.138] (helo=mail.embedded-brains.de) by sslproxy02.your-server.de with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92) (envelope-from ) id 1kUXf1-0002nd-JO for newlib@sourceware.org; Mon, 19 Oct 2020 18:05:23 +0200 Received: from localhost (localhost.localhost [127.0.0.1]) by mail.embedded-brains.de (Postfix) with ESMTP id 4ACB62A1686 for ; Mon, 19 Oct 2020 18:04:11 +0200 (CEST) Received: from mail.embedded-brains.de ([127.0.0.1]) by localhost (zimbra.eb.localhost [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id j_vP44BAu7Zv for ; Mon, 19 Oct 2020 18:04:10 +0200 (CEST) Received: from localhost (localhost.localhost [127.0.0.1]) by mail.embedded-brains.de (Postfix) with ESMTP id 8190B2A1681 for ; Mon, 19 Oct 2020 18:04:10 +0200 (CEST) X-Virus-Scanned: amavisd-new at zimbra.eb.localhost Received: from mail.embedded-brains.de ([127.0.0.1]) by localhost (zimbra.eb.localhost [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 2I7TXNb4lucy for ; Mon, 19 Oct 2020 18:04:10 +0200 (CEST) Received: from linux-diu0.suse (unknown [192.168.96.161]) by mail.embedded-brains.de (Postfix) with ESMTP id 638252A1684 for ; Mon, 19 Oct 2020 18:04:10 +0200 (CEST) From: Sebastian Huber To: newlib@sourceware.org Subject: [PATCH 04/11] RB_REMOVE invokes RB_REMOVE_COLOR either when Date: Mon, 19 Oct 2020 18:05:17 +0200 Message-Id: <20201019160522.15408-5-sebastian.huber@embedded-brains.de> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201019160522.15408-1-sebastian.huber@embedded-brains.de> References: <20201019160522.15408-1-sebastian.huber@embedded-brains.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Authenticated-Sender: smtp-embedded@poldinet.de X-Virus-Scanned: Clear (ClamAV 0.102.4/25962/Mon Oct 19 15:57:02 2020) X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: newlib@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Newlib mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 19 Oct 2020 16:05:31 -0000 From: dougm child is red or child is null. In the first case, RB_REMOVE_COLOR just ch= anges the child to black and returns. With this change, RB_REMOVE handles that = case, and drops the child argument to RB_REMOVE_COLOR, since that value is alwa= ys null. RB_REMOVE_COLOR is changed to remove a couple of unneeded tests, and to eliminate some deep indentation. RB_ISRED is defined to combine a null check with a test for redness, to replace that combination in several places. Reviewed by: markj Tested by: pho Differential Revision: https://reviews.freebsd.org/D25032 --- newlib/libc/include/sys/tree.h | 137 ++++++++++++++++----------------- 1 file changed, 65 insertions(+), 72 deletions(-) diff --git a/newlib/libc/include/sys/tree.h b/newlib/libc/include/sys/tre= e.h index b1e5e23a7..d5adfdd4e 100644 --- a/newlib/libc/include/sys/tree.h +++ b/newlib/libc/include/sys/tree.h @@ -321,6 +321,7 @@ struct { \ #define RB_RIGHT(elm, field) (elm)->field.rbe_right #define RB_PARENT(elm, field) (elm)->field.rbe_parent #define RB_COLOR(elm, field) (elm)->field.rbe_color +#define RB_ISRED(elm, field) ((elm) !=3D NULL && RB_COLOR(elm, field) =3D= =3D RB_RED) #define RB_ROOT(head) (head)->rbh_root #define RB_EMPTY(head) (RB_ROOT(head) =3D=3D NULL) =20 @@ -396,7 +397,7 @@ struct { \ #define RB_PROTOTYPE_INSERT_COLOR(name, type, attr) \ attr void name##_RB_INSERT_COLOR(struct name *, struct type *) #define RB_PROTOTYPE_REMOVE_COLOR(name, type, attr) \ - attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct t= ype *) + attr void name##_RB_REMOVE_COLOR(struct name *, struct type *) #define RB_PROTOTYPE_REMOVE(name, type, attr) \ attr struct type *name##_RB_REMOVE(struct name *, struct type *) #define RB_PROTOTYPE_INSERT(name, type, attr) \ @@ -439,12 +440,11 @@ attr void \ name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \ { \ struct type *parent, *gparent, *tmp; \ - while ((parent =3D RB_PARENT(elm, field)) !=3D NULL && \ - RB_COLOR(parent, field) =3D=3D RB_RED) { \ + while (RB_ISRED((parent =3D RB_PARENT(elm, field)), field)) { \ gparent =3D RB_PARENT(parent, field); \ if (parent =3D=3D RB_LEFT(gparent, field)) { \ tmp =3D RB_RIGHT(gparent, field); \ - if (tmp && RB_COLOR(tmp, field) =3D=3D RB_RED) { \ + if (RB_ISRED(tmp, field)) { \ RB_COLOR(tmp, field) =3D RB_BLACK; \ RB_SET_BLACKRED(parent, gparent, field);\ elm =3D gparent; \ @@ -460,7 +460,7 @@ name##_RB_INSERT_COLOR(struct name *head, struct type= *elm) \ RB_ROTATE_RIGHT(head, gparent, tmp, field); \ } else { \ tmp =3D RB_LEFT(gparent, field); \ - if (tmp && RB_COLOR(tmp, field) =3D=3D RB_RED) { \ + if (RB_ISRED(tmp, field)) { \ RB_COLOR(tmp, field) =3D RB_BLACK; \ RB_SET_BLACKRED(parent, gparent, field);\ elm =3D gparent; \ @@ -481,11 +481,11 @@ name##_RB_INSERT_COLOR(struct name *head, struct ty= pe *elm) \ =20 #define RB_GENERATE_REMOVE_COLOR(name, type, field, attr) \ attr void \ -name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct ty= pe *elm) \ +name##_RB_REMOVE_COLOR(struct name *head, struct type *parent) \ { \ - struct type *tmp; \ - while ((elm =3D=3D NULL || RB_COLOR(elm, field) =3D=3D RB_BLACK) && \ - elm !=3D RB_ROOT(head)) { \ + struct type *elm, *tmp; \ + elm =3D NULL; \ + do { \ if (RB_LEFT(parent, field) =3D=3D elm) { \ tmp =3D RB_RIGHT(parent, field); \ if (RB_COLOR(tmp, field) =3D=3D RB_RED) { \ @@ -493,32 +493,29 @@ name##_RB_REMOVE_COLOR(struct name *head, struct ty= pe *parent, struct type *elm) RB_ROTATE_LEFT(head, parent, tmp, field);\ tmp =3D RB_RIGHT(parent, field); \ } \ - if ((RB_LEFT(tmp, field) =3D=3D NULL || \ - RB_COLOR(RB_LEFT(tmp, field), field) =3D=3D RB_BLACK) &&\ - (RB_RIGHT(tmp, field) =3D=3D NULL || \ - RB_COLOR(RB_RIGHT(tmp, field), field) =3D=3D RB_BLACK)) {\ + if (!RB_ISRED(RB_LEFT(tmp, field), field) && \ + !RB_ISRED(RB_RIGHT(tmp, field), field)) { \ RB_COLOR(tmp, field) =3D RB_RED; \ elm =3D parent; \ parent =3D RB_PARENT(elm, field); \ - } else { \ - if (RB_RIGHT(tmp, field) =3D=3D NULL || \ - RB_COLOR(RB_RIGHT(tmp, field), field) =3D=3D RB_BLACK) {\ - struct type *oleft; \ - if ((oleft =3D RB_LEFT(tmp, field)) \ - !=3D NULL) \ - RB_COLOR(oleft, field) =3D RB_BLACK;\ - RB_COLOR(tmp, field) =3D RB_RED; \ - RB_ROTATE_RIGHT(head, tmp, oleft, field);\ - tmp =3D RB_RIGHT(parent, field); \ - } \ - RB_COLOR(tmp, field) =3D RB_COLOR(parent, field);\ - RB_COLOR(parent, field) =3D RB_BLACK; \ - if (RB_RIGHT(tmp, field)) \ - RB_COLOR(RB_RIGHT(tmp, field), field) =3D RB_BLACK;\ - RB_ROTATE_LEFT(head, parent, tmp, field);\ - elm =3D RB_ROOT(head); \ - break; \ + continue; \ } \ + if (!RB_ISRED(RB_RIGHT(tmp, field), field)) { \ + struct type *oleft; \ + if ((oleft =3D RB_LEFT(tmp, field)) \ + !=3D NULL) \ + RB_COLOR(oleft, field) =3D RB_BLACK; \ + RB_COLOR(tmp, field) =3D RB_RED; \ + RB_ROTATE_RIGHT(head, tmp, oleft, field); \ + tmp =3D RB_RIGHT(parent, field); \ + } \ + RB_COLOR(tmp, field) =3D RB_COLOR(parent, field); \ + RB_COLOR(parent, field) =3D RB_BLACK; \ + if (RB_RIGHT(tmp, field)) \ + RB_COLOR(RB_RIGHT(tmp, field), field) =3D RB_BLACK; \ + RB_ROTATE_LEFT(head, parent, tmp, field); \ + elm =3D RB_ROOT(head); \ + break; \ } else { \ tmp =3D RB_LEFT(parent, field); \ if (RB_COLOR(tmp, field) =3D=3D RB_RED) { \ @@ -526,59 +523,54 @@ name##_RB_REMOVE_COLOR(struct name *head, struct ty= pe *parent, struct type *elm) RB_ROTATE_RIGHT(head, parent, tmp, field);\ tmp =3D RB_LEFT(parent, field); \ } \ - if ((RB_LEFT(tmp, field) =3D=3D NULL || \ - RB_COLOR(RB_LEFT(tmp, field), field) =3D=3D RB_BLACK) &&\ - (RB_RIGHT(tmp, field) =3D=3D NULL || \ - RB_COLOR(RB_RIGHT(tmp, field), field) =3D=3D RB_BLACK)) {\ + if (!RB_ISRED(RB_LEFT(tmp, field), field) && \ + !RB_ISRED(RB_RIGHT(tmp, field), field)) { \ RB_COLOR(tmp, field) =3D RB_RED; \ elm =3D parent; \ parent =3D RB_PARENT(elm, field); \ - } else { \ - if (RB_LEFT(tmp, field) =3D=3D NULL || \ - RB_COLOR(RB_LEFT(tmp, field), field) =3D=3D RB_BLACK) {\ - struct type *oright; \ - if ((oright =3D RB_RIGHT(tmp, field)) \ - !=3D NULL) \ - RB_COLOR(oright, field) =3D RB_BLACK;\ - RB_COLOR(tmp, field) =3D RB_RED; \ - RB_ROTATE_LEFT(head, tmp, oright, field);\ - tmp =3D RB_LEFT(parent, field); \ - } \ - RB_COLOR(tmp, field) =3D RB_COLOR(parent, field);\ - RB_COLOR(parent, field) =3D RB_BLACK; \ - if (RB_LEFT(tmp, field)) \ - RB_COLOR(RB_LEFT(tmp, field), field) =3D RB_BLACK;\ - RB_ROTATE_RIGHT(head, parent, tmp, field);\ - elm =3D RB_ROOT(head); \ - break; \ + continue; \ + } \ + if (!RB_ISRED(RB_LEFT(tmp, field), field)) { \ + struct type *oright; \ + if ((oright =3D RB_RIGHT(tmp, field)) \ + !=3D NULL) \ + RB_COLOR(oright, field) =3D RB_BLACK; \ + RB_COLOR(tmp, field) =3D RB_RED; \ + RB_ROTATE_LEFT(head, tmp, oright, field); \ + tmp =3D RB_LEFT(parent, field); \ } \ + RB_COLOR(tmp, field) =3D RB_COLOR(parent, field); \ + RB_COLOR(parent, field) =3D RB_BLACK; \ + if (RB_LEFT(tmp, field)) \ + RB_COLOR(RB_LEFT(tmp, field), field) =3D RB_BLACK; \ + RB_ROTATE_RIGHT(head, parent, tmp, field); \ + elm =3D RB_ROOT(head); \ + break; \ } \ - } \ - if (elm) \ - RB_COLOR(elm, field) =3D RB_BLACK; \ + } while (!RB_ISRED(elm, field) && parent !=3D NULL); \ + RB_COLOR(elm, field) =3D RB_BLACK; \ } =20 #define RB_GENERATE_REMOVE(name, type, field, attr) \ attr struct type * \ name##_RB_REMOVE(struct name *head, struct type *elm) \ { \ - struct type *child, *parent, *parent_old, *old =3D elm; \ + struct type *child, *old, *parent, *parent_old, *right; \ int color; \ + \ + old =3D elm; \ parent_old =3D parent =3D RB_PARENT(elm, field); \ + right =3D RB_RIGHT(elm, field); \ color =3D RB_COLOR(elm, field); \ - if (RB_LEFT(elm, field) =3D=3D NULL) { \ - elm =3D child =3D RB_RIGHT(elm, field); \ - if (elm !=3D NULL) \ - RB_PARENT(elm, field) =3D parent; \ - } else if (RB_RIGHT(elm, field) =3D=3D NULL) { \ + if (RB_LEFT(elm, field) =3D=3D NULL) \ + elm =3D child =3D right; \ + else if (right =3D=3D NULL) \ elm =3D child =3D RB_LEFT(elm, field); \ - RB_PARENT(elm, field) =3D parent; \ - } else { \ - elm =3D RB_RIGHT(old, field); \ - if ((child =3D RB_LEFT(elm, field)) =3D=3D NULL) { \ - child =3D RB_RIGHT(elm, field); \ + else { \ + if ((child =3D RB_LEFT(right, field)) =3D=3D NULL) { \ + child =3D RB_RIGHT(right, field); \ RB_RIGHT(old, field) =3D child; \ - parent =3D elm; \ + parent =3D elm =3D right; \ } else { \ do \ elm =3D child; \ @@ -586,8 +578,6 @@ name##_RB_REMOVE(struct name *head, struct type *elm)= \ child =3D RB_RIGHT(elm, field); \ parent =3D RB_PARENT(elm, field); \ RB_LEFT(parent, field) =3D child; \ - if (child !=3D NULL) \ - RB_PARENT(child, field) =3D parent; \ RB_PARENT(RB_RIGHT(old, field), field) =3D elm; \ } \ RB_PARENT(RB_LEFT(old, field), field) =3D elm; \ @@ -600,8 +590,11 @@ name##_RB_REMOVE(struct name *head, struct type *elm= ) \ RB_LEFT(parent_old, field) =3D elm; \ else \ RB_RIGHT(parent_old, field) =3D elm; \ - if (color =3D=3D RB_BLACK) \ - name##_RB_REMOVE_COLOR(head, parent, child); \ + if (child !=3D NULL) { \ + RB_PARENT(child, field) =3D parent; \ + RB_COLOR(child, field) =3D RB_BLACK; \ + } else if (color !=3D RB_RED && parent !=3D NULL) \ + name##_RB_REMOVE_COLOR(head, parent); \ while (parent !=3D NULL) { \ RB_AUGMENT(parent); \ parent =3D RB_PARENT(parent, field); \ --=20 2.26.2