看内核的网络子系统又碰到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所在的结构体的首地址。