git: e8286eb29516 - main - sys/queue.h: add STAILQ_REVERSE

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Tue, 06 May 2025 17:15:05 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=e8286eb295165ea1852abe4ae355f85ad1ec3356

commit e8286eb295165ea1852abe4ae355f85ad1ec3356
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2025-05-03 08:13:14 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2025-05-06 17:14:56 +0000

    sys/queue.h: add STAILQ_REVERSE
    
    The implementation of the traditional interview question about in-place
    reversing of the single-linked list.
    
    Reviewed by:    markj, olce
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D50131
---
 sys/sys/queue.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/sys/sys/queue.h b/sys/sys/queue.h
index a29ad6b94f03..b2ba3d63bb0c 100644
--- a/sys/sys/queue.h
+++ b/sys/sys/queue.h
@@ -564,6 +564,21 @@ struct {								\
 		(head2)->stqh_last = &STAILQ_FIRST(head2);		\
 } while (0)
 
+#define	STAILQ_REVERSE(head, type, field) do {				\
+	if (STAILQ_EMPTY(head))						\
+		break;							\
+	QUEUE_TYPEOF(type) *_Var, *_Varp, *_Varn;			\
+	for (_Var = STAILQ_FIRST(head), _Varp = NULL;			\
+	    _Var != NULL;) {						\
+		_Varn = STAILQ_NEXT(_Var, field);			\
+		STAILQ_NEXT(_Var, field) = _Varp;			\
+		_Varp = _Var;						\
+		_Var = _Varn;						\
+	}								\
+	(head)->stqh_last = &STAILQ_NEXT(STAILQ_FIRST(head), field);	\
+	(head)->stqh_first = _Varp;					\
+} while (0)
+
 #define STAILQ_END(head)	NULL