转自: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 *))->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 ={,'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 ;
}
执行结果:
jibo@jibo-VirtualBox:~/cv_work/work/list/container_of $ ./main
test_struct->num =
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 *))->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 ;
}
执行结果为:
jibo@jibo-VirtualBox:~/cv_work/work/list/container_of1 $ ./main
test_struct->num =
test_struct->ch =A
test_struct->ch =0.000000

注意,由于这里并没有使用一个具体的结构体变量,所以成员num和f1的值是不确定的。

container_of分析--可用good【转】的更多相关文章

  1. container_of分析【转】

    转自:http://blog.csdn.net/tigerjibo/article/details/8299589 1.container_of宏 1> Container_of在Linux内核 ...

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

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

  3. 自定义View系列教程02--onMeasure源码详尽分析

    深入探讨Android异步精髓Handler 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)- 核心基础 Android多分辨率适配框架(2)- 原理剖析 Andr ...

  4. 项目管理知识体系指南(PMBOOK指南)(第5版) 阅读摘要

    1.7.2 项目经理的人际技能 领导力: 团队建设: 激励: 沟通: 影响力: 决策能力: 政治和文化意识: 谈判: 建立信任: 冲突管理: 教练技术: 3.4 规划过程组 在制定项目管理计划和项目文 ...

  5. 《JAVA学习笔记(1---13-4)》

    [1]问题: 1.什么叫做面向过程? 2.什么叫做面向对象? 解答: 1: 所谓的面向过程就是我们是一个执行者,我们要开发一个项目,这个项目要求要实现很多功能,作为执行者的我们就需要 去一个一个的找这 ...

  6. CVS 文件自动移 tag 的 Python 脚本

    CVS 文件自动移 tag 的 Python 脚本 背景 工作中使用的版本管理工具是 CVS,在两次发布中,如果修改的文件比较少,会选择用移 Tag 的方式来生成一个新 Tag 发布.文件比较少的情况 ...

  7. 【2013Esri全球用户大会精彩案例】GIS for Philadelphia’s Finest --费城警用GIS

     行业领域:警务   拥有6000多警员和侦探的费城警察局,历时三年,搭建了费城警用GIS,目前可以对每天发生的事进行汇总(如图1),并可动态的进行热点分析(如图2).区域指挥官可方便的查看警员的活动 ...

  8. STLC - 软件测试生命周期

    什么是软件测试生命周期(STLC)? 软件测试生命周期(STLC)定义为执行软件测试的一系列活动. 它包含一系列在方法上进行的活动,以帮助认证您的软件产品. 图 - 软件测试生命周期的不同阶段 每个阶 ...

  9. Week4——结对练习&团队作业1

    Deadline: 2017-10-14 10:00PM,以博客发表日期为准. 评分基准: 按时交 - 有分(结对代码-10分,结对博客-10分,团队博客-10分),检查的项目包括后文的三个方面 按要 ...

随机推荐

  1. Kotlin操作符重载:把标准操作加入到任何类中(KAD 17)

    作者:Antonio Leiva 时间:Mar 21, 2017 原文链接:https://antonioleiva.com/operator-overload-kotlin/ 就像其他每种语言一样, ...

  2. mysql数据库----Pymysql

    本节重点: pymysql下载和使用 sql注入 增.删.改:conn.commit() 查:fetchone.fetchmany.fetchall 一.pymysql的下载和使用 之前我们都是通过M ...

  3. Django 运行Admin 页面时出现 UnicodeDecodeError: 'gbk' codec can't decode byte XXXX解决方法

    具体报错信息 Traceback (most recent call last): File "D:\Anaconda3\lib\site-packages\django\core\hand ...

  4. [USACO19JAN]Cow Poetry

    题面 Solution: 这是一道很好的dp题. 一开始看不懂题面没有一点思路,看了好久题解才看懂题目... \(y[i]\) 为第 \(i\) 个词结尾,\(l[i]\) 为第 \(i\) 个词长度 ...

  5. jira+mysql+破解+中文+compose

    1.制作docker-compose.yml 2.安装 $ docker stack deploy -c docker-compose.yml mshk_jira

  6. gradle在build之后执行任务

    在打包后一般会有copy jar文件的需求. 先在build.gradle文件中定义你的task: task myCopy{ println "some copy code..." ...

  7. sping事务的理解

    阅读数:2020 一.事务的基本原理 Spring事务的本质其实就是数据库对事务的支持,没有数据库的事务支持,spring是无法提供事务功能的.对于纯JDBC操作数据库,想要用到事务,可以按照以下步骤 ...

  8. REST接口设计规范

    URI格式规范 URI(Uniform Resource Identifiers) 统一资源标示符 URL(Uniform Resource Locator) 统一资源定位符 URI的格式定义如下: ...

  9. CTSC2018 & APIO2018 颓废 + 打铁记

    CTSC2018 & APIO2018 颓废 + 打铁记 CTSC 5 月 6 日 完美错过报道,到酒店领了房卡放完行李后直接奔向八十中拿胸牌.饭票和资料.试机时是九省联考的题,从来没做过,我 ...

  10. Why is the ibdata1 file continuously growing in MySQL?

    We receive this question about the ibdata1 file in MySQL very often in Percona Support. The panic st ...