Container_of in Linux Kernel

Source: Internet
Author: User

Container_of in Linux Kernel

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

Among them, typeof (X) is an extension of GCC to C, which refers to the X type. First: (type *) 0 assume that the 0 address is forcibly converted to the passed type, and its member Member is used for the type,

Using the typeof operator, a pointer of the typeof (x) _ mptr type is temporarily generated and assigned to PTR. Offsetof is also a macro:

# Define offsetof (type, member) (size_t) & (type *) 0)-> Member)

It refers to: (type *) 0)-> the number of bytes of the Member Address starting from type (that is, the offset). Therefore:

(Type *) (char *) _ mptr-offsetof (type, member ));

It refers to the address value containing the member type, which is a common usage in the kernel.

 

 

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.