linux中container_of详解
linux中container_of详解作用详解typeof(((type *)0)->member)const typeof(((type )0)->member)__mptr = (ptr);offsetof(type, member))(type *)((char *)__mptr - offsetof(type, member))举例作用根据一个结构体变量中的一个成员变量的指针
linux中container_of详解
作用
根据一个结构体变量中的一个成员变量的指针来获取指向整个结构体变量的指针
详解
container_of出现在linux/kernel.h中,定义如下:
/**
* 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) );})
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE*)0)->MEMBER)
ptr是成员变量的指针, type是指结构体的类型, member是成员变量的名字。
typeof
首先看下typeof,是用于返回一个变量的类型,这是GCC编译器的一个扩展功能,也就是说typeof是编译器相关的。既不是C语言规范的所要求,也不是某个标准的一部分。
(((type *)0)->member)
((TYPE *)0)将0转换为type类型的结构体指针,换句话说就是让编译器认为这个结构体是开始于程序段起始位置0,开始于0地址的话,我们得到的成员变量的地址就直接等于成员变量的偏移地址了。 (((type *)0)->member) 引用结构体中MEMBER成员。
const typeof(((type )0)->member)__mptr = (ptr);
这句代码意思是用typeof()获取结构体里member成员属性的类型,然后定义一个该类型的临时指针变量__mptr,并将ptr所指向的member的地址赋给__mptr; 为什么不直接使用 ptr 而要多此一举呢? 我想可能是为了避免对 ptr 及prt 指向的内容造成破坏。
offsetof(type, member))
((size_t) &((TYPE*)0)->MEMBER)
size_t定义和架构有关
size_t是一个非负数,所以size_t通常用来计数(因为计数不需要负数区)
为了使程序有很好的移植性,因此内核使用size_t和,而不是int,unsigned。 ((size_t) &((TYPE*)0)->MEMBER) 结合之前的解释,我们可以知道这句话的意思就是求出MEMBER相对于0地址的一个偏移值。
(type *)((char *)__mptr - offsetof(type, member))
这句话的意思就是,把 __mptr 转换成 char * 类型, 因为 offsetof 得到的偏移量是以字节为单位。 两者相减得到结构体的起始位置, 再强制转换成 type 类型。
举例
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
typedef struct student
{
int id;
char name[30];
int math;
}Student;
int main()
{
Student stu;
Student *sptr = NULL;
stu.id = 123456;
strcpy(stu.name,"feizhufeifei");
stu.math = 90;
sptr = container_of(&stu.id,Student,id);
printf("sptr=%p\n",sptr);
sptr = container_of(&stu.name,Student,name);
printf("sptr=%p\n",sptr);
sptr = container_of(&stu.math,Student,id);
printf("sptr=%p\n",sptr);
return 0;
}
结果:
sptr=0xffffcb90
sptr=0xffffcb94
sptr=0xffffcbb4

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)