From nobody Mon Apr 08 18:21:17 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4VCy8Z070Qz5HTp0; Mon, 8 Apr 2024 18:21:18 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4VCy8Y5HYzz4bMk; Mon, 8 Apr 2024 18:21:17 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1712600477; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=/mglCGXpXclDYHNObbpqMpEEuVK2K+FDfR+9U2GgrbA=; b=dpv9+YKB6/DYlhFgL+HfmDJ79LYs9fvtKPUGmreDnfyrz2c0eGlXAo1VRW8H6x0gRRlLYo VXtNSkNEqlFBKv1zCZHqb7HBrPmUYksGJjDLD9fzqt/6zK0t1h7Hh8MjZ0AIYNircBYpK2 B7fma3Pn73adheeKfpT+QRH6DeEnVNnuhhP821qHXyNeS8CS1qBg0Yf5Ag67GADJkSK4br bD3bzloeylOefC+Nuu7YhuNzIAURFMW+TCBkOXqP7o7ZDg98M81vaLKyBWtnzINah0F0+2 ksLkYhxGjgFPdZhFpOeqJPXea42FqcDF7ez17Jl+BEXI8M66sPs5klhpA7O2rA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1712600477; a=rsa-sha256; cv=none; b=TzbbAhkfTXPuB4nNZ+BneT7RJegMJZH/6TLClYKLVw7fgOL5vVy4FmuG8gpEjXIzk7GZND LFLyOSo3T7uNTifc80YXstujD8wjA/2fFyIJb0EjlqnSDxGDelUK2dKJEiDMfrfKkvxUGp hasuxuOy385XX6/d5RtyTg4t+b/f8VSinHb7XY1M3ifbclVLK9Sj67/ORHzJTR6lnfLfHF 9N0OBDdM8o02ECLWB7sCrWDtCosooTgoI/iIDdo6Dq6HNEDMcarUXe4GV4gnt5VQwcnCN0 OWQEoeBmPE3wcKlD4jix0i0X7a0cxZ0HjqhEgYROU2wWDBsbM0bzLqtAVzPSGw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1712600477; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=/mglCGXpXclDYHNObbpqMpEEuVK2K+FDfR+9U2GgrbA=; b=O4U6DlbPR2dzYj+cwxxz8KygzQCp3XaZi1LdsyB4qSzTdBcSPxG9HhfCF3tqIQxyBmX+RC 110IR8k4nTQ6Fn0NYVnMKLEHvvYjAQticBJXsoDiefMw/M5k9B5aMtxwPbImjI4gGHxJeg CXegQLZwtr8DqM2hEBV2L23zHfEVOZZs8pvpTz9ZbaN2wliRlaN9lumoW71LXRkMPTKivv EIebfWIYoh3blRFCdA+XeMIP1JlLg0voMFgel7jkxut/mwxdwtVX9mtnPNHymh6y0yNeYU d4fQeoL4waFe2aoXj2j4ORyQw+uIrrcws/JvAvO+8OlVidqzTgDuEhIq40//mQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4VCy8Y4vKZzM66; Mon, 8 Apr 2024 18:21:17 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 438ILHNW019143; Mon, 8 Apr 2024 18:21:17 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 438ILHOW019140; Mon, 8 Apr 2024 18:21:17 GMT (envelope-from git) Date: Mon, 8 Apr 2024 18:21:17 GMT Message-Id: <202404081821.438ILHOW019140@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= Subject: git: 7f479dee4897 - main - sys/queue.h: Add {LIST,TAILQ}_REPLACE(). List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 7f479dee48973541da8c869b888b953161301c3b Auto-Submitted: auto-generated The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=7f479dee48973541da8c869b888b953161301c3b commit 7f479dee48973541da8c869b888b953161301c3b Author: Dag-Erling Smørgrav AuthorDate: 2024-04-08 18:16:40 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2024-04-08 18:16:46 +0000 sys/queue.h: Add {LIST,TAILQ}_REPLACE(). MFC after: 1 week Obtained from: NetBSD Sponsored by: Klara, Inc. Reviewed by: cperciva, imp Differential Revision: https://reviews.freebsd.org/D44679 --- share/man/man3/Makefile | 2 ++ share/man/man3/queue.3 | 34 ++++++++++++++++++++++++++++++++-- sys/sys/queue.h | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/share/man/man3/Makefile b/share/man/man3/Makefile index b1f9cc087837..d82ff2bccd51 100644 --- a/share/man/man3/Makefile +++ b/share/man/man3/Makefile @@ -215,6 +215,7 @@ MLINKS+= queue.3 LIST_CLASS_ENTRY.3 \ queue.3 LIST_NEXT.3 \ queue.3 LIST_PREV.3 \ queue.3 LIST_REMOVE.3 \ + queue.3 LIST_REPLACE.3 \ queue.3 LIST_SWAP.3 \ queue.3 SLIST_CLASS_ENTRY.3 \ queue.3 SLIST_CLASS_HEAD.3 \ @@ -283,6 +284,7 @@ MLINKS+= queue.3 LIST_CLASS_ENTRY.3 \ queue.3 TAILQ_NEXT.3 \ queue.3 TAILQ_PREV.3 \ queue.3 TAILQ_REMOVE.3 \ + queue.3 TAILQ_REPLACE.3 \ queue.3 TAILQ_SWAP.3 MLINKS+= stats.3 stats_tpl_alloc.3 \ stats.3 stats_tpl_fetch_allocid.3 \ diff --git a/share/man/man3/queue.3 b/share/man/man3/queue.3 index 41d88cb6ffce..be890fd2cc88 100644 --- a/share/man/man3/queue.3 +++ b/share/man/man3/queue.3 @@ -25,7 +25,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd September 8, 2016 +.Dd April 8, 2024 .Dt QUEUE 3 .Os .Sh NAME @@ -90,6 +90,7 @@ .Nm LIST_NEXT , .Nm LIST_PREV , .Nm LIST_REMOVE , +.Nm LIST_REPLACE , .Nm LIST_SWAP , .Nm TAILQ_CLASS_ENTRY , .Nm TAILQ_CLASS_HEAD , @@ -116,6 +117,7 @@ .Nm TAILQ_NEXT , .Nm TAILQ_PREV , .Nm TAILQ_REMOVE , +.Nm TAILQ_REPLACE , .Nm TAILQ_SWAP .Nd implementations of singly-linked lists, singly-linked tail queues, lists and tail queues @@ -185,6 +187,7 @@ lists and tail queues .Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME" .Fn LIST_PREV "TYPE *elm" "LIST_HEAD *head" "TYPE" "LIST_ENTRY NAME" .Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME" +.Fn LIST_REPLACE "TYPE *elm" "TYPE *new" "LIST_ENTRY NAME" .Fn LIST_SWAP "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME" .\" .Fn TAILQ_CLASS_ENTRY "CLASSTYPE" @@ -212,6 +215,7 @@ lists and tail queues .Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME" .Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME" .Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME" +.Fn TAILQ_REPLACE "TAILQ_HEAD *head" "TYPE *elm" "TYPE *new" "TAILQ_ENTRY NAME" .Fn TAILQ_SWAP "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TYPE" "TAILQ_ENTRY NAME" .\" .Sh DESCRIPTION @@ -963,6 +967,17 @@ removes the element from the list. .Pp The macro +.Fn LIST_REPLACE +replaces the element +.Fa elm +with +.Fa new +in the list. +The element +.Fa new +must not already be on a list. +.Pp +The macro .Nm LIST_SWAP swaps the contents of .Fa head1 @@ -1221,6 +1236,17 @@ removes the element from the tail queue. .Pp The macro +.Fn TAILQ_REPLACE +replaces the element +.Fa elm +with +.Fa new +in the tail queue. +The element +.Fa new +must not already be on a list. +.Pp +The macro .Nm TAILQ_SWAP swaps the contents of .Fa head1 @@ -1235,7 +1261,7 @@ struct entry { ... TAILQ_ENTRY(entry) entries; /* Tail queue. */ ... -} *n1, *n2, *n3, *np; +} *n1, *n2, *n3, *n4, *np; TAILQ_INIT(&head); /* Initialize the queue. */ @@ -1253,6 +1279,10 @@ TAILQ_INSERT_BEFORE(n2, n3, entries); TAILQ_REMOVE(&head, n2, entries); /* Deletion. */ free(n2); + +n4 = malloc(sizeof(struct entry)); /* Replacement. */ +TAILQ_REPLACE(&head, n3, n4, entries); +free(n3); /* Forward traversal. */ TAILQ_FOREACH(np, &head, entries) np-> ... diff --git a/sys/sys/queue.h b/sys/sys/queue.h index 2b11d54cdd45..0479d780fd85 100644 --- a/sys/sys/queue.h +++ b/sys/sys/queue.h @@ -110,6 +110,7 @@ * _REMOVE_AFTER + - + - * _REMOVE_HEAD + + + + * _REMOVE s + s + + * _REPLACE - + - + * _SWAP + + + + * */ @@ -609,6 +610,21 @@ struct { \ TRASHIT(*oldprev); \ } while (0) +#define LIST_REPLACE(elm, elm2, field) do { \ + QMD_SAVELINK(oldnext, (elm)->field.le_next); \ + QMD_SAVELINK(oldprev, (elm)->field.le_prev); \ + QMD_LIST_CHECK_NEXT(elm, field); \ + QMD_LIST_CHECK_PREV(elm, field); \ + LIST_NEXT((elm2), field) = LIST_NEXT((elm), field); \ + if (LIST_NEXT((elm2), field) != NULL) \ + LIST_NEXT((elm2), field)->field.le_prev = \ + &(elm2)->field.le_next; \ + (elm2)->field.le_prev = (elm)->field.le_prev; \ + *(elm2)->field.le_prev = (elm2); \ + TRASHIT(*oldnext); \ + TRASHIT(*oldprev); \ +} while (0) + #define LIST_SWAP(head1, head2, type, field) do { \ QUEUE_TYPEOF(type) *swap_tmp = LIST_FIRST(head1); \ LIST_FIRST((head1)) = LIST_FIRST((head2)); \ @@ -863,6 +879,24 @@ struct { \ QMD_TRACE_ELEM(&(elm)->field); \ } while (0) +#define TAILQ_REPLACE(head, elm, elm2, field) do { \ + QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \ + QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \ + QMD_TAILQ_CHECK_NEXT(elm, field); \ + QMD_TAILQ_CHECK_PREV(elm, field); \ + TAILQ_NEXT((elm2), field) = TAILQ_NEXT((elm), field); \ + if (TAILQ_NEXT((elm2), field) != TAILQ_END(head)) \ + TAILQ_NEXT((elm2), field)->field.tqe_prev = \ + &(elm2)->field.tqe_next; \ + else \ + (head)->tqh_last = &(elm2)->field.tqe_next; \ + (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \ + *(elm2)->field.tqe_prev = (elm2); \ + TRASHIT(*oldnext); \ + TRASHIT(*oldprev); \ + QMD_TRACE_ELEM(&(elm)->field); \ +} while (0) + #define TAILQ_SWAP(head1, head2, type, field) do { \ QUEUE_TYPEOF(type) *swap_first = (head1)->tqh_first; \ QUEUE_TYPEOF(type) **swap_last = (head1)->tqh_last; \