offsetof与container_of宏分析
offsetof宏:结构体成员相对结构体的偏移位置
container_of:根据结构体成员的地址来获取结构体的地址
offsetof 宏
原型:
#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
(TYPE *)0
非常巧妙,告诉编译器有一个指向结构体 TYPE 的指针,其地址是0,然后取该指针的 MEMBER 地址 &((TYPE *)0)->MEMBER
,因为基址是0,所以这时获取到的 MEMBER 的地址就是相当于在结构体 TYPE 中的偏移量了。
Example:
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
struct TYPE{
int mem;
int member;
};
int main()
{
struct TYPE type;
printf("&type = %p\n", &type);
printf("&type.member = %p\n", &type.member);
printf("&((struct type *)0)->member = %lu\n", ((size_t)&((struct TYPE *)0)->member) );
printf("offsetof(struct TYPE member) = %zd\n", offsetof(struct TYPE, member));
return 0;
}
/*
result:
&type = 0x7ffc1104a110
&type.member = 0x7ffc1104a114
&((struct type *)0)->member = 4
offsetof(struct TYPE member) = 4
*/
container_of 宏
原型:linux-4.18.5
/**
* 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) ({ \
void *__mptr = (void *)(ptr); \
BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \
!__same_type(*(ptr), void), \
"pointer type mismatch in container_of()"); \
((type *)(__mptr - offsetof(type, member))); })
网上所见更多是底下这个版本:
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
第一部分:void *__mptr = (void *)(ptr);
或const typeof( ((type *)0)->member ) *__mptr = (ptr);
两个的差别在于 __mptr 的类型一个是 void * ,一个是 type *。
void * 较为容易理解,下面来看看 type *:
关于 typeof 关键字其作用是返回变量的类型,简单理解就是如下,详细可参见GCC typeof在kernel中的使用——C语言的“编译时多态”
int a;
typeof(a) b; //这等同于int b;
typeof(&a) c; //这等同于int* c;
因此const typeof( ((type *)0)->member ) *__mptr = (ptr);
的作用就是通过 typeof 获取结构体成员 member 的类型,然后定义一个这个类型的指针变量 __mptr 并将其赋值为 ptr。
第二部分:(type *)( (char *)__mptr - offsetof(type,member) )
,通过offsetof
宏计算出 member 在 type 中的偏移,然后用 member 的实际地址__mptr
减去偏移,得到 type 的起始地址。从上面关于offsetof
宏的 Example 也可以验证这一点:
&type.member = 0x7ffc1104a114 - &((struct type *)0)->member = 4 = &type = 0x7ffc1104a110
offsetof与container_of宏分析的更多相关文章
- 对offsetof、 container_of宏和结构体的理解
offsetof 宏 #include<stdio.h> #define offsetoff(type, member) ((int)&((type*)0)->me ...
- linux中offsetof与container_of宏定义
linux内核中offsetof与container_of的宏定义 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->M ...
- (转)offsetof与container_of宏[总结]
1.前言 今天在看代码时,遇到offsetof和container_of两个宏,觉得很有意思,功能很强大.offsetof是用来判断结构体中成员的偏移位置,container_of宏用来根据成员的地址 ...
- offsetof与container_of宏[总结]
1.前言 今天在看代码时,遇到offsetof和container_of两个宏,觉得很有意思,功能很强大.offsetof是用来判断结构体中成员的偏移位置,container_of宏用来根据成员的地址 ...
- 【转】container_of宏 分析
在学习Linux驱动的过程中,遇到一个宏叫做container_of.该宏定义在include/linux/kernel.h中,首先来贴出它的代码: /** * container_of - cast ...
- container_of宏
title: container_of宏 date: 2019/7/24 15:49:26 toc: true --- container_of宏 解析 在linux链表结构中有这样一个宏,通过成员变 ...
- container_of宏定义分析---linux内核
问题:如何通过结构中的某个变量获取结构本身的指针??? 关于container_of宏定义在[include/linux/kernel.h]中:/*_** container_of - cast a ...
- 内核中container_of宏的详细分析【转】
转自:http://blog.chinaunix.net/uid-30254565-id-5637597.html 内核中container_of宏的详细分析 16年2月28日09:00:37 内核中 ...
- linux内核container_of宏定义分析
看见一个哥们分析container_of很好,转来留给自己看 一.#define offsetof(TYPE, MEMBER) ((size_t) & ((TYPE *)0)->MEMB ...
随机推荐
- Js中JSON.stringify()与JSON.parse()
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.因为采用独立于语言的文本格式,也使用了类似于C语言家族的习惯,拥有了这些特性使使JSON称为理想的数据交换语 ...
- Python之面向对象之单例模式的四种方式
一.内容 保证一个类只有一个实例,并提供一个访问它的全局访问点 二.角色 单利 三.使用场景 当类只有一个实例而且客户可以从一个众所周知的访问点访问它时 比如:数据库链接.Socket创建链接 四.优 ...
- java数据结构1--数组、排序和Arrays工具类
数组:Array 数组的定义 数组的内存结构 数组定义常见问题 数组常见操作 Java参数传递问题--值传递 二维数组 1.数组概念 同一种类型数据的集合,可以是基本数据类型,也可以是引用数据类型. ...
- IP地址(参考百度百科)
题目1:用子网掩码划分网络时,如果划分8个子网,则需要4位.因为24-2>=8.(全0代表网络自身,全1代表广播地址,所以减2) 以C类地址为例,(1)8个子网,则4位子网,主机号为后4位,主机 ...
- 【NOIP2016提高A组模拟9.15】Map
题目 分析 发现,当原图是一棵树的时候,那么新建一条边后,就会变成环套树, 而环内的所有点对都是安全点对,如果环中有k个点,答案就是\(k(k-1)\) 联想到,当把原图做一遍tarjan缩点,每个环 ...
- 对vue的solt的理解
//父 <children> <span>12345</span>//这边不会显示 </children> //子 components: { chil ...
- C#调用Python(二)
python文件中有引入其他包.模块 一.源码 1.1 python源码,源码.python 打包方法,以及打包后的程序文件.请移步https://www.cnblogs.com/zhuanjiao ...
- GO语言学习笔记1-输入带空格的字符串
最近开始学习GO语言,并做了一些编程练习.有道题要输入带空格的字符串,这在C/C++中很容易实现,但GO中好像并不那么容易.学过C/C++的可能都知道,在C中可以使用gets()函数,在C++可以使用 ...
- python 小题
python 小题:给定一个字符串,找出不含有重复字符的最长子串的长度.示例 1:输入: "abcabcbb"输出: 3 解释: 无重复字符的最长子串是 "abc&quo ...
- chrome浏览器调试js,结果Sources里面找不到js文件解决办法
页面出现问题,就debug,这是前端开发工程师最常见的做法,但是有时候,我们打开开发者工具,在sources查找js文件,却发现怎么也找不到,无法设置断点.但是文件在network选项卡里确实 ...