Skip to content

Commit

Permalink
[list] Add list_is_first_entry() and list_is_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 Mar 24, 2018
1 parent ac4fbd4 commit 6be010d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/include/ipxe/list.h
Expand Up @@ -376,6 +376,28 @@ extern void extern_list_splice_tail_init ( struct list_head *list,
member ); \
( ( &prev->member == (head) ) ? NULL : prev ); } )

/**
* Test if entry is first in a list
*
* @v entry List entry
* @v head List head
* @v member Name of list field within iterator's type
* @ret is_first Entry is first in the list
*/
#define list_is_first_entry( entry, head, member ) \
( (head)->next == &(entry)->member )

/**
* Test if entry is last in a list
*
* @v entry List entry
* @v head List head
* @v member Name of list field within iterator's type
* @ret is_last Entry is last in the list
*/
#define list_is_last_entry( entry, head, member ) \
( (head)->prev == &(entry)->member )

/**
* Iterate over a list
*
Expand Down
21 changes: 21 additions & 0 deletions src/tests/list_test.c
Expand Up @@ -419,6 +419,27 @@ static void list_test_exec ( void ) {
ok ( list_prev_entry ( &list_tests[1], list, list ) == &list_tests[5] );
ok ( list_next_entry ( &list_tests[1], list, list ) == NULL );

/* Test list_is_first_entry() and list_is_last_entry() */
INIT_LIST_HEAD ( list );
list_add_tail ( &list_tests[4].list, list );
list_add_tail ( &list_tests[8].list, list );
list_add_tail ( &list_tests[3].list, list );
list_add_tail ( &list_tests[6].list, list );
ok ( list_is_first_entry ( &list_tests[4], list, list ) );
ok ( ! list_is_first_entry ( &list_tests[8], list, list ) );
ok ( ! list_is_first_entry ( &list_tests[3], list, list ) );
ok ( ! list_is_first_entry ( &list_tests[6], list, list ) );
ok ( ! list_is_last_entry ( &list_tests[4], list, list ) );
ok ( ! list_is_last_entry ( &list_tests[8], list, list ) );
ok ( ! list_is_last_entry ( &list_tests[3], list, list ) );
ok ( list_is_last_entry ( &list_tests[6], list, list ) );
list_del ( &list_tests[4].list );
ok ( list_is_first_entry ( &list_tests[8], list, list ) );
list_del ( &list_tests[8].list );
list_del ( &list_tests[6].list );
ok ( list_is_first_entry ( &list_tests[3], list, list ) );
ok ( list_is_last_entry ( &list_tests[3], list, list ) );

/* Test list_for_each() */
INIT_LIST_HEAD ( list );
list_add_tail ( &list_tests[6].list, list );
Expand Down

0 comments on commit 6be010d

Please sign in to comment.