內核中container_of宏的詳細分析【轉】
阿新 • • 發佈:2018-09-06
按位與 指針常量 並且 cast 例子 找到 eof 運算符 gnu
轉自:http://blog.chinaunix.net/uid-30254565-id-5637597.html
- 內核中container_of宏的詳細分析
-
16年2月28日09:00:37
-
內核中有一個大名鼎鼎的宏-----container_of();這個宏定義如下所示,為了表示一下敬意,我就把註釋一起粘貼下來了:
-
/**
-
* container_of - cast a member of a structure out to the containing structure
-
* @ptr: the pointer to the member.
-
* @type: the type of the container struct this is embedded in.
-
* @member: the name of the member within the struct.
-
*
-
*/
-
#define container_of(ptr, type, member) ({ \
-
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
-
(type *)( (char *)__mptr - offsetof(type,member) );})
-
先來說這個宏的意義:它根據結構體中某成員變量的指針來求出指向整個結構體的指針。指針類型從結構體某成員變量類型轉換為 該結構體類型。
-
-
比如先定義一個結構體:
-
struct test {
-
char name[20] ;
-
char i;
-
int j;
-
};
-
假如,我們不小心知道了變量j的地址,那麽我們想要通過j的地址來找到整個結構體test的地址,怎麽來找呢???
-
-
(一) offsetof宏:
-
結構體是一個線性存儲的結構,無論在哪存放,j相對於整個結構體的地址的偏移值是不變的,於是,如果我們能夠求出來這個偏移值的話,那麽用j的地址減去這個偏移值不就是整個結構體的地址麽~這是一個樸素的想法,內核中也確實這麽做的~關鍵是怎麽求出這個偏移值?內核的非常聰明的采取了下面的方法:
-
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
-
(1)首先通過(TYPE *)0將0轉換為TYPE類型的指針;
-
(2)((TYPE *)0)->MEMBER 訪問結構中的數據成員;
-
(3)&(((TYPE *)0)->MEMBER)取出數據成員的地址;
-
(4)(size_t)(&(((TYPE*)0)->MEMBER))結果轉換類型; 註意這裏:這個&是取地址符號,不是按位與,註意運算符號的優先級。
-
-
巧妙之處在於將地址0強制類型轉換成(TYPE*),結構體以內存空間首地址0作為起始地址,則各個結構體成員變量的偏移地址就等於其成員變量相對於整個結構體首地址的偏移量 。即:&(((TYPE *)0)->MEMBER)就是取出其成員變量的偏移地址,(size_t)(&(((TYPE*)0)->MEMBER))經過size_t的強制類型轉換以後,其數值為結構體內的偏移量。
-
需要明確的一點是,地址就是地址,它沒有類型之分,你把它強制轉換成什麽類型它就是什麽類型,所以在c語言中有各種強制類型轉換。
-
用下面的例子來說明:
-
#include <stdio.h>
-
-
struct test {
-
char i;
-
int j;
-
int k;
-
};
-
-
int main(int argc, char const *argv[])
-
{
-
struct test *temp = 0;
-
-
printf("%p \n", &(temp->j));
-
printf("%d \n", (size_t) &(temp->j));
-
printf("%p \n", &(temp->k));
-
printf("%d \n", (size_t) &(temp->k));
-
-
return 0;
-
}
-
運行結果是:
-
0x4
-
4
-
0x8
-
8
-
可以看出來,通過采用這種方式,就可以求出來結構體中成員變量相對與整個結構體首地址的偏移量。
-
-
(二) container_of宏
-
如果理解了上面的部分,再看這個container_of宏就不是那麽難了,我們先想想它怎麽實現:
-
假設我們知道一個test類型的結構體裏面的一個成員變量j的地址,那麽需要先求出這個j變量相對於整個結構體地址的偏移值,然後用這個j的地址減去這個偏移值就行了。但是還有一點,這個j變量的數據類型是什麽樣的?這個雖然我們知道test數據類型,但是我們怎麽取出來j對應的數據類型呢?這時候就用到typeof關鍵字了,typeof是GNU
C對標準C的擴展,它的作用是根據變量獲取變量的數據類型。
-
下面來看這些代碼:
-
#define container_of(ptr, type, member) ({ \
-
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
-
(type *)( (char *)__mptr - offsetof(type,member) );})
-
首先
-
(1)((type *)0)->member為設計一個type類型的結構體,並且這個結構體的的起始地址為0,然後將它指向我們知道的member變量,然後通過typeof( ((type *)0)->member )來獲得member對應的數據類型。
-
(2)const typeof( ((type *)0)->member ) *__mptr = (ptr);意思是聲明一個與member同一個類型的指針常量 *__mptr,並初 始化為ptr.
-
(3)(char *)__mptr - offsetof(type,member)意思是__mptr的地址減去member在該struct中的偏移量得到的地 址, 這樣得到的就是整個結構體的首地址。
-
(4)得到首地址後還沒有完,上面說了,地址只是一個地址,它沒有數據類型,所以最後再進行一一次強制類型轉換,轉換成我們需要的type類型的,即:
-
(type *)( (char *)__mptr - offsetof(type,member) );
-
(5)({ })這個擴展返回程序塊中最後一個表達式的值。註意這個 container_of宏是兩個表達式語句的綜合。 相當與順序執行了兩個語句,這時候得到的地址就是member成員所在結構體的首地址。
-
要註意的是代碼高亮處 ,(char *)__mptr 的作用是將__mptr 強制轉換為字符指針類型,必須的!!!如果__mptr為整形指針 __mptr - offset 相當於減去sizeof(int)*offset個字節!!!
-
-
下面再看一個程序來溫習一下:
-
-
#include <stdio.h>
-
-
#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) );})
-
-
struct test {
-
char name[20] ;
-
char i;
-
int j;
-
};
-
-
int main(int argc, char const *argv[])
-
{
-
struct test temp = {"zer0", ‘a‘, 26};
-
-
printf("&temp = %p.\n", &temp);
-
printf("&temp.i = %p.\n", &temp.i);
-
printf("&temp.j = %p.\n", &temp.j);
-
printf("offset of i = %d.\n", offsetof(struct test, i));
-
printf("offset of j = %d.\n", offsetof(struct test, j));
-
printf("&temp = %p.\n", container_of(&temp.i, struct test, i));
-
printf("&temp = %p.\n", container_of(&temp.j, struct test, j));
-
-
struct test *tmp = container_of(&temp.i, struct test, i);
-
printf("tmp->name : %s, tmp->i : %c, tmp->j : %d.\n", tmp->name, tmp->i, tmp->j);
-
return 0;
-
}
-
-
運行結果如下所示:
-
&temp = 0xbf8b41b0.
-
&temp.i = 0xbf8b41c4.
-
&temp.j = 0xbf8b41c8.
-
offset of i = 20.
-
offset of j = 24.
-
&temp = 0xbf8b41b0.
-
&temp = 0xbf8b41b0.
- tmp->name : zer0, tmp->i : a, tmp->j : 26.
內核中container_of宏的詳細分析【轉】