zoukankan      html  css  js  c++  java
  • container_of宏定义分析---linux内核

    问题:如何通过结构中的某个变量获取结构本身的指针???

    关于container_of宏定义在[include/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) );})
    container_of在Linux Kernel中的应用非常广泛,它用于获得某结构中某成员的入口地址.

    关于offsetof宏定义在[include/linux/stddef.h]中:
    #define offsetof(type, member) ((size_t) &((type *)0)->member)
    type是某struct的类型 0是一个假想type类型struct,member是该struct中的一个成员. 由于该struct的基地址为0, member的地址就是该成员相对与struct头地址的偏移量.
    关于typeof,这是gcc的C语言扩展保留字,用于声明变量类型.
    const typeof( ((type *)0->member ) *__mptr = (ptr);意思是声明一个与member同一个类型的指针常量 *__mptr,并初始化为ptr.
    (type *)( (char *)__mptr - offsetof(type,member) );意思是__mptr的地址减去member在该struct中的偏移量得到的地址, 再转换成type型指针. 该指针就是member的入口地址了.

    例一:

    用一个测试程序test.c来说明

    #include<stdio.h>
    struct student{
        char name[20]; 
        char sex;
    }stu={"zhangsan",'m'};

    main()
    {
        struct student *stu_ptr;    //存储container_of宏的返回值
        int offset;                        //存储offsetof宏的返回值


    //下面三行代码等同于 container_of(&stu.sex,struct student, sex )参数带入的情形
    //首先定义一个_mptr指针,类型为struct student 结构体中sex成员的类型
        const typeof(((struct student*)0)->sex) *_mptr = &stu.sex;
    //typeof 为获取(((struct student*)0)->sex)的类型,此处此类型为char
    //((struct student*)0)在offsetof处讲解

        offset = (int)(&((struct student *)0)->sex);
    /**

    *((struct student*)0)为 把 0地址 强制转化为指向student结构体类型的指针
    *该指针从地址 0 开始的 21个字节用来存放name 与 sex,(char name[20] 与 char sex 共 21 字节)
    *sex存放在第20个字节处(从0字节开始)
    *&((struct student *)0)->sex 取出sex地址(此处即为20) 并强制转化为整形
    *所以offset为20,后面的printf结果将证明这一点

    **/
        stu_ptr = (struct student *)((char*)_mptr - offset);
    /**

    *((char*)_mptr - offset)此处先把_mptr指针转化为字符形指针
    *(为什么这么做呢? 如果_mptr为整形指针 _mptr - offset 相当于减去 sizeof(int)*offset个字节)
    *减去 offset值 相当于 得到_mptr所在结构体的首地址(即stu的地址)
    *然后我们把 该地址 强制转化为 struct student类型即可正常使用了

    **/
        printf("offsetof stu.sex = %d/n",offset); 
        printf("stu_ptr->name:%s/tstu_ptr->sex:%c/n", stu_ptr->name, stu_ptr->sex);
        return 0;
    }

    例二:

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

    比如,有一个结构体变量,其定义如下:

       1. struct demo_struct {
       2.     type1 member1;
       3.     type2 member2;
       4.     type3 member3;
       5.     type4 member4;
       6. };
       7.
       8. struct demo_struct demo;

    同时,在另一个地方,获得了变量demo中的某一个域成员变量的指针,比如

       1. type3 *memp = get_member_pointer_from_somewhere();

    此时,如果需要获取指向整个结构体变量的指针,而不仅仅只是其某一个域成员变量的指针,我们就可以这么做:

       1. struct demo_struct *demop = container_of(memp, struct demo_struct, member3);
    首先,我们将container_of(memp, struct demo_struct, type3)根据宏的定义进行展开如下:

       1. struct demo_struct *demop = ({                      /
       2.     const typeof( ((struct demo_struct *)0)->member3 ) *__mptr = (memp);    /
       3.     (struct demo_struct *)( (char *)__mptr - offsetof(struct demo_struct, member3) );})

    其中,typeof是GNU C对标准C的扩展,它的作用是根据变量获取变量的类型。因此,上述代码中的第2行的作用是首先使用typeof获取结构体域变量member3的类型为 type3,然后定义了一个type3指针类型的临时变量__mptr,并将实际结构体变量中的域变量的指针memp的值赋给临时变量__mptr。

    假设结构体变量demo在实际内存中的位置如下图所示:
         demo
    +-------------+ 0xA000
    |   member1              |
    +-------------+ 0xA004
    |   member2             |
    |                                |
    +-------------+ 0xA010
    |   member3             |
    |                                |
    +-------------+ 0xA018
    |   member4             |
    +-------------+

    则,在执行了上述代码的第2行之后__mptr的值即为0xA010。

    再看上述代码的第3行,其中需要说明的是offsetof,它定义在include/linux/stddef.h中,其定义如下:

       1. 24#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

    同样,我们将上述的offsetof调用展开,即为:

       1. (struct demo_struct *)( (char *)__mptr - ((size_t) &((struct demo_struct *)0)->member3) );

    可见,offsetof的实现原理就是取结构体中的域成员相对于地址0的偏移地址,也就是域成员变量相对于结构体变量首地址的偏移。

    因此,offsetof(struct demo_struct, member3)调用返回的值就是member3相对于demo变量的偏移。结合上述给出的变量地址分布图可知,offsetof(struct demo_struct, member3)将返回0x10。

    于是,由上述分析可知,此时,__mptr==0xA010,offsetof(struct demo_struct, member3)==0x10。
    因此, (char *)__mptr - ((size_t) &((struct demo_struct *)0)->member3) == 0xA010 - 0x10 == 0xA000,也就是结构体变量demo的首地址(如上图所示)。

    由此,container_of实现了根据结构体变量中的一个域成员变量的指针来获取指向整个结构体变量的指针的功能。

    参考链接:

    http://blog.csdn.net/yinkaizhong/article/details/4093795

    http://www.cnitblog.com/puppypyb/archive/2008/08/20/48172.aspx

    http://bbs.ednchina.com/BLOG_ARTICLE_216992.HTM

    http://blog.chinaunix.net/uid-26510579-id-3198843.html

    http://blog.sina.com.cn/s/blog_55a7eb8201009elt.html

  • 相关阅读:
    日期时间格式
    input表单加disable的后无法获取其value值
    登录判断,清除中间变量
    弹出层嵌套
    转换qq音乐格式为mp3
    layui双击表格,跳转修改页面
    设置输入电话号码为11位
    layui表格设置,分数合计
    解决数据库传回时间类型为数字
    自定义标签
  • 原文地址:https://www.cnblogs.com/followyourdream/p/3369816.html
Copyright © 2011-2022 走看看