Container_of in Linux Kernel
# Define container_of (PTR, type, member) ({\ const typeof (type *) 0)-> member) * _ mptr = (PTR ); \ (type *) (char *) _ mptr-offsetof (type, member ));})
Among them, typeof (X) is an extension of GCC to C, which refers to the X type. First: (type *) 0 assume that the 0 address is forcibly converted to the passed type, and its member Member is used for the type,
Using the typeof operator, a pointer of the typeof (x) _ mptr type is temporarily generated and assigned to PTR. Offsetof is also a macro:
# Define offsetof (type, member) (size_t) & (type *) 0)-> Member)
It refers to: (type *) 0)-> the number of bytes of the Member Address starting from type (that is, the offset). Therefore:
(Type *) (char *) _ mptr-offsetof (type, member ));
It refers to the address value containing the member type, which is a common usage in the kernel.