summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMichael Brown2016-08-31 16:05:22 +0200
committerMichael Brown2016-08-31 16:06:36 +0200
commit161c80af5bacf52a9f488551361c25be601eabb9 (patch)
tree03930a2f5121bfed2a9436c47998ce385eca2f90 /src
parent[crypto] Generalise X.509 "valid" field to a "flags" field (diff)
downloadipxe-161c80af5bacf52a9f488551361c25be601eabb9.tar.gz
ipxe-161c80af5bacf52a9f488551361c25be601eabb9.tar.xz
ipxe-161c80af5bacf52a9f488551361c25be601eabb9.zip
[list] Add list_next_entry() and list_prev_entry()
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src')
-rw-r--r--src/include/ipxe/list.h28
-rw-r--r--src/tests/list_test.c23
2 files changed, 51 insertions, 0 deletions
diff --git a/src/include/ipxe/list.h b/src/include/ipxe/list.h
index 6a9b76f9..274fb64c 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 352c87da..f016a32e 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 );