]> git.gag.com Git - fw/openocd/commitdiff
helper/list: add list_for_each_entry_direction()
authorAntonio Borneo <borneo.antonio@gmail.com>
Thu, 16 Dec 2021 10:25:32 +0000 (11:25 +0100)
committerAntonio Borneo <borneo.antonio@gmail.com>
Mon, 14 Feb 2022 15:10:41 +0000 (15:10 +0000)
Use a bool flag to specify if the list should be forward or
backward iterated.

Change-Id: Ied19d049f46cdcb7f50137d459cc7c02014526bc
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/6784
Tested-by: jenkins
src/helper/list.h

index a7cd4ad37f2f4a03747471987d4ffad619da7c9a..552a3202a5535666175808d1cfef7b691cc089d2 100644 (file)
@@ -656,6 +656,20 @@ static inline void list_splice_tail_init(struct list_head *list,
             !list_entry_is_head(pos, head, member);                    \
             pos = list_prev_entry(pos, member))
 
+/**
+ * list_for_each_entry_direction - iterate forward/backward over list of given type
+ * @param forward the iterate direction, true for forward, false for backward.
+ * @param pos     the type * to use as a loop cursor.
+ * @param head    the head for your list.
+ * @param member  the name of the list_head within the struct.
+ */
+#define list_for_each_entry_direction(forward, pos, head, member)              \
+       for (pos = forward ? list_first_entry(head, typeof(*pos), member)       \
+                                          : list_last_entry(head, typeof(*pos), member);       \
+                !list_entry_is_head(pos, head, member);                                                \
+                pos = forward ? list_next_entry(pos, member)                                   \
+                                          : list_prev_entry(pos, member))
+
 /**
  * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
  * @param pos    the type * to use as a start point