相关疑难解决方法(0)

了解Linux内核中的container_of宏

当我浏览Linux内核时,我找到了一个container_of定义如下的宏:

#define container_of(ptr, type, member) ({                      \
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
        (type *)( (char *)__mptr - offsetof(type,member) );})
Run Code Online (Sandbox Code Playgroud)

我理解container_of做了什么,但我不明白的是最后一句,也就是说

(type *)( (char *)__mptr - offsetof(type,member) );})
Run Code Online (Sandbox Code Playgroud)

如果我们使用宏如下:

container_of(dev, struct wifi_device, dev);
Run Code Online (Sandbox Code Playgroud)

最后一句的相应部分是:

(struct wifi_device *)( (char *)__mptr - offset(struct wifi_device, dev);
Run Code Online (Sandbox Code Playgroud)

看起来什么都不做.有人可以在这里填补空白吗?

c linux-kernel c-preprocessor

71
推荐指数
5
解决办法
7万
查看次数

是否有宏定义来检查Linux内核版本?

我想知道是否有一个gcc宏会告诉我Linux内核版本,所以我可以适当地设置变量类型.如果没有,我将如何定义我自己的宏呢?

c linux macros gcc linux-kernel

29
推荐指数
1
解决办法
3万
查看次数

标签 统计

c ×2

linux-kernel ×2

c-preprocessor ×1

gcc ×1

linux ×1

macros ×1