[PATCH 1/3] lib: sbi_list: Add a helper for reverse list iteration

Samuel Holland samuel.holland at sifive.com
Mon Jun 16 20:21:35 PDT 2025


Some use cases require iterating through a list in both directions.

Signed-off-by: Samuel Holland <samuel.holland at sifive.com>
---

 include/sbi/sbi_list.h | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/include/sbi/sbi_list.h b/include/sbi/sbi_list.h
index 9e56c526..b1a6b3ab 100644
--- a/include/sbi/sbi_list.h
+++ b/include/sbi/sbi_list.h
@@ -160,4 +160,15 @@ static inline void sbi_list_del_init(struct sbi_dlist *entry)
 	     &pos->member != (head); 	\
 	     pos = sbi_list_entry(pos->member.next, typeof(*pos), member))
 
+/**
+ * Iterate over list of given type in reverse order
+ * @param pos the type * to use as a loop cursor.
+ * @param head the head for your list.
+ * @param member the name of the list_struct within the struct.
+ */
+#define sbi_list_for_each_entry_reverse(pos, head, member) \
+	for (pos = sbi_list_entry((head)->prev, typeof(*pos), member); \
+	     &pos->member != (head); \
+	     pos = sbi_list_entry(pos->member.prev, typeof(*pos), member))
+
 #endif
-- 
2.47.2




More information about the opensbi mailing list