当前位置: 首页 > news >正文

Linux container_of() 宏定义

container_of 宏

今天遇到了一段这样的代码,大致意思是 通过该struct结构体变量的成员的地址来反推该struct结构体变量的地址

并且用到了内核的宏,container_of()

static inline struct nova_inode_info *NOVA_I(struct inode *inode)
{return container_of(inode, struct nova_inode_info, vfs_inode);
}

查了查该宏位于include/linux/kernel.h文件

功能:由结构体变量的某个成员变量的内存地址来得到该结构体变量的内存地址

参数:

  1. 该结构体成员变量的地址(指针)

    2. 该结构体的定义类型(type)
    3. 该结构体成员的变量名()
    

具体实现:

#define container_of(ptr, type, member) ({                      \const typeof( ((type *)0)->member ) *__mptr = (ptr);    \(type *)( (char *)__mptr - offsetof(type,member) );})

原理:

  1. 用到GCC的typeof预处理语句,typeof(x),返回参数x的类型
  2. 用到了offsetof(type,member),由一个结构成员member,得到相对于结构开头的字节偏移量 .在<stddef.h>

container_of()做了两步.

  1. 模拟该结构体变量指针指向成员然后通过typeof()反推成员的数据类型,const typeof( ((type *)0)->member),然后定义一个该成员变量类型的指针(就是ptr的数据类型)const typeof( ((type *)0)->member) *__mptr = (ptr)
  2. __mptr转换为char *,按照1个字节计算,__mptr减去成员偏移量就是结构体变量的基地址了。再将该地址(指针)转换为type指针
  3. 最后表达式返回的是 (type *) (结构体变量基地址) 指针
1. const typeof( ((type *)0)->member) *__mptr = (ptr)
2. (char *)__mptr - offsetof(type,member)

测试:

#include <stdio.h>
#include <stddef.h>#define everything_to_string(x) #x 
/*
#define container_of(ptr,type,member) ({ \const typeof( ((type *)0)->member ) *__mptr = (ptr); \(type *)( (char *)__mptr - offsetof(type,member) );})
*/
//const struct member *__mptr=ptr;
//(struct Student *)( (char *)__mptr - offsetof(type,member) ) 
#define container_of(ptr,type,member) ({ \const typeof( ((type *)0)->member ) *__mptr = (ptr); \(type *)((char *)__mptr - offsetof(type,member)); })struct info {int a;int b;
};
struct Student {char name[4]; // 0int age;      // 4int grade;    // 8int sex;      // 12struct info last;
};int main()
{struct Student student={"123",22,100,1,{10,20}};struct info *ptr=&student.last;struct Student *pstudent = container_of(ptr,struct Student,last);printf("[%p]\n",pstudent);printf("[%p]\n",ptr);return 0;
}

参考资料

  1. https://stackoverflow.com/questions/15832301/understanding-container-of-macro-in-the-linux-kernel
  2. https://gaomf.cn/2017/10/07/C_typeof/
  3. https://www.runoob.com/cprogramming/c-macro-offsetof.html
http://www.lryc.cn/news/157997.html

相关文章:

  • 详解python中的序列类型---列表list
  • Unity 引擎中国版 “团结引擎” 发布
  • MindsDB为许多不支持内置机器学习的数据库带来了机器学习功能
  • 世界级黑客丨电脑犯罪界的汉尼拔
  • 【Matlab】Matlab实现数据的动态显示方法
  • 【Android】SDK安装及配置
  • ETCD详解
  • React笔记(五)hook
  • vue3中使用viewerjs实现图片预览效果
  • Erlang:Linux下使用observer、debugger进行调试
  • 2023 年高教社杯全国大学生数学建模竞赛-E 题 黄河水沙监测数据分析详解+思路+Python代码
  • 一生一芯10——verilator v5.008环境搭建
  • 信息化发展27
  • leetcode做题笔记129. 求根节点到叶节点数字之和
  • 任务管理系统所需功能概述
  • 一文学会K8s集群搭建
  • Win10右键 nvidia rtx desktop manager 怎么删除(最新)
  • MySQL加密的几种常见方式
  • Android文字识别-阿里云OCR调用
  • 度矩阵、邻接矩阵
  • 20个经典巧妙电路合集
  • 2023全国大学生数学建模ABCDE选题建议,思路模型,小白要怎么选?难度怎么样
  • 【力扣每日一题】2023.9.5 从两个数字数组里生成最小数字
  • 跳出Lambda表达式forEach()循环解决思路
  • 2023年度AWS SAP直冲云霄训练营学习分享
  • 2023高教社杯 国赛数学建模E题思路 - 黄河水沙监测数据分析
  • 06_快速入门案例实战之电商网站商品管理:集群健康检查,文档CRUD
  • 机车整备场数字孪生 | 图扑智慧铁路
  • Fair|Fur —— Geometry Nodes
  • java八股文面试[设计模式]——行为型模式