Linux內核中Container_Of宏的詳細解釋
我們在分析Linux內核鏈表的時候注意到內核在求解結構體偏移的時候巧妙的使用了container_of宏定義,今天我們來詳細剖析下內核到底是如何求解結構體成員變量的地址的。
- 1. 結構體在內存中是如何存儲的
- 2. container_of宏
- 3. typeof
- 4. (((type *)0)->member)
- 5. const typeof(((type * )0) ->member)*__mptr = (ptr);
- 6. offsetof(type, member))
- 7. (type * )((char * )__mptr - offsetof(type, member))
- 8. 舉例
1. 結構體在內存中是如何存儲的
- 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;
- }
打印結果如下:
- //結構體的地址
- Student:0xffffcbb0
- //結構體第一個成員的地址
- stu.ID:0xffffcbb0 //偏移地址 +0
- stu.name:0xffffcbb4//偏移地址 +4
- stu.math:0xffffcbd4//偏移地址 +24
??我們可以看到,結構體的地址和結構體第一個成員的地址是相同的。這也就是我們之前在拒絕造輪子!如何移植并使用Linux內核的通用鏈表(附完整代碼實現)中提到的為什么在結構體中要把 struct list_head放在首位。
不太理解的再看下這兩個例子:
struct A { int a; char b; int c; char d; };a 偏移為 0 , b 偏移為 4 , c 偏移為 8 (大于 4 + 1 的 4 的最小整數倍), 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 。
我們可以看到,結構體中成員變量在內存中存儲的其實是偏移地址。也就是說結構體A的地址+成員變量的偏移地址 = 結構體成員變量的起始地址。
因此,我們也可以根據結構體變量的起始地址和成員變量的偏移地址來反推出結構體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)); })
??首先看下三個參數, ptr是成員變量的指針, type是指結構體的類型, member是成員變量的名字。
??container_of宏的作用是通過結構體內某個成員變量的地址和該變量名,以及結構體類型,找到該結構體變量的地址。這里使用的是一個利用編譯器技術的小技巧,即先求得結構成員在結構中的偏移量,然后根據成員變量的地址反過來得出主結構變量的地址。下面具體分析下各個部分。
3. typeof
首先看下typeof,是用于返回一個變量的類型,這是GCC編譯器的一個擴展功能,也就是說typeof是編譯器相關的。既不是C語言規范的所要求,也不是某個標準的一部分。
typeof
- 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轉換為type類型的結構體指針,換句話說就是讓編譯器認為這個結構體是開始于程序段起始位置0,開始于0地址的話,我們得到的成員變量的地址就直接等于成員變量的偏移地址了。
(((type *)0)->member) 引用結構體中MEMBER成員。
- typedef struct student{
- int id;
- char name[30];
- int math;
- }Student;
- int main()
- {
- //這里時把結構體強制轉換成0地址,然后打印name的地址。
- printf("%d\r\n",&((Student *)0)->name);//4
- return 0;
- }
5. const typeof(((type * )0) ->member)*__mptr = (ptr);
這句代碼意思是用typeof()獲取結構體里member成員屬性的類型,然后定義一個該類型的臨時指針變量__mptr,并將ptr所指向的member的地址賦給__mptr;
為什么不直接使用 ptr 而要多此一舉呢?我想可能是為了避免對 ptr 及prt 指向的內容造成破壞。
6. offsetof(type, member))
- ((size_t) &((TYPE*)0)->MEMBER)
size_t是標準C庫中定義的,在32位架構中被普遍定義為:
- typedef unsigned int size_t;
而在64位架構中被定義為:
- typedef unsigned long size_t;
可以從定義中看到,size_t是一個非負數,所以size_t通常用來計數(因為計數不需要負數區):
- for(size_t i=0;i<300;i++)
為了使程序有很好的移植性,因此內核使用size_t,而不是int,unsigned。((size_t) &((TYPE*)0)->MEMBER) 結合之前的解釋,我們可以知道這句話的意思就是求出MEMBER相對于0地址的一個偏移值。
7. (type * )((char * )__mptr - offsetof(type, member))
這句話的意思就是,把 __mptr 轉換成 char* 類型。因為 offsetof 得到的偏移量是以字節為單位。兩者相減得到結構體的起始位置, 再強制轉換成 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;
- }
運行結果如下:
- 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;
- }
本文轉載自微信公眾號「嵌入式與Linux那些事」,作者為少。轉載本文請聯系嵌入式與Linux那些事公眾號。