内核之container_of的理解

来源:互联网 发布:南京图书销售软件 编辑:程序博客网 时间:2024/05/16 17:50

最近在公司用户态看到有些地方使用了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 - ((size_t) &((type*)0)->member) );})
简单的理解一下这个container_of宏函数:

第一句:首先定义了一个__mptr的指针,这个指针的类型是typeof( ((type *)0)->member ),这个指针里存放的数据是ptr的值,ptr是什么呢,ptr是type结构体中member成员的地址指针。

第二句:(size_t) &((type *)0)->member这个的意思是将0地址强转为type *类型,然后对member成员取地址,那么现在我们可以想到,对于一个以0地址为头指针的结构体,member成员的地址即为头指针和member间的相对偏移。最后用__mptr减去这个相对偏移即可以得到结构体type的头指针。 

现在一起探讨一个问题,为什么要定义一个__mptr来存储ptr的值,我们完全可以用下面的宏:

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

   (type *)((char *)(ptr) - ((size_t)&((type *)0->member)));})

效果也是一样的嘛,希望知道原因的高手不吝赐教。。。

0 0