david/ipxe
david
/
ipxe
Archived
1
0
Fork 0

[list] Add list_next_entry() and list_prev_entry()

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown 2016-08-31 15:05:22 +01:00
parent ff28b22568
commit 161c80af5b
2 changed files with 51 additions and 0 deletions

View File

@ -348,6 +348,34 @@ extern void extern_list_splice_tail_init ( struct list_head *list,
( type * ) NULL : \
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
*

View File

@ -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 );