CONTAINER_OF macro Definition Parsing

Source: Internet
Author: User

CONTAINER_OF macros, defined in Kernel.h:

/*** Container_of-cast a member of a structure out to the containing structure* @ptr: The pointer to the     member.* @t Ype: The type of the     container struct This is embedded in.* @member: The name of the member within the struct.**/#     Define CONTAINER_OF (PTR, type, member) ({             /         const typeof (((type *) 0)->member) *__mptr = (PTR);     /         (Type *) ((char *) __mptr-offsetof (Type,member));})
Container_of is widely used in Linux kernel to get the address of a struct from a struct member. Definition: typeof: This is the C language extension reserved word for gcc, used to get the type from the variable offsetof in stddef.h, to get the relative offset of a struct member
#define OFFSETOF (Type, MEMBER) ((size_t) & ((TYPE *) 0)->member)
How did the Offsetof do it? It forces the 0 address into the type* type, and then converts the address of its member member to the size_t type. That is, if a struct-body type is 0, the address of its member member is the relative offset (in char) between the member member and the type address. OK, let's start parsing container_of: This macro passed in 3 parameters: PTR (the address of the member of type), type (struct type), member (name of member) First line: Const typeof (((type *) 0) Member) *__mptr = (PTR), the first to get the member address correctly, because there is no type of incoming member in the parameter, so typeof (((type *) 0)->member) * Get the type of member, The expression in typeof brackets is similar to that in Offsetof, and member is then used typeof to get its type. So the result of the first line is that __mptr is a constant pointer to the member type that points to the PTR address. Second line: (Type *) ((char *) __mptr-offsetof (type,member));
Once the address of the member is obtained, the address of the struct can be obtained by subtracting the offset of the member relative to the struct body. Finally, the entire logic is completed by turning this address into type*.


CONTAINER_OF macro Definition Parsing

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.