Macros in Linux kernel linked list

Source: Internet
Author: User

/* 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.