CONTAINER_OF macros, defined in Kernel.h:
/*** Container_of-cast a member of a structure out to the containing structure* @ptr: The pointer to the member.* @t Ype: 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));})
Container_of is widely used in Linux kernel to get the address of a struct from a struct member. Definition: typeof: This is the C language extension reserved word for gcc, used to get the type from the variable offsetof in stddef.h, to get the relative offset of a struct member
#define OFFSETOF (Type, MEMBER) ((size_t) & ((TYPE *) 0)->member)
How did the Offsetof do it? It forces the 0 address into the type* type, and then converts the address of its member member to the size_t type. That is, if a struct-body type is 0, the address of its member member is the relative offset (in char) between the member member and the type address. OK, let's start parsing container_of: This macro passed in 3 parameters: PTR (the address of the member of type), type (struct type), member (name of member) First line: Const typeof (((type *) 0) Member) *__mptr = (PTR), the first to get the member address correctly, because there is no type of incoming member in the parameter, so typeof (((type *) 0)->member) * Get the type of member, The expression in typeof brackets is similar to that in Offsetof, and member is then used typeof to get its type. So the result of the first line is that __mptr is a constant pointer to the member type that points to the PTR address. Second line: (Type *) ((char *) __mptr-offsetof (type,member));
Once the address of the member is obtained, the address of the struct can be obtained by subtracting the offset of the member relative to the struct body. Finally, the entire logic is completed by turning this address into type*.
CONTAINER_OF macro Definition Parsing