Archive for the ‘skill’ tag
container_of
#define container_of(p,t,n) (t*)((p)-&(((t*)0)->n))
再来看看Linux Kernel中的实现。
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})