[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20100708084654.3554.53100.stgit@zurg>
Date: Thu, 8 Jul 2010 12:46:54 +0400
From: Konstantin Khlebnikov <khlebnikov@...nvz.org>
To: <linux-kernel@...r.kernel.org>
Subject: [PATCH RFC 1/3] list: Introduce list entry pop/peek operations
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@...nvz.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.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists