list_entry()宏

list_entry()宏

#define list_entry(ptr, type, member) \
       ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))

    ptr是指向list_head类型链表的指针,type为一个结构,而member为结构type中的一个域,类型为list_head,这个宏返回指向type结构的指针。在内核代码中大量引用了这个宏,因此,搞清楚这个宏的含义和用法非常重要。

   
设有如下结构体定义:
typedef struct xxx
{
     ……(结构体中其他域,令其总大小为size1)
     type1 member;
     ……(结构体中其他域)
}type;


定义变量:
   type a;
   type * b;
   type1 * ptr;
执行:
   ptr=&(a.member);
   b=list_entry(ptr,type,member);
则可使b指向a,得到了a的地址

如何做到的呢?

先看&((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 *)型的指针,大功告成。


西邮陈莉君老师【http://www.lupaworld.com/26540/viewspace-119901.html

list_entry()宏:

--------------------------------------------------------------------------------------------------

/**

* list_entry - get the struct for this entry

* @ptr:   the &struct list_head pointer.

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

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

*/

#define list_entry(ptr, type, member) \

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

--------------------------------------------------------------------------------------------------

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

  

type

        |----------|

        | |

          | |

          |----------|

ptr--> | member --|

          |----------|

          | |

        | |

         |----------|

        图2 list_entry()宏的示意图

为了便于理解,在此给予进一步说明。

例如my_list结构:

struct my_list{

void *mydata;

struct list_head list;

}; struct list_head *pos;  

则list_entry(pos, mylist, list)宏,就可以根据pos的值,获取mylist的地址,也就是指向mylist的指针,这样,我们就可以存取mylist->mydata字段了。

可为什么能够达到这样的效果?

list_entry(pos, mylist, list) 展开以后为:

((struct my_list *)((char *)(pos) - (unsigned long)(&((struct my_list *)0)->list)))

这看起来会使大多数人眩晕,但仔细分析一下,实际很简单。

((size_t) &(type *)0)->member)把0地址转化为type结构的指针,然后获取该结构中member成员的指针,并将其强制转换为size_t类型。于是,由于结构从0地址开始定义,因此,这样求出member的成员地址,实际上就是它在结构中的偏移量。为了更好的理解这些,我们可以写一段程序来验证:

---------------------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

struct foobar{

unsigned int foo;

char bar;

char boo;

};

int main(int argc, char** argv){

struct foobar tmp;

printf("address of &tmp is= %p\n\n", &tmp);

printf("address of tmp->foo= %p \t offset of tmp->foo= %lu\n", &tmp.foo, (unsigned long) &((struct foobar *)0)->foo);

printf("address of tmp->bar= %p \t offset of tmp->bar= %lu\n", &tmp.bar, (unsigned long) &((struct foobar *)0)->bar);

printf("address of tmp->boo= %p \t offset of tmp->boo= %lu\n\n", &tmp.boo, (unsigned long) &((struct foobar *)0)->boo);

printf("computed address of &tmp using:\n");

printf("\taddress and offset of tmp->foo= %p\n",

(struct foobar *) (((char *) &tmp.foo) - ((unsigned long) &((struct foobar *)0)->foo)));

printf("\taddress and offset of tmp->bar= %p\n",

(struct foobar *) (((char *) &tmp.bar) - ((unsigned long) &((struct foobar *)0)->bar)));

printf("\taddress and offset of tmp->boo= %p\n",

(struct foobar *) (((char *) &tmp.boo) - ((unsigned long) &((struct foobar *)0)->boo)));

return 0;

}

Output from this code is:

address of &tmp is= 0xbfffed00

address of tmp->foo= 0xbfffed00 offset of tmp->foo= 0

address of tmp->bar= 0xbfffed04 offset of tmp->bar= 4

address of tmp->boo= 0xbfffed05 offset of tmp->boo= 5

computed address of &tmp using:

address and offset of tmp->foo= 0xbfffed00

address and offset of tmp->bar= 0xbfffed00

address and offset of tmp->boo= 0xbfffed00

----------------------------------------------------------------------------------------

到此,我们对链表的实现机制有所了解,但在此止步的话,我们依然无法领略这风景背后的韵味。

尽管list.h是内核代码中的头文件,但我们可以把它移植到用户空间使用。且看下一讲,链表接口之应用。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值