/* List_entry-get the struct for this entry
* @ PTR: The & struct list_head pointer.
* @ Type: the type of the struct this is embedded in.
* @ Member: the name of the list_struct within the struct .*/
# Define list_entry (PTR, type, member) (type *) (char *) (PTR)-(unsigned long) (& (type *) 0) -> member )))
We use the list_entry () macro to access linked list data in the Linux linked list.
The principle is that the pointer PTR points to the Member member in the struct type. Through the pointer PTR, the starting address of the struct type is returned.
Definition (size_t) & (type *) 0)-> member) means to convert the 0 address to a pointer of the type structure, and then obtain the pointer of the Member member in the structure, and forcibly convert it to size_t type
& (Type *) 0)-> member:
If "0" is forcibly converted to the pointer type, the pointer must point to "0" (the base address of the Data Segment ). Because the pointer is of the "type *" type, the address of a Type Variable member domain with the "0" base address can be obtained. 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 ))):
(Char *) (PTR) makes the increment and decrement step of the pointer one byte, (unsigned long) (& (type *) 0)-> Member) it is equal to the number of offset bytes from the member to the base address of the structure to which the member belongs. The two get the address of the struct and convert it to a pointer of type.