转自:http://blog.csdn.net/chn89/article/details/7085454
在Linux内核编程中广泛使用了container_of宏,有必要对该宏的使用及实现做一个详细的分析。
1,作用
原型container_of(ptr,type,member)。ptr是指向类型为type的结构体中member元素的指针,该宏最终返回类型为type的结构体的指针。
举个例子,
- struct my_struct{
- int i;
- int y;
- };
- struct my_struct A;
- //这里假设知道A中y变量的地址,求A的地址
- int *ptr = &(A.y);
- container_of(ptr,struct my_struct,y) //返回A的地址。
struct my_struct{
int i;
int y;
};
struct my_struct A;
//这里假设知道A中y变量的地址,求A的地址
int *ptr = &(A.y);
container_of(ptr,struct my_struct,y) //返回A的地址。
2,实现原理
- 1 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
- 2 #define container_of(ptr, type, member) ({ \
- 3 const typeof(((type *)0)->member) * __mptr = (ptr); \
- 4 (type *)((char *)__mptr - offsetof(type, member)); })
1 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
2 #define container_of(ptr, type, member) ({ \
3 const typeof(((type *)0)->member) * __mptr = (ptr); \
4 (type *)((char *)__mptr - offsetof(type, member)); })
网上对这部分的分析已经比较多了,这里不太详述。大体是offsetof计算出member变量在type中的偏移量,container_of在ptr的基础上减去偏移量获得整个结构的基地址。
3,若干问题
1,上述3行似乎无用,定义如下似乎也可以
- #define container_of(ptr, type, member) ({ \
- (type *)((char *)ptr - offsetof(type, member)); })
#define container_of(ptr, type, member) ({ \
(type *)((char *)ptr - offsetof(type, member)); })
答曰:其实正常输入来说,上述代码同样可以工作,第3行的主要作用在于类型检测,阻止参数输入错误的情况。这里举个例子来说明
- #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)); })
- #endif
- #define container_of2(ptr, type, member) ({ \
- (type *)((char *)ptr - offsetof(type, member)); })
- struct my{
- int i;
- int y;
- };
- char k=0;
- int main(){
- struct my qq;
- struct my *qqp;
- qq.y=8;
- printf("%p %p\n",&(qq.y),&qq);
- qqp=container_of(&(qq.y),struct my,y); //OK
- qqp=container_of2(&(qq.y),struct my,y);//OK
- qqp=container_of(&k,struct my,y); //这里会因为参数不匹配而报错
- qqp=container_of2(&k,struct my,y); //没有报错,得到了一个错误的结果
- return 0;
- }
#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)); })
#endif
#define container_of2(ptr, type, member) ({ \
(type *)((char *)ptr - offsetof(type, member)); })
struct my{
int i;
int y;
};
char k=0;
int main(){
struct my qq;
struct my *qqp;
qq.y=8;
printf("%p %p\n",&(qq.y),&qq);
qqp=container_of(&(qq.y),struct my,y); //OK
qqp=container_of2(&(qq.y),struct my,y);//OK
qqp=container_of(&k,struct my,y); //这里会因为参数不匹配而报错
qqp=container_of2(&k,struct my,y); //没有报错,得到了一个错误的结果
return 0;
}
2 offsetof 中的地址0很容易理解,这样可以很容易的计算出偏移量。但是第3行中的地址0怎么理解呢?
答:由于使用了typeof,这里仅仅时取得数据类型。typeof是GCC的扩展。关于typeof的使用,举个例子
int *p;
typeof(*p) = int
于是typeof(((type *)0)->member)就是得到了type中member元素的类型。于是我的理解就是这里仅仅是为了取得数据类型,于是0的取值不重要,我改成了100,1000完全没有影响。使用0估计是因为用习惯了,总得有个数,要是随便写的话,也许引起的误解会更多。自己的想法,和实验的结果,欢迎指正。