Re: How container_of() works?

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

 



On Fri, May 05, 2006 at 04:40:00PM -0700, Arun Srinivasan wrote:
> 235 <http://lxr.linux.no/source/include/linux/kernel.h?a=arm#L235> */***
> 236 <http://lxr.linux.no/source/include/linux/kernel.h?a=arm#L236> * *
> container_of - cast a member of a structure out to the containing
> structure*
> 237 <http://lxr.linux.no/source/include/linux/kernel.h?a=arm#L237> * **
> 238 <http://lxr.linux.no/source/include/linux/kernel.h?a=arm#L238> * *
> @ptr:        the pointer to the member.*
> 239 <http://lxr.linux.no/source/include/linux/kernel.h?a=arm#L239> * *
> @type:       the type of the container struct this is embedded in.*
> 240 <http://lxr.linux.no/source/include/linux/kernel.h?a=arm#L240> * *
> @member:     the name of the member within the struct.*
> 241 <http://lxr.linux.no/source/include/linux/kernel.h?a=arm#L241> * **
> 242 <http://lxr.linux.no/source/include/linux/kernel.h?a=arm#L242> * */*
> 243 <http://lxr.linux.no/source/include/linux/kernel.h?a=arm#L243>
> #define container_of
> <http://lxr.linux.no/ident?a=arm;i=container_of>(ptr
> <http://lxr.linux.no/ident?a=arm;i=ptr>, type
> <http://lxr.linux.no/ident?a=arm;i=type>, member) ({                  
>   \
> 244 <http://lxr.linux.no/source/include/linux/kernel.h?a=arm#L244>    
>    const typeof( ((type <http://lxr.linux.no/ident?a=arm;i=type>
> *)0)->member ) *__mptr = (ptr
> <http://lxr.linux.no/ident?a=arm;i=ptr>);    \
> 245 <http://lxr.linux.no/source/include/linux/kernel.h?a=arm#L245>    
>    (type <http://lxr.linux.no/ident?a=arm;i=type> *)( (char *)__mptr
> - offsetof <http://lxr.linux.no/ident?a=arm;i=offsetof>(type
> <http://lxr.linux.no/ident?a=arm;i=type>,member) );})
> 
> Can someone please explain how the above macro works and in what
> situations it is used ? Thanks.

	http://www.kroah.com/log/linux/container_of.html

It's used all the time :)

thanks,

greg k-h

--
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