一、前言
在我们进行内核开发的时候,我们经常会看到container_of
函数的身影,今天我们就来深入的分析一下container_of
函数.
二、container_of
功能介绍
container_of
函数的作用是给定结构体中某个成员的地址(ptr
),该结构体类型(type
)和该成员的名字(member
)从而获取这个成员所在的结构体变量的首地址。
三、container_of
源码分析
源码所在位置:kernel6.1 /include/linux/container_of.h
/**
* 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); \
static_assert(__same_type(*(ptr), ((type *)0)->member) || \
__same_type(*(ptr), void), \
"pointer type mismatch in container_of()"); \
((type *)(__mptr - offsetof(type, member))); })
1. 参数介绍:
ptr : 结构体中某个成员的地址
type : 该成员所在的结构体类型
member : 该成员在结构体中的名称
2. 源码中的宏介绍:
2.1 __same_type
/* Are two types/vars the same type (ignoring qualifiers)? */
#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
__same_type : 的作用是检查两个变量或类型是否具有相同的类型,忽略类型修饰符(如const
、volatile
等)
__builtin_types_compatible_p(typeof(a), typeof(b)) : 是一个 GCC
编译器内置的函数宏。它用于检查 typeof(a)
和 typeof(b)
表达式的类型是否完全相同,忽略类型修饰符。
typeof : 是 C
语言的一个运算符,用于获取表达式的类型。
举个例子:
int x = 10;
const int y = 20;
// 以下表达式都会返回 1(true)
__same_type(x, y);
__same_type(int, int);
__same_type(int, const int);
2.2 offsetof
#type: 结构体类型
#member: 该结构体中的成员
#__builtin_offsetof(type, member): 这是一个 GCC 编译器内置的函数宏。它用于计算结构体成员 member 在其所属结构体 type 中的字节偏移量。
#define offsetof(type, member) __builtin_offsetof(type, member)
offsetof : 的作用是获取一个结构体成员在其所在结构体中的字节偏移量。
3. 整体源码分析:
#这是宏定义的开始,接受三个参数:
#ptr: 指向某个结构体成员的指针
#type: 该成员所在的结构体类型
#member: 该成员在结构体中的名称
#define container_of(ptr, type, member) ({ \
#将输入的 ptr 指针转换为通用的 void * 指针,并存储在临时变量 __mptr 中。
void *__mptr = (void *)(ptr); \
#static_assert的作用是在编译时对给定的常量表达式进行检查。
#static_assert是一个编译时断言,用于检查 ptr 指针所指向的类型,是否与 type 中 member 成员的类型相同。
#如果类型不同,编译将会失败。
#唯一的例外是 void 类型,因为它可以指向任何类型的数据。
static_assert(__same_type(*(ptr), ((type *)0)->member) || \
__same_type(*(ptr), void), \
#如果类型不匹配,编译器将会显示这个错误信息。
"pointer type mismatch in container_of()"); \
#通过 offsetof 宏获取 member 成员在 type 结构体中的字节偏移量。
#然后从 __mptr 指针减去这个偏移量,得到 type 结构体对象的起始地址。
#最后强制将这个地址转换为 type * 指针,并返回。
((type *)(__mptr - offsetof(type, member))); })
四. container_of
例程
#include <stdio.h>
#include <stdint.h>
#include<stddef.h>
/**
* 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); \
static_assert(__same_type(*(ptr), ((type *)0)->member) || \
__same_type(*(ptr), void), \
"pointer type mismatch in container_of()"); \
((type *)(__mptr - offsetof(type, member))); })
/* Are two types/vars the same type (ignoring qualifiers)? */
#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
//这是因为如果是C11 或更高版本的编译器,可以直接使用 static_assert 函数。 如果使用的是较低版本的 C 编译器,则需要使用_Static_assert
#define static_assert _Static_assert
struct Person {
char name[32];
int age;
float height;
};
int main() {
struct Person john = {
.name = "John Doe",
.age = 35,
.height = 1.75f
};
// 获取 john.age 成员的地址
int *age_ptr = &john.age;
// 使用 container_of 宏获取 Person 结构体的指针
struct Person *person = container_of(age_ptr, struct Person, age);
printf("Name: %s\n", person->name);
printf("Age: %d\n", person->age);
printf("Height: %.2f\n", person->height);
return 0;
}
运行结果:
我们可以看到,我们已经成功获取了这个成员所在的结构体变量的首地址。