深入理解Linux内核list_entry()宏
来源:互联网 发布:福建广电网络中考查分 编辑:程序博客网 时间:2024/05/16 11:28
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
----------------------------------------------------------------------------------------
转载 :http://hi.baidu.com/gmwandodxdbnosq/item/a2904b107160ddfb756a84f0
- 深入理解Linux内核list_entry()宏
- 深入分析 Linux 内核链表 list_entry...
- 深入分析 Linux 内核链表 list_entry...
- linux内核中的list_entry宏
- 内核链表list_entry()宏详解
- 内核list_entry宏分析
- 深入理解LINUX内核
- 深入理解 Linux 内核
- 深入理解linux内核
- 深入理解 Linux 内核
- 深入理解Linux内核
- Linux内核中的list_head结构体以及list_entry宏
- 深入理解Linux内核(一)
- 深入理解linux内核---进程(上)
- 深入理解linux内核---进程(下)
- 《深入理解linux内核》笔记(一)
- 《深入理解LINUX内核》笔记(二)
- 《深入理解LINUX内核》笔记(三)
- screen命令相关
- DES算法的C#实现
- Java反射
- 在 Eclipse 中使用 JUnit4 进行单元测试
- Nginx 日志分析 AWStats + JAWStats 安装配置
- 深入理解Linux内核list_entry()宏
- 后危机时代,DCS的新征程
- Apache HTTP Server 与 Tomcat 的三种连接方式介绍
- 一个单链表C++简单的实现版本-转自chinaunix
- RHEL 5.4 + Nginx + Mediawiki
- Sap basis
- DHCP协议原理及其实现流程
- 对象脚本语言BeanShell介绍
- 烤鸭、面饼和甜面酱之朴素关联