summaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorNeels Hofmeyr <nhofmeyr@sysmocom.de>2017-01-11 00:33:10 +0100
committerNeels Hofmeyr <nhofmeyr@sysmocom.de>2017-01-21 01:12:32 +0100
commit505a22fc5102b69cdb7a910fb65da67e41d7f649 (patch)
treecb04fee09326777b00a3d66d86dd68e044f23db4 /include
parentb0a4234c49ba6338bc78fe83b7f85af2571b70f6 (diff)
linuxlist.h: add llist_count()
After subchan_demux.c in libosmo-abis, osmo-bts/common/vty.c and openbsc's gtphub_test.c, more places would like to count the llist items (mostly unit tests). Instead of proliferating numerous local implementations, add here. NOTE: other than the previous llist_len() implementations, this one returns an *unsigned* length, which might need some adjusting of current callers. Call this llist_count() rather than llist_len() to highlight the fact that this is actively iterating. This also avoids a potential naming conflict when library versions mismatch. Change-Id: Ic49adc7a346f5722bf624d7d3b4a735e4220ae15
Diffstat (limited to 'include')
-rw-r--r--include/osmocom/core/linuxlist.h16
1 files changed, 16 insertions, 0 deletions
diff --git a/include/osmocom/core/linuxlist.h b/include/osmocom/core/linuxlist.h
index 1c833950..affa8273 100644
--- a/include/osmocom/core/linuxlist.h
+++ b/include/osmocom/core/linuxlist.h
@@ -351,6 +351,22 @@ static inline void llist_splice_init(struct llist_head *llist,
for ((pos) = (pos)->next, prefetch((pos)->next); (pos) != (head); \
(pos) = (pos)->next, ({ smp_read_barrier_depends(); 0;}), prefetch((pos)->next))
+/*! \brief count nr of llist items by iterating.
+ * \param head The llist head to count items of.
+ * \returns Number of items.
+ *
+ * This function is not efficient, mostly useful for small lists and non time
+ * critical cases like unit tests.
+ */
+static inline unsigned int llist_count(struct llist_head *head)
+{
+ struct llist_head *entry;
+ unsigned int i = 0;
+ llist_for_each(entry, head)
+ i++;
+ return i;
+}
+
/*!
* }@
*/