码迷,mamicode.com
首页 > 其他好文 > 详细

container_of

时间:2016-05-15 22:52:02      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

 1 /**
 2  * container_of - cast a member of a structure out to the containing structure
 3  * @ptr:    the pointer to the member.
 4  * @type:   the type of the container struct this is embedded in.
 5  * @member: the name of the member within the struct.
 6  *
 7  */
 8 #define container_of(ptr, type, member) ({           9     const typeof( ((type *)0)->member ) *__mptr = (ptr);    10     (type *)( (char *)__mptr - offsetof(type,member) );})

它的作用显而易见,那就是根据一个结构体变量中的一个域成员变量的指针来获取指向整个结构体变量的指针

1 #undef offsetof
2 #ifdef __compiler_offsetof
3 #define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
4 #else
5 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
6 #endif

 



container_of

标签:

原文地址:http://www.cnblogs.com/embedded-linux/p/5496462.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!