Re: [PATCH] block/genhd.c: fix sparse warning

From: Al Viro
Date: Thu Apr 16 2009 - 14:33:40 EST


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.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/