container_of是linux内核中常用的一个宏函数,其用途是通过结构体的某个成员变量的指针倒推出结构体变量的指针。在内核代码中,container_of的实现如下:
#ifndef offsetof #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) #endif #ifndef container_of /** * 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)); }) #endif首先,宏函数offsetof是用来计算结构体的起始到成员类型MEMBER的偏移量,其参数为:
TYPE为结构体的类型
MEMBER为结构体成员的类型
((TYPE *)0)表示虚拟的结构体变量指针,其起始地址为0,((TYPE *)0)->MEMBER表示MEMER类型的成员,&((TYPE *)0)->MEMBER则取MEMBER成员的地址,如果考虑结构体变量的起始地址为0,则MEMBER的地址,就是结构体指针0到MEMBER成员的偏移量
container_of的是三个参数是:
ptr:成员变量的指针
type:结构体的类型
member:与ptr对应的结构体成员的类型
其宏函数可以分解为以下几个部分
typeof(((type *)0)->member) * __mptr; //typeof是GNU C对标准C的扩展,它的作用是根据变量获取变量的类型,表示定义了一个结构体成员member类型的变量__mptr
__mptr = (ptr); //将结构体成员变量ptr的指针赋值给__mptr
通过offsetof(type, member)计算出结构体变量起始指针到member类型变量的偏移长度,
用__mptr即结构体成员变量的指针减去偏移长度,就可以得到结构体变量的地址指针
以下是一个例子
#include <stdio.h> #ifndef offsetof #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) #endif #ifndef container_of /** * 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)); }) #endif struct cat{ int age; int weight; char name[10]; }; int main(int argc, char** argv) { struct cat tom; tom.age = 3; tom.weight = 4; strcpy(tom.name, "Tom Cat"); struct cat *who; who = (struct cat*)container_of(&tom.name, struct cat, name); if (who == &tom) { printf("who is tom\n"); } who = NULL; who = (struct cat*)container_of(&tom.weight, struct cat, weight); if (who == &tom) { printf("who is tom\n"); } return 0; }
#who is tom
#who is tom