summaryrefslogtreecommitdiffstats
path: root/src/list.h
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2023-05-20 14:37:56 -0400
committerTavian Barnes <tavianator@tavianator.com>2023-05-20 14:37:56 -0400
commitbc952b51f48905392cec5685853fbb44565057a8 (patch)
tree7c133a045684d4bdc4f023ba134f3a37b792dca6 /src/list.h
parentf642b3dd1271e4a1503ce7629ba36e25d6a59fce (diff)
downloadbfs-bc952b51f48905392cec5685853fbb44565057a8.tar.xz
list: Return the removed item from SLIST_POP()
Diffstat (limited to 'src/list.h')
-rw-r--r--src/list.h21
1 files changed, 16 insertions, 5 deletions
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 <stddef.h>
+#include <string.h>
/**
* 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.