nzo*_*xia 5 macros linux-kernel c-preprocessor
而不是将container_of定义为:
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr);
(type *)( (char *)__mptr - offsetof(type,member) );})
Run Code Online (Sandbox Code Playgroud)
为什么这不会简单地工作:
#define container_of(ptr, type, member) ({ \
(type *)( (char *)(ptr) - offsetof(type,member) );})
Run Code Online (Sandbox Code Playgroud)
定义中第一行的用法是什么?
它增加了某种程度的类型安全性.使用你的第二个版本,我可以为ptr传递任何内容,它可以正常编译.使用内核的版本,如果你传入的指针ptr与你的类型不匹配,你至少会得到一个警告type.member.
| 归档时间: |
|
| 查看次数: |
423 次 |
| 最近记录: |