uclinux内核的container_of
来源:互联网 发布:linux 搭建go编译环境 编辑:程序博客网 时间:2024/06/10 08:29
快乐虾
http://blog.csdn.net/lights_joy/
lights@hb165.com
本文适用于
ADSP-BF561
uclinux-2008r1.5-rc3 (smp patch)
欢迎转载,但请保留作者信息
内核中大量使用container_of这个宏,用以进行数据结构的向上转换,比如这样一段代码:
static int platform_match(struct device * dev, struct device_driver * drv)
{
struct platform_device *pdev = container_of(dev, struct platform_device, dev);
return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
}
在这里struct platform_device定义为:
struct platform_device {
const char * name;
u32 id;
struct device dev;
u32 num_resources;
struct resource * resource;
};
注意,在这个结构体里面包含了一个叫device的结构体,可以将struct platform_device称为父结构体,将struct device称为子结构体。
在上述函数中,传递进来的dev指针将指向platform_device这个父结构体中的dev成员,而contianer_of这个宏的作用则在于通过这个结构体成员的指针获得其父结构体的指针,在这里就是platform_device这一结构体的指针。
要达到这一目的很容易,只要将这个成员的指针减去此成员在整个结构体内的偏移量自然就可以了。看看container_of的定义:
/**
* 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) );})
这个宏定义的第一行构建一个叫__mptr的临时变量,实际上并不是必须的,它的作用只是强制编译器对数据类型进行检查。第二行里面的__mptr实际就是子结构体成员的指针。
偏移量的计算由offsetof宏完成:
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
这种方法到处都在用,没什么新奇的。
- uclinux内核的container_of
- uclinux内核的console
- 内核中的container_of的实现
- Linux内核的Container_of机制
- 内核之container_of的理解
- uclinux内核线程的创建
- uclinux内核的文件系统类型
- uclinux内核的任务切换
- 编译自己的uclinux内核
- linux内核链表宏 container_of的详细推导
- 【转】Linux内核的container_of宏
- 对linux内核宏container_of的理解
- Linux内核中container_of宏的理解
- 对linux内核宏container_of的理解
- Linux内核中container_of宏的理解
- linux内核的container_of()宏定义分析
- Linux内核中container_of宏的理解
- 内核宏定义container_of的理解
- PostgreSQL数据库忘记密码的解决方案
- 从源码看spring applicationContext在web容器中加载过程
- EJB的学习
- Direct3D 9.0 SDK 文档 (中文版)
- [zt]Flex 3: 构建高级用户界面 添加拖放支持4
- uclinux内核的container_of
- cookie与session知识点总结
- AOL邮箱的客户端设置
- 使用P4D 编写Python Extension
- System corrupt again (running in linux cd)
- 拉格朗日插值算法Lagrange-Interpolynomials
- 环形队列的一些看法
- [zt]Flex 3: 构建高级用户界面 添加拖放支持5
- 人际沟通---与同事交往“十大忌”