MACRO:
# Define list_entry (PTR, type, member )\
(Type *) (char *) (PTR)-(unsigned long) (& (type *) 0)-> member )))
Function to find the pointer of a container from the member pointer of a structure
Principle: PTR is a pointer to a Member member of the type struct.
& (Type *) 0)-> Member
Forcibly convert 0 to pointer type, that is, 0 is an address, which is the base address of the segment. Take the address of the domain variable Member of the struct whose base address is 0, then this address is equal to the number of offset bytes from the member domain to the base address of the struct.
(Type *) (char *) (PTR)-(unsigned long) (& (type *) 0)-> member )))
PTR is the pointer to the Member member in a struct of type, minus the offset of the member in the struct to get the starting address of the struct.
The following three macros are defined in list. h.
# Define list_entry (PTR, type, member)Container_of (PTR, type, member)
# Define container_of (PTR, type, member )({\
Const typeof (type *) 0)-> member) * _ mptr = (PTR );\
(Type *) (char *) _ mptr-offsetof (type, member ));})
# Define offsetof (type, member) (size_t) & (type *) 0)-> Member)