container_of用法及实现
container_of 有的情况下,只知道 struct结构中莫个成员的指针,而需要知道整个struct的指针 (如网卡驱动里面,list)
struct DDD {
int a;
int b;
int c;
int d;};struct DDD ddd;
|------------| <------- 得到这个
| a |
|------------|
| b |
|------------|
| c | <------- 已知这个
|------------|
| d |
|------------|
//得到成员在结构
#define offsetof(TYPE, MEMBER) ((unsigned int) &((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member) ({ \
const typeof(((type *)0)->member) * __mptr = (ptr); \
(type *)((char *)__mptr - offsetof(type, member)); }) int main()
{
printf("ddd is %p and ddd.c is %p \r\n",&ddd,&ddd.c);
struct DDD * p= container_of(&ddd.c,struct DDD,c);
printf("calc p is %p \r\n",p);
return 0;
}
/*
output:
ddd is 0x601030 and ddd.c is 0x601038
calc p is 0x601030
结果与想象
*/
container_of用法及实现的更多相关文章
- c&c++中的宏
1 c&c++中的宏 do {...} while (0); offsetof & container_of 2 引用 [1] do {...} while (0) 在宏定义中的作用 ...
- container_of 的用法
1.问题:如何通过结构中的某个变量获取结构本身的指针???关于container_of见kernel.h中:/*** container_of - cast a member of a structu ...
- 刨一刨内核container_of()的设计精髓
新年第一帖,总得拿出点干货才行,虽然这篇水分还是有点大,大家可以晒干了温水冲服.这段时间一直在整理内核学习的基础知识点,期间又碰到了container_of()这个宏,当然还包括一个叫做offseto ...
- container_of
在学习Linux驱动的过程中,遇到一个宏叫做container_of.该宏定义在include/linux/kernel.h中,首先来贴出它的代码: /** * container_of - cast ...
- container_of分析【转】
转自:http://blog.csdn.net/tigerjibo/article/details/8299589 1.container_of宏 1> Container_of在Linux内核 ...
- 嵌入式C语言自我修养 04:Linux 内核第一宏:container_of
4.1 typeof 关键字 ANSI C 定义了 sizeof 关键字,用来获取一个变量或数据类型在内存中所占的存储字节数.GNU C 扩展了一个关键字 typeof,用来获取一个变量或表达式的类型 ...
- container_of分析--可用good【转】
转自:http://blog.csdn.net/tigerjibo/article/details/8299589 版权声明:本文为博主原创文章,未经博主允许不得转载. 1.container_of宏 ...
- 关于container_of和list_for_each_entry 及其相关函数的分析
Linux代码看的比较多了,经常会遇到container_of和list_for_each_entry,特别是 list_for_each_entry比较多,因为Linux经常用到链表,虽然知道这些函 ...
- (linux)container_of()宏
在学习Linux驱动的过程中,遇到一个宏叫做container_of. 该宏定义在include/linux/kernel.h中,首先来贴出它的代码: /** * container_of - ...
随机推荐
- 模仿jquery的一些实现
wylUtil.js //w作为window的形参,就表示window (function(w) { // 定义一个全局的window.wyl变量,就类似于jquery里的$,Jquery对象 w.w ...
- hdu 2841 Visible Trees
/** 大意: 求[1,m], [1,n] 之间有多少个数互素...做了 1695 ,,这题就so easy 了 **/ #include <iostream> #include < ...
- 软件源(Software Sources)
写在前面:浏览了很多国内外的网站,看了很多关于软件源(Software Sources)设置的文章,发现有很多文章中对软件源的设置存在误解,为了让新人能顺利进入Ubuntu的大家庭,特地作此文,详细地 ...
- 【xcode】错误之Could not launch "" failed to get the task for process
http://blog.csdn.net/teng_ontheway/article/details/8467932 在Xcode下编译工程正常,在模拟器下运行正常,最后在真机上运行的时候出现了如下错 ...
- asp.net 开发注意的几点
WIN7中组件服务中的DCOM配置找不到Microsoft Excel应用程序的解决办法: 这主要是64位系统的问题,excel是32位的组件,所以在正常的系统组件服务里是看不到的 可以通过在运行里面 ...
- nginx的 CPU参数worker_processes和worker_cpu_affinity使用说明
官方说明: http://wiki.nginx.org/NginxChsHttpMainModule#worker_cpu_affinity http://wiki.nginx.org/NginxCh ...
- (step4.2.1) hdu 1372(Knight Moves——BFS)
解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...
- [Android学习笔记5]四大应用组件之一:Service 下
绑定方式的Service使用 在实现绑定服务时,最重要的是定义onBind()回调方法返回的接口,有三种方式: 1. 继承Binder类 2. 使用Messenger 3. 使用AIDL 这里对1,2 ...
- Handler总结
一.整体工程图 二.activity_handler.xml <?xml version="1.0" encoding="utf-8"?> < ...
- 【linux】 linux gpio操作
欢迎转载,转载时需保留作者信息,谢谢. 邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http:// ...