From: Joern Engel <joern@logfs.org>
To: linux-kernel@vger.kernel.org
Cc: Chris Mason <chris.mason@fusionio.com>,
linux-btrfs@vger.kernel.org, Joern Engel <joern@logfs.org>
Subject: [PATCH 1/2] list: add list_for_each_entry_del
Date: Mon, 3 Jun 2013 13:28:04 -0400 [thread overview]
Message-ID: <1370280485-10047-2-git-send-email-joern@logfs.org> (raw)
In-Reply-To: <1370280485-10047-1-git-send-email-joern@logfs.org>
I have seen a lot of boilerplate code that either follows the pattern of
while (!list_empty(head)) {
pos = list_entry(head->next, struct foo, list);
list_del(pos->list);
...
}
or some variant thereof.
With this patch in, people can use
list_for_each_entry_del(pos, head, list) {
...
}
The patch also adds a list_for_each_del variant, even though I have
only found a single user for that one so far.
Signed-off-by: Joern Engel <joern@logfs.org>
---
include/linux/list.h | 33 +++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+)
diff --git a/include/linux/list.h b/include/linux/list.h
index 6a1f8df..e09fe10 100644
--- a/include/linux/list.h
+++ b/include/linux/list.h
@@ -361,6 +361,17 @@ static inline void list_splice_tail_init(struct list_head *list,
#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member)
+static inline struct list_head *list_first_del(struct list_head *head)
+{
+ struct list_head *item;
+
+ if (list_empty(head))
+ return NULL;
+ item = head->next;
+ list_del(item);
+ return item;
+}
+
/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop cursor.
@@ -483,6 +494,28 @@ static inline void list_splice_tail_init(struct list_head *list,
pos = list_entry(pos->member.next, typeof(*pos), member))
/**
+ * list_for_each_remove - iterate over a list, deleting each entry
+ * @pos: the &struct list_head to use as a loop cursor.
+ * @head: the head of your list.
+ *
+ * Calls list_del() on pos on each iteration
+ */
+#define list_for_each_del(pos, head) \
+ while ((pos = list_first_del(head)))
+
+/**
+ * list_for_each_entry_remove - iterate over a list of given type, deleting each entry
+ * @pos: the type * to use as loop cursor.
+ * @head: the head of your list.
+ * @member: the name of the list_struct within the struct
+ *
+ * Calls list_del() on pos on each iteration
+ */
+#define list_for_each_entry_del(pos, head, member) \
+ while (pos = list_entry(list_first_del(head), typeof(*pos), member), \
+ &pos->member)
+
+/**
* list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
--
1.7.10.4
next prev parent reply other threads:[~2013-06-03 18:57 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-06-03 17:28 [PATCH 0/2] introduce list_for_each_entry_del Joern Engel
2013-06-03 17:28 ` Joern Engel [this message]
2013-06-06 19:32 ` [PATCH 1/2] list: add list_for_each_entry_del Andy Shevchenko
2013-06-06 18:12 ` Jörn Engel
2013-06-06 19:49 ` Andy Shevchenko
2013-06-07 16:36 ` Jörn Engel
2013-06-07 18:30 ` Andy Shevchenko
2013-06-07 18:48 ` Jörn Engel
2013-06-08 0:03 ` Andy Shevchenko
2013-06-03 17:28 ` [PATCH 2/2] btrfs: use list_for_each_entry_del Joern Engel
2013-06-03 18:07 ` [PATCH 0/2] introduce list_for_each_entry_del Jörn Engel
2013-06-03 20:49 ` Christoph Hellwig
2013-06-03 19:36 ` Jörn Engel
2013-06-03 19:55 ` Jörn Engel
2013-06-04 14:48 ` Christoph Hellwig
2013-06-04 14:53 ` Chris Mason
2013-06-04 20:09 ` Arne Jansen
2013-06-04 18:44 ` Jörn Engel
2013-06-05 2:03 ` [PATCH 1/2] list: add while_list_drain_entry Jörn Engel
2013-06-05 14:32 ` David Sterba
2013-06-05 2:04 ` [PATCH 2/2] btrfs: use while_list_drain_entry Jörn Engel
2013-06-05 2:09 ` [PATCH 0/2] introduce list_for_each_entry_del Jörn Engel
2013-06-05 6:53 ` Arne Jansen
2013-06-05 14:25 ` David Sterba
2013-07-05 20:41 ` Jörn Engel
2013-07-05 20:41 ` [PATCH 1/2] list: add list_del_each_entry Jörn Engel
2013-07-05 22:38 ` Filipe David Manana
2013-07-15 17:35 ` Jörn Engel
2013-07-05 20:41 ` [PATCH 2/2] btrfs: use list_del_each_entry Jörn Engel
2013-07-08 4:37 ` [PATCH 0/2] introduce list_for_each_entry_del Dave Chinner
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=1370280485-10047-2-git-send-email-joern@logfs.org \
--to=joern@logfs.org \
--cc=chris.mason@fusionio.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.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 NNTP newsgroup(s).