Chinaunix首页 | 论坛 | 博客
  • 博客访问: 898388
  • 博文数量: 194
  • 博客积分: 7991
  • 博客等级: 少将
  • 技术积分: 2067
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-09 22:03
文章分类

全部博文(194)

文章存档

2010年(8)

2009年(71)

2008年(109)

2007年(6)

我的朋友

分类:

2008-01-09 16:13:11

 
container_of在内核中很常见,意思是说,知道一个数据结构的某个成员的地址的话,就能找到这个结构的地址。其实好理解,数据结构在内存地址空间上是连续的,某个成员的地址减去该成员在数据结构中的偏移量就能得到数据结构的地址。
 
使用container_of常常能得到意外的惊喜,因为有了它,你看到的将不仅仅是这一个成员的地址了,你有能力访问到这个数据结构的所有的成员。
 

/*
 * container_of - cast a member of a structure out to the containing structure
 * @ptr:     the pointer to the member.
 * @type: the type of the container struct this is embedded in.
 * @member:    the name of the member within the struct.
*/

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

sxg

 

阅读(768) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~