Container_of (PTR, type, member)

Source: Internet
Author: User

 

The pointer PTR points to the Member member in the struct type. The pointer PTR returns the starting address of the struct type.

# 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)
1. PTR is a physical address and its type is the same as that of member. In the end, typeof (type *) 0)-> member) is used)
The member type is automatically returned by the compiler.
2. type is a struct containing members of member.
3. offsetof (type, member) is the Offset Value of the Member member in the type struct. The size range is 0 ~ Sizeof (type) byte (because 0 address is the starting address of the type data structure)
4. PTR-offsetof () is equivalent to the physical start address of the type struct parent variable containing the PTR, Which is forcibly converted to (type *).

Example:

# Define list_entry (PTR, type, member )\
Container_of (PTR, type, member)

# Define list_for_each_entry (Pos, Head, member )\
For (Pos = list_entry (head)-> next, typeof (* POS), member );\
Prefetch (POS-> member. Next), & Pos-> member! = (Head );\
Pos = list_entry (POS-> member. Next, typeof (* POS), member ))
//-------------------------------------------------------------
List_entry (head)-> next, typeof (* POS), member) returns (head)-> next after the position of the physical pointer minus offsetof () bytes of data, physical address of the parent variable pos. the type of the parent variable is automatically returned (gliethttp) by typeof (* POS) during compilation ).
Therefore, list_for_each_entry traverses the childs struct of Type typeof (* POS) mounted under the head. Of course, each child struct contains similar two-way linked list list_head items such as struct list_head node, in this way, the cyclic pos will point to each child on the two-way linked list in turn. (member is the variable name defined in the Child type)
//-------------------------------------------------------------
The following program is excerpted from drivers/USB/driver. C.
Struct usb_dynids {
Spinlock_t lock;
Struct list_head list;
};
Struct usb_dynid {
Struct list_head node;
Struct usb_device_id ID;
};
Static const struct usb_device_id * usb_match_dynamic_id (struct usb_interface * INTF,
Struct usb_driver * DRV)
{
Struct usb_dynid * dynid;

Spin_lock (& DRV-> dynids. Lock );
// 1. DRV-> dynids. List is head, that is, root, Father, just as struct usb_dynids above
// 2. dynid is child, where DRV-> dynids. List. Next stores
// The physical address value of the struct list_head type named node.
// 3. It looks complicated. In fact, it is much easier to translate (gliethttp)
// The model is for (child; child! = Head; child = Child-> next)
// For (dynid = container_of (DRV-> dynids. List. Next, struct usb_dynid, nod );
// Dynid-> node! = & DRV-> dynids. List;
// Dynid = container_of (dynid-> node. Next, struct usb_dynid, nod)
//)
List_for_each_entry (dynid, & DRV-> dynids. List, node ){
If (usb_match_one_id (INTF, & dynid-> ID )){
Spin_unlock (& DRV-> dynids. Lock );
Return & dynid-> ID;
}
}
Spin_unlock (& DRV-> dynids. Lock );
Return NULL;
}

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.