本文的主要内容:
一、container_of的作用
二、container_of的定义
三、container_of的简单实现
一、container_of的作用
一般的我们通过结构体变量的地址可以找到其成员的地址,但是反过来一般是行不通的。在linux内核中就有这样的一个宏:container_of,它可以实现根据结构体成员的地址,找到这个结构体变量的地址,从而对结构体中的其他成员进行访问。
二、container_of的定义
container_of这个宏在Linux内核的tools\perf\util\include\linux\Kernel.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(