转载:http://blog.chinaunix.net/uid-20608849-id-3027972.html

由于内核中定义了很多复杂的数据结构,而它们的实例中的成员在作为函数参数传递的时,函数中可能需要对它的包含者中的其他的兄弟成员进行处理,这就需要只根据成员地址就可以获取整个结构体变量的地址的操作。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 *))->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})

巧妇难为无米之炊,无论如何,都需要告知container_of该整体结构体变量的类型以及当前成员的指针和成员名。typeof用来获取成员的类型并定义一个临时变量__mptr来存储当前成员的地址。offsetof用来获取当前成员相对于整体结构体地址的偏移。它定义为:

 include/linux/compiler-gcc4.h
#define __compiler_offsetof(a,b) __builtin_offsetof(a,b) include/linux/stddef.h
#ifdef __compiler_offsetof
#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
#else
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif

如果定义了__compiler_offsetof,则使用Gcc编译器内建的offsetof宏,它的作用和此处定义的offsetof相同。它将0地址作为当前结构的首地址,从而直接通过指针访问成员得到的地址即为偏移。将实际使用的结构体中的成员指针__mptr减去offsetof,就得到了结构体的地址。

 #include <stdio.h>
......
typedef struct man
{
char name[];
unsigned int id;
unsigned char age;
char address[];
}man_t; int main()
{
man_t tom = {"Tom", , , "ShangHai China"};
man_t *man = NULL; printf("tom:%p, tom.age:%p, offsetof(man_t, age): %d\n",
&tom, &tom.age, offsetof(man_t, age)); man = container_of(&tom.age, man_t, age);
printf("tom.name:%s, tom.id:%d, tom.age:%u, tom.address:%s\n",
man->name, man->id, man->age, man->address); return ;
}

测试结果如下:

 tom:0xbf85cda4, tom.age:0xbf85cdc8, offsetof(man_t, age):
tom.name:Tom, tom.id:, tom.age:, tom.address:ShangHai China
 

linux tricks 之 container_of.的更多相关文章

  1. linux内核宏container_of

    首先来个简单版本 /* given a pointer @ptr to the field @member embedded into type (usually * struct) @type, r ...

  2. Linux内核中container_of宏的详细解释

    上一节拒绝造轮子!如何移植并使用Linux内核的通用链表(附完整代码实现)我们在分析Linux内核链表的时候注意到内核在求解结构体偏移的时候巧妙的使用了container_of宏定义,今天我们来详细剖 ...

  3. linux tricks 之 BUILD_BUG_ON_ZERO.

    ------------------------------------------- 本文系作者原创, 欢迎大家转载! 转载请注明出处:netwalker.blog.chinaunix.net -- ...

  4. linux tricks 之 FIELD_SIZEOF.

    ------------------------------------------- 本文系作者原创, 欢迎大家转载! 转载请注明出处:netwalker.blog.chinaunix.net -- ...

  5. linux tricks 之 bitmap分析.

    ------------------------------------------- 本文系作者原创, 欢迎大家转载! 转载请注明出处:netwalker.blog.chinaunix.net -- ...

  6. linux tricks 之数据对齐。

    转载:http://blog.chinaunix.net/uid-20608849-id-3027953.html   内核为了保持最大的兼容性和代码灵活性,不可能直接对某个数据类型定义它的大小范围. ...

  7. linux tricks 之VA系列函数.

    VA函数(variable argument function),参数个数可变函数,又称可变参数函数.C/C++编程中,系统提供给编程人员的va函数很少.*printf()/*scanf()系列函数, ...

  8. linux tricks 之 typeof用法.

    typeof是gcc的扩展功能,比较简单,是用来取得参数类型,具体可参考gcc官网的解释. https://gcc.gnu.org/onlinedocs/gcc/Typeof.html ------- ...

  9. linux内核代码container_of

    它的作用显而易见,那就是根据一个结构体变量中的一个域成员变量的指针来获取指向整个结构体变量的指针. typedef unsigned int __kernel_size_t; typedef __ke ...

随机推荐

  1. hdu 1048 The Hardest Problem Ever

    import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(Strin ...

  2. Unity3d三大光照渲染介绍

      重要:在目前市面上常见的游戏引擎中,主要采用以下三种灯光实现方式: 顶点照明渲染路径细节 Vertex Lit Rendering Path Details 正向渲染路径细节 Forward Re ...

  3. 锋利的jQuery-4--animate()的用法

    1.一般动画: $("btn").click(function(){ $("div").animate({"left" : "+= ...

  4. C语言产生随机数

    rand产生随机数 #include"stdio.h" #include"stdlib.h" void main() { int i; for(i=0;i< ...

  5. web.config 加密/解密

    (Aspnet_regiis.exe) 这样的一个工具,可以对站点的.config文件的节进行加密 方法: #> 加密:aspnet_regiis -pef "加密的web.confi ...

  6. JavaScript初级教程(Jquery)

    序,学习前端页面编程技术,JS是不得不学的一门技术,目前JS不仅可以作为前端编程语言,在服务器端也有了一定发展,例如NodeJS.废话不多书,本篇博客主要介绍JS作为前端语言,怎样获得和改变HTML标 ...

  7. 关于 jsonp 跨域

    jquery的jsonp真是一个很深沉的东西.....我一直以为,jsonp就是和json一样,然而并不是,jsonp的返回格式必须包在定义的回调里面...就是这样,默认是cb.要不然就报错,就是这样 ...

  8. Hello Kitty微信主题很可爱?小心财产安全!

    个性化是产品服务的一个趋势.微信很火,可为什么微信主题只有一个呢?你让那些小女生情何以堪?这时HelloKitty微信主题.大嘴猴.哆啦A梦等一大批主题在网上出现了,有些打着免费的旗号却做着盗号的勾当 ...

  9. PHP网页数据正则采集

    $url ="https://********"; $contents = file_get_contents($url); //抓取页面数据 //如果出现中文乱码使用下面代码 / ...

  10. android menu showAsAction属性

    app中有一个菜单(menu),showAsAction主要是针对这个菜单的显示起作用的,它有三个可选项:always:总是显示在界面上 never:不显示在界面上,只让出现在右边的三个点中 ifRo ...