linux中offsetof与container_of宏定义
linux內核中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含義:獲取結構體中某個成員變量相對于結構體首地址的內存位置偏移;
container_of含義:根據結構體中某個成員變量的內存地址獲取結構體的內存首地址。
?
offsetof宏詳解:
#define offsetof(TYPE, MEMBER)??? ((size_t) &((TYPE *)0)->MEMBER)
1)、參數TYPE為結構體的類型定義,MEMBER為結構體成員變量名稱;
2)、實現方法比較取巧:將0強制轉化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));
最終結果為: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)、參數ptr為結構體成員的內存地址,type為結構體類型定義,member為結構體成員變量名稱;
2)、typeof為gun編譯器系列的內置函數,函數返回當前變量的類型;
3)、const typeof( ((type *)0)->member ) *__mptr = (ptr); 這行定義了一個member類型的指針變量__mptr,并且值初始化為ptr(注意:這里type也是0強制轉換);
4)、(type *)( (char *)__mptr - offsetof(type,member) );
這行則是將結構體成員變量的內存地址減去此成員變量與結構體首地址的偏移(offsetof前面已講解),即為結構體的內存首地址;
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);
運行結果必然是&stru與ptr的內存地址是一樣的,這個例子只是做一個證明,實際使用時肯定是先不知道結構體首地址的,需要由結構體變量地址計算結構體首地址。
?
其實總結起來很簡單,要想根據一個結構體變量的指針計算出結構體本身的指針,只需要當前變量指針減去變量到結構體首的偏移(base = ptr - offset)。
?
轉載于:https://www.cnblogs.com/fly-narrow/p/4704309.html
總結
以上是生活随笔為你收集整理的linux中offsetof与container_of宏定义的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: git分支的衍合
- 下一篇: linux 其他常用命令