mirror of
https://github.com/systemd/systemd.git
synced 2025-01-10 05:18:17 +03:00
list: add LIST_CLEAR() helper that empties the list
This commit is contained in:
parent
c0083c9ce0
commit
d327b7752f
@ -192,6 +192,18 @@
|
||||
_p; \
|
||||
})
|
||||
|
||||
#define LIST_CLEAR(name, head, free_func) \
|
||||
_LIST_CLEAR(name, head, free_func, UNIQ_T(elem, UNIQ))
|
||||
|
||||
/* Clear the list, destroing each element with free_func */
|
||||
#define _LIST_CLEAR(name, head, free_func, elem) \
|
||||
({ \
|
||||
typeof(head) elem; \
|
||||
while ((elem = LIST_POP(name, head))) \
|
||||
free_func(elem); \
|
||||
head; \
|
||||
})
|
||||
|
||||
/* Now include "macro.h", because we want our definition of assert() which the macros above use. We include
|
||||
* it down here instead of up top, since macro.h pulls in log.h which in turn needs our own definitions. */
|
||||
#include "macro.h"
|
||||
|
@ -1,5 +1,6 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "list.h"
|
||||
|
||||
int main(int argc, const char *argv[]) {
|
||||
@ -254,5 +255,29 @@ int main(int argc, const char *argv[]) {
|
||||
assert_se(LIST_POP(item_list, head) == items + 0);
|
||||
assert_se(LIST_POP(item_list, head) == NULL);
|
||||
|
||||
/* No-op on an empty list */
|
||||
|
||||
LIST_CLEAR(item_list, head, free);
|
||||
|
||||
/* A non-empty list is cleared */
|
||||
|
||||
assert_se(LIST_PREPEND(item_list, head, new0(list_item, 1)));
|
||||
assert_se(LIST_PREPEND(item_list, head, new0(list_item, 1)));
|
||||
|
||||
LIST_CLEAR(item_list, head, free);
|
||||
|
||||
assert_se(head == NULL);
|
||||
|
||||
/* A list can be cleared partially */
|
||||
|
||||
assert_se(LIST_PREPEND(item_list, head, new0(list_item, 1)));
|
||||
assert_se(LIST_PREPEND(item_list, head, new0(list_item, 1)));
|
||||
assert_se(LIST_PREPEND(item_list, head, items + 0) == items + 0);
|
||||
|
||||
LIST_CLEAR(item_list, head->item_list_next, free);
|
||||
|
||||
assert_se(head == items + 0);
|
||||
assert_se(head->item_list_next == NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user