linux内核代码container_of
2016-05-16 11:11
405 查看
它的作用显而易见,那就是根据一个结构体变量中的一个域成员变量的指针来获取指向整个结构体变量的指针。
typedef unsigned int __kernel_size_t; typedef __kernel_size_t size_t; #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) /** * 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) );}) struct person { char name[10]; int age; int num; } ocjPerson; int main(void) { struct person* p = container_of(&ocjPerson.age, struct person, age); printf("the value of p = %x\n", p); printf("the value of &ocjPerson = %x\n", &ocjPerson); return 0; } /* 打印结果: the value of p == the value of &ocjPerson 分析: container_of(&ocjPerson.age, struct person, age); ({ \ const typeof( ((struct person *)0)->age ) *__mptr = (&ocjPerson.age); \ (struct person *)( (char *)__mptr - ((size_t) &((struct person *)0)->age) );}) A. typeof是GNU C对标准C的扩展,它的作用是根据变量获取变量的类型 B.#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) 分析一下这个offsetof宏的运行机理: 一共4步 1. ( (TYPE *)0 ) 将零转型为TYPE类型指针; 2. ((TYPE *)0)->MEMBER 访问结构中的数据成员; 3. &( ( (TYPE *)0 )->MEMBER )取出数据成员的地址; 4.(size_t)(&(((TYPE*)0)->MEMBER))结果转换类型。巧妙之处在于将0转换成(TYPE*),结构以内存空间首地址0作为起始地址,则成员地址自然为偏移地址; */
相关文章推荐
- linux下如何查看某个软件 是否安装??? 安装路径在哪???
- ARM平台基于嵌入式Linux部署ROS
- CentOS 下JDK安装
- linux 基本命令
- 查看LINUX进程内存占用情况
- Linux - service 命令简介
- 【DSP开发】【Linux开发】Linux下PCI设备驱动程序开发
- linux centos下mysql数据库的主从复制环境搭建
- linux在虚拟终端中的一些快捷方式
- 【Linux开发】Linux模块机制浅析
- centos7防火墙使用(firewalld)
- linux检测文件夹是否存在
- 【Linux开发】内核模块简介
- CentOS7安装RabbitMQ
- Linux系统上安装mysql5.7.12
- linux 查看线程状态
- 第四节,Linux基础命令
- Linux下的压缩解压缩命令详解
- linux下find命令和文件的三种时间
- linux中fork()函数详解(原创!!实例讲解) (转载)