#define container_of(ptr, type, member)
/* 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