offsetof宏和container_of宏

offsetof宏:已知类型为TYPE的结构体变量,并且知道其内部有一个成员为MEMBER,在未知结构体变量指针的情况下求MEMBER成员与该结构体变量首地址之间的偏移量。

#define offsetof(TYPE,MEMBER)       ((int)&((TYPE *)0)->MEMBER)

container_of宏:已知已知类型为type的结构体变量,并且知道其内部有一个成员为member,且知道MEMBER成员的指针为ptr,求该结构体变量的指针

#define container_of(ptr,type,member)    ({const typeof(((type *)0)->member) *_mptr = (ptr);    \
                                         (type *)((char *)_mptr -offsetof(type,member));})

猜你喜欢

转载自blog.csdn.net/weixin_39330853/article/details/81842403