Linux Kernel container_of macro definition analysis

Source: Internet
Author: User

Http://www.cnitblog.com/puppypyb/archive/2008/08/20/48172.aspx

Linux Kernel container_of macro definition analysis

I. # define offsetof (type, member) (size_t) & (type *) 0)-> Member)
1. (type *) 0) convert zero to type pointer;
2. (type *) 0)-> data member in the member access structure;
3. & (type *) 0)-> member) retrieves the address of the data member;
4. (size_t) (& (type *) 0)-> member) result conversion type;
The trick is to convert 0 to (type *). The structure uses the first address 0 in the memory space as the starting address, and the member address is naturally an offset address.
Example:
# Include <stdio. h>
Typedef struct _ Test
{
Char I;
Int J;
Char K;
} Test;
Int main ()
{
Test * p = 0;
Printf ("% P \ n", & (p-> K ));
}
Answer: 00000008
Self-analysis: here we use a small technique using compiler technology, that is, first obtain the offset address of the structure member variable in the struct to the first address of the struct, then, the first address of the struct is 0, so that the offset address is the offset of the struct variable in the struct, that is, the distance between the struct member variable and the beginning of the struct. In offsetof (), the address of this Member member is actually the offset of the Member member from the structure variable in the type data structure. Given a structure, offsetof (type, member) is a constant. list_entry () uses this constant offset to obtain the variable address of the linked list data item.

Ii. container_of ()
Container_of () from \ Linux \ kernel. h
Note in the kernel: container_of-cast a member of a tructure out to the containing structure.
PTR: the pointer to the member.
Type: the type of the container struct this is embedded in.
Member: the name of the member within the truct.

# Define container_of (PTR, type, member )({\
Const typeof (type *) 0)-> member) * _ mptr = (PTR );\
(Type *) (char *) _ mptr-offsetof (type, member ));})

Analyze by yourself:
1. (Type *) 0-> member is designed as a Type type struct, the starting address is 0, the compiler adds the starting address of the struct to the offset of the struct member variable to get the offset address of the struct member variable. Because the starting address of the struct is 0, therefore, the offset address of the struct member variable is equal to the offset of the member variable from the start part of the struct in the structure. That is, & (type *) 0-> member is the offset address of the member variable. And it is equal to the offset in the Structure Body: (size_t) (& (type *) 0)-> member) after size_t forced type conversion, the value is the offset of the structure body. The offset is obtained by offsetof.
2. typeof (type *) 0)-> member) is the variable type used to retrieve members of member. Use its _ mptr pointer. PTR as the pointer to the member variable. _ Mptr is a constant pointer of the member data type, pointing to the variable pointed to by PTR.
3. Convert (char *) _ mptr to a byte pointer. (Char *) _ mptr-offsetof (type, member) is used to find the starting address of the struct (char * pointer), and then (type *) (char *) _ mptr-offsetof (type, member) converts the starting pointer of a byte struct to the starting pointer of A type * struct under the action of (type.
4. ({}) The extension returns the value of the last expression in the program block.
This is the first pointer from a member variable pointer of the struct. The pointer type is converted from a member variable type of the struct to the struct 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.