]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
include/linux/list.h: add a macro to test if entry is pointing to the head
authorAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Fri, 16 Oct 2020 03:11:31 +0000 (20:11 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 22 Sep 2021 09:47:57 +0000 (11:47 +0200)
commit e130816164e244b692921de49771eeb28205152d upstream.

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.

While here, convert list_for_each_entry*() family of macros to use a new one.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reviewed-by: Cezary Rojewski <cezary.rojewski@intel.com>
Link: https://lkml.kernel.org/r/20200929134342.51489-1-andriy.shevchenko@linux.intel.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
include/linux/list.h

index de04cc5ed53673ebea7362bea2a43f8355018125..d2c12ef7a4e32adb0956437bc20e686ef890b620 100644 (file)
@@ -484,6 +484,15 @@ static inline void list_splice_tail_init(struct list_head *list,
             pos != (head); \
             pos = n, n = pos->prev)
 
+/**
+ * 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.
+ */
+#define list_entry_is_head(pos, head, member)                          \
+       (&pos->member == (head))
+
 /**
  * list_for_each_entry -       iterate over list of given type
  * @pos:       the type * to use as a loop cursor.
@@ -492,7 +501,7 @@ static inline void list_splice_tail_init(struct list_head *list,
  */
 #define list_for_each_entry(pos, head, member)                         \
        for (pos = list_first_entry(head, typeof(*pos), member);        \
-            &pos->member != (head);                                    \
+            !list_entry_is_head(pos, head, member);                    \
             pos = list_next_entry(pos, member))
 
 /**
@@ -503,7 +512,7 @@ static inline void list_splice_tail_init(struct list_head *list,
  */
 #define list_for_each_entry_reverse(pos, head, member)                 \
        for (pos = list_last_entry(head, typeof(*pos), member);         \
-            &pos->member != (head);                                    \
+            !list_entry_is_head(pos, head, member);                    \
             pos = list_prev_entry(pos, member))
 
 /**
@@ -528,7 +537,7 @@ static inline void list_splice_tail_init(struct list_head *list,
  */
 #define list_for_each_entry_continue(pos, head, member)                \
        for (pos = list_next_entry(pos, member);                        \
-            &pos->member != (head);                                    \
+            !list_entry_is_head(pos, head, member);                    \
             pos = list_next_entry(pos, member))
 
 /**
@@ -542,7 +551,7 @@ static inline void list_splice_tail_init(struct list_head *list,
  */
 #define list_for_each_entry_continue_reverse(pos, head, member)                \
        for (pos = list_prev_entry(pos, member);                        \
-            &pos->member != (head);                                    \
+            !list_entry_is_head(pos, head, member);                    \
             pos = list_prev_entry(pos, member))
 
 /**
@@ -554,7 +563,7 @@ static inline void list_splice_tail_init(struct list_head *list,
  * Iterate over list of given type, continuing from current position.
  */
 #define list_for_each_entry_from(pos, head, member)                    \
-       for (; &pos->member != (head);                                  \
+       for (; !list_entry_is_head(pos, head, member);                  \
             pos = list_next_entry(pos, member))
 
 /**
@@ -567,7 +576,7 @@ static inline void list_splice_tail_init(struct list_head *list,
  * Iterate backwards over list of given type, continuing from current position.
  */
 #define list_for_each_entry_from_reverse(pos, head, member)            \
-       for (; &pos->member != (head);                                  \
+       for (; !list_entry_is_head(pos, head, member);                  \
             pos = list_prev_entry(pos, member))
 
 /**
@@ -580,7 +589,7 @@ static inline void list_splice_tail_init(struct list_head *list,
 #define list_for_each_entry_safe(pos, n, head, member)                 \
        for (pos = list_first_entry(head, typeof(*pos), member),        \
                n = list_next_entry(pos, member);                       \
-            &pos->member != (head);                                    \
+            !list_entry_is_head(pos, head, member);                    \
             pos = n, n = list_next_entry(n, member))
 
 /**
@@ -596,7 +605,7 @@ static inline void list_splice_tail_init(struct list_head *list,
 #define list_for_each_entry_safe_continue(pos, n, head, member)                \
        for (pos = list_next_entry(pos, member),                                \
                n = list_next_entry(pos, member);                               \
-            &pos->member != (head);                                            \
+            !list_entry_is_head(pos, head, member);                            \
             pos = n, n = list_next_entry(n, member))
 
 /**
@@ -611,7 +620,7 @@ static inline void list_splice_tail_init(struct list_head *list,
  */
 #define list_for_each_entry_safe_from(pos, n, head, member)                    \
        for (n = list_next_entry(pos, member);                                  \
-            &pos->member != (head);                                            \
+            !list_entry_is_head(pos, head, member);                            \
             pos = n, n = list_next_entry(n, member))
 
 /**
@@ -627,7 +636,7 @@ static inline void list_splice_tail_init(struct list_head *list,
 #define list_for_each_entry_safe_reverse(pos, n, head, member)         \
        for (pos = list_last_entry(head, typeof(*pos), member),         \
                n = list_prev_entry(pos, member);                       \
-            &pos->member != (head);                                    \
+            !list_entry_is_head(pos, head, member);                    \
             pos = n, n = list_prev_entry(n, member))
 
 /**