linux/list.h中container_of宏背后的原理

woo*_*tok 31 c struct pointers linked-list linux-kernel

在linux内核列表的实现中/include/linux/list.h,container_of宏的第一行(下面粘贴)背后的基本原理是什么?

const typeof( ((type *)0)->member ) *__mptr = (ptr);
Run Code Online (Sandbox Code Playgroud)

在我的示例代码中,我删除了这一行并将定义更改为

#define container_of(ptr, type, member) ({                      \
     (type *)( (char *)ptr - offsetof(type,member) );})
Run Code Online (Sandbox Code Playgroud)

我的代码仍显示预期的结果.那么第一行是多余的吗?或者它有一些我不知道的隐藏陷阱?

我在Faq/LinkedLists找到的代码

/**
 * 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) );})

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
Run Code Online (Sandbox Code Playgroud)

Mat*_*Mat 34

它增加了一些类型检查.使用您的版本,这可以很好地编译(没有警告):

struct foo { int bar; };

....

float a;
struct foo *var = container_of(&a, foo, bar);
Run Code Online (Sandbox Code Playgroud)

使用内核版本,编译器报告:

warning: initialization from incompatible pointer type
Run Code Online (Sandbox Code Playgroud)

很好地解释了宏如何工作:容器 _of Greg Kroah-Hartman.