它的作用显而易见,那就是根据一个结构体变量中的一个域成员变量的指针来获取指向整个结构体变量的指针。

typedef unsigned int    __kernel_size_t;
typedef __kernel_size_t size_t;
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) /**
* 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 *))->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );}) struct person
{
char name[];
int age;
int num;
} ocjPerson; int main(void)
{
struct person* p = container_of(&ocjPerson.age, struct person, age); printf("the value of p = %x\n", p);
printf("the value of &ocjPerson = %x\n", &ocjPerson); return ;
} /*
打印结果:
  the value of p == the value of &ocjPerson 分析:
container_of(&ocjPerson.age, struct person, age);
({ \
const typeof( ((struct person *)0)->age ) *__mptr = (&ocjPerson.age); \
(struct person *)( (char *)__mptr - ((size_t) &((struct person *)0)->age) );}) A. typeof是GNU C对标准C的扩展,它的作用是根据变量获取变量的类型 B.#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
分析一下这个offsetof宏的运行机理:
一共4步
1. ( (TYPE *)0 ) 将零转型为TYPE类型指针;
2. ((TYPE *)0)->MEMBER 访问结构中的数据成员;
3. &( ( (TYPE *)0 )->MEMBER )取出数据成员的地址;
4.(size_t)(&(((TYPE*)0)->MEMBER))结果转换类型。巧妙之处在于将0转换成(TYPE*),结构以内存空间首地址0作为起始地址,则成员地址自然为偏移地址; */

linux内核代码container_of的更多相关文章

  1. [转] Linux内核代码风格 CodingStyle [CH]

    from:http://blog.csdn.net/jiang_dlut/article/details/8163731 中文版维护者: 张乐 Zhang Le <r0bertz@gentoo. ...

  2. 从linux内核代码分析操作系统启动过程

    朱宇轲 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 在本次的实验中, ...

  3. 第三次阅读赵炯博士的《linux内核代码完全注释》:序

    这是我第三次阅读linux内核代码完全注释了,当然前两次也没有读完,第一次读到第五章,第二次第七章. 所以说,赵炯博士对我最大的帮助时介绍了intel386的结构,以及内核编程的方法. 至于真正的内核 ...

  4. 使用QEMU调试Linux内核代码

    http://blog.chinaunix.net/uid-20729583-id-1884617.html http://www.linuxidc.com/Linux/2014-08/105510. ...

  5. win7 变WIFI热点 & 在线Linux 内核代码

    https://daniel.haxx.se/docs/sshproxy.html netsh wlan set hostednetwork mode=allow ssid=mywifi  key=1 ...

  6. linux内核代码的编写初步以及makefile的配置

    在linux内核代码开发中,头文件不能包含标准C头文件,只能采用GNC标准 而且内核开发中没有main函数,只有init 和 exit ,这是每个内核模块中必须要包含的函数模块. 在GNU C标准中, ...

  7. Linux学习笔记:【004】Linux内核代码风格

    Chinese translated version of Documentation/CodingStyle   If you have any comment or update to the c ...

  8. Linux内核代码

    全局描述符表GDT(Global Descriptor Table): (1)在整个系统中,全局描述符表(注意这里是表,表只有一张)GDT只有一张(一个处理器对应一个GDT). (2)GDT可以被放在 ...

  9. linux内核代码注释 赵炯 第三章引导启动程序

    linux内核代码注释 第三章引导启动程序 boot目录中的三个汇编代码文件   bootsect.s和setup.s采用近似intel的汇编语法,需要8086汇编器连接器as86和ld86 head ...

随机推荐

  1. 10247 - Complete Tree Labeling(递推高精度)

    Problem B Complete Tree Labeling! Input: standard input Output: standard output Time Limit: 45 secon ...

  2. PHP新闻系统开发流程

    PHP新闻系统开发流程一.系统总体设计 (一)系统功能描述和功能模块划分 (二)系统流程分析 (三)系统所用文件二.数据库设计 (一)创建数据库 (二)设计表结构三.新闻发布模块开发 (一)新闻首页 ...

  3. 关于switch的思考和总结

    1.通常每个case的末尾都应该加个break; 否则会default分支也会被执行 var score = 40;switch (score){case 50:console.log('50');/ ...

  4. BZOJ 1023

    program bzoj1023; uses math; ; maxn=; maxm=; type edge=record togo,next:longint; end; var n,m,cnt,in ...

  5. http://qt-project.org/wiki/Category:Developing_with_Qt::QtWebKit#ff7c0fcd6a31e735a61c001f75426961

    404: Not Found | Qt Project QtWebKit documentation http://dwz.cn/hr2013

  6. 入门Html

    html 超文本标记语言(Hypertext Markup Language),用于描写网页文档的标记语言,最新的版本为5.0.由万维网制定 和更新,其实它本质还是文本文件,只不过需要浏览器来解释显示 ...

  7. IPv4头部结构具体解释

    IPv4头部结构具体解释 下面为书中原文摘录: $(function () { $('pre.prettyprint code').each(function () { var lines = $(t ...

  8. 变脸不变质的桥梁模式(Bridge Pattern)

    有一哥们是搞山寨货的,什么流行就搞什么.自己有个厂子,前些时间服装挣钱,就生产衣服,如今搞手机挣钱,搞手机,这哥们非常聪明,就换了个产品,工人,厂房都不变.他是怎么做到的?用类图来模拟一下: 由类图能 ...

  9. webform基础介绍及页面传值(session,cookie)、跳转页面

    一,IIS 1.首先知道IIS是个什么东西:它是web服务器软件,安装在服务器上,接受客户端发来的请求,并传送给服务器端,然后响应请求并送回给客户端.类似于饭店里的服务员. 2.会安装IIS——控制面 ...

  10. JSPatch技术文档

    一.背景需求介绍 为什么我们需要一个热修复(hot-fix)技术? 工作中容易犯错.bug难以避免. 开发和测试人力有限. 苹果Appstore审核周期太长,一旦出现严重bug难以快速上线新版本. 作 ...