container_of是内核中常用的一个宏。定义如下:
#define container_of(ptr, type, member) ({ \
const typeof(((type *)0)->member) * __mptr = (ptr); \
(type *)((char *)__mptr - offsetof(type, member)); })
设个宏共有三个参数:
- ptr:结构体成员member的地址
- type:结构体的类型
- member:结构体成员
container_of宏的作用就是通过结构体type的成员member的地址ptr求得该结构体的首地址。
做法其实很简单,只需要求得member在结构体中的偏移地址,再使用ptr减去偏移地址就可以了。
在container_of的定义中,offsetof宏就是用来实现获取偏移地址的功能的。
offsetof宏定义如下:
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
其实道理也很简单,将0地址强转成结构体类型的指针,再取以0地址为开始的结构体中member成员的地址,就是成员member在结构体中的偏移地址。
但是在container_of宏的的定义中,还有一个语句:
const typeof( ((type *)0)->member ) *__mptr = (ptr);
该句的作用其实是一个错误检查,防止ptr与member类型不匹配。