您的位置:首页 > 运维架构 > Linux

linux核list_for_each_entry(pos, head, member)分析

2012-03-30 15:34 471 查看
这里大概叙述了typeof是一个什么东西,怎么用,实际上可以用简单的话来重述。如果你对sizeof很熟悉的话,那么大可进行类推,sizeof(exp.)返回的是exp.的数据类型大小,那么typeof(exp.)返回的就是exp.的
数据类型。值得注意的是在上面的话里我们可以看到,如果编译选项中指定了使用标准C,那么gcc的扩展使用可能会受到影响,不过在gcc编译条件下使用 __typeof__依然可以正常工作,这和使用asm是一样的。当然如果是在其他的编译器条件下,这样做也不行了,只能自定义一个macro去使用,也 就是说跟gcc没啥关系了,你愿意把typeof咋实现都可以

宏list_for_each_entry:

   /**

401 * list_for_each_entry  -       iterate over list of given type

402 * @pos:        the type * to use as a loop cursor.

403 * @head:       the head for your list.

404 * @member:     the name of the list_struct within the struct.

405 */

406#define list_for_each_entry(pos, head, member)                          \

407        for (pos = list_entry((head)->next, typeof(*pos), member);      \

408             prefetch(pos->member.next), &pos->member != (head);        \

409             pos = list_entry(pos->member.next, typeof(*pos), member))

复制代码
list_entry((head)->next, typeof(*pos), member)返回(head)->next物理指针所处位置向前减去offsetof()个字节数据之后, 其父变量pos的物理地址,父变量的类型在编译时由typeof(*pos)自动返回.
所以list_for_each_entry遍历head下面挂接的类型为typeof(*pos)的childs结构体们,当然每个child结构体包含struct list_head node之类相似的双向链表list_head类型项,就这样通过循环pos将依次指向双向链表上的各个child.(member就是child类型中被定义的变量名)

其中用到了函数list_entry():
这个函数的作用在图1中表示就是可以通过已知的指向member子项的指针,获得整个结构体的指针(地址)

/**

329 * list_entry - get the struct for this entry

330 * @ptr:        the &struct list_head pointer.

331 * @type:       the type of the struct this is embedded in.

332 * @member:     the name of the list_struct within the struct.

333 */

334#define list_entry(ptr, type, member) \

335        container_of(ptr, type, member)

复制代码
和函数prefetch:

#define prefetch(x) __builtin_prefetch(x)

复制代码
其中用到了builtin_prefetch:
prefetch的含义是告诉cpu那些元素有可能马上就要用到,告诉cpu预取一下,这样可以提高速度
其中用到了函数container_of():

/**

487 * container_of - cast a member of a structure out to the containing structure

488 * @ptr:        the pointer to the member.

489 * @type:       the type of the container struct this is embedded in.

490 * @member:     the name of the member within the struct.

491 *

492 */

493#define container_of(ptr, type, member) ({                      \

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

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

复制代码
下面这个链接讲container_of的作用,很详细了:
http://blog.chinaunix.net/u1/58968/showart_461749.html

再附一个很好的测试container_of作用的链接:
http://hi.baidu.com/xiquanlian/blog/item/a070d658642ea482810a18e3.html

其中又用到了offsetof()函数:
lxr上找到的源码:

#define offset_of(type, memb) \

  47        ((unsigned long)(&((type *)0)->memb))

复制代码
转一篇网上对它的分析:
原文链接在这:
http://cutebunny.blog.51cto.com/301216/67517
offsetof(TYPE, MEMBER)
该宏在Linux内核代码(版本2.6.22)中定义如下:
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER); 
分析:
(TYPE *)0,将 0 强制转换为 TYPE 型指针,记 p = (TYPE *)0,p是指向TYPE的指针,它的值是0。那么 p->MEMBER 就是 MEMBER 这个元素了,而&(p->MEMBER)就是MENBER的地址,而基地址为0,这样就巧妙的转化为了TYPE中的偏移量。再把结果强制转
换为size_t型的就OK了,size_t其实也就是int。
typedef __kernel_size_t  size_t;
typedef unsigned int __kernel_size_t; 
可见,该宏的作用就是求出MEMBER在TYPE中的偏移量。

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)-&g
a9e4
t;MEMBER)

#define container_of(ptr, type, member) ({                      \

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

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

 

    分析可知__mptr指向的是一个type结构里typeof(((type *)0)->member)类型member成员的指针,offsetof(type,member)是这个成员在结构中的偏移,单位是字节,所以为了计算type结构的起始地址,__mptr减去它自己的偏移。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息