/**
* 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) );})
container_of在Linux Kernel中的應用非常廣泛,它用于通過結構體某個成員變量的地址獲得整個結構體的入口地址。
首先解釋下宏定義中出現的另外兩個關鍵字或者宏定義。
typeof:獲得變量類型。
如:
char* pc;
typeof(*pc) c;
c = ‘a’;
這個例子中,typeof(*pc)就相當于char,所以變量c的類型就是char型變量。
offsetof(type,member):member變量在type結構體中的偏移量。
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)-》MEMBER)
TYPE是某struct的類型,0是一個假想TYPE類型struct,MEMBER是該struct中的一個成員。 由于該struct的基地址為0, MEMBER的地址就是該成員相對與struct頭地址的偏移量。
下面介紹container_of的具體實現:container_of(ptr, type, member):
const typeof( ((type *)0)-》member ) *__mptr = (ptr);
意思是聲明一個與member同一個類型的指針常量 *__mptr,并初始化為ptr,注意typeof( ((type *)0)-》member )就是獲得member變量的類型。前面加上const,是為了保證后面的0不被改動。
(type *)( (char *)__mptr - offsetof(type,member) );意思是__mptr的地址減去member在該struct中的偏移量得到的地址, 再轉換成type型指針。 該指針就是member所在結構體的入口地址了。為什么要先強制轉換為char類型指針呢? 如果_mptr為整形指針 _mptr - offset 相當于減去 sizeof(int)*offset個字節。
評論