container_of的作用的通过结构体成员变量地址获取这个结构体的地址。内核函数调用常常给函数传入的是结构体成员地址,然后在函数里面又想使用这个结构体里面的其他成员变量,所以就引发了问题。

static void sensor_suspend(struct early_suspend *h)                   
{
struct sensor_private_data *sensor =
container_of(h, struct sensor_private_data, early_suspend);
if (sensor->ops->suspend)
sensor->ops->suspend(sensor->client);
}


early_suspend是sensor_private_data 里面的一个成员,通过这个成员的地址获取sensor_private_data结构体变量的地址,从而调用里面的成员变量client。

如何使用container_of

container_of需要传入三个参数,第一个参数是一个指针,第二个参数是结构体类型,第三个是对应第二个参数里面的结构体里面的成员。

container_of(ptr, type, member)

ptr:表示结构体中member的地址 h

type:表示结构体类型 struct sensor_private_data
member:表示结构体中的成员 early_suspend type里面一定要有这个成员,不能瞎搞啊

返回结构体的首地址



#yyds干货盘点#Linux驱动中container_of的作用_成员变量

#yyds干货盘点#Linux驱动中container_of的作用_成员变量_02

一些知识

({})、第一个先说这个表达式,很多人可能懂,可能在很多地方见到这个表达式,但是自己却没有注意,这个表达式返回最后一个表达式的值。比如x=({a;b;c;d;}),最终x的值应该是d。

typeof获取变量的类型

这个我们很少看到,这个关键字是C语言关键字的拓展,返回变量的类型,具体可以看GCC里面的介绍 ​​https://gcc.gnu.org/onlinedocs/gcc/Typeof.html​

++Another way to refer to the type of an expression is with typeof. The syntax of using of this keyword looks like sizeof, but the construct acts semantically like a type name defined with typedef.++

代码例子:

void main(void)
{
int a = 6;
typeof(a) b =9;
printf("%d %d/n",a,b);
}


(struct st*)0的作用

现在我们需要量一个结构体的长度,我们也可以用尺子来量,我们只要找到这个0刻度的位置就可以了。同理,即使我们不知道0刻度位置,我们首尾刻度相减一样可以计算出结构体的长度。 但是在C语言里什么是尺子呢?你想到的可能是sizeof,不幸的是,这个并不能满足我们的需要,所以才有了(struct st *),这个当作尺子真的再好不过了。

struct st{
int a;
int b;
}*p_st,n_st;

void main(void)
{
printf("%p/n",&((struct st*)0)->b);
}


上面的代码

(struct st*)0


这个的意思就是把这个结构体放到0刻度上面开始量了,然后量到哪里呢?

&((struct st*)0)->b)


这个就体现出来了,量到b的位置。所以上面的输出应该是4

看完上面的解释,应该知道下面这两个代码的功能是一样的。

typeof ((struct st*)0)->b) c; // 取b的类型来声明c
int c;


其实不只是对于0,用其他数字一样是有效的,比如下面的代码,编译器关心的是类型,而不在乎这个数字。

printf("%p/n",&((struct st*)4)->b  -4 );


如果现在需要你把一个数组的首地址设置为0,要怎么做呢?

先思考一下,假设这里延迟了几分钟。

代码如下:

struct A {
short array[100];
};

int main(int argc, char *argv[])
{
int i = 10;

A* a = (A*)0;
printf("%p %d %d/n",a,sizeof(short), &a->array[20]);
getchar();
return 1;
}
//输出 00000000 2 40


offsetof(TYPE, MEMBER)

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE*)0)->MEMBER)


size_t 这个有不懂的可以百度下,就是unsigned 的整数,在32位和64位下长度不同,所以这个offsetof就是获取结构体的偏移长度

const int* p的作用

上面的宏定义里面还有一个小知识点

const typeof( ((type *)0)->member ) *__mptr


上面的代码可以简写成

const int * __mptr


这个说明什么问题呢?这个说明__mptr指向的整型数据是一个const(常数)。 这就涉及到两外两个知识

int * const __mptr;//表示__mptr的值不能改变
//和
const int * const __mptr; //表示__mptr不能改变而且指向的内容也不能改变


container_of 剖析

看完上面的几个知识点,再来看container_of这个宏就显得非常清晰了。我把解析部分写在下面的代码注释里面。

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE*)0)->MEMBER)
#define container_of(ptr, type, member) ({ /
const typeof( ((type *)0)->member ) *__mptr = (const typeof( ((type *)0)->member ) *)(ptr); /
(type *)( (char *)__mptr - offsetof(type,member) );})
//-----分割线------
struct st{
int a;
int b;
}*pt;
//用这个来举例
container_of(&pt->a,struct st,a)
const typeof( ((struct st *)0)->a ) *__mptr = (const typeof( ((struct st *)0)->a ) *)(&pt->a);
const int *__mptr = (int *)(&pt->a);//第一句解析完,实际上就是获取a的地址。
(type *)( (char *)__mptr - offsetof(type,member) );
//这个变成
(struct st *)( (char *)__mptr - ((unsigned int) &((struct st*)0)->a));
//这句的意思,把a的地址减去a对结构体的偏移地址长度,那就是结构体的地址位置了。