summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Brown2024-02-13 17:26:34 +0100
committerMichael Brown2024-02-14 17:40:05 +0100
commite10dfe5dc7a5985333c85d6b196196b5cce9303a (patch)
tree49e1ea4937eb76ea084838a4fd213960beed84c7
parent[list] Add list_is_head_entry() (diff)
downloadipxe-e10dfe5dc7a5985333c85d6b196196b5cce9303a.tar.gz
ipxe-e10dfe5dc7a5985333c85d6b196196b5cce9303a.tar.xz
ipxe-e10dfe5dc7a5985333c85d6b196196b5cce9303a.zip
[list] Add list_for_each_entry_safe_continue()
Signed-off-by: Michael Brown <mcb30@ipxe.org>
-rw-r--r--src/include/ipxe/list.h16
-rw-r--r--src/tests/list_test.c32
2 files changed, 48 insertions, 0 deletions
diff --git a/src/include/ipxe/list.h b/src/include/ipxe/list.h
index 53edfc9f..2f02e71f 100644
--- a/src/include/ipxe/list.h
+++ b/src/include/ipxe/list.h
@@ -490,6 +490,22 @@ extern void extern_list_splice_tail_init ( struct list_head *list,
pos = list_entry ( pos->member.prev, typeof ( *pos ), member ) )
/**
+ * Iterate over subsequent entries in a list, safe against deletion
+ *
+ * @v pos Iterator
+ * @v tmp Temporary value (of same type as iterator)
+ * @v head List head
+ * @v member Name of list field within iterator's type
+ */
+#define list_for_each_entry_safe_continue( pos, tmp, head, member ) \
+ for ( list_check ( (head) ), \
+ pos = list_entry ( pos->member.next, typeof ( *pos ), member ), \
+ tmp = list_entry ( pos->member.next, typeof ( *tmp ), member ); \
+ &pos->member != (head); \
+ pos = tmp, \
+ tmp = list_entry ( tmp->member.next, typeof ( *tmp ), member ) )
+
+/**
* Test if list contains a specified entry
*
* @v entry Entry
diff --git a/src/tests/list_test.c b/src/tests/list_test.c
index f18c63f2..c24e8082 100644
--- a/src/tests/list_test.c
+++ b/src/tests/list_test.c
@@ -518,6 +518,38 @@ static void list_test_exec ( void ) {
list_iterate_entry_ok ( list_for_each_entry_continue_reverse, "",
pos, list, list );
+ /* Test list_for_each_entry_safe_continue() */
+ INIT_LIST_HEAD ( list );
+ list_add_tail ( &list_tests[9].list, list );
+ list_add_tail ( &list_tests[4].list, list );
+ list_add_tail ( &list_tests[2].list, list );
+ list_add_tail ( &list_tests[5].list, list );
+ list_add_tail ( &list_tests[7].list, list );
+ {
+ char *expecteds[] = { "94257", "9457", "947", "94" };
+ char **expected = expecteds;
+ pos = &list_tests[4];
+ list_for_each_entry_safe_continue ( pos, tmp, list, list ) {
+ list_contents_ok ( list, *expected );
+ list_del ( &pos->list );
+ expected++;
+ list_contents_ok ( list, *expected );
+ }
+ }
+ list_contents_ok ( list, "94" );
+ {
+ char *expecteds[] = { "94", "4", "" };
+ char **expected = expecteds;
+ ok ( pos == list_entry ( list, struct list_test, list ) );
+ list_for_each_entry_safe_continue ( pos, tmp, list, list ) {
+ list_contents_ok ( list, *expected );
+ list_del ( &pos->list );
+ expected++;
+ list_contents_ok ( list, *expected );
+ }
+ }
+ ok ( list_empty ( list ) );
+
/* Test list_contains() and list_contains_entry() */
INIT_LIST_HEAD ( list );
INIT_LIST_HEAD ( &list_tests[3].list );