Liuw's Thinkpad

想要赢就先学会输,想要成功就先学会失败

Archive for the ‘skill’ tag

container_of

with 3 comments

#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) );})

Written by liuw

December 1st, 2009 at 10:47 pm

Posted in Programming

Tagged with , , ,