container_of()宏

  最近学习为Android添加编写内核驱动,遇到的第一个问题就是linux中大名鼎鼎的container_of()宏。

  container_of()是Linux内核中一个非常重要的宏,主要功能就是:根据结构体中一个域成员的地址获取整个结构体的地址。

  container_of()定义如下:

 1 /**
 2  * container_of - cast a member of a structure out to the containing structure
 3  * @ptr:    the pointer to the member.
 4  * @type:    the type of the container struct this is embedded in.
 5  * @member:    the name of the member within the struct.
 6  *
 7  */
 8 #define container_of(ptr, type, member) ({             9     const typeof( ((type *)0)->member ) *__mptr = (ptr);    10     (type *)( (char *)__mptr - offsetof(type,member) );})

  路径:(Android源码)//kernel/goldfish/include/linux/kernel.h(学习android,就使用Android中的kernel代码了)。

  该宏的执行主要分为两步:

  第一步、定义一个变量保存传入的域成员的地址。

const typeof( ((type *)0)->member ) *__mptr = (ptr);

  以0为内存地址并强转为type类型(传入的结构体),通过->member(传入的域成员)获取到结构体的域成员,并用typeof()取得域成员的类型,以域成员的类型定义一个临时变量__mptr,把域成员的地址ptr保存到__mptr中。

  第二步、使用域成员地址减去域成员相对于结构体首地址的偏移。

  这一步首先使用offsetof()取得域成员相对于结构体首地址的偏移,再使用域成员的实际地址__mptr减去该偏移就可以得到结构体的首地址。

  下面以一张图来形象的描述该过程:

  

  当前传入域成员k的地址0x0010008,需要获取整个结构体的地址。首先使用offsetof()获取k相对于结构体地址的偏移8,再用0x0010008-8=0x00010000,这就是结构体的地址。

  理解清楚了就会发现是很简单的一件事。

  参考:

  http://www.cnblogs.com/sdphome/archive/2011/09/14/2176624.html

  http://www.cnblogs.com/hicjiajia/archive/2012/07/01/2571791.html

  http://blog.csdn.net/npy_lp/article/details/7010752

时间: 2024-07-30 04:31:27

container_of()宏的相关文章

深入浅出实例解析linux内核container_of宏

做一件事情首先应该知道它的目的是什么. container_of的目的:如何通过结构中的某个变量获取结构本身的指针. 总体思路:假想一下,你的结构体中有好几个成员,你如何通过里面的"任一成员"获取整个结构体的首地址呢.container_of的做法就是通过typeof定义一个与"任一成员"同类型的指针变量pvar_a(假设变量名就是pvar_a),并让指针变量pvar_a指向这个"任一成员",然后用 "pvar_a的地址" 减

第十七天:APCS和container_of宏

APCS 全称:ARM 过程调用标准 如果要写用来与编译后的 C 连接的汇编代码,则必须使用 APCS. 今天的课程最终的两个目标:使用符合APCS标准的汇编写输出hello world  以及编写container_of宏 .这两个的推导过程比较复杂和具有跳跃性.结论的话要记住两个知识点,一:编写汇编函数的时候要对相应的寄存器进行保护.保护代码需要记住,每次写函数的时候都要用到.二:container_of实现了根据一个结构体变量中的一个域成员变量的指针来获取指向整个结构体变量的指针的功能.以

C语言之offset_of宏和container_of宏

通过结构体整体变量来访问其中各个元素,本质上是通过指针方式来访问的,形式上是通过.的方式来访问的(这时候其实是编译器帮我们自动计算了偏移量). 1:offset_of宏 作用:计算结构体中某个元素和结构体首地址的偏移量(其实质是通过编译器来帮我们计算). 定义: #define offsetof(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER) 参数分析:TYPE是结构体类型,MEMBER是结构体中一个元素的元素名 返回值:member元素相对于整个结构体

C语言之offsetof宏和container_of宏

首先我们要明白一点通过结构体变量来访问结构体中的各个元素时,其本质上是 通过指针的方式来实现访问的,只不过是这个时候编译器帮自动帮我们计算了每个 元素与结构体起始地址之间的偏移量而已 一:offsetof宏: #define offsetof(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER) 1:参数与返回值分析: (1)TYPE是结构体类型,MEMBER是结构体中一个元素的元素名 (2)这个宏返回的是member元素相对于整个结构体变量的首地址的偏移量,

container_of宏定义解析

container_of宏,定义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 th

对offsetof、 container_of宏和结构体的理解

offsetof 宏 #include<stdio.h> #define offsetoff(type, member)      ((int)&((type*)0)->member) /* ((type*)0)->member 释义:声明一个相应类型的结构体指针,该指针指向0地址处.再通过该指针访问各元素.我们只要获取一个指针就能访问其内部的元素吗,可以这么搞?其实我是想联系全局和非全局变量,通过上面这个代码也许你不明白我要表达的意思.请继续慢慢看,直到本文后面,结合本文

container_of宏剖析

container_of宏剖析//该宏位于include/linux/kernel.h 1.定义格式 /** * 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 nam

linux中offsetof与container_of宏定义

linux内核中offsetof与container_of的宏定义 #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

offsetof与container_of宏分析

offsetof宏:结构体成员相对结构体的偏移位置 container_of:根据结构体成员的地址来获取结构体的地址 offsetof 宏 原型: #define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER) (TYPE *)0非常巧妙,告诉编译器有一个指向结构体 TYPE 的指针,其地址是0,然后取该指针的 MEMBER 地址 &((TYPE *)0)->MEMBER,因为基址是0,所以这时获取到的 MEMBER