container_of
定义在<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) );})
它的功能是通过一个结构体成员的地址,得到结构体的地址。举例如下:
struct st_A
{
int member_b;
int member_c;
};
struct st_A a;
则container_of(&(a.member_c), struct st_A, member_c)
会得到变量a
的地址,也就是&a
的值。