container_of 见解

来源:互联网 发布:网页在线视频剪辑软件 编辑:程序博客网 时间:2024/05/22 01:29

看内核的网络子系统又碰到container_of,以前碰过,又忘了。今天重温,发现真不一样


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

const typeof(((type *)0)->member) * __mptr = (ptr);\

(type *)((char *)__mptr - offsetof(type, member)); })


const typeof(((type *)0)->member) * __mptr = (ptr);

这个定义一个指针__mptr等于ptr,这个很关键,因为const typeof(((type *)0)->member) * 这个定义是0地址偏移量,赋值之后就是ptr,

这个再经过(type *)((char *)__mptr - offsetof(type, member));

我们就得到ptr所在的结构体的首地址。



0 0
原创粉丝点击