Re: How container_of() works?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



cheng long wrote:
Hi,

Why can't we define the "container_of" as this?

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


the line you dropped:

|
               const typeof( ((type *)0)->member ) *__mptr = (ptr); |


gives the type-safety that makes this construct safe to use / un-mis-usable / useful.

this line requires that the <type> actually has the member
*and* that the <ptr> is actually of that declared <type>

in Greg's words:

In order to do this kind of pointer manipulation, the code has to know the type of pointer being passed to it. The driver core only passes in the type of driver structure registered with it, so this type of manipulation is safe. This also prevents other parts of the kernel from modifying the unique fields of the structure used to control the subsystem's code.



--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux