Detailed Linux kernel container_of functions _unix Linux

Source: Internet
Author: User

Objective

In the Linux kernel, container_of functions are used very widely, such as Linux kernel lists list_head , work Queues work_struct .

In the Linux kernel, the well-known macro container_of() , in fact, its syntax is very simple, but a few pointers to the flexible application, it is divided into two steps:

The first step is to first define a temporary data type (through typeof( ((type *)0)->member ) to get the ptr same pointer variable and __mptr then use it to save ptr the value.

In the second step, the (char *)__mptr value that is subtracted from the member offset in the structure is the first address of the entire structure variable (the return value of the entire macro is the first address).

So what is this macro for? Let's take a look at how it is defined in the kernel.

Oh, at first glance do not know what dongdong.

Let's take a look at this, which is, and container_of(ptr,type,member) ptr represents the type member pointer, type, and member respectively.

Look at an example:

Struct Test
  {
    int i;
    Int J;
    char k;
  };
  Struct Test temp;

Now, if I want to find the first address of temp through TEMP.J's address, I can use it.container_of(&temp.j,struct test,j);

The function we know now container_of() is to find the first address of the struct variable by the address of a member of a struct variable.

Here's a look at some of the more complicated things:

Let's use the struct test Zhang Zhan.

 
 

One of these typeof is the GNU C extension to standard C, which is to get the type of the variable based on the variable. Therefore, the above code is used to first use the typeof type of the get struct member J int , then the top of the int pointer type, __mptr and assign the address of the member in the struct variable to a temporary variable __mptr .

(struct test *) ((char *) __mptr-offsetof (struct test,j));

Then let's take a look offsetof(struct test,j) at the following definition in the kernel

Unfold (size_t)&((struct test *)0)->j , what is this stuff?

At first also do not understand, here to thank Cao Zhongming Teacher's enthusiastic help, a language awakened dream people, oh, can be so understanding.

Which size_t is an integral type, then we know that the final result is a cosmetic value, which is the offset of J relative to the 0 address. Maybe now you're going to ask, what's with the whole thing, look at the following:

Program Run Result:

found that no if the second value is subtracted from the last value, the first value can be obtained.

Look back at it:

(struct test *) ((char *) __mptr-offsetof (struct test,j));

Is it possible to get the structure variables the first address of temp, is it too subtle?

Summarize

A macro is so subtle in the Linux kernel that there is a long way to go before you know a lot about Linux. Well, the above is the entire content of this article, I hope this article content for everyone's work and learning can help, if there are questions can be exchanged message, thank you for your support cloud habitat community.

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.