diff mbox series

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

Message ID 20221114112842.38565-1-andriy.shevchenko@linux.intel.com
State New
Headers show
Series [v1,1/4] list: Introduce list_count() to count existing nodes | expand

Commit Message

Andy Shevchenko Nov. 14, 2022, 11:28 a.m. UTC
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@linux.intel.com>
---
 include/linux/list.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
diff mbox series

Patch

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