diff options
-rw-r--r-- | src/include/ipxe/list.h | 28 | ||||
-rw-r--r-- | src/tests/list_test.c | 23 |
2 files changed, 51 insertions, 0 deletions
diff --git a/src/include/ipxe/list.h b/src/include/ipxe/list.h index 6a9b76f..274fb64 100644 --- a/src/include/ipxe/list.h +++ b/src/include/ipxe/list.h @@ -349,6 +349,34 @@ extern void extern_list_splice_tail_init ( struct list_head *list, list_entry ( (list)->prev, type, member ) ) /** + * Get the container of the next entry in a list + * + * @v pos Current list entry + * @v head List head + * @v member Name of list field within iterator's type + * @ret next Next list entry, or NULL at end of list + */ +#define list_next_entry( pos, head, member ) ( { \ + typeof (pos) next = list_entry ( (pos)->member.next, \ + typeof ( *(pos) ), \ + member ); \ + ( ( &next->member == (head) ) ? NULL : next ); } ) + +/** + * Get the container of the previous entry in a list + * + * @v pos Current list entry + * @v head List head + * @v member Name of list field within iterator's type + * @ret next Next list entry, or NULL at end of list + */ +#define list_prev_entry( pos, head, member ) ( { \ + typeof (pos) prev = list_entry ( (pos)->member.prev, \ + typeof ( *(pos) ), \ + member ); \ + ( ( &prev->member == (head) ) ? NULL : prev ); } ) + +/** * Iterate over a list * * @v pos Iterator diff --git a/src/tests/list_test.c b/src/tests/list_test.c index 352c87d..f016a32 100644 --- a/src/tests/list_test.c +++ b/src/tests/list_test.c @@ -396,6 +396,29 @@ static void list_test_exec ( void ) { ok ( list_first_entry ( list, struct list_test, list ) == NULL ); ok ( list_last_entry ( list, struct list_test, list ) == NULL ); + /* Test list_next_entry() and list_prev_entry() */ + INIT_LIST_HEAD ( list ); + list_add_tail ( &list_tests[5].list, list ); + list_add_tail ( &list_tests[3].list, list ); + list_add_tail ( &list_tests[1].list, list ); + list_add_tail ( &list_tests[7].list, list ); + ok ( list_prev_entry ( &list_tests[5], list, list ) == NULL ); + ok ( list_next_entry ( &list_tests[5], list, list ) == &list_tests[3] ); + ok ( list_prev_entry ( &list_tests[3], list, list ) == &list_tests[5] ); + ok ( list_next_entry ( &list_tests[3], list, list ) == &list_tests[1] ); + ok ( list_prev_entry ( &list_tests[1], list, list ) == &list_tests[3] ); + ok ( list_next_entry ( &list_tests[1], list, list ) == &list_tests[7] ); + ok ( list_prev_entry ( &list_tests[7], list, list ) == &list_tests[1] ); + ok ( list_next_entry ( &list_tests[7], list, list ) == NULL ); + list_del ( &list_tests[7].list ); + ok ( list_prev_entry ( &list_tests[1], list, list ) == &list_tests[3] ); + ok ( list_next_entry ( &list_tests[1], list, list ) == NULL ); + list_del ( &list_tests[3].list ); + ok ( list_prev_entry ( &list_tests[5], list, list ) == NULL ); + ok ( list_next_entry ( &list_tests[5], list, list ) == &list_tests[1] ); + ok ( list_prev_entry ( &list_tests[1], list, list ) == &list_tests[5] ); + ok ( list_next_entry ( &list_tests[1], list, list ) == NULL ); + /* Test list_for_each() */ INIT_LIST_HEAD ( list ); list_add_tail ( &list_tests[6].list, list ); |