Re: [PATCH 0/4] locks: flock and lease related bugfixes, and remove i_flctx counters

From: Linus Torvalds
Date: Tue Feb 17 2015 - 15:20:25 EST


On Tue, Feb 17, 2015 at 11:55 AM, Linus Torvalds
<torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
>
> I do believe that we should add a "list_count()" function, so that we could write
>
> *flock_count = list_count(&ctx->flc_flock);
>
> instead of that horribly ugly
>
> list_for_each_entry(lock, &ctx->flc_flock, fl_list)
> ++(*flock_count);
>
> thing. But that's a separate cleanup.

The helper function would possibly be something like this.

Untested. It may mix-count, or it might do unspeakable acts on your
pets. No guarantees.

Linus
include/linux/list.h | 28 ++++++++++++++++++++++++++++
1 file changed, 28 insertions(+)

diff --git a/include/linux/list.h b/include/linux/list.h
index feb773c76ee0..495566be02e1 100644
--- a/include/linux/list.h
+++ b/include/linux/list.h
@@ -190,6 +190,20 @@ static inline int list_empty(const struct list_head *head)
}

/**
+ * list_count - count number of entries on list
+ * @head: the list to count.
+ */
+static inline int list_count(const struct list_head *head)
+{
+ int len = 0;
+ const struct list_head *p = head;
+
+ while ((p = p->next) != head)
+ len++;
+ return len;
+}
+
+/**
* list_empty_careful - tests whether a list is empty and not being modified
* @head: the list to test
*
@@ -611,6 +625,20 @@ static inline int hlist_empty(const struct hlist_head *h)
return !h->first;
}

+/**
+ * hlist_count - count number of entries on hlist
+ * @head: the list to count.
+ */
+static inline int hlist_count(const struct hlist_head *head)
+{
+ int len = 0;
+ const struct hlist_node *p;
+
+ for (p = head->first; p; p = p->next)
+ len++;
+ return len;
+}
+
static inline void __hlist_del(struct hlist_node *n)
{
struct hlist_node *next = n->next;