[PATCH v1 1/4] list: Introduce list_count() to count existing nodes

From: Andy Shevchenko
Date: Mon Nov 14 2022 - 06:29:21 EST


Some of the existing users, and definitely will be new ones, want to
count existing nodes in the list. Provide a generic API for that.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>
---
include/linux/list.h | 13 +++++++++++++
1 file changed, 13 insertions(+)

diff --git a/include/linux/list.h b/include/linux/list.h
index 61762054b4be..098eccf8c1b6 100644
--- a/include/linux/list.h
+++ b/include/linux/list.h
@@ -655,6 +655,19 @@ static inline void list_splice_tail_init(struct list_head *list,
!list_is_head(pos, (head)); \
pos = n, n = pos->prev)

+/**
+ * list_count - count nodes in the list
+ * @head: the head for your list.
+ */
+#define list_count(head) \
+({ \
+ struct list_head *__tmp; \
+ size_t __i = 0; \
+ list_for_each(__tmp, head) \
+ __i++; \
+ __i; \
+})
+
/**
* list_entry_is_head - test if the entry points to the head of the list
* @pos: the type * to cursor
--
2.35.1