Chinaunix首页 | 论坛 | 博客
  • 博客访问: 63434
  • 博文数量: 16
  • 博客积分: 1415
  • 博客等级: 上尉
  • 技术积分: 140
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-29 08:51
文章分类

全部博文(16)

文章存档

2011年(8)

2010年(8)

我的朋友

分类: LINUX

2010-12-29 14:49:12

在学习Linux驱动的过程中,遇到一个很好玩的内核函数,准确的说是一个很好玩的宏,叫做container_of。该宏定义在include/linux/kernel.h中,首先来贴出它的代码:
  1. 439/**
  2. 440 * container_of - cast a member of a structure out to the containing structure
  3. 441 * @ptr:        the pointer to the member.
  4. 442 * @type:       the type of the container struct this is embedded in.
  5. 443 * @member:     the name of the member within the struct.
  6. 444 *
  7. 445 */
  8. 446#define container_of(ptr, type, member) ({                      \
  9. 447        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
  10. 448        (type *)( (char *)__mptr - offsetof(type,member) );})
它的作用显而易见,那就是根据一个结构体变量中的一个域成员变量的指针来获取指向整个结构体变量的指针。比如,有一个结构体变量,其定义如下:
  1. struct demo_struct {
  2.     type1 member1;
  3.     type2 member2;
  4.     type3 member3;
  5.     type4 member4;
  6. };

  7. 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的实现的理解:
首先,我们将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实现了根据一个结构体变量中的一个域成员变量的指针来获取指向整个结构体变量的指针的功能。




the specific code is as the following:

#include
#include

//understandinf of the container_of macro

#define container_of(ptr, type, member) ({                      \
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
        (type *)( (char *)__mptr - offsetof(type,member) );})


struct test {
        int x;
        int y;
};

int main(void)
{
        struct test t = { .x = 10, .y = 20 };

        printf("&t      = %p\n", &t);
        printf("&t.x    = %p\n", &t.x);
        printf("&t.y    = %p\n", &t.y);
        printf("offsetof(struct test, y) = %d\n", offsetof(struct test, y));

        printf("start position: %p\n",container_of(&t.y,struct test,y));

        return 0;
}

the effect is :

thewayma@theway:~/Desktop$ ./a.out
&t      = 0xbfec45c8
&t.x    = 0xbfec45c8
&t.y    = 0xbfec45cc
offsetof(struct test, y) = 4
start position: 0xbfec45c8
thewayma@theway:~/Desktop$ 
阅读(674) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-12-30 14:06:48

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com