From: Sebastian Huber <sebastian.huber@embedded-brains.de>
To: newlib@sourceware.org
Subject: [PATCH 01/11] Add RB_REINSERT(3), a low overhead alternative to
Date: Mon, 19 Oct 2020 18:05:14 +0200 [thread overview]
Message-ID: <20201019160522.15408-2-sebastian.huber@embedded-brains.de> (raw)
In-Reply-To: <20201019160522.15408-1-sebastian.huber@embedded-brains.de>
From: trasz <trasz@FreeBSD.org>
removing a node and reinserting it back with an updated key.
This is one of dependencies for the upcoming stats(3) code.
Reviewed by: cem
Obtained from: Netflix
MFC after: 2 weeks
Sponsored by: Klara Inc, Netflix
Differential Revision: https://reviews.freebsd.org/D21786
---
newlib/libc/include/sys/tree.h | 26 ++++++++++++++++++++++++--
1 file changed, 24 insertions(+), 2 deletions(-)
diff --git a/newlib/libc/include/sys/tree.h b/newlib/libc/include/sys/tree.h
index fd66ceba1..0eacf951d 100644
--- a/newlib/libc/include/sys/tree.h
+++ b/newlib/libc/include/sys/tree.h
@@ -393,7 +393,8 @@ struct { \
RB_PROTOTYPE_NFIND(name, type, attr); \
RB_PROTOTYPE_NEXT(name, type, attr); \
RB_PROTOTYPE_PREV(name, type, attr); \
- RB_PROTOTYPE_MINMAX(name, type, attr);
+ RB_PROTOTYPE_MINMAX(name, type, attr); \
+ RB_PROTOTYPE_REINSERT(name, type, attr);
#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) \
@@ -412,6 +413,8 @@ struct { \
attr struct type *name##_RB_PREV(struct type *)
#define RB_PROTOTYPE_MINMAX(name, type, attr) \
attr struct type *name##_RB_MINMAX(struct name *, int)
+#define RB_PROTOTYPE_REINSERT(name, type, attr) \
+ attr struct type *name##_RB_REINSERT(struct name *, struct type *)
/* Main rb operation.
* Moves node close to the key of elm to top
@@ -429,7 +432,9 @@ struct { \
RB_GENERATE_NFIND(name, type, field, cmp, attr) \
RB_GENERATE_NEXT(name, type, field, attr) \
RB_GENERATE_PREV(name, type, field, attr) \
- RB_GENERATE_MINMAX(name, type, field, attr)
+ RB_GENERATE_MINMAX(name, type, field, attr) \
+ RB_GENERATE_REINSERT(name, type, field, cmp, attr)
+
#define RB_GENERATE_INSERT_COLOR(name, type, field, attr) \
attr void \
@@ -758,6 +763,22 @@ name##_RB_MINMAX(struct name *head, int val) \
return (parent); \
}
+#define RB_GENERATE_REINSERT(name, type, field, cmp, attr) \
+attr struct type * \
+name##_RB_REINSERT(struct name *head, struct type *elm) \
+{ \
+ struct type *cmpelm; \
+ if (((cmpelm = RB_PREV(name, head, elm)) != NULL && \
+ cmp(cmpelm, elm) >= 0) || \
+ ((cmpelm = RB_NEXT(name, head, elm)) != NULL && \
+ cmp(elm, cmpelm) >= 0)) { \
+ /* XXXLAS: Remove/insert is heavy handed. */ \
+ RB_REMOVE(name, head, elm); \
+ return (RB_INSERT(name, head, elm)); \
+ } \
+ return (NULL); \
+} \
+
#define RB_NEGINF -1
#define RB_INF 1
@@ -769,6 +790,7 @@ name##_RB_MINMAX(struct name *head, int val) \
#define RB_PREV(name, x, y) name##_RB_PREV(y)
#define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
#define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
+#define RB_REINSERT(name, x, y) name##_RB_REINSERT(x, y)
#define RB_FOREACH(x, name, head) \
for ((x) = RB_MIN(name, head); \
--
2.26.2
next prev 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 ` Sebastian Huber [this message]
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 ` [PATCH 05/11] Remove from RB_REMOVE_COLOR some null checks Sebastian Huber
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-2-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).