From bc952b51f48905392cec5685853fbb44565057a8 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sat, 20 May 2023 14:37:56 -0400 Subject: list: Return the removed item from SLIST_POP() --- src/bftw.c | 6 ++---- src/list.h | 21 ++++++++++++++++----- src/xspawn.c | 4 +--- 3 files changed, 19 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/bftw.c b/src/bftw.c index 6ec5cfa..966f03d 100644 --- a/src/bftw.c +++ b/src/bftw.c @@ -780,8 +780,7 @@ static bool bftw_pop_dir(struct bftw_state *state) { return false; } - state->file = state->dirs.head; - SLIST_POP(&state->dirs); + state->file = SLIST_POP(&state->dirs); return true; } @@ -952,8 +951,7 @@ static void bftw_list_sort(struct bftw_list *list) { // Split for (struct bftw_file *hare = list->head; hare && (hare = hare->next); hare = hare->next) { - struct bftw_file *tortoise = list->head; - SLIST_POP(list); + struct bftw_file *tortoise = SLIST_POP(list); SLIST_APPEND(&left, tortoise); } SLIST_EXTEND(&right, list); diff --git a/src/list.h b/src/list.h index 87e2bf2..89f00ed 100644 --- a/src/list.h +++ b/src/list.h @@ -79,6 +79,7 @@ #define BFS_LIST_H #include +#include /** * Initialize a singly-linked list. @@ -250,17 +251,27 @@ * A pointer to the item to remove, either &list->head or &prev->next. * @param node (optional) * If specified, use item->node.next rather than item->next. + * @return + * The removed item. */ #define SLIST_REMOVE(list, ...) SLIST_REMOVE_(list, __VA_ARGS__, ) #define SLIST_REMOVE_(list, cursor, ...) \ - LIST_BLOCK_(SLIST_REMOVE__((list), (cursor), LIST_NEXT_(__VA_ARGS__))) + SLIST_REMOVE__((list), (cursor), LIST_NEXT_(__VA_ARGS__)) #define SLIST_REMOVE__(list, cursor, next) \ - void *_next = (void *)(*cursor)->next; \ - (*cursor)->next = NULL; \ - *cursor = _next; \ - list->tail = _next ? list->tail : cursor; + (list->tail = (*cursor)->next ? list->tail : cursor, \ + slist_remove_impl(*cursor, cursor, &(*cursor)->next, list->tail, sizeof(*cursor))) + +// Helper for SLIST_REMOVE() +static inline void *slist_remove_impl(void *ret, void *cursor, void *next, void *tail, size_t size) { + // ret = *cursor; + // *cursor = ret->next; + memcpy(cursor, next, size); + // ret->next = *list->tail; (NULL) + memcpy(next, tail, size); + return ret; +} /** * Pop the head off a singly-linked list. diff --git a/src/xspawn.c b/src/xspawn.c index 00fb76e..740e38e 100644 --- a/src/xspawn.c +++ b/src/xspawn.c @@ -49,9 +49,7 @@ int bfs_spawn_init(struct bfs_spawn *ctx) { int bfs_spawn_destroy(struct bfs_spawn *ctx) { while (ctx->head) { - struct bfs_spawn_action *action = ctx->head; - SLIST_POP(ctx); - free(action); + free(SLIST_POP(ctx)); } return 0; -- cgit v1.2.3