佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > Linux內(nèi)核宏Container_Of的詳細(xì)解釋

Linux內(nèi)核宏Container_Of的詳細(xì)解釋

熱門標(biāo)簽:智能手機(jī) 美圖手機(jī) 使用U盤裝系統(tǒng) 百度競價點(diǎn)擊價格的計算公式 阿里云 硅谷的囚徒呼叫中心 檢查注冊表項(xiàng) 網(wǎng)站建設(shè)

1. 結(jié)構(gòu)體在內(nèi)存中是如何存儲的

int main() 
{ 
 
 Student stu; 
 stu.id = 123456; 
 strcpy(stu.name,"feizhufeifei"); 
 stu.math = 90; 
 stu.PE = 80; 
 printf("Student:%p\r\n",&stu); 
 printf("stu.ID:%p\r\n",&stu.ID); 
 printf("stu.name:%p\r\n",&stu.name); 
 printf("stu.math:%p\r\n",&stu.math); 
 return 0; 
} 

打印結(jié)果如下:

//結(jié)構(gòu)體的地址 
Student:0xffffcbb0 
//結(jié)構(gòu)體第一個成員的地址 
stu.ID:0xffffcbb0  //偏移地址 +0 
stu.name:0xffffcbb4//偏移地址 +4 
stu.math:0xffffcbd4//偏移地址 +24 


我們可以看到,結(jié)構(gòu)體的地址和結(jié)構(gòu)體第一個成員的地址是相同的。這也就是我們之前在拒絕造輪子!如何移植并使用Linux內(nèi)核的通用鏈表(附完整代碼實(shí)現(xiàn))中提到的為什么在結(jié)構(gòu)體中要把 struct list_head放在首位。

不太理解的再看下這兩個例子:

  • struct A { int a; char b; int c; char d; };a 偏移為 0 , b 偏移為 4 , c 偏移為 8 (大于 4 + 1 的 4 的最小整數(shù)倍), d 偏移為 12 。A 對齊為 4 ,大小為 16 。
  • struct B { int a; char b; char c; long d; };a 偏移為 0 , b 偏移為 4 , c 偏移為 5 , d 偏移為 8 。B 對齊為 8 , 大小為 16 。

我們可以看到,結(jié)構(gòu)體中成員變量在內(nèi)存中存儲的其實(shí)是偏移地址。也就是說結(jié)構(gòu)體A的地址+成員變量的偏移地址 = 結(jié)構(gòu)體成員變量的起始地址。

因此,我們也可以根據(jù)結(jié)構(gòu)體變量的起始地址和成員變量的偏移地址來反推出結(jié)構(gòu)體A的地址。

2. 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)); }) 


??首先看下三個參數(shù), ptr是成員變量的指針, type是指結(jié)構(gòu)體的類型, member是成員變量的名字。

??container_of宏的作用是通過結(jié)構(gòu)體內(nèi)某個成員變量的地址和該變量名,以及結(jié)構(gòu)體類型,找到該結(jié)構(gòu)體變量的地址。這里使用的是一個利用編譯器技術(shù)的小技巧,即先求得結(jié)構(gòu)成員在結(jié)構(gòu)中的偏移量,然后根據(jù)成員變量的地址反過來得出主結(jié)構(gòu)變量的地址。下面具體分析下各個部分。

3. typeof

首先看下typeof,是用于返回一個變量的類型,這是GCC編譯器的一個擴(kuò)展功能,也就是說typeof是編譯器相關(guān)的。既不是C語言規(guī)范的所要求,也不是某個標(biāo)準(zhǔn)的一部分。

int main() 
{ 
 int a = 5; 
 //這里定義一個和a類型相同的變量b 
 typeof(a) b  = 6; 
 printf("%d,%d\r\n",a,b);//5 6 
 return 0; 
} 

4. (((type *)0)->member)

((TYPE *)0) 將0轉(zhuǎn)換為type類型的結(jié)構(gòu)體指針,換句話說就是讓編譯器認(rèn)為這個結(jié)構(gòu)體是開始于程序段起始位置0,開始于0地址的話,我們得到的成員變量的地址就直接等于成員變量的偏移地址了。

(((type *)0)->member) 引用結(jié)構(gòu)體中MEMBER成員。

typedef struct student{ 
 int id; 
 char name[30]; 
 int math; 
}Student; 
int main() 
{ 
 //這里時把結(jié)構(gòu)體強(qiáng)制轉(zhuǎn)換成0地址,然后打印name的地址。 
 printf("%d\r\n",&((Student *)0)->name);//4 
 return 0; 
} 

5. const typeof(((type * )0) ->member)*__mptr = (ptr);

這句代碼意思是用typeof()獲取結(jié)構(gòu)體里member成員屬性的類型,然后定義一個該類型的臨時指針變量__mptr,并將ptr所指向的member的地址賦給__mptr;

為什么不直接使用 ptr 而要多此一舉呢?我想可能是為了避免對 ptr prt 指向的內(nèi)容造成破壞。

6. offsetof(type, member))

((size_t) &((TYPE*)0)->MEMBER)

size_t是標(biāo)準(zhǔn)C庫中定義的,在32位架構(gòu)中被普遍定義為:

typedef unsigned int size_t;

而在64位架構(gòu)中被定義為:

typedef unsigned long size_t;

可以從定義中看到,size_t是一個非負(fù)數(shù),所以size_t通常用來計數(shù)(因?yàn)橛嫈?shù)不需要負(fù)數(shù)區(qū)):

for(size_t i=0;i<300;i++)

為了使程序有很好的移植性,因此內(nèi)核使用size_t,而不是int,unsigned。((size_t) &((TYPE*)0)->MEMBER) 結(jié)合之前的解釋,我們可以知道這句話的意思就是求出MEMBER相對于0地址的一個偏移值。

7. (type * )((char * )__mptr - offsetof(type, member))

這句話的意思就是,把 __mptr 轉(zhuǎn)換成 char* 類型。因?yàn)?code> offsetof 得到的偏移量是以字節(jié)為單位。兩者相減得到結(jié)構(gòu)體的起始位置, 再強(qiáng)制轉(zhuǎn)換成 type 類型。

8. 舉例

#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) );}) 
         
typedef struct student 
{ 
 int id; 
 char name[30]; 
 int math; 
}Student; 
 
int main() 
{ 
    Student stu; 
        Student *sptr = NULL; 
  stu.id = 123456; 
  strcpy(stu.name,"zhongyi"); 
  stu.math = 90; 
        sptr = container_of(&stu.id,Student,id); 
        printf("sptr=%p\n",sptr); 
        sptr = container_of(&stu.name,Student,name); 
        printf("sptr=%p\n",sptr); 
        sptr = container_of(&stu.math,Student,id); 
        printf("sptr=%p\n",sptr); 
        return 0;  
} 


運(yùn)行結(jié)果如下:

sptr=0xffffcb90
sptr=0xffffcb90
sptr=0xffffcbb4

宏展開可能會看的更清楚一些

int main() 
{ 
    Student stu; 
        Student *sptr = NULL; 
  stu.id = 123456; 
  strcpy(stu.name,"zhongyi"); 
  stu.math = 90; 
  //展開替換 
        sptr = ({ const unsigned char  *__mptr = (&stu.id); (Student *)( (char *)__mptr - ((size_t) &((Student *)0)->id) );}); 
        printf("sptr=%p\n",sptr); 
        //展開替換 
        sptr = ({ const unsigned char  *__mptr = (&stu.name); (Student *)( (char *)__mptr - ((size_t) &((Student *)0)->name) );}); 
        printf("sptr=%p\n",sptr); 
        //展開替換 
        sptr = ({ const unsigned int *__mptr = (&stu.math); (Student *)( (char *)__mptr - ((size_t) &((Student *)0)->math) );}); 
        printf("sptr=%p\n",sptr); 
        return 0;  
} 

到此這篇關(guān)于Linux內(nèi)核中Container_Of宏的詳細(xì)解釋的文章就介紹到這了,更多相關(guān)Linux內(nèi)核中Container_Of宏內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

標(biāo)簽:湘潭 賀州 山南 黃山 通遼 煙臺 懷化 湖北

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Linux內(nèi)核宏Container_Of的詳細(xì)解釋》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    三江| 拜城县| 东光县| 富阳市| 岳西县| 会昌县| 湖北省| 沂南县| 澜沧| 湖南省| 滦平县| 沾化县| 梁平县| 钟祥市| 五莲县| 东安县| 万山特区| 四平市| 东阳市| 进贤县| 鸡西市| 乌拉特后旗| 平邑县| 盐亭县| 长葛市| 福清市| 辛集市| 扎兰屯市| 云龙县| 汝州市| 承德市| 正蓝旗| 策勒县| 牟定县| 周宁县| 阿勒泰市| 齐河县| 宝鸡市| 尚义县| 运城市| 清河县|