[Mesa-dev] [PATCH v2 8/13] util/list: Add C99-based iterator macros

Jason Ekstrand jason at jlekstrand.net
Wed May 6 10:52:54 PDT 2015


v2: Use LIST_ENTRY instead of container_of in iterators
---
 src/util/list.h | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/src/util/list.h b/src/util/list.h
index 191030c..795e98d 100644
--- a/src/util/list.h
+++ b/src/util/list.h
@@ -141,6 +141,39 @@ static inline void list_delinit(struct list_head *item)
 #define LIST_FOR_EACH_ENTRY_FROM_REV(pos, start, head, member)		\
    for (pos = NULL, pos = container_of((start), pos, member);		\
 	&pos->member != (head);						\
-	pos = container_of(pos->member.prev, pos, member))
+
+#define list_for_each_entry(type, pos, head, member)                    \
+   for (type *pos = LIST_ENTRY(type, (head)->next, member);             \
+	&pos->member != (head);                                         \
+	pos = LIST_ENTRY(type, pos->member.next, member))
+
+#define list_for_each_entry_safe(type, pos, head, member)               \
+   for (type *pos = LIST_ENTRY(type, (head)->next, member),             \
+	     *__next = LIST_ENTRY(type, pos->member.next, member);      \
+	&pos->member != (head);                                         \
+	pos = __next,                                                   \
+        __next = LIST_ENTRY(type, __next->member.next, member))
+
+#define list_for_each_entry_rev(type, pos, head, member)                \
+   for (type *pos = LIST_ENTRY(type, (head)->prev, member);             \
+	&pos->member != (head);                                         \
+	pos = LIST_ENTRY(type, pos->member.prev, member))
+
+#define list_for_each_entry_safe_rev(type, pos, head, member)           \
+   for (type *pos = LIST_ENTRY(type, (head)->prev, member),             \
+	     *__prev = LIST_ENTRY(type, pos->member.prev, member);      \
+	&pos->member != (head);                                         \
+	pos = __prev,                                                   \
+        __prev = LIST_ENTRY(type, __prev->member.prev, member))
+
+#define list_for_each_entry_from(type, pos, start, head, member)        \
+   for (type *pos = LIST_ENTRY(type, (start), member);                  \
+	&pos->member != (head);                                         \
+	pos = LIST_ENTRY(type, pos->member.next, member))
+
+#define list_for_each_entry_from_rev(type, pos, start, head, member)    \
+   for (type *pos = LIST_ENTRY(type, (start), member);                  \
+	&pos->member != (head);                                         \
+	pos = LIST_ENTRY(type, pos->member.prev, member))
 
 #endif /*_UTIL_LIST_H_*/
-- 
2.3.6



More information about the mesa-dev mailing list