container_of宏
title: container_of宏
date: 2019/7/24 15:49:26
toc: true
container_of宏
解析
在linux链表结构中有这样一个宏,通过成员变量的地址找到他所在结构体的首地址,通过一个容器(结构体)中某个成员的指针得到指向这个容器(结构体)的指针,简单的说就是通过成员找容器。
- 计算成员结构体的偏移量,实现offsetof的功能
- 成员变量地址-偏移量=结构体首地址
#define list_entry(ptr, type, member) container_of(ptr, type, member)
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#define list_entry(link, type, member) \
((type *)((char *)(link)-(unsigned long)(&((type *)0)->member)))
在linux中有两种实现,第一种实现多了一个类型检测,检查传入的地址是否是member类型
const typeof( ((type *)0)->member ) *__mptr = (ptr);
关于计算偏移量,就是将0地址转换为结构体首地址,那么成员变量的地址就是偏移地址了
参考链接
offsetof与container_of宏总结 https://www.cnblogs.com/Anker/p/3472271.html
list_entry的宏定义 http://www.cppblog.com/baby-fly/archive/2011/01/27/139446.html
内核链表
https://blog.csdn.net/thisway_diy/article/details/84952783#t5
https://panqiincs.me/2017/06/17/linux-kernel-linked-list-explained/
container_of宏的更多相关文章
- 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宏用来根据成员的地址 ...
- container_of宏定义分析---linux内核
问题:如何通过结构中的某个变量获取结构本身的指针??? 关于container_of宏定义在[include/linux/kernel.h]中:/*_** container_of - cast a ...
- container_of宏剖析
container_of宏剖析//该宏位于include/linux/kernel.h 1.定义格式 /** * container_of - cast a member of a structure ...
- 内核中container_of宏的详细分析【转】
转自:http://blog.chinaunix.net/uid-30254565-id-5637597.html 内核中container_of宏的详细分析 16年2月28日09:00:37 内核中 ...
- offsetof与container_of宏[总结]
1.前言 今天在看代码时,遇到offsetof和container_of两个宏,觉得很有意思,功能很强大.offsetof是用来判断结构体中成员的偏移位置,container_of宏用来根据成员的地址 ...
- offsetof与container_of宏分析
offsetof宏:结构体成员相对结构体的偏移位置 container_of:根据结构体成员的地址来获取结构体的地址 offsetof 宏 原型: #define offsetof(TYPE, MEM ...
- typeof, offsetof, container_of宏
container_of宏实现如下: #define container_of(ptr, type, member) ({ \ )->member ) *__mptr = (ptr); \ (t ...
- offsetof宏与container_of宏
offsetof宏与container_of宏1.由结构体指针进而访问各元素的原理(1)通过结构体整体变量来访问其中各个元素,本质上是通过指针方式来访问的,形式上是通过.的方式来访问的(这个时候其实是 ...
随机推荐
- Activiti服务类- IdentityService服务类
转自:https://www.cnblogs.com/liuqing576598117/p/9815013.html 一.内置用户组(角色)设计表概念 用户和组(或者叫做角色),多对多关联,通过关联表 ...
- 关于IE8的兼容性问题
DOCTYPE 首先需要确保你的HTML页面开始部分要有DOCTYPE声明.DOCTYPE告诉浏览器使用什么样的HTML或XHTML规范来解析HTML文档,具体会影响: 对标记.attributes ...
- danfu添加商品实例
GoodsBaseInfoVO extends GoodsBaseInfo JSONResponse saveOrUpdateBaseGoodinfo void insertGoodBaseInfo ...
- UVA1674 闪电的能量 树剖
UVA1674 闪电的能量 树剖 题面 水.树剖模板 #include <cstdio> #include <cstring> #include <algorithm&g ...
- 【概率论】1-2:计数方法(Counting Methods)
title: [概率论]1-2:计数方法(Counting Methods) categories: Mathematic Probability keywords: Counting Methods ...
- VMWare Fusion 8 序列号
FY75A-06W1M-H85PZ-0XP7T-MZ8E8 ZY7TK-A3D4N-08EUZ-TQN5E-XG2TF FG1MA-25Y1J-H857P-6MZZE-YZAZ6
- 内存管理1 retain & release
内存管理法则 1:谁创建谁释放alloc /new/ copy------>release/autorelease.一一对应,不是你创建的就不用你释放. 2:除了alloc /new/ copy ...
- Ubuntu14.04 挂载u盘
插入u盘后, $cat /proc/partitions 发现多了 sdb sdb4 sdb是统称,所以新插入的U盘就是/dev/sdb4 $ls /dev |grep sdb sdb sdb4 查看 ...
- 【spring源码分析】IOC容器初始化——查漏补缺(三)
前言:本文分析InitializingBean和init-method方法,其实该知识点在AbstractAutowireCapableBeanFactory#initializeBean方法中有所提 ...
- CTF辅助脚本
首先推荐这篇文章,网上有多次转载,这是我见过日期比较早的 CTF中那些脑洞大开的编码和加密 凯撒密码 flag='flag{abcdef}' c='' n=20 for i in flag: if ' ...