]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[list] Add list_last_entry()
authorMichael Brown <mcb30@ipxe.org>
Tue, 8 May 2012 09:46:39 +0000 (10:46 +0100)
committerMichael Brown <mcb30@ipxe.org>
Tue, 8 May 2012 11:49:01 +0000 (12:49 +0100)
Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/include/ipxe/list.h
src/tests/list_test.c

index 4daf12b2e0282d2b914d8e35aab359aaeec1f594..b14a83d703761c1ca33577705e198ea18e57e136 100644 (file)
@@ -324,6 +324,19 @@ static inline void list_splice_tail_init ( struct list_head *list,
          ( type * ) NULL :                             \
          list_entry ( (list)->next, type, member ) )
 
+/**
+ * Get the container of the last entry in a list
+ *
+ * @v list             List head
+ * @v type             Containing type
+ * @v member           Name of list field within containing type
+ * @ret first          First list entry, or NULL
+ */
+#define list_last_entry( list, type, member )          \
+       ( list_empty ( (list) ) ?                       \
+         ( type * ) NULL :                             \
+         list_entry ( (list)->prev, type, member ) )
+
 /**
  * Iterate over a list
  *
index e237d56a8c9ae71964793939531450d17871a08b..5184b30899ccd6c0840368e52c58eb4c535e3438 100644 (file)
@@ -368,16 +368,28 @@ static void list_test_exec ( void ) {
        ok ( list_entry ( &list_tests[3].list, struct list_test, list )
             == &list_tests[3] );
 
-       /* Test list_first_entry() */
+       /* Test list_first_entry() and list_last_entry() */
        INIT_LIST_HEAD ( list );
        list_add_tail ( &list_tests[9].list, list );
        list_add_tail ( &list_tests[5].list, list );
        list_add_tail ( &list_tests[6].list, list );
        ok ( list_first_entry ( list, struct list_test, list )
             == &list_tests[9] );
+       ok ( list_last_entry ( list, struct list_test, list )
+            == &list_tests[6] );
        list_del ( &list_tests[9].list );
        ok ( list_first_entry ( list, struct list_test, list )
             == &list_tests[5] );
+       ok ( list_last_entry ( list, struct list_test, list )
+            == &list_tests[6] );
+       list_del ( &list_tests[6].list );
+       ok ( list_first_entry ( list, struct list_test, list )
+            == &list_tests[5] );
+       ok ( list_last_entry ( list, struct list_test, list )
+            == &list_tests[5] );
+       list_del ( &list_tests[5].list );
+       ok ( list_first_entry ( list, struct list_test, list ) == NULL );
+       ok ( list_last_entry ( list, struct list_test, list ) == NULL );
 
        /* Test list_for_each() */
        INIT_LIST_HEAD ( list );