Container_of () macro

Source: Internet
Author: User

Recently learning to add a kernel driver for Android, the first problem encountered is the famous container_of () macro in Linux.

Container_of () is a very important macro in the Linux kernel, and the main function is to get the address of the entire struct based on the address of a domain member in the struct.

Container_of () is defined as follows:

1 /**2 * Container_of-cast A member of a structure out to the containing structure3 * @ptr: The pointer to the member.4 * @type: The type of the container struct this was embedded in.5 * @member: The name of the member within the struct.6  *7  */8 #defineContainer_of (PTR, type, member) ({9     Const typeof((Type *)0)->member) *__mptr =(PTR); Ten(Type *) ( (Char*) __mptr-offsetof (Type,member));})

Path: (Android Source)//kernel/goldfish/include/linux/kernel.h (Learn Android, just use the kernel code in Android).

The implementation of the macro is divided into two main steps:

The first step is to define a variable to hold the address of the incoming domain member.

Const typeof (((type *)0)->member) *__mptr = (PTR);

With a memory address of 0 and a strong conversion type (incoming struct), get the domain member of the struct by->member (incoming domain member), take the type of the domain member with typeof (), define a temporary variable __mptr as the type of the domain member, Save the address of the domain member ptr to __mptr.

The second step is to use the domain member address minus the offset of the domain member relative to the first address of the struct.

This step first uses offsetof () to get the offset of the domain member relative to the first address of the struct, and then uses the actual address of the domain member __mptr minus the offset to obtain the first address of the struct.

The following image describes the process in a single graph:

  

The address of the current incoming domain member K, 0x0010008, needs to get the address of the entire struct. First, use Offsetof () to get the K-relative to the structure of the address of the offset 8, and then use the 0x0010008-8=0x00010000, this is the address of the struct.

Understanding clearly will find it is a very simple thing.

Reference:

Http://www.cnblogs.com/sdphome/archive/2011/09/14/2176624.html

Http://www.cnblogs.com/hicjiajia/archive/2012/07/01/2571791.html

http://blog.csdn.net/npy_lp/article/details/7010752

Container_of () macro

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.