contanier_of offsetof list_for_each list_for_each_entry

来源:互联网 发布:计算机c语言基础知识 编辑:程序博客网 时间:2024/04/28 04:57

list_for_eachlist_for_each_entry详解  

一、list_for_each

1.list_for_each原型
#define list_for_each(pos, head) \
    for (pos = (head)->next, prefetch(pos->next); pos != (head); \
    pos = pos->next, prefetch(pos->next))
它实际上是一个 for 循环,利用传入的pos 作为循环变量,从表头 head开始,逐项向后(next方向)移动 pos ,直至又回到 head prefetch() 可以不考虑,用于预取以提高遍历速度)。
注意:此宏必要把list_head放在数据结构第一项成员,至此,它的地址也就是结构变量的地址。

2.使用方法(以访问当前进程的子进程为例):

struct list_head {
 struct list_head *next, *prev;
};

struct task_struct 中有如下定义:
struct list_head children;

所以

struct task_struct *task;

struct list_head *list;

list_for_each(list,¤t->chilidren) {

              task = list_entry(list, struct task_struct, sibling);/*task指向当前的某个子进程*/

}

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

二、list_for_each_entry
Linux内核源码中,经常要对链表进行操作,其中一个很重要的宏是list_for_each_entry
意思大体如下:
假设只有两个结点,则第一个member代表head
list_for_each_entry的作用就是循环遍历每一个pos中的member子项。
1
pos:                                                           pos:
___________                                        ____________
|                       |                                     |                          |
|                       |                                     |                          |
|    ...........        |                                     |   ................       |
|                       |                                     |                           |
|                       |                                     |                           |
|     member:    |                   _________|__> member    |
|   {                  |                |                     |  {                       |
|         *prev;    |                |                     |       *prev;        |
|         *next;        --|----------                    |        *next;-------------
|    }                |                                      |  }                      |             |
|—^———— |                                      |____________|             |
       |                                                                                                      |
       |                                                                                                     |
       |_____________________________________________|
list_for_each_entry: 
   
#define list_for_each_entry(pos, head, member)                          \
        for (pos = list_entry((head)->next, typeof(*pos), member);      \
             prefetch(pos->member.next), &pos->member != (head);        \
             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子项的指针,获得整个结构体的指针(地址)
#define list_entry(ptr, type, member) \
        container_of(ptr, type, member) 
和函数prefetch: 
#define prefetch(x) __builtin_prefetch(x) 

其中用到了builtin_prefetch:
prefetch的含义是告诉cpu那些元素有可能马上就要用到,告诉cpu预取一下,这样可以提高速度
其中用到了函数container_of(): 
493#define container_of(ptr, type, member) ({                      \
494        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
495        (type *)( (char *)__mptr - offsetof(type,member) );}) 
其中又用到了offsetof()函数:
lxr上找到的源码: 
#define offset_of(type, memb) \
  47        ((unsigned long)(&((type *)0)->memb)) 
offsetof(TYPE, MEMBER)
该宏在Linux内核代码(版本2.6.22)中定义如下:
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER); 
分析:
(TYPE *)0,将 强制转换为 TYPE 型指针,记 p = (TYPE *)0p是指向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; 
可见,该宏的作用就是求出MEMBERTYPE中的偏移量。

关于list_entry的解释

list_entry这样定义:
#define list_entry(ptr, type, member) \
             ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))

描述

我们使用list_entry()宏在linux链表中访问链表数据。

  原理为指针ptr指向结构体type中的成员member;通过指针ptr,返回结构体type的起始地址。

       定义中((size_t) &(type *)0)->member)意为:把0地址转化为type结构的指针,然后获取该结构中member成员的指针,并将其强制转换为size_t类型

解释 &((type *)0)->member

  “0”强制转化为指针类型,则该指针一定指向“0”(数据段基址)。因为指针是“type *”型的,所以可取到以“0”为基地址的一个type型变量member域的地址。那么这个地址也就等于member域到结构体基地址的偏移字节数。

  ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))

  (char *)(ptr)使得指针的加减操作步长为一字节,(unsigned long)(&((type *)0)->member)等于ptr指向的member到该member所在结构体基地址的偏移字节数。二者一减便得出该结构体的地址,转换为 (type *)型的指针。

 

例一;

container_of宏定义在[include/linux/kernel.h]中:

#define container_of(ptr, type, member) /

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

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

offsetof宏定义在[include/linux/stddef.h]中:

#define offsetof(type, member) ((size_t) &((type *)0)->member)

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

 

#include<stdio.h>

struct student{

char name[20];

char sex;

}stu={"zhangsan",'m'};

s

main()

{

struct student *stu_ptr; //存储container_of宏的返回值

int offset; //存储offsetof宏的返回值

//下面三行代码等同于 container_of(&stu.sex,struct student, sex )参数带入的情形

 

const typeof(((struct student*)0)->sex) *_mptr = &stu.sex;

//首先定义一个 _mptr指针,类型为struct student结构体中sex成员的类型

//typeof 为获取(((struct student*)0->sex)的类型,此处此类型为char

//((struct student*)0)在offsetof处讲解

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

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

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;

}

例二:

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

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) );})

其中,typeofGNU 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==0xA010offsetof(struct demo_struct, member3)==0x10

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

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

 

 

0 0
原创粉丝点击