title: container_of宏

date: 2019/7/24 15:49:26

toc: true

container_of宏

解析

在linux链表结构中有这样一个宏,通过成员变量的地址找到他所在结构体的首地址,通过一个容器(结构体)中某个成员的指针得到指向这个容器(结构体)的指针,简单的说就是通过成员找容器。

  1. 计算成员结构体的偏移量,实现offsetof的功能
  2. 成员变量地址-偏移量=结构体首地址
#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宏的更多相关文章

  1. linux中offsetof与container_of宏定义

    linux内核中offsetof与container_of的宏定义 #define offsetof(TYPE, MEMBER)    ((size_t) &((TYPE *)0)->M ...

  2. (转)offsetof与container_of宏[总结]

    1.前言 今天在看代码时,遇到offsetof和container_of两个宏,觉得很有意思,功能很强大.offsetof是用来判断结构体中成员的偏移位置,container_of宏用来根据成员的地址 ...

  3. container_of宏定义分析---linux内核

    问题:如何通过结构中的某个变量获取结构本身的指针??? 关于container_of宏定义在[include/linux/kernel.h]中:/*_** container_of - cast a ...

  4. container_of宏剖析

    container_of宏剖析//该宏位于include/linux/kernel.h 1.定义格式 /** * container_of - cast a member of a structure ...

  5. 内核中container_of宏的详细分析【转】

    转自:http://blog.chinaunix.net/uid-30254565-id-5637597.html 内核中container_of宏的详细分析 16年2月28日09:00:37 内核中 ...

  6. offsetof与container_of宏[总结]

    1.前言 今天在看代码时,遇到offsetof和container_of两个宏,觉得很有意思,功能很强大.offsetof是用来判断结构体中成员的偏移位置,container_of宏用来根据成员的地址 ...

  7. offsetof与container_of宏分析

    offsetof宏:结构体成员相对结构体的偏移位置 container_of:根据结构体成员的地址来获取结构体的地址 offsetof 宏 原型: #define offsetof(TYPE, MEM ...

  8. typeof, offsetof, container_of宏

    container_of宏实现如下: #define container_of(ptr, type, member) ({ \ )->member ) *__mptr = (ptr); \ (t ...

  9. offsetof宏与container_of宏

    offsetof宏与container_of宏1.由结构体指针进而访问各元素的原理(1)通过结构体整体变量来访问其中各个元素,本质上是通过指针方式来访问的,形式上是通过.的方式来访问的(这个时候其实是 ...

随机推荐

  1. 捕获错误并处理try-catch

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. winfrom 窗体首次加载

    #region Override Functions /// <summary> /// OnLoad /// </summary> /// <param name=&q ...

  3. AtCoder Beginner Contest 142【D题】【判断素数的模板+求一个数的因子的模板】

    D - Disjoint Set of Common Divisors Problem Statement Given are positive integers AA and BB. Let us ...

  4. Django RestFramework (DRF)

    准备: 下载 pip install djangorestframework 一 APIView源码解析 1 预备知识 CBV(class based view)FBV(function based ...

  5. 顺序表应用7:最大子段和之分治递归法(SDUT 3664)

    #include <bits/stdc++.h> using namespace std; const int maxn = 50005; int num = 0; struct node ...

  6. 部署web前端的react项目到linux服务器

    部署web前端的react项目到linux服务器 项目的目录结构 ``` ├─dlls #dlls编译后的问题 ├─doc #帮助文件入口 │ ├─src │ ├─apps #各个功能模块放在这里 │ ...

  7. Vue2 响应式原理

    我们经常用vue的双向绑定,改变data的某个属性值,vue就马上帮我们自动更新视图,下面我们看看原理. Object的响应式原理: 可以看到,其实核心就是把object的所有属性都加上getter. ...

  8. P2663 越越的组队

    原题链接  https://www.luogu.org/problem/P2663 很容易看出来是个背包问题嘛: 体积是总分的一半,求最高分,每个同学选或不选,是个 01背包问题. 自信地交上去之后发 ...

  9. DB缓存一致性

    直接硬核干货,去掉前戏. 方案大致说明 1:假设对redis中存在一对key,value的对应关系是 key=money,value=666 2:当修改线程修改key时先将key设置成value=66 ...

  10. vmware压缩vmdk文件大小

    在搭建靶机环境的过程中总是遇见vmdk越来越大,导致上传时间变长.记一下压缩vmdk的方法 ;sync;rm -f zero.fill /usr/bin/vmware-toolbox-cmd disk ...