[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20231020102901.3354273-2-mszeredi@redhat.com>
Date: Fri, 20 Oct 2023 12:28:59 +0200
From: Miklos Szeredi <mszeredi@...hat.com>
To: linux-kernel@...r.kernel.org
Cc: Linus Torvalds <torvalds@...ux-foundation.org>,
Takashi Iwai <tiwai@...e.com>,
Mauro Carvalho Chehab <mchehab@...nel.org>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
Jakub Kicinski <kuba@...nel.org>,
Paolo Abeni <pabeni@...hat.com>,
Al Viro <viro@...iv.linux.org.uk>,
Christian Brauner <brauner@...nel.org>,
James Bottomley <James.Bottomley@...senpartnership.com>
Subject: [RFC PATCH 1/2] add list_for_each_entry_del()
Add a new list iteration macro that always takes the first element of the
list and removes it, until the list is empty.
Add a list_for_each_entry_del_init() variant too.
This is a common pattern.
Signed-off-by: Miklos Szeredi <mszeredi@...hat.com>
---
include/linux/list.h | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/include/linux/list.h b/include/linux/list.h
index 164b4d0e9d2a..db23a9ee98f0 100644
--- a/include/linux/list.h
+++ b/include/linux/list.h
@@ -921,6 +921,35 @@ static inline size_t list_count_nodes(struct list_head *head)
#define list_safe_reset_next(pos, n, member) \
n = list_next_entry(pos, member)
+/**
+ * list_for_each_entry_del - iterate list and remove elements
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the list_head within the struct.
+ *
+ * Delete first element from list before the iteration. Iterate until
+ * the list is empty.
+ */
+#define list_for_each_entry_del(pos, head, member) \
+ while (!list_empty(head) && \
+ ({ pos = list_first_entry(head, typeof(*(pos)), member);\
+ list_del(&(pos)->member); 1; }))
+
+/**
+ * list_for_each_entry_del_init - iterate list, remove and reinitialize elements
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the list_head within the struct.
+ *
+ * Delete first element from list before the iteration using list_del_init().
+ * Iterate until the list is empty.
+ */
+#define list_for_each_entry_del_init(pos, head, member) \
+ while (!list_empty(head) && \
+ ({ pos = list_first_entry(head, typeof(*(pos)), member);\
+ list_del_init(&(pos)->member); 1; }))
+
+
/*
* Double linked lists with a single pointer list head.
* Mostly useful for hash tables where the two pointer list head is
--
2.41.0
Powered by blists - more mailing lists