/* 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(ptr, type, member) ({ const typeof( ((type *)0)->member ) *__mptr = (ptr); (type *)( (char *)__mptr - offsetof(type,member) );})
输入:
结构体一个成员的member地址ptr,
结构体的类型,
结构体一个成员member的名,
输出:
得到这个结构体变量的地址;
分析:
结构体类型是确定的,结构体成员 相对 结构体定义的起始地址 偏移是确定的;
知道了一个结构体成员的地址,可以根据偏移得到 结构体定义的起始地址;
Author: Yangkai Wang
wang_yangkai@163.com
Coding in 2021/05/07
#define container_of(ptr, type, member)
原文:https://www.cnblogs.com/WangYangkai/p/14738645.html