mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH RFC 1/3] list: Introduce list entry pop/peek operations
@ 2010-07-08  8:46 Konstantin Khlebnikov
  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
  0 siblings, 2 replies; 3+ messages in thread
From: Konstantin Khlebnikov @ 2010-07-08  8:46 UTC (permalink / raw)
  To: linux-kernel

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.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2010-07-08  8:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-07-08  8:46 [PATCH RFC 1/3] list: Introduce list entry pop/peek operations Konstantin Khlebnikov
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

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®