summaryrefslogtreecommitdiffstats
path: root/src/list.h
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2024-09-19 12:14:06 -0400
committerTavian Barnes <tavianator@tavianator.com>2024-09-19 12:14:06 -0400
commitcf2eb17c48a23d49bf517df58e34e30529c16576 (patch)
treee746c589fb25d62bdc7ea9803a8f2e983b563ba5 /src/list.h
parent5307ef2ed38b665892798aa10295dd68401a38df (diff)
downloadbfs-cf2eb17c48a23d49bf517df58e34e30529c16576.tar.xz
list: New drain_slist() macro
Diffstat (limited to 'src/list.h')
-rw-r--r--src/list.h27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/list.h b/src/list.h
index b30a96e..50c06a0 100644
--- a/src/list.h
+++ b/src/list.h
@@ -429,6 +429,33 @@ static inline void *slist_remove_impl(void *ret, void *cursor, void *next, size_
item = _next)
/**
+ * Loop over a singly-linked list, popping each item.
+ *
+ * @param type
+ * The list item type.
+ * @param item
+ * The induction variable name.
+ * @param list
+ * The list to drain.
+ * @param node (optional)
+ * If specified, use head->node.next rather than head->next.
+ */
+#define drain_slist(type, item, ...) \
+ drain_slist_(type, item, __VA_ARGS__, )
+
+#define drain_slist_(type, item, list, ...) \
+ drain_slist__(type, item, (list), LIST_NEXT_(__VA_ARGS__))
+
+#define drain_slist__(type, item, list, next) \
+ for (type *item = list->head; item && \
+ (SLIST_CHECK_(list), \
+ list->head = item->next, \
+ list->tail = list->head ? list->tail : &list->head, \
+ item->next = NULL, \
+ true); \
+ item = list->head)
+
+/**
* Initialize a doubly-linked list.
*
* @param list