天天看點

盤點Linux核心源碼中使用宏定義的若幹技巧(2)

5. typeof和0指針

這個在大名鼎鼎的container_of就有出現,事實上一些面試題有時候也喜歡跟這個沾點邊。

點選(此處)折疊或打開

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

哦,看到沒,這裡就直接使用了大括号({...})。第一個宏offsetof用來獲得一個結構體中某一成員MEMBER與該結構體起始位址的偏移量,這個用法很有創意,0指針,充分利用了編譯器的技巧。比如下面的代碼,輸出的結果是4:

struct test{

        int a;

        int b;

};

int main(void)

{

        printf("offset_b = %ld\n", (size_t)(&((struct test*)0)->b));

        return 0;

}

6. 宏參數的靜态檢查

下面的宏來自子產品參數的定義部分:

#define __module_param_call(prefix, name, ops, arg, isbool, perm)    \

    /* Default value instead of permissions? */            \

    static int __param_perm_check_##name __attribute__((unused)) =    \

    BUILD_BUG_ON_ZERO((perm) 0 || (perm) > 0777 || ((perm) & 2))    \

    + BUILD_BUG_ON_ZERO(sizeof(""prefix) > MAX_PARAM_PREFIX_LEN);    \

    static const char __param_str_##name[] = prefix #name;        \

    static struct kernel_param __moduleparam_const __param_##name    \

    __used                                \

    __attribute__ ((unused,__section__ ("__param"),aligned(sizeof(void *)))) \

    = { __param_str_##name, ops, perm, isbool ? KPARAM_ISBOOL : 0,    \

     { arg } }

其中第3-5行定義了一個事實上并不會用到的變量__param_perm_check_##name,為了防止編譯器産生未使用變量的警告,該變量後面使用了__attribute__((unused))屬性。這個并不會使用到的變量在該宏中的唯一作用是對變量參數perm和prefix的大小進行一個靜态檢查,如果BUILD_BUG_ON_ZERO中的條件為TRUE,那麼将會産生一個編譯錯誤。

繼續閱讀