内核中的 container_of() 宏定义
container_of() 宏的功能就是通过结构体中的一个元素,来找到这个结构体的首地址。先来看看他的代码:
// include/linux/kernel.h
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *) ((char *)__mptr - offsetof(type, member)); })
// include/linux/stddef.h
#define offsetof(TYPE, MEMBER) ((size_t) &((TPYE *)0)->MEMBER)
container_of() 中,第一个参数ptr是结构体中某一个成员的指针;第二个参数type是这个结构体的类型;第三个参数member是这个成员的名字。另外,typeof() 是编译器提供的一个功能,可以得到某一个变量的数据类型,如:
int a;
typeof(a) b; // 这里的typeof(a) b; 就相当于int b;
b = 10;
在 container_of 和 offsetof 中,都有一个比较巧妙的设计:(type *)0。我们可以这样理解,将地址为0的一个指针定义为TYPE类型(TYPE一般为结构体),那么TYPE加上某一个偏移量就可以得到TYPE中某一个成员的地址,而这个偏移量就是排这个成员之前的所有成员变量所占的空间大小。如:
struct test {
char n;
int id;
};
那么,n的地址就可以看作是0x0,id的地址可以看作是0x0 + 0x8。
编程实例
#include <stdio.h>
#include <string.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)); })
struct testtype {
char name[8];
int id;
};
int main()
{
struct testtype test, *ret;
int a;
strcpy(test.name, "TEST");
test.id = 100;
ret = NULL;
ret = container_of(test.name, struct testtype, name);
printf("name: %s\nid: %d\n", ret->name, ret->id);
ret = NULL;
ret = container_of(&test.id, struct testtype, id);
printf("name: %s\nid: %d\n", ret->name, ret->id);
return 0;
}
执行结果:
name: TEST
id: 100
name: TEST
id: 100