From cf2eb17c48a23d49bf517df58e34e30529c16576 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 19 Sep 2024 12:14:06 -0400 Subject: list: New drain_slist() macro --- src/list.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/list.h') diff --git a/src/list.h b/src/list.h index b30a96e..50c06a0 100644 --- a/src/list.h +++ b/src/list.h @@ -428,6 +428,33 @@ static inline void *slist_remove_impl(void *ret, void *cursor, void *next, size_ item && (SLIST_CHECK_(list), _next = item->next, true); \ 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. * -- cgit v1.2.3