mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* container-of Implementation
@ 2013-01-14 10:46 Schrober
  2013-01-14 12:22 ` Mikael Pettersson
  2013-01-14 12:23 ` Geert Uytterhoeven
  0 siblings, 2 replies; 3+ messages in thread
From: Schrober @ 2013-01-14 10:46 UTC (permalink / raw)
  To: linux-kernel

Hi,

I wondered why the container_of implementation is so complicated.

#define container_of(ptr, type, member) ({			\
	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
	(type *)( (char *)__mptr - offsetof(type,member) );})

isn't the __mptr not unnecessary? Why not following version?

#define container_of(ptr, type, member) \
    ((type *)((char *)(ptr) - offsetof(type, member)))
-- 
Franz Schrober

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: container-of Implementation
  2013-01-14 10:46 container-of Implementation Schrober
@ 2013-01-14 12:22 ` Mikael Pettersson
  2013-01-14 12:23 ` Geert Uytterhoeven
  1 sibling, 0 replies; 3+ messages in thread
From: Mikael Pettersson @ 2013-01-14 12:22 UTC (permalink / raw)
  To: Schrober; +Cc: linux-kernel

Schrober writes:
 > Hi,
 > 
 > I wondered why the container_of implementation is so complicated.
 > 
 > #define container_of(ptr, type, member) ({			\
 > 	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
 > 	(type *)( (char *)__mptr - offsetof(type,member) );})
 > 
 > isn't the __mptr not unnecessary? Why not following version?
 > 
 > #define container_of(ptr, type, member) \
 >     ((type *)((char *)(ptr) - offsetof(type, member)))

Compile-time type checking.  The first version requires ptr to be
assignment-compatible with the type of the struct member, the second
version accepts random junk for ptr.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: container-of Implementation
  2013-01-14 10:46 container-of Implementation Schrober
  2013-01-14 12:22 ` Mikael Pettersson
@ 2013-01-14 12:23 ` Geert Uytterhoeven
  1 sibling, 0 replies; 3+ messages in thread
From: Geert Uytterhoeven @ 2013-01-14 12:23 UTC (permalink / raw)
  To: Schrober; +Cc: linux-kernel

On Mon, Jan 14, 2013 at 11:46 AM, Schrober <franzschrober@yahoo.de> wrote:
> I wondered why the container_of implementation is so complicated.
>
> #define container_of(ptr, type, member) ({                      \
>         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
>         (type *)( (char *)__mptr - offsetof(type,member) );})
>
> isn't the __mptr not unnecessary? Why not following version?
>
> #define container_of(ptr, type, member) \
>     ((type *)((char *)(ptr) - offsetof(type, member)))

The __mptr construct will cause a compiler warning if ptr is of the
wrong type.

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-01-14 12:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-14 10:46 container-of Implementation Schrober
2013-01-14 12:22 ` Mikael Pettersson
2013-01-14 12:23 ` Geert Uytterhoeven

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®