溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Linux 宏定義之 offsetof 與 container_of(十九)

發布時間:2020-06-04 10:10:59 來源:網絡 閱讀:2245 作者:上帝之子521 欄目:軟件技術

        今天我們來看看 Linux 中的兩個經典的宏:offsetof 與 container_of。下來我們先來看看它們兩個的宏定義,如下

#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE*)0)->MEMBER)
#endif

#ifndef container_of
#define container_of(ptr, type, member) ({                \
        const typeof(((type*)0)->member)* __mptr = (ptr); \
        (type*)((char*))__mptr - offsetof(type, member); })
#endif

        要想看懂這兩個宏,我們就先來看看編譯器做了什么? offsetof 是用于計算 TYPE 結構體中 MEMBER 成員的偏移位置。編譯器清楚的知道結構體成員變量的偏移位置,通過結構體變量首地址與偏移量定位成員變量。下來我們通過測試代碼來進行說明

#include <stdio.h>

#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE*)0)->MEMBER)
#endif

struct ST
{
    int i;     // 0
    int j;     // 4
    char c;    // 8
};

void func(struct ST* pst)
{
    int* pi = &(pst->i);    //  0
    int* pj = &(pst->j);    //  4
    char* pc = &(pst->c);   //  8

    printf("pst = %p\n", pst);
    printf("pi = %p\n", pi);
    printf("pj = %p\n", pj);
    printf("pc = %p\n", pc);
}

int main()
{
    struct ST s = {0};

    func(&s);
    func(NULL);

    printf("offset i: %d\n", offsetof(struct ST, i));
    printf("offset j: %d\n", offsetof(struct ST, j));
    printf("offset c: %d\n", offsetof(struct ST, c));

    return 0;
}

        我們來看看結果

Linux 宏定義之 offsetof 與 container_of(十九)

        我們看到 pst 和 pi 打印的地址值是一樣的,J 和 c 分別加 4。以 NULL 為參數傳進去更加看的明顯,而直接調用 offsetof 宏,它的效果和 NULL 是一樣的。由此,它的作用就顯而易見了,用于獲取 TYPE 結構體中的 MEMBER 的偏移量。

        下來我們來看看 container_of 宏,首先講解下({ }),它是 GNU C 編譯器的語法擴展,它與逗號表達式的作用類似,結果為最后一個語句的值。如下所示

Linux 宏定義之 offsetof 與 container_of(十九)

        typeof 是 GNU C 編譯器特有的關鍵字,它只在編譯器生效,用于得到變量的類型。用法如下

Linux 宏定義之 offsetof 與 container_of(十九)

        最后的原理如下圖所示

 Linux 宏定義之 offsetof 與 container_of(十九)

        下來我們來編程進行分析說明

#include <stdio.h>

#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE*)0)->MEMBER)
#endif

#ifndef container_of
#define container_of(ptr, type, member) ({		         \
        const typeof(((type*)0)->member)* __mptr = (ptr);   \
        (type*)((char*)__mptr - offsetof(type, member)); })
#endif

#ifndef container_of_new
#define container_of_new(ptr, type, member) ((type*)((char*)(ptr) - offsetof(type, member)))
#endif


struct ST
{
    int i;     // 0
    int j;     // 4
    char c;    // 8
};

void method_1()
{
    int a = 0;
    int b = 0;

    int r = (
           a = 1,
           b = 2,
           a + b
                );

    printf("r = %d\n", r);
}

void method_2()
{
    int r = ( {
                  int a = 1;
                  int b = 2;

                  a + b;
              } );

    printf("r = %d\n", r);
}

void type_of()
{
    int i = 100;
    typeof(i) j = i;
    const typeof(j)* p = &j;

    printf("sizeof(j) = %d\n", sizeof(j));
    printf("j = %d\n", j);
    printf("*p = %d\n", *p);
}

int main()
{

    method_1();
    method_2();
    type_of();

    struct ST s = {0};
    char* pc = &s.c;
    int e = 0;
    int* pe = &e;

    struct ST* pst = container_of(pc, struct ST, c);

    printf("&s = %p\n", &s);
    printf("pst = %p\n", pst);

    return 0;
}

        我們來編譯看看結果

Linux 宏定義之 offsetof 與 container_of(十九)

        編譯的時候報了 4 個警告,但是不影響我們的輸出,看看運行結果

Linux 宏定義之 offsetof 與 container_of(十九)

        上面的兩個輸出 r 的值是一樣的,它們的寫法是等價的。用 container_of 宏調用的時候,s 和 pst 的地址值是一樣的。那么我們用自己定義的 container_of_new 宏來調用 pe 試試呢?看看結果

Linux 宏定義之 offsetof 與 container_of(十九)

        編譯的時候已經給出警告了,說 pc 的類型是不對的。然后我們來運行看看結果

Linux 宏定義之 offsetof 與 container_of(十九)

        我們發現最后打印的 s 和 pst 的值竟然是不一樣的。由此可以看出,原生的 container_of 宏寫法雖然復雜點,但是它的安全性是最高的。通過今天對 offsetof 與 container_of 宏的剖析,總結如下:1、編譯器清楚的知道結構體成員變量的偏移位置;2、({ }) 與逗號表達式類似,結果為最后一個語句的值;3、typeof 只在編譯期生效,用于得到變量的類型;4、container_of 使用 ({ }) 進行類型的安全檢查。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女