From: Konstantin Khlebnikov <khlebnikov@openvz.org>
To: <linux-kernel@vger.kernel.org>
Subject: [PATCH RFC 1/3] list: Introduce list entry pop/peek operations
Date: Thu, 8 Jul 2010 12:46:54 +0400 [thread overview]
Message-ID: <20100708084654.3554.53100.stgit@zurg> (raw)
Introduce macroses:
list_pop_entry
list_pop_last_entry
list_pop_init_entry
list_pop_init_last_entry
list_peek_entry
list_peek_last_entry
as syntax sugar for widely used constructions like this:
while (!list_empty(&list_head)) {
entry = list_first_entry(&list_head, struct entry, member);
list_del(&entry->member);
...
}
with this macro it become:
while (list_pop_entry(&entry, &list_head, member)) {
...
}
All macros returns true if the list is not empty,
otherwise they not change the output variable.
list_pop_init_* call list_del_init instead list_del.
list_peek_* not remove entries from the list.
Signed-off-by: Konstantin Khlebnikov <khlebnikov@openvz.org>
---
include/linux/list.h | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 88 insertions(+), 0 deletions(-)
diff --git a/include/linux/list.h b/include/linux/list.h
index 5d57a3a..6a6e9d3 100644
--- a/include/linux/list.h
+++ b/include/linux/list.h
@@ -360,6 +360,94 @@ static inline void list_splice_tail_init(struct list_head *list,
list_entry((ptr)->next, type, member)
/**
+ * list_peek_entry - check is list non-empty and return list first entry
+ * @ptr: the pointer to struct * to save first entry
+ * @head: the struct list_head pointer.
+ * @member: the name of the list_struct within the struct.
+ *
+ * Return true if list was not empty and @ptr is changed
+ */
+#define list_peek_entry(ptr, head, member) ({ \
+ bool __empty = list_empty(head); \
+ if (!__empty) \
+ *(ptr) = list_first_entry(head, typeof(**(ptr)), member); \
+ (!__empty); })
+
+/**
+ * list_peek_last_entry - check is list non-empty and return list last entry
+ * @ptr: the pointer to struct * to save last entry
+ * @head: the struct list_head pointer.
+ * @member: the name of the list_struct within the struct.
+ *
+ * Return true if list was not empty and @ptr is changed
+ */
+#define list_peek_last_entry(ptr, head, member) ({ \
+ bool __empty = list_empty(head); \
+ if (!__empty) \
+ *(ptr) = list_entry((head)->prev, typeof(**(ptr)), member); \
+ (!__empty); })
+
+/**
+ * list_pop_entry - delete and return list first entry
+ * @ptr: the pointer to struct * whereto save result
+ * @head: the struct list_head pointer.
+ * @member: the name of the list_struct within the struct.
+ *
+ * Return true if list was not empty and @ptr is changed
+ */
+#define list_pop_entry(ptr, head, member) ({ \
+ bool __empty = list_empty(head); \
+ if (!__empty) { \
+ *(ptr) = list_first_entry(head, typeof(**(ptr)), member); \
+ list_del(&(*(ptr))->member); \
+ } (!__empty); })
+
+/**
+ * list_pop_init_entry - delete, reinitialise and return list first entry
+ * @ptr: the pointer to struct * whereto save result
+ * @head: the struct list_head pointer.
+ * @member: the name of the list_struct within the struct.
+ *
+ * Return true if list was not empty and @ptr is changed
+ */
+#define list_pop_init_entry(ptr, head, member) ({ \
+ bool __empty = list_empty(head); \
+ if (!__empty) { \
+ *(ptr) = list_first_entry(head, typeof(**(ptr)), member); \
+ list_del_init(&(*(ptr))->member); \
+ } (!__empty); })
+
+/**
+ * list_pop_last_entry - delete and return list last entry
+ * @ptr: the pointer to struct * whereto save result
+ * @head: the struct list_head pointer.
+ * @member: the name of the list_struct within the struct.
+ *
+ * Return true if list was not empty and @ptr is changed
+ */
+#define list_pop_last_entry(ptr, head, member) ({ \
+ bool __empty = list_empty(head); \
+ if (!__empty) { \
+ *(ptr) = list_entry((head)->prev, typeof(**(ptr)), member); \
+ list_del(&(*(ptr))->member); \
+ } (!__empty); })
+
+/**
+ * list_pop_init_last_entry - delete, reinitialise and return list last entry
+ * @ptr: the pointer to struct * whereto save result
+ * @head: the struct list_head pointer.
+ * @member: the name of the list_struct within the struct.
+ *
+ * Return true if list was not empty and @ptr is changed
+ */
+#define list_pop_init_last_entry(ptr, head, member) ({ \
+ bool __empty = list_empty(head); \
+ if (!__empty) { \
+ *(ptr) = list_entry((head)->prev, typeof(**(ptr)), member); \
+ list_del_init(&(*(ptr))->member); \
+ } (!__empty); })
+
+/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
next reply other threads:[~2010-07-08 8:46 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-07-08 8:46 Konstantin Khlebnikov [this message]
2010-07-08 8:47 ` [PATCH RFC 2/3] slist: singly-linked stack implementation Konstantin Khlebnikov
2010-07-08 8:47 ` [PATCH RFC 3/3] qlist: singly-linked queue implementation Konstantin Khlebnikov
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=20100708084654.3554.53100.stgit@zurg \
--to=khlebnikov@openvz.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
all inboxes | Powered by JetHome®