转自:http://blog.csdn.net/tigerjibo/article/details/8299589

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分析【转】的更多相关文章

  1. container_of分析--可用good【转】

    转自:http://blog.csdn.net/tigerjibo/article/details/8299589 版权声明:本文为博主原创文章,未经博主允许不得转载. 1.container_of宏 ...

  2. 转:container_of分析 研究内核的博客

    源地址:http://blog.csdn.net/tigerjibo/article/details/8299589 2012-12-15 19:23 1636人阅读 评论(2) 收藏 举报   目录 ...

  3. Linux内核学习散知识整理

    1.container_of(ptr, type, member) 使用方法:根据指向结构体type的成员member的指针ptr,获取指向改结构体的指针 /** * container_of - c ...

  4. container_of宏定义分析---linux内核

    问题:如何通过结构中的某个变量获取结构本身的指针??? 关于container_of宏定义在[include/linux/kernel.h]中:/*_** container_of - cast a ...

  5. 内核中container_of宏的详细分析【转】

    转自:http://blog.chinaunix.net/uid-30254565-id-5637597.html 内核中container_of宏的详细分析 16年2月28日09:00:37 内核中 ...

  6. 关于container_of和list_for_each_entry 及其相关函数的分析

    Linux代码看的比较多了,经常会遇到container_of和list_for_each_entry,特别是 list_for_each_entry比较多,因为Linux经常用到链表,虽然知道这些函 ...

  7. linux内核container_of宏定义分析

    看见一个哥们分析container_of很好,转来留给自己看 一.#define offsetof(TYPE, MEMBER) ((size_t) & ((TYPE *)0)->MEMB ...

  8. 关于container_of函数分析

    #include <stdio.h> #define offset_of(type,member) ((int)&(((type *)0)->member)) #define ...

  9. 【转】container_of宏 分析

    在学习Linux驱动的过程中,遇到一个宏叫做container_of.该宏定义在include/linux/kernel.h中,首先来贴出它的代码: /** * container_of - cast ...

随机推荐

  1. shell的sed命令

    sed命令用于在线编辑文本,它一次处理一行内容. 命令语法: sed [-n/e/f/r/i] [cmd] [InFile] 参数解释: 选项与参数: -n: quiet/silent,安静模式,经过 ...

  2. False Ordering LightOJ - 1109(暴力。。唉,。又是一个水题。。)

    We define b is a Divisor of a number a if a is divisible by b. So, the divisors of 12 are 1, 2, 3, 4 ...

  3. Unity使用C#实现简单Scoket连接及服务端与客户端通讯

    简介: 网络编程是个很有意思的事情,偶然翻出来很久之前刚开始看Socket的时候写的一个实例,贴出来吧 Unity中实现简单的Socket连接,c#中提供了丰富的API,直接上代码. 服务端代码: [ ...

  4. bzoj4385 & POJ2015 Wilcze doły

    Description 给定一个长度为n的序列,你有一次机会选中一段连续的长度不超过d的区间,将里面所有数字全部修改为0.请找到最长的一段连续区间,使得该区间内所有数字之和不超过p. Input 第一 ...

  5. 【UOJ228】基础数据结构练习题(线段树)

    [UOJ228]基础数据结构练习题(线段树) 题面 UOJ 题解 我们来看看怎么开根? 如果区间所有值都相等怎么办? 显然可以直接开根 如果\(max-sqrt(max)=min-sqrt(min)\ ...

  6. 简短的创建Ajax对象代码

    假如你的脚本只针对某个浏览器开发,那么创建XMLHTTP是很简单的一件事,用XMLHttpRequest或者ActiveXObject即可.但事实上绝大多数的时候,我们都要考虑兼容,于是我们通常写成: ...

  7. RF - selenium - 常用关键字

    1. 打开浏览器 Open Browser    htpp://www.testclass.net    chrome 2. 关闭浏览器 Close Browsers Close All Browse ...

  8. 个人常用Linux文件操作命令

    本文只记录个人工作中最常用到的命令.   关机和用户 shutdown -h now 关机 reboot 重启 sync 非正常关机前执行,强制将内存缓冲区中的数据立即写入磁盘中 logout 注销 ...

  9. 关于JSON的解析方式

    借鉴:站在巨人的肩膀上 一.json-lib json-lib最开始的也是应用最广泛的json解析工具,json-lib 不好的地方确实是依赖于很多第三方包,在Json.org网站上,Java可以使用 ...

  10. [USACO09NOV]硬币的游戏A Coin Game

    https://daniu.luogu.org/problemnew/show/P2964 dp[i][j] 表示桌面上还剩i枚硬币时,上一次取走了j个的最大得分 枚举这一次要拿k个,转移到dp[i- ...