日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux中offsetof与container_of宏定义

發布時間:2025/4/16 linux 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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宏定义的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。