Container_of in Linux

Source: Internet
Author: User

In Linux, The container_of (PTR, type, member) macro is used to pass in the Member Address PTR domain of the struct type. The first address of the struct variable is returned, which is defined as follows:

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

 

There are two unique features of gnu c in this macro:

1: typeof (x) indicates the type of X, which is supported by gnu c and not supported by ansi c.

2: gnu c regards compound statements contained in parentheses as an expression called a statement expression, which can appear in any allowed expression. We can use loop variables and local variables that can only be used in composite statements in statement expressions.

For example, int AA = ({3; 43-5;}); if you use a compiler that does not support gnu c, an error is reported. If you use GCC, the value of AA is 38.
Using this feature of gnu c, we can avoid the side effects of some macro definitions, such as using

# Define min_t (type, x, y )\
({Type _ x = (x); Type _ y = (y) ;__ x <__ y? _ X :__ y })
Replace

# Define min (x, y) (x) <(y )? (X) :( y ))
Avoid the side effects of passing in min (A ++, B ++ ).

 

The following describes the implementation of container_of:

1: const typeof (type *) 0)-> member) * _ mptr = (PTR); defines a variable of the same type as member _ mptr, and assign the Member Address to it.

 

2: # define offsetof (type, member) (size_t) & (type *) 0)-> Member)
The macro is used to input the struct type and the domain member, and returns the offset address of the field in the struct.

 

3: (type *) (char *) _ mptr-offsetof (type, member); obtain the first address of the struct and convert it to type *.

 

 

The test procedure is as follows:
# Include <stdio. h>

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

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

Typedef struct contain_s
{
Char;
Int B;
Char C [10];
} Contain_t;

Int main (void)
{
Contain_t * pcon;
Contain_t con = {'3', 4, "ABC "};
Pcon = container_of (& con. B, contain_t, B );

Printf ("ADDR & con % P, ADDR pcon % P \ n", & con, pcon );

Return 0;
}

Output: ADDR & con 0xbfd4bad8, ADDR pcon 0xbfd4bad8

Usage of the typeof keyword

Http://www.cublog.cn/u3/101356/showart_2081601.html

Reprinted from:

Http://rookieubuntu.blog.sohu.com/179419212.html

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.