lkml.org 
[lkml]   [2013]   [Jan]   [14]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: container-of Implementation
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.


\
 
 \ /
  Last update: 2013-01-14 14:01    [W:0.051 / U:0.628 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site