揭开linux内核中container_of的神秘面纱
来源:互联网 发布:域名跳转 绕过备案 编辑:程序博客网 时间:2024/05/18 00:58
/** * container_of - cast a member of a structure out to the containing structure * @ptr:<span style="white-space:pre"></span>the pointer to the member. * @type:<span style="white-space:pre"></span>the type of the container struct this is embedded in. * @member:<span style="white-space:pre"></span>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)); })
呵呵,乍一看不知道是什么东东。
我们先来分析一下container_of(ptr,type,member),这里面有ptr,type,member分别代表指针、类型、成员。看一个例子:
Struct test
{
int i;
int j;
char k;
};
Struct test temp;
现在我们知道container_of()的作用就是通过一个结构变量中一个成员的地址找到这个结构体变量的首地址。
我们用上面的struct test张展一下
Const typeof(((struct test *)0)->j) * __mptr = (&temp.j);
其中,typeof是GNU C对标准C的扩展,它的作用是根据变量获取变量的类型。因此,上述代码的作用是首先使用typeof获取结构体成员j的类型为int,然后顶一个int指针类型的临时变量__mptr,并将结构体变量中的成员的地址赋给临时变量__mptr。
补充:Const int *__mptr = (&temp.j);
(struct test *)((char *)__mptr - offsetof(struct test,j));
接着我们来看一下offsetof(struct test,j),他在内核中如下定义:
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
展开(size_t)&((struct test *)0)->j,这是什么东东?
一开始也不明白,这里要感谢曹老师老师的热心帮助,一语惊醒梦中人,呵呵,可以是这样理解。
其中size_t是整型,那么我们可以知道最终的结果是一个整形值,也就是j相对于0地址的偏移量。也许现在你会问,整出这么个玩意干嘛,下面看个列子:
发现没有如果把第二个值 减去最后一个值,就能得到第一个值。
再回首一下它:
(struct test *)((char *)__mptr - offsetof(struct test,j));
是不是可以获得结构体变量temp的首地址呀,是不是太精妙了呀,linux内核中随随便便一个宏就有如此精妙,呵呵,想想对linux了解非常多的牛人,还有很长一段路。
这篇文章确实是写的好,特别是例子印象深刻!重点推荐。
- 揭开linux内核中container_of的神秘面纱
- 揭开linux内核中container_of的神秘面纱
- 揭开linux内核中container_of的神秘面纱
- 揭开linux内核中container_of的神秘面纱
- 揭开linux内核中container_of的神秘面纱
- 揭开linux内核中container_of的神秘面纱
- 揭开linux内核中container_of的神秘面纱
- 揭开linux内核中container_of的神秘面纱
- 揭开linux内核中container_of的神秘面纱
- 揭开linux内核中container_of的神秘面纱
- 揭开linux内核中container_of的神秘面纱
- 揭开linux内核中container_of的神秘面纱
- 揭开linux内核中container_of的神秘面纱
- 揭开linux内核中container_of的神秘面纱
- 揭开Linux Proc文件系统的神秘面纱
- 揭开Linux系统内核调试器神秘面纱(上)
- 揭开Linux系统内核调试器神秘面纱(下)
- 揭开木马的神秘面纱
- 个人学习第一阶段——关于html和css的技巧及问题总结(一)
- i.mx6 lvds接口的DE模式液晶屏驱动调试
- VC 6 项目转 VS2010
- xapges引用jquery
- SIGALRM信号
- 揭开linux内核中container_of的神秘面纱
- STL算法 -------- 逆转和旋转
- ios-day10-03(省份和城市联动。UIPickerView的使用、使用UIPickerView的reloadComponent:方法刷新某一列)
- hdu2203
- 多线程并发处理的新思路:Parallel的使用
- leetCode191Number of 1 Bits
- BitmapFactory.Options介绍(整理自查阅)
- 《iOS总结》推送消息-MickyChiang
- IOS从背景图中取色