container_of宏深究

转自:http://blog.csdn.net/chn89/article/details/7085454

 

在Linux内核编程中广泛使用了container_of宏,有必要对该宏的使用及实现做一个详细的分析。

1,作用

原型container_of(ptr,type,member)。ptr是指向类型为type的结构体中member元素的指针,该宏最终返回类型为type的结构体的指针。
举个例子,

  1.  struct my_struct{  
  2.      int i;  
  3.      int y;  
  4.  };  
  5. struct my_struct A;  
  6. //这里假设知道A中y变量的地址,求A的地址   
  7. int *ptr = &(A.y);  
  8. 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. 1  #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)  
  2. 2  #define container_of(ptr, type, member) ({          \  
  3. 3      const typeof(((type *)0)->member) * __mptr = (ptr); \  
  4. 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行似乎无用,定义如下似乎也可以

  1. #define container_of(ptr, type, member) ({          \   
  2.             (type *)((char *)ptr - offsetof(type, member)); })  
 #define container_of(ptr, type, member) ({          \
             (type *)((char *)ptr - offsetof(type, member)); })

    答曰:其实正常输入来说,上述代码同样可以工作,第3行的主要作用在于类型检测,阻止参数输入错误的情况。这里举个例子来说明

  1. #include <stdio.h>   
  2.   
  3. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)   
  4.   
  5. #define container_of(ptr, type, member) ({          \   
  6.      const typeof(((type *)0)->member) * __mptr = (ptr); \  
  7.      (type *)((char *)__mptr - offsetof(type, member)); })  
  8. #endif   
  9.   
  10. #define container_of2(ptr, type, member) ({          \   
  11.      (type *)((char *)ptr - offsetof(type, member)); })  
  12.    
  13. struct my{   
  14.     int i;  
  15.     int y;  
  16. };  
  17. char k=0;  
  18.   
  19. int main(){  
  20.   struct my qq;   
  21.   struct my *qqp;  
  22.   
  23.   qq.y=8;  
  24.   printf("%p %p\n",&(qq.y),&qq);  
  25.   
  26.   qqp=container_of(&(qq.y),struct my,y); //OK   
  27.   qqp=container_of2(&(qq.y),struct my,y);//OK   
  28.   
  29.  qqp=container_of(&k,struct my,y);  //这里会因为参数不匹配而报错   
  30.   qqp=container_of2(&k,struct my,y);  //没有报错,得到了一个错误的结果   
  31.   return 0;  
  32. }  
#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估计是因为用习惯了,总得有个数,要是随便写的话,也许引起的误解会更多。自己的想法,和实验的结果,欢迎指正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值