在浏览Linux内核时,我发现了一个container_of
宏,其定义如下:
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
我明白container_的作用,但我不明白的是最后一句话
(type *)( (char *)__mptr - offsetof(type,member) );})
如果我们按如下方式使用宏:
container_of(dev, struct wifi_device, dev);
最后一句的相应部分是:
(struct wifi_device *)( (char *)__mptr - offset(struct wifi_device, dev);
看起来什么都没做.