List_entry ()

Source: Internet
Author: User

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)

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.