内核中container_of宏的详细分析【转】

转自:http://blog.chinaunix.net/uid-30254565-id-5637597.html

  1. 内核中container_of宏的详细分析
  2. 16年2月28日09:00:37
  3. 内核中有一个大名鼎鼎的宏-----container_of();这个宏定义如下所示,为了表示一下敬意,我就把注释一起粘贴下来了:
  4. /**
  5.  * container_of - cast a member of a structure out to the containing structure
  6.  * @ptr:    the pointer to the member.
  7.  * @type:    the type of the container struct this is embedded in.
  8.  * @member:    the name of the member within the struct.
  9.  *
  10.  */
  11. #define container_of(ptr, type, member) ({            
  12.     const typeof( ((type *)0)->member ) *__mptr = (ptr);    
  13.     (type *)( (char *)__mptr - offsetof(type,member) );})
  14. 先来说这个宏的意义:它根据结构体中某成员变量的指针来求出指向整个结构体的指针。指针类型从结构体某成员变量类型转换为 该结构体类型。

  15. 比如先定义一个结构体:
  16. struct test {
  17.     char name[20] ;
  18.     char i;
  19.     int j;
  20. };
  21. 假如,我们不小心知道了变量j的地址,那么我们想要通过j的地址来找到整个结构体test的地址,怎么来找呢???

  22. (一) offsetof宏:
  23. 结构体是一个线性存储的结构,无论在哪存放,j相对于整个结构体的地址的偏移值是不变的,于是,如果我们能够求出来这个偏移值的话,那么用j的地址减去这个偏移值不就是整个结构体的地址么~这是一个朴素的想法,内核中也确实这么做的~关键是怎么求出这个偏移值?内核的非常聪明的采取了下面的方法:
  24. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  25. (1)首先通过(TYPE *)0将0转换为TYPE类型的指针;
  26. (2)((TYPE *)0)->MEMBER 访问结构中的数据成员;
  27. (3)&(((TYPE *)0)->MEMBER)取出数据成员的地址;
  28. (4)(size_t)(&(((TYPE*)0)->MEMBER))结果转换类型; 注意这里:这个&是取地址符号,不是按位与,注意运算符号的优先级。

  29. 巧妙之处在于将地址0强制类型转换成(TYPE*),结构体以内存空间首地址0作为起始地址,则各个结构体成员变量的偏移地址就等于其成员变量相对于整个结构体首地址的偏移量 。即:&(((TYPE *)0)->MEMBER)就是取出其成员变量的偏移地址,(size_t)(&(((TYPE*)0)->MEMBER))经过size_t的强制类型转换以后,其数值为结构体内的偏移量。
  30. 需要明确的一点是,地址就是地址,它没有类型之分,你把它强制转换成什么类型它就是什么类型,所以在c语言中有各种强制类型转换。
  31. 用下面的例子来说明:
  32. #include <stdio.h>

  33. struct test {
  34.     char i;
  35.     int j;
  36.     int k;
  37. };

  38. int main(int argc, char const *argv[])
  39. {
  40.     struct test *temp = 0;

  41.     printf("%p ", &(temp->j));
  42.     printf("%d ", (size_t) &(temp->j));
  43.     printf("%p ", &(temp->k));
  44.     printf("%d ", (size_t) &(temp->k));

  45.     return 0;
  46. }
  47. 运行结果是:
  48. 0x4
  49. 4
  50. 0x8
  51. 8
  52. 可以看出来,通过采用这种方式,就可以求出来结构体中成员变量相对与整个结构体首地址的偏移量。

  53. (二) container_of宏
  54. 如果理解了上面的部分,再看这个container_of宏就不是那么难了,我们先想想它怎么实现:
  55. 假设我们知道一个test类型的结构体里面的一个成员变量j的地址,那么需要先求出这个j变量相对于整个结构体地址的偏移值,然后用这个j的地址减去这个偏移值就行了。但是还有一点,这个j变量的数据类型是什么样的?这个虽然我们知道test数据类型,但是我们怎么取出来j对应的数据类型呢?这时候就用到typeof关键字了,typeof是GNU C对标准C的扩展,它的作用是根据变量获取变量的数据类型。
  56. 下面来看这些代码:
  57. #define container_of(ptr, type, member) ({            
  58.     const typeof( ((type *)0)->member ) *__mptr = (ptr);    
  59.     (type *)( (char *)__mptr - offsetof(type,member) );})
  60. 首先
  61. (1)((type *)0)->member为设计一个type类型的结构体,并且这个结构体的的起始地址为0,然后将它指向我们知道的member变量,然后通过typeof( ((type *)0)->member )来获得member对应的数据类型。
  62. (2)const typeof( ((type *)0)->member ) *__mptr = (ptr);意思是声明一个与member同一个类型的指针常量 *__mptr,并初 始化为ptr.
  63. (3)(char *)__mptr - offsetof(type,member)意思是__mptr的地址减去member在该struct中的偏移量得到的地 址, 这样得到的就是整个结构体的首地址。
  64. (4)得到首地址后还没有完,上面说了,地址只是一个地址,它没有数据类型,所以最后再进行一一次强制类型转换,转换成我们需要的type类型的,即:
  65. (type *)( (char *)__mptr - offsetof(type,member) );
  66. (5)({ })这个扩展返回程序块中最后一个表达式的值。注意这个 container_of宏是两个表达式语句的综合。 相当与顺序执行了两个语句,这时候得到的地址就是member成员所在结构体的首地址。
  67. 要注意的是代码高亮处 ,(char *)__mptr 的作用是将__mptr 强制转换为字符指针类型,必须的!!!如果__mptr为整形指针 __mptr - offset 相当于减去sizeof(int)*offset个字节!!!

  68. 下面再看一个程序来温习一下:

  69. #include <stdio.h>

  70. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  71. #define container_of(ptr, type, member) ({            
  72.     const typeof( ((type *)0)->member ) *__mptr = (ptr);    
  73.     (type *)( (char *)__mptr - offsetof(type,member) );})

  74. struct test {
  75.     char name[20] ;
  76.     char i;
  77.     int j;
  78. };

  79. int main(int argc, char const *argv[])
  80. {
  81.     struct test temp = {"zer0", 'a', 26};

  82.     printf("&temp = %p. ", &temp);
  83.     printf("&temp.i = %p. ", &temp.i);
  84.     printf("&temp.j = %p. ", &temp.j);
  85.     printf("offset of i = %d. ", offsetof(struct test, i));
  86.     printf("offset of j = %d. ", offsetof(struct test, j));
  87.     printf("&temp = %p. ", container_of(&temp.i, struct test, i));
  88.     printf("&temp = %p. ", container_of(&temp.j, struct test, j));

  89.     struct test *tmp = container_of(&temp.i, struct test, i);
  90.     printf("tmp->name : %s, tmp->i : %c, tmp->j : %d. ", tmp->name, tmp->i,                                             tmp->j);
  91.     return 0;
  92. }

  93. 运行结果如下所示:
  94. &temp = 0xbf8b41b0.
  95. &temp.i = 0xbf8b41c4.
  96. &temp.j = 0xbf8b41c8.
  97. offset of i = 20.
  98. offset of j = 24.
  99. &temp = 0xbf8b41b0.
  100. &temp = 0xbf8b41b0.
  101. tmp->name : zer0, tmp->i : a, tmp->j : 26.
原文地址:https://www.cnblogs.com/sky-heaven/p/9597183.html