Skip to content

Commit

Permalink
[list] Add list_last_entry()
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Brown <mcb30@ipxe.org>
  • Loading branch information
mcb30 committed May 8, 2012
1 parent 8a0331c commit 6ba7fb7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/include/ipxe/list.h
Expand Up @@ -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
*
Expand Down
14 changes: 13 additions & 1 deletion src/tests/list_test.c
Expand Up @@ -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 );
Expand Down

0 comments on commit 6ba7fb7

Please sign in to comment.