public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Al Viro <viro@ZenIV.linux.org.uk>
To: H Hartley Sweeten <hartleys@visionengravers.com>
Cc: Vegard Nossum <vegard.nossum@gmail.com>, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] block/genhd.c: fix sparse warning
Date: Thu, 16 Apr 2009 19:33:27 +0100	[thread overview]
Message-ID: <20090416183327.GC26366@ZenIV.linux.org.uk> (raw)
In-Reply-To: <BD79186B4FD85F4B8E60E381CAEE1909015F8F6D@mi8nycmail19.Mi8.com>

On Thu, Apr 16, 2009 at 02:03:39PM -0400, H Hartley Sweeten wrote:

> Maybe something like:
> 
> 	#define container_of(ptr, type, member) ({			\
> 		const typeof( ((type *)0)->member ) *__m_##ptr = (ptr);	\
> 		(type *)( (char *)__m_##ptr - offsetof(type,member) );})
> 
> I don't know if that actually works. ;-)

_Think_ of it.  Will do bleeding wonders if you say
	container_of(f(), struct foo, bar)
won't it?

No, the real solution for that is this:
#define container_of(ptr, type, member) (type *)	\
	((char *)(1 ? (ptr) : ((const typeof(((type *)0)->member) *)(ptr))) \
         - offsetof(type, member))
Same amount of typechecking, no local variables, no ({ }) uses.

      reply	other threads:[~2009-04-16 18:33 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-04-16  1:53 [PATCH] block/genhd.c: fix sparse warning H Hartley Sweeten
2009-04-16  4:46 ` Vegard Nossum
2009-04-16  5:42   ` Al Viro
2009-04-16  7:17     ` Vegard Nossum
2009-04-16 18:03   ` H Hartley Sweeten
2009-04-16 18:33     ` Al Viro [this message]

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=20090416183327.GC26366@ZenIV.linux.org.uk \
    --to=viro@zeniv.linux.org.uk \
    --cc=hartleys@visionengravers.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=vegard.nossum@gmail.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