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;
	 stru

你可能感兴趣的:(Linux,设备驱动开发)