【Linux API 揭秘】container_of函数详解
【Linux API 揭秘】container_of函数详解
Linux Version:6.6
Author:Donge
Github:linux-api-insides
1、container_of函数介绍
container_of可以说是内核中使用最为频繁的一个函数了,简单来说,它的主要作用就是根据我们结构体中的已知的成员变量的地址,来寻求该结构体的首地址,直接看图,更容易理解。

下面我们看看
linux是如何实现的吧
2、container_of函数实现
/**
* 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.
*
* WARNING: any const qualifier of @ptr is lost.
*/
#define container_of(ptr, type, member) ({ \
void *__mptr = (void *)(ptr); \
static_assert(__same_type(*(ptr), ((type *)0)->member) || \
__same_type(*(ptr), void), \
"pointer type mismatch in container_of()"); \
((type *)(__mptr - offsetof(type, member))); })
函数名称:container_of
文件位置:include/linux/container_of.h
该函数里面包括了一些封装好的宏定义以及函数,比如:static_assert、__same_type、offsetof,以及一些指针的特殊用法,比如:(type *)0),下面我们一一拆解来看。

2.1 static_assert
/**
* static_assert - check integer constant expression at build time
*
* static_assert() is a wrapper for the C11 _Static_assert, with a
* little macro magic to make the message optional (defaulting to the
* stringification of the tested expression).
*
* Contrary to BUILD_BUG_ON(), static_assert() can be used at global
* scope, but requires the expression to be an integer constant
* expression (i.e., it is not enough that __builtin_constant_p() is
* true for expr).
*
* Also note that BUILD_BUG_ON() fails the build if the condition is
* true, while static_assert() fails the build if the expression is
* false.
*/
#define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)
#define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
函数名称:static_assert
文件位置:include/linux/build_bug.h
函数解析:该宏定义主要用来 在编译时检查常量表达式,如果表达式为假,编译将失败,并打印传入的报错信息
expr:该参数表示传入进来的常量表达式...:表示编译失败后,要打印的错误信息_Static_assert:C11中引入的关键字,用于判断表达式expr并打印错误信息msg。
在container_of函数中,主要用来断言判断
static_assert(
__same_type(*(ptr), ((type *)0)->member) || __same_type(*(ptr), void) ,
"pointer type mismatch in container_of()"
);
2.2 __same_type
/* Are two types/vars the same type (ignoring qualifiers)? */
#ifndef __same_type
# define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
#endif
函数名称:__same_type
函数解析:该宏定义用于检查两个变量是否是同种类型
__builtin_types_compatible_p:gcc的内建函数,判断两个参数的类型是否一致,如果是则返回1typeof:gcc的关键字,用于获取变量的类型信息
了解完__same_type,想要理解__same_type(*(ptr), ((type *)0)->member),需要先弄明白(type *)0的含义。
更多干货可见:高级工程师聚集地,助力大家更上一层楼!
2.3 (type *)0
(type *)0,该如何理解这个表达式呢?
首先,
type是我们传入进来的结构体类型,比如上面讲到的struct test,而这里所做的可以理解为强制类型转换:(struct test *)addr。addr可以表示内存空间的任意的地址,我们在强制转换后,默认后面一片的内存空间存储的是该数据结构。

而
(type *)0的作用,也就是默认将0地址处的内存空间,转换为该数据类型。

我们就把
0,当作我们正常的addr地址变量来操作,((type *)0)->member,就是获取我们结构体的成员对象。((type *)0)->member:是一种常见的技巧,用于直接获取结构体type的成员member的类型,而不需要定义一个type类型的对象。
2.4 offsetof
#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif
函数名称:offsetof
函数解析:该宏定义用于获取结构体中指定的成员,距离该结构体偏移量。

TYPE:表示结构体的类型MEMBER:表示指定的结构体成员__builtin_offsetof:gcc内置函数,直接返回偏移量。
在新的linux源码中,直接引用了gcc内置的函数,而在老的内核源码中,该偏移量的实现方式如下:
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
同样用到了((TYPE *)addr),上面我们知道
((TYPE *)addr)->MEMBER:表示获取该结构体的成员&((TYPE *)addr)->MEMBER):加了一个&,表示地址,取该成员的内存地址。比如我们
addr=0x00000010,那么&((TYPE *)0x00000010)->MEMBER)就相当于0x00000010+size比如我们
addr=0,那么&((TYPE *)0)->MEMBER)就相当于size
到这里,我们对container_of函数内部涉及的相关知识了然于胸,下面我们再来看container_of,简直容易到起飞。
2.5 container_of
#define container_of(ptr, type, member) ({ \
void *__mptr = (void *)(ptr); \
static_assert(__same_type(*(ptr), ((type *)0)->member) || \
__same_type(*(ptr), void), \
"pointer type mismatch in container_of()"); \
((type *)(__mptr - offsetof(type, member))); })
static_assert:断言信息,避免我们传入的参数类型不对,而做的编译检查处理,直接忽略。
#define container_of(ptr, type, member) ({ \
void *__mptr = (void *)(ptr); \
((type *)(__mptr - offsetof(type, member))); })
offsetof(type, member):计算的是结构体中的成员的偏移量,这里称为size(__mptr - offsetof(type, member)):也就是根据我们已知的成员变量地址,计算出来结构体的首地址((type *)(__mptr - offsetof(type, member))):最后强制转换为(type *),结构体指针。
比如,我们已知的结构体成员的地址为
0xffff0000,计算之后如下:

3、总结
linux内核中,小小的一个函数,内部包括的技巧如此之多:static_assert、__same_type、(type *)0、offsetof。
了解完内部完整的实现手法之后,我们也可以手码一个container_of了 :)

【Linux API 揭秘】container_of函数详解的更多相关文章
- Linux内核中container_of函数详解
http://www.linuxidc.com/Linux/2016-08/134481.htm
- Linux C 中 fork() 函数详解
一.fork入门知识 一个进程,包括代码.数据和分配给进程的资源.fork() 函数通过系统调用创建一个与原来进程几乎完全相同的进程,也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不同 ...
- Linux操作系统shell与函数详解
shell和函数的定义 1. linux shell 函数 将一组命令集或语句形成一个可用的块, 这些语句块称为函数. 2. shell 函数的组成 函数名:函数名字,注意一个脚本中函数名要唯一, ...
- (笔记)Linux下的ioctl()函数详解
我这里说的ioctl函数是指驱动程序里的,因为我不知道还有没有别的场合用到了它,所以就规定了我们讨论的范围.写这篇文章是因为我前一阵子被ioctl给搞混了,这几天才弄明白它,于是在这里清理一下头脑. ...
- [fork]Linux中的fork函数详解
---------------------------------------------------------------------------------------------------- ...
- Linux内核中kzalloc函数详解
**************************************************************************************************** ...
- Linux下的ioctl()函数详解
我这里说的ioctl函数是指驱动程序里的,因为我不知道还有没有别的场合用到了它,所以就规定了我们讨论的范围.写这篇文章是因为我前一阵子被ioctl给搞混了,这几天才弄明白它,于是在这里清理一下头脑. ...
- Linux系统调用--mmap/munmap函数详解【转】
转自:http://www.cnblogs.com/leaven/archive/2011/01/14/1935199.html http://linux.chinaunix.net/techdoc/ ...
- 【Linux 进程】fork函数详解
一.fork入门知识 一个进程,包括代码.数据和分配给进程的资源.fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程,也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不同, ...
- Linux 系统 文件锁 fcntl函数详解
#include <unistd.h> #include <fcntl.h> int fcntl(int fd, int cmd); int fcntl(int fd, int ...
随机推荐
- 《CTFshow-Web入门》05. Web 41~50
@ 目录 web41 题解 原理 web42 题解 原理 web43 题解 原理 web44 题解 原理 web45 题解 原理 web46 题解 原理 web47 题解 web48 题解 web49 ...
- CodeForces 1367E Necklace Assembly
题意 给定一个字符串\(s\),长度为\(n\),一根项链为一个环,定义一根项链为\(k-beautiful\),则该项链顺时针转\(k\)下后与原项链相等,给出\(k\),请构造一根最长的\(k-b ...
- CodeForces 1332E Height All the Same
题意 对于一个\(n*m\)的矩阵,有两种操作 一个格子加二 一个格子和另一个相邻的格子同时加一 通过这两种操作最终使得所有矩阵元素相等 对于矩阵元素来说,有\(L\leq a_{i,j}\leq R ...
- 「luogu - P4313」文理分科 Mincut
link. Pretty nice practice for the min-cut trick. Starting out we eliminate the constraint that if f ...
- 编译nw-node版本的插件
编译nw-node版本的插件 下载nwjs对应版本的nodejs 原始源码目录 yh@yh:~/addon$ tree . ├── addon.cc ├── binding.gyp ├── CppLi ...
- mysql8安装踩坑记
背景:已安装mysql5.7版本 问题一:默认的3306端口被占用 进入mysql5.7的my.ini文件,更改port为3307或者其他未被占用的端口 问题二:Install/Remove of t ...
- Python socket实现ftp文件下载服务
简要 使用Python socket和多线程实现一个FTP服务下载.下面的示例是固定下载某一个任意格式文件. 仅仅为了展示如果使用socket和多线程进行文件下载 服务端代码 import socke ...
- 其它——python操作kafka实践
文章目录 1.先看最简单的场景,生产者生产消息,消费者接收消息,下面是生产者的简单代码. ------------------------------------------------------- ...
- GO语言基础之基本运算符
GO语言基础之基本运算符 目录 GO语言基础之基本运算符 一.运算符 内置运算符: 二.算术运算符 三.关系运算符 四.逻辑运算符 五.位运算符 六.赋值运算符 一.运算符 作用:运算符用于在程序运行 ...
- Rust WebAssembly 绑定输入类型(基于 Serde)
前言 单位有个项目要共享前后端检查策略后端用的正好也是 Rust,但是 Rust 默认的 wasm-bindgen 包中提供的转换操作非常少,像 Vec<T> <=> Arra ...