溫馨提示×

溫馨提示×

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

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

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

發(fā)布時間:2020-06-04 10:10:59 來源:網(wǎng)絡(luò) 閱讀:2193 作者:上帝之子521 欄目:軟件技術(shù)

        今天我們來看看 Linux 中的兩個經(jīng)典的宏: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 是用于計(jì)算 TYPE 結(jié)構(gòu)體中 MEMBER 成員的偏移位置。編譯器清楚的知道結(jié)構(gòu)體成員變量的偏移位置,通過結(jié)構(gòu)體變量首地址與偏移量定位成員變量。下來我們通過測試代碼來進(jìn)行說明

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

        我們來看看結(jié)果

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

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

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

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

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

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

        最后的原理如下圖所示

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

        下來我們來編程進(jìn)行分析說明

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

        我們來編譯看看結(jié)果

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

        編譯的時候報(bào)了 4 個警告,但是不影響我們的輸出,看看運(yùn)行結(jié)果

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

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

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

        編譯的時候已經(jīng)給出警告了,說 pc 的類型是不對的。然后我們來運(yùn)行看看結(jié)果

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

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

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI