linux内核中宏container_of是干什么的?
Linux Kernel Version 4.14
1. container_of是干什么的?
已知一个结构体中某个成员的首指针,那么就可以通过宏container_of来获得此结构体的首指针
2 先看看container_of是如何定义的吧
#define container_of(ptr, type, member) \
(type *)((char *)(ptr) - (char *) &((type *))->member)
解析一下各参数:
ptr: 结构体成员的指针
type: 结构体的类型
member: 结构体成员的名字
2. 举一个例子
2.1 有两个结构体如下:
struct element1 {
struct element2 struct_element2;
int element3;
}
struct element2 {
int element4;
}
2.2 定义一个struct element1变量element
struct element1 element
2.3 已知结构体变量element中成员element3的地址为0x2d,通过结构体struct element1中的成员element3来获取结构体变量element的地址
container_of(element3_ptr,struct element1,element3)
2.4 拆开
(struct element1 *)((char *)(&element3) - (char *) &((struct element1 *)0)->element3)
从已知可得:
&element3=0x2d;
(char *)(&element3)=0x2d;
(struct element1 *)0=地址为0x0,指向的内容为一个struct element1结构体;
((struct element1 *)0)->element3=结构体中element3的内容;
&((struct element1 *)0)->element3=取结构体中element3的地址,这是偏移地址,为0x04;
(char *)&((struct element1 *)0)->element3=0x04;
最终:
((char *)(&element3) - (char *) &((struct element1 *)0)->element3)=0x2d - 0x04) = 0x29
(struct element1 *)0x29 = 0x29,0x29这个地址指向的是一个结构体struct element1,那么这就是element变量的地址
可以结合下图观看:

linux内核中宏container_of是干什么的?的更多相关文章
- linux内核第一宏 container_of
内核第一宏 list_entry()有着内核第一宏的美称,它被设计用来通过结构体成员的指针来返回结构体的指针.现在就让我们通过一步步的分析,来揭开它的神秘面纱,感受内核第一宏设计的精妙之处. 整理分析 ...
- linux内核中的subsys_initcall是干什么的?
注意:使用的内核源码版本为5.1.3 1. subsys_initcall长什么样子? 它其实是个宏定义,定义如下: #define subsys_initcall(fn) __define_ ...
- linux内核中宏likely和unlikely到底做了些什么?
1. 先看看它们长啥样吧!(它们有两种定义,第一种是使能了程序trace功能的宏定义,第二种是普通的宏定义,咱们分析普通宏定义吧) # define likely(x) __builtin_expec ...
- 嵌入式C语言自我修养 04:Linux 内核第一宏:container_of
4.1 typeof 关键字 ANSI C 定义了 sizeof 关键字,用来获取一个变量或数据类型在内存中所占的存储字节数.GNU C 扩展了一个关键字 typeof,用来获取一个变量或表达式的类型 ...
- (十)Linux内核中的常用宏container_of
Container_of在Linux内核中是一个常用的宏,用于从包含在某个结构中的指针获得结构本身的指针,通俗地讲就是通过结构体变量中某个成员的首地址进而获得整个结构体变量的首地址. Containe ...
- Linux内核中的常用宏container_of
Container_of在Linux内核中是一个常用的宏,用于从包含在某个结构中的指针获得结构本身的指针,通俗地讲就是通过结构体变量中某个成员的首地址进而获得整个结构体变量的首地址. Containe ...
- Linux内核中的常用宏container_of其实很简单【转】
转自:http://blog.csdn.net/npy_lp/article/details/7010752 开发平台:Ubuntu11.04 编 译器:gcc version 4.5.2 (Ubun ...
- 剖析linux内核中的宏---------container_of
#define container_of(ptr, type, member) ({ \ const typeof(((type *)0)->member) * __mptr = (ptr); ...
- Linux内核中的fastcall和asmlinkage宏
代码中看见:#define _fastcall 所以了解下fastcall -------------------------------------------------------------- ...
随机推荐
- CentOS中利用Docker安装Redis
CentOS中利用Docker安装Redis 1.拉取镜像 #docker pull redis:4.0.10 2.加载镜像 #docker run -p 6379:6379 --name test- ...
- sqlserver中查询表字段的sql语句
sqlserver中的表信息字段信息这些东西也是放到系统表中的,以下sql语句用于查询某表的字段信息. select t1.id object_id,t1.name object_name,t2.va ...
- arcgis api for javascript 添加图层时设置标注,自定义符号
<!DOCTYPE html> <html> <head> <meta charset="gb2312"> <meta nam ...
- Unity shader学习之半兰伯特光照模型
半兰伯特光照模型,为Valve公司在开发游戏<半条命>时提出的一种技术,用于解决漫反射光无法到达区域无任凭明暗变化,丢失模型细节表现的问题. 其公式如下: Cdiffuse = Cligh ...
- ModuleNotFoundError: No module named '_pydevd_bundle.pydevd_cython' error on debug
现象:pycharm调试代码出现错误:ModuleNotFoundError: No module named '_pydevd_bundle.pydevd_cython' error on debu ...
- html5-css选择器
/*/**{color: red}p{color: green}#div1{background: blue;padding-top: 15px;}.kk{background: blue;borde ...
- I Count Tow Three
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#inclu ...
- 安装模块时报错“error: Microsoft Visual C++ 14.0 is required…”
安装pymssql时报错:在安装的过程中遇到了“error: Microsoft Visual C++ 14.0 is required…” 解决办法: 进入https://www.lfd.uci.e ...
- 20165305 实验二:Java面向对象程序设计
2-1 参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST 参考http://www.cnblogs.com/rocedu/p/67 ...
- windows环境下 curl 安装和使用
curl下载地址:https://curl.haxx.se/download.html,拉到页面最底下,选择红色选中的那个CAB的进行下载,如下图所示: 下载完成后,解压. 解决windows控制台c ...