Re: container-of Implementation

2013-01-14 Thread Geert Uytterhoeven
On Mon, Jan 14, 2013 at 11:46 AM, Schrober  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 -- ge...@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
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: container-of Implementation

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


Re: container-of Implementation

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


Re: container-of Implementation

2013-01-14 Thread Geert Uytterhoeven
On Mon, Jan 14, 2013 at 11:46 AM, Schrober franzschro...@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 -- ge...@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
--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/