[vlc-devel] [PATCH 1/3] list: add vlc_list_reverse_foreach

Maxime ... mmeisson at outlook.fr
Fri Sep 20 15:20:37 CEST 2019


---
 include/vlc_list.h | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/include/vlc_list.h b/include/vlc_list.h
index 45263cc789..ea40b8544d 100644
--- a/include/vlc_list.h
+++ b/include/vlc_list.h
@@ -216,6 +216,14 @@ struct vlc_list_it vlc_list_it_start(const struct vlc_list *head)
     return (struct vlc_list_it){ head, first, first->next };
 }
 
+static inline
+struct vlc_list_it vlc_list_it_reverse_start(const struct vlc_list *head)
+{
+    struct vlc_list *first = head->prev;
+
+    return (struct vlc_list_it){ head, first, first->prev };
+}
+
 static inline bool vlc_list_it_continue(const struct vlc_list_it *restrict it)
 {
     return it->current != it->head;
@@ -229,6 +237,14 @@ static inline void vlc_list_it_next(struct vlc_list_it *restrict it)
     it->next = next->next;
 }
 
+static inline void vlc_list_it_prev(struct vlc_list_it *restrict it)
+{
+    struct vlc_list *next = it->next;
+
+    it->current = next;
+    it->next = next->prev;
+}
+
 #define vlc_list_entry_aligned_size(p) \
     ((sizeof (*(p)) + sizeof (max_align_t) - 1) / sizeof (max_align_t))
 
@@ -263,6 +279,28 @@ static inline void vlc_list_it_next(struct vlc_list_it *restrict it)
                                        pos, member), true); \
          vlc_list_it_next(&(vlc_list_it_##pos)))
 
+/**
+ * List iteration macro.
+ *
+ * This macro iterates over all elements (excluding the head) of a list,
+ * in reversed order from the first to the last.
+ *
+ * For each iteration, it sets the cursor variable to the current element.
+ *
+ * \param pos Cursor pointer variable identifier.
+ * \param head Head pointer of the list to iterate [IN].
+ * \param member Identifier of the member of the data type
+ *               serving as list node.
+ * \note It it safe to delete the current item while iterating.
+ * It is however <b>not</b> safe to delete another item.
+ */
+#define vlc_list_reverse_foreach(pos, head, member) \
+    for (struct vlc_list_it vlc_list_it_##pos = vlc_list_it_reverse_start(head); \
+         vlc_list_it_continue(&(vlc_list_it_##pos)) \
+          && ((pos) = vlc_list_entry_p((vlc_list_it_##pos).current, \
+                                       pos, member), true); \
+         vlc_list_it_prev(&(vlc_list_it_##pos)))
+
 /**
  * Converts a list node pointer to an element pointer.
  *
-- 
2.17.1



More information about the vlc-devel mailing list