container_of宏
1.container_of宏
1> Container_of在Linux内核中是一个常用的宏,用于从包含在某个结构中的指针获得结构本身的指针,通俗地讲就是通过结构体变量中某个成员的首地址进而获得整个结构体变量的首地址。
2>接口:
container_of(ptr, type, member)
ptr:表示结构体中member的地址
type:表示结构体类型
member:表示结构体中的成员
通过ptr的地址可以返回结构体的首地址
3> container_of的实现:
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
其实它的语法很简单,只是一些指针的灵活应用,它分两步:
第一步,首先定义一个临时的数据类型(通过typeof( ((type *)0)->member )获得)与ptr相同的指针变量__mptr,然后用它来保存ptr的值。
说明:typeof是GNU C对标准C的扩展,它的作用是根据变量获取变量的类型《typeof关键字在linux 内核中很常见》
第二步,用(char *)__mptr减去member在结构体中的偏移量,得到的值就是整个结构体变量的首地址(整个宏的返回值就是这个首地址)。
关于offsetof的用法可参见offsetof宏的使用。
2. 举例来说明container_of的使用
1>正确示例:
#include <stdio.h>
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
struct test_struct {
int num;
char ch;
float f1;
};
int main(void)
{
struct test_struct *test_struct;
struct test_struct init_struct ={12,'a',12.3};
char *ptr_ch = &init_struct.ch;
test_struct = container_of(ptr_ch,struct test_struct,ch);
printf("test_struct->num =%d\n",test_struct->num);
printf("test_struct->ch =%c\n",test_struct->ch);
printf("test_struct->ch =%f\n",test_struct->f1);
return 0;
}
执行结果:
jibo@jibo-VirtualBox:~/cv_work/work/list/container_of $ ./main
test_struct->num =12
test_struct->ch =a
test_struct->ch =12.300000
2>错误示例:
#include <stdio.h>
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
struct test_struct {
int num;
char ch;
float f1;
};
int main(void)
{
struct test_struct *test_struct;
char real_ch = 'A';
char *ptr_ch = &real_ch;
test_struct = container_of(ptr_ch,struct test_struct,ch);
printf("test_struct->num =%d\n",test_struct->num);
printf("test_struct->ch =%c\n",test_struct->ch);
printf("test_struct->ch =%f\n",test_struct->f1);
return 0;
}
执行结果为:
jibo@jibo-VirtualBox:~/cv_work/work/list/container_of1 $ ./main
test_struct->num =0
test_struct->ch =A
test_struct->ch =0.000000
注意,由于这里并没有使用一个具体的结构体变量,所以成员num和f1的值是不确定的。
container_of宏相关推荐
- linux中offsetof与container_of宏定义
linux内核中offsetof与container_of的宏定义 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->M ...
- offsetof宏和container_of宏
文章目录 1 offsetof宏 1.1 宏的作用 1.2 编译器做了什么 2 container_of宏 2.1 ({})是什么? 2.2 typeof 2.3 原理分析 1 offsetof宏 1 ...
- 【嵌入式】C语言高级编程-container_of宏(04)
00. 目录 文章目录 00. 目录 01. typeof 关键字 02. typeof与宏结合 03. typeof在内核源码中应用 04. container_of 宏分析 05. contain ...
- container_of宏定义分析---linux内核
问题:如何通过结构中的某个变量获取结构本身的指针??? 关于container_of宏定义在[include/linux/kernel.h]中: /*_* * container_of - cast ...
- (六)linux内核中的offsetof与container_of宏
参考: offsetof与container_of宏[总结] #define offsetof(type, member) (size_t)&(((type*)0)->member)#d ...
- Linux字符设备驱动中container_of宏的作用
Linux字符设备驱动中container_of宏的作用 首先看看这个宏的原型: container_of(ptr,type,member) 功能:根据一个结构体变量中的一个成员变量的指针来获取指向整 ...
- 内核中container_of宏的详细分析【转】
转自:http://blog.chinaunix.net/uid-30254565-id-5637597.html 内核中container_of宏的详细分析 16年2月28日09:00:37 内核中 ...
- Linux内核中的container_of宏
container_of宏用于根据已知结构体某个成员的地址得到整个结构体变量的地址,宏定义如下: #define container_of(ptr, type, member) ({ \ const ...
- Linux内核中container_of宏的理解
对 typeof 的理解: 实际上, typeof 并不是宏定义,它是GCC的关键字,是GCC特有的特性.如果只知道一个变量的名字要得到其类型,并不是宏定义能够完成的,这需要编译时的信息.所以,typ ...
最新文章
- python中findroot_python文件查找之find命令
- Linux 下配置 phpredis 的过程和遇到的问题
- 深入了解jquery中的ajax方法参数
- python 进入E盘_anaconda python环境与原有python环境的坑
- Quartus17.0 + ModelSim - IntelFpga安装及破解
- oracle数据文件头损坏6,恢复数据库时遇到数据文件头损坏 | 信春哥,系统稳,闭眼上线不回滚!...
- 删除2345输入法和智能云输入法这两个狗屁玩意的注册表
- C语言——学习笔记(全)
- 南方cass简码识别大全_cass-简码识别详细分解.doc
- 惠普m154a状态页_惠普m154a感叹号闪烁
- 雷军:别轻易尝试,要做好准备再去创业
- 如何保证网络安全,常见的网络安全技术
- 天龙八部元宝兑换代码
- ROS TF2 中的 四元数 基础部分
- 第二章 求之不得,寤寐思服。悠哉悠哉,辗转反侧
- 用php获取本机的IP
- Noip—p1309 瑞士轮
- 大道至简第七、八章读后感
- 苹果手机发短信怎么知道对方已读
- linux mysql搭建禅道详细教程_如何在Linux服务器上部署禅道