From: Andrew Morton <akpm@linux-foundation.org>
To: Masami Hiramatsu <mhiramat@redhat.com>
Cc: ananth@in.ibm.com, jkenisto@us.ibm.com,
linux-kernel@vger.kernel.org, systemtap@sources.redhat.com,
prasanna@in.ibm.com, shaohua.li@intel.com, davem@davemloft.net,
fche@redhat.com
Subject: Re: [PATCH -mm 1/5] list.h: add list_singleton
Date: Fri, 14 Mar 2008 14:00:07 -0700 [thread overview]
Message-ID: <20080314140007.a7b495d7.akpm@linux-foundation.org> (raw)
In-Reply-To: <47DAE2C4.2060303@redhat.com>
On Fri, 14 Mar 2008 16:40:36 -0400
Masami Hiramatsu <mhiramat@redhat.com> wrote:
> Add list_singleton to check a list has just one entry.
>
> list_singleton is useful to check whether a list_head which
> have been temporarily allocated for listing objects can be
> released or not.
>
> Signed-off-by: Masami Hiramatsu <mhiramat@redhat.com>
> ---
> include/linux/list.h | 9 +++++++++
> 1 file changed, 9 insertions(+)
>
> Index: 2.6.25-rc5-mm1/include/linux/list.h
> ===================================================================
> --- 2.6.25-rc5-mm1.orig/include/linux/list.h
> +++ 2.6.25-rc5-mm1/include/linux/list.h
> @@ -211,6 +211,15 @@ static inline int list_empty_careful(con
> return (next == head) && (next == head->prev);
> }
>
> +/**
> + * list_singleton - tests whether a list has just one entry.
> + * @head: the list to test.
> + */
> +static inline int list_singleton(const struct list_head *head)
> +{
> + return !list_empty(head) && (head->next == head->prev);
> +}
> +
This hurts my brain.
If your usage pattern is:
struct foo {
...
struct list_head bar_list; /* A list of `struct bar's */
};
struct bar {
struct list_head list; /* Attached to foo.bar_list */
...
};
then yes, list_singleton() makes sense.
But in other usage patterns it does not:
struct foo {
struct bar *bar_list;
...
};
struct bar {
struct list_head list; /* All the other bars go here */
...
};
In the second case, emptiness is signified by foo.bar_list==NULL. And in
this case, code which does
if (foo->bar_list && list_singleton(&foo->bar_list->list))
will fail if there is a single item on the list!
The second usage pattern is uncommon and list_empty() also returns
misleading answers when list_heads are used this way.
So I guess we can proceed with your list_singleton(), but I'd just like to
flag this possible confusion, see what people think..
next prev parent reply other threads:[~2008-03-14 21:02 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-03-14 20:40 [PATCH -mm 1/5] list.h: add list_singleton Masami Hiramatsu
2008-03-14 21:00 ` Andrew Morton [this message]
2008-03-14 22:22 ` Masami Hiramatsu
2008-03-15 22:36 ` Peter Zijlstra
2008-03-17 15:04 ` Masami Hiramatsu
2008-03-17 15:13 ` Peter Zijlstra
2008-03-17 20:52 ` [PATCH -mm] list.h: rename list_singleton to list_is_singular Masami Hiramatsu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20080314140007.a7b495d7.akpm@linux-foundation.org \
--to=akpm@linux-foundation.org \
--cc=ananth@in.ibm.com \
--cc=davem@davemloft.net \
--cc=fche@redhat.com \
--cc=jkenisto@us.ibm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mhiramat@redhat.com \
--cc=prasanna@in.ibm.com \
--cc=shaohua.li@intel.com \
--cc=systemtap@sources.redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox