diff options
author | Michael Brown | 2018-03-24 22:26:19 +0100 |
---|---|---|
committer | Michael Brown | 2018-03-24 22:32:06 +0100 |
commit | 6be010d9199df96deb3c120c19a5bd4591d9c596 (patch) | |
tree | 9295f0b459c5b41c0d532555074555f4f06b130e | |
parent | [tls] Ensure received data list is initialised before calling tls_free() (diff) | |
download | ipxe-6be010d9199df96deb3c120c19a5bd4591d9c596.tar.gz ipxe-6be010d9199df96deb3c120c19a5bd4591d9c596.tar.xz ipxe-6be010d9199df96deb3c120c19a5bd4591d9c596.zip |
[list] Add list_is_first_entry() and list_is_last_entry()
Signed-off-by: Michael Brown <mcb30@ipxe.org>
-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 ); |