linux內(nèi)核中offsetof與container_of的宏定義
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
/*** 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) );})
offsetof含義:獲取結(jié)構(gòu)體中某個成員變量相對于結(jié)構(gòu)體首地址的內(nèi)存位置偏移;
container_of含義:根據(jù)結(jié)構(gòu)體中某個成員變量的內(nèi)存地址獲取結(jié)構(gòu)體的內(nèi)存首地址。
offsetof宏詳解:#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
1)、參數(shù)TYPE為結(jié)構(gòu)體的類型定義,MEMBER為結(jié)構(gòu)體成員變量名稱;
2)、實現(xiàn)方法比較取巧:將0強制轉(zhuǎn)化TYPE類型的指針,然后對MEMBER變量取址;
3)、代碼示例:
typedef struct
{
int a;
char b;
void *c;
} off_struct_s;
PRintf("offset a:%d b:%d c:%d/n", offsetof(off_strcut_s, a), offsetof(off_strcut_s, b), offsetof(off_strcut_s, c));
最終結(jié)果為:offset a:0 b:4 c:8
container_of宏詳解:#define container_of(ptr, type, member) ({ /const typeof( ((type *)0)->member ) *__mptr = (ptr); /(type *)( (char *)__mptr - offsetof(type,member) );})
1)、參數(shù)ptr為結(jié)構(gòu)體成員的內(nèi)存地址,type為結(jié)構(gòu)體類型定義,member為結(jié)構(gòu)體成員變量名稱;
2)、typeof為gun編譯器系列的內(nèi)置函數(shù),函數(shù)返回當(dāng)前變量的類型;
3)、const typeof( ((type *)0)->member ) *__mptr = (ptr); 這行定義了一個member類型的指針變量__mptr,并且值初始化為ptr(注意:這里type也是0強制轉(zhuǎn)換);
4)、(type *)( (char *)__mptr - offsetof(type,member) );
這行則是將結(jié)構(gòu)體成員變量的內(nèi)存地址減去此成員變量與結(jié)構(gòu)體首地址的偏移(offsetof前面已講解),即為結(jié)構(gòu)體的內(nèi)存首地址;
5)、代碼示例:
off_struct_s stru;
void *ptr;
ptr = (void *)(container_of(&stru.c, off_struct_s, c));
printf("stru_addr:%p container_addr:%p/n", &stru, ptr);
運行結(jié)果必然是&stru與ptr的內(nèi)存地址是一樣的,這個例子只是做一個證明,實際使用時肯定是先不知道結(jié)構(gòu)體首地址的,需要由結(jié)構(gòu)體變量地址計算結(jié)構(gòu)體首地址。
其實總結(jié)起來很簡單,要想根據(jù)一個結(jié)構(gòu)體變量的指針計算出結(jié)構(gòu)體本身的指針,只需要當(dāng)前變量指針減去變量到結(jié)構(gòu)體首的偏移(base = ptr - offset)。
新聞熱點
疑難解答
圖片精選