Skip to contentSkip to conversation

list: Add a macro to test if entry is pointing to the head

PATCH Superseded Andy Shevchenko 1 participant 1 message Sep 29, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 list: Add a macro to test if entry is pointing to the head

Add a macro to test if entry is pointing to the head of the list which is useful in cases like:

  list_for_each_entry(pos, &head, member) {
    if (cond)
      break;
  }
  if (list_entry_is_head(pos, &head, member))
    return -ERRNO;

that allows to avoid additional variable to be added to track if loop has not been stopped in the middle.

---
 include/linux/list.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
include/linux/list.h +11 -0
index 796975c3c35c..49efaa8c3403 100644
@@ -770,6 +770,17 @@ static inline void list_splice_tail_init(struct list_head *list,
 #define list_safe_reset_next(pos, n, member)				\
 	n = list_next_entry(pos, member)
 
+/**
+ * list_entry_is_head - test if the entry points to the head of the list
+ * @pos:	the type * to cursor
+ * @head:	the head for your list.
+ * @member:	the name of the list_head within the struct.
+ *
+ * This macro can be used to check if the loop over the list wasn't stopped.
+ */
+#define list_entry_is_head(pos, head, member)				\
+	(&pos->member == (head))
+
 /*
  * Double linked lists with a single pointer list head.
  * Mostly useful for hash tables where the two pointer list head is
Signature · 1 line
2.28.0