天天看點

Linux核心中關于宏container_of的使用

在Linux核心代碼中多處使用了宏container_of,關于container_of 的宏的定義在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) );})
           

container_of宏中的參數ptr代表指針、type代表類型、member代表成員

簡單的來說:container_of宏的作用是通過結構體中一個成員的位址找到這個結構體

struct test{
   
	 char data
           

繼續閱讀