Monday, June 3, 2013

container_of understanding

container_of (pointer, container_type, container_field)
pointer: points to be included in the address of the container_field
container_type: the type of structure that contains container_field
container_field: container_type a member name

The macro structure via a pointer container_type container_field pointer address, return address of the structure of this container_type
Acer to achieve
/ ** 
* container_of - cast a Member of a Structure out to the containing Structure 
* @ PTR: the pointer to the Member. 
* @ type: the type of the container struct this is Embedded in. 
* @ Member: the name of the Member within the struct. 

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

typeof (((type *) 0) -> member)
Gets the type of the structure member members

About offsetof
# Define offsetof (TYPE, MEMBER) ((size_t) & ((TYPE *) 0) -> MEMBER)
Members MEMBER return the offset in the TYPE

No comments:

Post a Comment