From 118a9053f04d0e215bb3fe20562990cc73ae69a2 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Fri, 31 May 2024 11:55:50 -0400 Subject: list: New SLIST_SPLICE() macro --- src/list.h | 24 ++++++++++++++++++++---- src/prelude.h | 2 +- 2 files changed, 21 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/list.h b/src/list.h index 61d0e5b..d95483f 100644 --- a/src/list.h +++ b/src/list.h @@ -323,6 +323,25 @@ #define SLIST_PREPEND_(list, item, ...) \ LIST_VOID_(SLIST_INSERT_(list, &(list)->head, item, __VA_ARGS__)) +/** + * Splice a singly-linked list into another. + * + * @param dest + * The destination list. + * @param cursor + * A pointer to the item to splice after, e.g. &list->head or list->tail. + * @param src + * The source list. + */ +#define SLIST_SPLICE(dest, cursor, src) \ + LIST_VOID_(SLIST_SPLICE_((dest), (cursor), (src))) + +#define SLIST_SPLICE_(dest, cursor, src) \ + *src->tail = *cursor, \ + *cursor = src->head, \ + dest->tail = *dest->tail ? src->tail : dest->tail, \ + SLIST_INIT(src) + /** * Add an entire singly-linked list to the tail of another. * @@ -332,10 +351,7 @@ * The source list. */ #define SLIST_EXTEND(dest, src) \ - SLIST_EXTEND_((dest), (src)) - -#define SLIST_EXTEND_(dest, src) \ - (src->head ? (*dest->tail = src->head, dest->tail = src->tail, SLIST_INIT(src)) : (void)0) + SLIST_SPLICE(dest, (dest)->tail, src) /** * Remove an item from a singly-linked list. diff --git a/src/prelude.h b/src/prelude.h index c4edcb2..0944df1 100644 --- a/src/prelude.h +++ b/src/prelude.h @@ -137,7 +137,7 @@ extern const char bfs_version[]; /** * Get the length of an array. */ -#define countof(array) (sizeof(array) / sizeof(0[array])) +#define countof(...) (sizeof(__VA_ARGS__) / sizeof(0[__VA_ARGS__])) /** * False sharing/destructive interference/largest cache line size. -- cgit v1.2.3