【转】Linux内核的container_of宏
来源:互联网 发布:e网购商城和淘宝一样吗 编辑:程序博客网 时间:2024/05/05 09:05
Linux内核的container_of宏
作用:
通过一个结构的成员的地址和结构名以及成员名,获得结构的首地址。
宏定义:
include/linux/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) );})
思路很简单:
ptr-(member在type内的偏移地址)
求member在type内的偏移地址有个小技巧,将地址0转换成一个member对象,然后这个对象的成员member的地址就是偏移量。
在宏的定义用用了一个C99的标准宏offsetof,它的作用是取得一个结构体中成员的偏移量。
一个比较常见的宏实现如下:
- /**
- * offset_of - get the offset of a member to its struct
- * @type: the name of the struct
- * @member: the name of the member
- */
- #define offset_of(type,member) ({ /
- (size_t)&(((type*)0)->member);})
在这个宏的最后一句,不能将__mptr转换成void*,因为void*指针不能参与算术运算。
这里是直接用typeof来定义类型。
比如这样:
- const typeof(a) b;
这是一个定义,将b定义成和a一样的类型,且是const的。
- 【转】Linux内核的container_of宏
- Linux内核container_of宏
- Linux内核,container_of 宏
- 对linux内核宏container_of的理解
- Linux内核中container_of宏的理解
- 对linux内核宏container_of的理解
- Linux内核中container_of宏的理解
- linux内核的container_of()宏定义分析
- Linux内核中container_of宏的理解
- 关于linux内核container_of宏的理解
- Linux内核的Container_of机制
- linux内核中的container_of宏
- Linux内核中的container_of宏
- Linux内核:container_of宏分析
- linux内核container_of宏解析
- Linux内核Container_of宏解析
- 对linux内核的container_of和offsetof宏的理解
- 对Linux内核中container_of宏的理…
- initrd 简述
- doubel类型的四舍五入 || EL表达式取整或者取固定位小数 目前最好的方法
- 卡尔曼
- 写在2010年深化经济体制改革的重点工作部署---我国重提科研举国体制 拟实现16个重大项目突破
- extjs iframe 关闭 自身所在 window
- 【转】Linux内核的container_of宏
- c#委托基础
- CE7略观
- remoting基础
- 字符串比较: equals() 与 ==
- myeclipse8.5安装axis2插件
- WHQL认证公司 提供高品质whql认证服务
- 若干年后,看看写这个文章的真实性,判断媒体是不是在胡喊野叫---英对冲基金押注中国经济崩盘 若成功可获利5亿美元
- wpf界面展示关于ListView展示有倒影效果