linux/list.h 中container_of 宏背后的基本原理
在/include/linux/list.h
中的linux内核列表的实现中,container_of
宏的第一行(粘贴在下面)背后的基本原理是什么?
const typeof( ((type *)0)->member ) *__mptr = (ptr);
在我的示例代码中,我删除了这一行并将定义更改为,
#define container_of(ptr, type, member) ({ \
(type *)( (char *)ptr - offsetof(type,member) );})
并且我的代码仍然显示预期结果。那么第一行是多余的吗?还是其中隐藏着一些我不知道的陷阱?
我在 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)
In the implementation of linux kernel lists in /include/linux/list.h
, what is the rationale behind the first line (pasted below) of the container_of
macro?
const typeof( ((type *)0)->member ) *__mptr = (ptr);
In a sample code of mine, I removed this line and changed the definition to
#define container_of(ptr, type, member) ({ \
(type *)( (char *)ptr - offsetof(type,member) );})
and my code still showed expected results. Is the first line redundant then? Or does it have some hidden trap that I am not aware of?
The code I found at 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它添加了一些类型检查。对于您的版本,编译良好(没有警告):
对于内核版本,编译器报告:
宏如何工作的良好解释:container_of,作者:Greg Kroah-Hartman。
It adds some type checking. With your version, this compiles fine (without warning):
With the kernel version, the compiler reports:
Good explanation of how the macro works: container_of by Greg Kroah-Hartman.