zoukankan      html  css  js  c++  java
  • 由linux内核某个片段(container_of)引发的对于C语言的深入理解



     
    /usr/src/linux-source-3.8.0/drivers/gpu/drm/radeon 这个文件夹以下

    去找到这个文件 mkregtable.c  打开,就能够看到了。

    #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 *)0)->member)*__mptr = (ptr);    
                         (type *)((char *)__mptr - offsetof(type, member)); })


          问题的引发事实上是看linux kernel development的时候看kernel data structure,看到的有个container_of 用来计算结构体里面元素的偏移量。

    问题重述:

              

    typeof(((type *)0)->member)

              

               这行代码为什么不会报错? 尝试把0 强制类型转换成指向type变量类型的指针,进而解引用这个指针指向结构体内部的member成员。


               尝试去解引用指向地址0x00极其附近的指针都是非法的。

    可是为什么会有这样的

    ((type *)0)->member

    使用方法呢? 


    问题的关键是我没有深刻的理解这个 -> 解引用操作符

    以下慢慢分析我们遇到的问题,适当的做一点知识的铺垫。。。


    #include <stddef.h>
    #include "stdio.h"
    
    int main()
    {
    
            struct foo
            {
                    int  a;
                    int  b;
            }temp;
    
            printf("temp:%d temp.a %d temp.b %d
    ",&temp,&temp.a,&temp.b);
            return 0;
    }

    jasonleaster@ubuntu:~$ ./a.out
    temp:1233957888 temp.a 1233957888 temp.b 1233957892

    上面这个demo就回想一下结构体变量本身的地址和结构体成员的地址之间的关系

    不难看出。结构体第一个元素的地址就是结构体的地址, 第一个元素地址加上第一个元素的大小作为偏移量。得到第二个元素的地址。


    继续铺垫一下。

    所谓的左值和右值就是left value, righit value.

    简单的说就是赋值符号= 左边的是左值。右边的是右值。当然有特殊情况。

    这个概念的出现主要是为了说明变量名和地址的联系


    assumption:

    int a;
    int b;//这是个坏习惯,变量没有初始化。

    。。

    为了说明问题。重点不在这里。

    //以下是重点: a = 10; b = a;


    对于a = 10。

    是把10这个常量写入到a 标记的地址中去。变量名a此时是地址


              而b = a;是把a的值赋值给b。注意,是将a地址中存储的值,写入到b标记的地址其中。这里有个动作须要注意。就是a作为右值,先是获知a标记的地址,然后去a标记的地址里面依照变量的类型。读数据!有个读的操作。


              而当a = 10;时。是没有这个对于a读数据的操作的。


    这样viewer应该可以感受到左值和右值的不同。


    上面这两个铺垫对于我们遇到的问题的理解十分关键。


    #include <stdio.h>
    
    int main()
    {
            struct foo
            {
                    int a;
                    int b;
                    int c;
                    int d;
            };
    
            (((struct foo*)0)->a);
            (((struct foo*)0)->b);
            return 0;
    }

    上面这个demo编译执行,全然没问题。



    可是改动一下:

    <span style="font-size:14px;">#include <stdio.h>
    
    int main()
    {
            struct foo
            {
                    int a;
                    int b;
                    int c;
                    int d;
            };
    
            (((struct foo*)0)->a);
            (((struct foo*)0)->b);
    
            printf("%d",(((struct foo*)0)->c));
    
            return 0;
    }</span>



    jasonleaster@ubuntu:~/Desktop$ ./a.out
    Segmentation fault (core dumped)


    呵呵。。。

    挂的妥妥的。。。问题。。。事实上非常easy

    这里printf要把这个数据打印出去,必然去读那个地址的数据。

    上帝啊,去读0x00附近的数据(此处确切的说是0x08)那是找死。。

    不挂才怪


    (((struct foo*)0)->c)


    被作为右值使用

    (((struct foo*)0)->a);

    而这里是没有赋值的,而且不会尝试去读取这个变量名标记的地址处的数据。

    于是不会有问题。


    上面没有printf的那个demo所以就一切OK


    我们再返回那个内核代码片段看看


    #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 *)0)->member)*__mptr = (ptr);    
                         (type *)((char *)__mptr - offsetof(type, member)); })

             typeof是gnu 对C扩展的keyword。值得注意的是这家伙c99里面都没有。typeof依据变量名能够返回变量的类型(碉堡了的操作,反正我是不知道怎么实现的)



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


    这个宏定义就是获得对于TYPE类型的变量(应该是个结构体,不然->干嘛),其成member的偏移量

    &取地址的操作结果是个右值,就是说&的结果仅仅能放在 赋值符号= 右边,不能放在左边,就这样。

    offsetof返回的是当前结构体TYPE成员MEMBER的地址偏移量

    const typeof(((type *)0)->member)*__mptr = (ptr);



    把__mptr强制类型转换成指向member所在结构体的指针

    并把ptr的值赋值给这个指针

    (type *)((char *)__mptr - offsetof(type, member));

     

    得到当前member在结构体中,在内存中的地址

     

     

    <span style="font-size:14px;">/************************************************************************
    code writer : EOF
    code date : 2014.04.18
    e-mail : jasonleaster@gmail.com
    code purpose :
            This isa simple demo for what container_of is usred for.
    If there is something wrong with my code, please touche me by e-mail.
    It's pleasure to get your feedback. Thank you.
     
    *************************************************************************/
     
    #include <stdio.h>
     
    #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)); } )
     
     
     
    int main()
    {
            struct foo
            {
                   int a;
                   int b;
                   int c;
                   int d;
            };
     
            structfoo temp;
       
    //-----If we don't know this block, we could also find the address of temp------------------------//
            struct foo number = {.a = 1,.b = 2,.c = 3,.d = 4};
       
            temp = number;
    //-----------------------------------------------------------------------------------------------//
     
    /*******************************************************************************************
         Just thinkabout that if we don't know what's the adress of the
    structure which contain some element we interestingin. But we knew
    the defination of the structure and some elements(eg.the temp
    structure's element -- b) of this type structure. Wecould make it
    by the macro -- container_of.
     
    ********************************************************************************************/
     
            structfoo* we_found = container_of(&temp.b,struct foo,b);
     
    //----we could get all message of the container whichthe element we knew located in--------//
     
           printf("structure we found
    ");
     
           printf("foo.a  =%d
    ",we_found->a);
           printf("foo.b  =%d
    ",we_found->b);
           printf("foo.c  =%d
    ",we_found->c);
           printf("foo.d  =%d
    ",we_found->d);
     
            return0;
    }</span>


     


    liuzjian@ubuntu:~/Desktop$ ./a.out
    structure we found
    foo.a  = 1
    foo.b  = 2
    foo.c  = 3

    foo.d  = 4

     

     

    这个demo帮我解释了contain_of的用处。

                如果有一些我们不知道的结构体部分,他的数据我们是不知道的,好比上面的number,详细细节我们不知道,如果我们仅有当中一个元素的地址,以及这个元素在结构体中定义的相应成员。以及我们知道这个结构体的定义,那么我们就能够获得这个结构体的全部数据细节。

     

    事实上最广的应用应该是内核数据结构,特殊的双向链表的使用了,这里使用container_of.




    update:2014年7月29日 凌晨一点


    一下是LDD上第三章的一段话,解释了container_of的用途

    Fortunately, in this case, the kernel hackers have done the tricky stuff for us, in the form of the container_of macro, defined in <linux/kernel.h> :

    container_of(pointer, container_type, container_field);

    This macro takes apointer to a field of type container_field, within a structure of type container_type, and returns a pointer to the containing structure.







  • 相关阅读:
    chrome jsonView插件安装
    Android之父Andy Rubin:被乔布斯羡慕嫉妒的天才
    一张图看懂苹果MacBook所有屏幕分辨率
    Mac如何让调整窗口大小更简单
    OS X快捷键小技巧
    magent编译安装及常见错误
    【STL】算法 — partial_sort
    Lucene 4.4 依据Int类型字段删除索引
    简易实现 TextView单行文本水平触摸滑动效果
    cocos2d js 怎样动态载入外部图片
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5354123.html
Copyright © 2011-2022 走看看