上一節拒絕造輪子!如何移植并使用Linux內核的通用鏈表(附完整代碼實作)我們在分析Linux內核鏈表的時候注意到內核在求解結構體偏移的時候巧妙的使用了container_of宏定義,今天我們來詳細剖析下內核到底是如何求解結構體成員變數的地址的,
文章目錄
- 結構體在記憶體中是如何存盤的
- container_of宏
- typeof
- (((type *)0)->member)
- const typeof(((type *)0)->member)*__mptr = (ptr);
- offsetof(type, member))
- (type *)((char *)__mptr - offsetof(type, member))
- 舉例
結構體在記憶體中是如何存盤的
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的地址,
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宏的作用是通過結構體內某個成員變數的地址和該變數名,以及結構體型別,找到該結構體變數的地址,這里使用的是一個利用編譯器技術的小技巧,即先求得結構成員在結構中的偏移量,然后根據成員變數的地址反過來得出主結構變數的地址,下面具體分析下各個部分:
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;
}
(((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;
}
const typeof(((type )0)->member)__mptr = (ptr);
?? 這句代碼意思是用typeof()獲取結構體里member成員屬性的型別,然后定義一個該型別的臨時指標變數__mptr,并將ptr所指向的member的地址賦給__mptr;
??為什么不直接使用 ptr 而要多此一舉呢? 我想可能是為了避免對 ptr 及prt 指向的內容造成破壞,
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地址的一個偏移值,
(type *)((char *)__mptr - offsetof(type, member))
?? 這句話的意思就是,把 __mptr 轉換成 char * 型別, 因為 offsetof 得到的偏移量是以位元組為單位, 兩者相減得到結構體的起始位置, 再強制轉換成 type 型別,
舉例
#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,"feizhufeifei");
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,"feizhufeifei");
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;
}
??養成習慣,先贊后看!如果覺得寫的不錯,歡迎關注,點贊,收藏,轉發,謝謝!
??以上代碼均為測驗后的代碼,如有錯誤和不妥的地方,歡迎指出,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/128591.html
標籤:其他
上一篇:【FLIR工業相機】一、環境配置:win10+VS2017+qt5+spinnaker+opencv+python
