]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[list] Add list_is_head_entry()
authorMichael Brown <mcb30@ipxe.org>
Wed, 14 Feb 2024 16:01:43 +0000 (16:01 +0000)
committerMichael Brown <mcb30@ipxe.org>
Wed, 14 Feb 2024 16:25:21 +0000 (16:25 +0000)
Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/include/ipxe/list.h
src/tests/list_test.c

index 8de2549844823260cc19642b128d5d5887cd376e..53edfc9f74bca711b3f9cbe73789438361d453ab 100644 (file)
@@ -398,6 +398,17 @@ extern void extern_list_splice_tail_init ( struct list_head *list,
 #define list_is_last_entry( entry, head, member )              \
        ( (head)->prev == &(entry)->member )
 
+/**
+ * Test if entry is the list head
+ *
+ * @v entry            List entry
+ * @v head             List head
+ * @v member           Name of list field within iterator's type
+ * @ret is_head                Entry is the list head
+ */
+#define list_is_head_entry( entry, head, member )              \
+       ( (head) == &(entry)->member )
+
 /**
  * Iterate over a list
  *
index d5b5c65db75760a9f715ea8a9334a65330c6f7af..f18c63f2de90abe9d5882fb76172629a413c87ed 100644 (file)
@@ -440,6 +440,22 @@ static void list_test_exec ( void ) {
        ok ( list_is_first_entry ( &list_tests[3], list, list ) );
        ok ( list_is_last_entry ( &list_tests[3], list, list ) );
 
+       /* Test list_is_head_entry() */
+       INIT_LIST_HEAD ( list );
+       list_add_tail ( &list_tests[1].list, list );
+       list_add_tail ( &list_tests[6].list, list );
+       list_add_tail ( &list_tests[8].list, list );
+       ok ( list_is_head_entry ( list_entry ( list, typeof ( *pos ), list ),
+                                 list, list ) );
+       ok ( ! list_is_head_entry ( &list_tests[1], list, list ) );
+       ok ( ! list_is_head_entry ( &list_tests[6], list, list ) );
+       ok ( ! list_is_head_entry ( &list_tests[8], list, list ) );
+       list_for_each_entry ( pos, list, list ) {
+               ok ( list_contains_entry ( pos, list, list ) );
+               ok ( ! list_is_head_entry ( pos, list, list ) );
+       }
+       ok ( list_is_head_entry ( pos, list, list ) );
+
        /* Test list_for_each() */
        INIT_LIST_HEAD ( list );
        list_add_tail ( &list_tests[6].list, list );