Linux中通用链表(list)的解析(5)
来源:互联网 发布:返利网好还是淘宝客 编辑:程序博客网 时间:2024/06/05 19:14
介绍一下list中的关键函数container_of:
/**
* 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) /
container_of(ptr, type, member)
关于container_of见kernel.h中:
/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
*/
#define container_of(ptr, type, member) ({ /
const typeof( ((type *)0)->member ) *__mptr = (ptr); /
(type *)( (char *)__mptr - offsetof(type,member) );})
container_of在Linux Kernel中的应用非常广泛,它用于获得某结构中某成员的入口地址.
关于offsetof见stddef.h中:
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
TYPE是某struct的类型 0是一个假想TYPE类型struct,MEMBER是该struct中的一个成员. 由于该struct的基地址为0, MEMBER的地址就是该成员相对与struct头地址的偏移量.
关于typeof,这是gcc的C语言扩展保留字,用于声明变量类型.
const typeof( ((type *)0->member ) *__mptr = (ptr);意思是声明一个与member同一个类型的指针常量 *__mptr,并初始化为ptr.
(type *)( (char *)__mptr - offsetof(type,member) );意思是__mptr的地址减去member在该struct中的偏移量得到的地址, 再转换成type型指针. 该指针就是member的入口地址了.
举例说明:
约定: 地址由高向低分配, 分配后指针指向高地址, 结构按逆序由高向低存储, 不做对齐处理.
struct snow
{
char name; // 1
int size; // 4
time_t hold; // 16
};
struct snow *p = (struct snow)malloc(sizeof(struct snow));
上面我们已经在堆中0x121至0x101分配了一个snow的struct, 并用p指针指向这段内存地址.
如果我们想取得p指向的结构中size变量的入口地址, 可以用container_of(p, struct snow, size);
这个调用的步骤是:
1` 取得p的指针地址, 121
2` 取得size在struct中的偏移量, 16
3` 相减获得size的入口地址, 105
通过这一个简单的例子应该能理解container_of的工作原理了, 这只是一个模仿的例子, 具体实现原理请参考ULK中的内存管理.
/**
* 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) /
container_of(ptr, type, member)
关于container_of见kernel.h中:
/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
*/
#define container_of(ptr, type, member) ({ /
const typeof( ((type *)0)->member ) *__mptr = (ptr); /
(type *)( (char *)__mptr - offsetof(type,member) );})
container_of在Linux Kernel中的应用非常广泛,它用于获得某结构中某成员的入口地址.
关于offsetof见stddef.h中:
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
TYPE是某struct的类型 0是一个假想TYPE类型struct,MEMBER是该struct中的一个成员. 由于该struct的基地址为0, MEMBER的地址就是该成员相对与struct头地址的偏移量.
关于typeof,这是gcc的C语言扩展保留字,用于声明变量类型.
const typeof( ((type *)0->member ) *__mptr = (ptr);意思是声明一个与member同一个类型的指针常量 *__mptr,并初始化为ptr.
(type *)( (char *)__mptr - offsetof(type,member) );意思是__mptr的地址减去member在该struct中的偏移量得到的地址, 再转换成type型指针. 该指针就是member的入口地址了.
举例说明:
约定: 地址由高向低分配, 分配后指针指向高地址, 结构按逆序由高向低存储, 不做对齐处理.
struct snow
{
char name; // 1
int size; // 4
time_t hold; // 16
};
struct snow *p = (struct snow)malloc(sizeof(struct snow));
上面我们已经在堆中0x121至0x101分配了一个snow的struct, 并用p指针指向这段内存地址.
如果我们想取得p指向的结构中size变量的入口地址, 可以用container_of(p, struct snow, size);
这个调用的步骤是:
1` 取得p的指针地址, 121
2` 取得size在struct中的偏移量, 16
3` 相减获得size的入口地址, 105
通过这一个简单的例子应该能理解container_of的工作原理了, 这只是一个模仿的例子, 具体实现原理请参考ULK中的内存管理.
- Linux中通用链表(list)的解析(5)
- Linux中通用链表(list)的解析(1)
- Linux中通用链表(list)的解析(2)
- Linux中通用链表(list)的解析(3)
- Linux中通用链表(list)的解析(4)
- Linux中通用链表(list)的解析(6)
- Linux中通用链表(list)的解析
- linux内核学习中-- 史上最全 linux通用链表“list.h”详解
- Linux内核通用链表<linux/list.h>阅读
- Linux内核通用链表 <linux/list.h>阅读
- Linux内核中的通用链表list.h在windows下的移植实现
- linux内核部件--通用双向链表list
- linux内核部件--通用双向链表list
- linux内核中有一个通用的双向循环list链表
- Java中List转List树的通用解决方法~
- linux内核list.h中对链表的操作
- java中List对象排序的通用方法
- linux中通用GPIO接口的操作
- 用glue库写了一个main还是,调用一个com成功
- JAVA设计模式(转载)
- (新招儿) 巧妙使用ComboBox.Items的Add方法添加列表项
- 阿翔编程学-WebService介绍及使用
- 真正的手机密码大全!(整理完整版)
- Linux中通用链表(list)的解析(5)
- 批处理文件
- 我的股票:2007/06/04 净值:1.7658
- GameEngine_D3D
- GameEngine_Tomer
- Oracle触发器应用
- Spring之IOC
- GameEngine_Input
- Oracle大对象的使用