diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/include/ipxe/list.h | 22 | ||||
-rw-r--r-- | src/tests/list_test.c | 21 |
2 files changed, 43 insertions, 0 deletions
diff --git a/src/include/ipxe/list.h b/src/include/ipxe/list.h index 274fb64c..8de25498 100644 --- a/src/include/ipxe/list.h +++ b/src/include/ipxe/list.h @@ -377,6 +377,28 @@ extern void extern_list_splice_tail_init ( struct list_head *list, ( ( &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 * * @v pos Iterator diff --git a/src/tests/list_test.c b/src/tests/list_test.c index f016a32e..d5b5c65d 100644 --- a/src/tests/list_test.c +++ b/src/tests/list_test.c @@ -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 ); |