list: add LIST_POP() helper that pops the first item off a linked list

This commit is contained in:
Lennart Poettering
2021-11-25 12:10:03 +01:00
parent 2a703778e9
commit 55cb63bf6e
2 changed files with 18 additions and 0 deletions
+9
View File
@@ -181,3 +181,12 @@
} \
(b) = NULL; \
} while (false)
#define LIST_POP(name, a) \
({ \
typeof(a)* _a = &(a); \
typeof(a) _p = *_a; \
if (_p) \
LIST_REMOVE(name, *_a, _p); \
_p; \
})
+9
View File
@@ -247,5 +247,14 @@ int main(int argc, const char *argv[]) {
assert_se(head == NULL);
LIST_PREPEND(item, head, items + 0);
LIST_PREPEND(item, head, items + 1);
LIST_PREPEND(item, head, items + 2);
assert_se(LIST_POP(item, head) == items + 2);
assert_se(LIST_POP(item, head) == items + 1);
assert_se(LIST_POP(item, head) == items + 0);
assert_se(LIST_POP(item, head) == NULL);
return 0;
}