`
xitong
  • 浏览: 6157521 次
文章分类
社区版块
存档分类
最新评论

container_of详解

 
阅读更多

container_of详解

#define container_of(ptr, type, member) ({ \

const typeof(((type *)0)->member) * __mptr = (ptr); \
(type *)((char *)__mptr - offsetof(type, member)); } )

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


container_of使用了C扩展关键字typeof,以及结构体成员变量偏移。下面来具体看看:
  • ((type *)0)是将0强制转化为type *,也就是指向type类型的指针;
  • ((type *)0)->member是访问type结构中的member成员;
  • const typeof( ((type *)0)->member ) *__mptr 定义一个与((type *)0)->member同种类型的指针变量(const变量);
  • const typeof( ((type *)0)->member ) *__mptr=(ptr)对常量指针变量__mptr赋值,__mptr=ptr;
  • (char *)__mptr - offsetof(type,member) 将__mptr强制转化为char类型指针,也就是__mptr;
  • ((size_t) &((TYPE *)0)->MEMBER得到member在type结构体中的偏移量的地址,然后减去member在结构体中的偏移量,的到的自然就是结构体起始地址;
  • (type *)( (char *)__mptr - offsetof(type,member) )最后再强制转化为(type *)类型得到ptr所在的结构体对象。
取得结构体一个字段的偏移地址的方法 offsetof(type,member) 宏,设置的十分巧妙,取得偏移后,具体对象的member地址-偏移地址,得到type对象的起始地址,设计相当的妙。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics