文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>container_of

container_of

时间:2009-04-11  来源:ruanbanyong1

container_of

这个函数的作用是根据结构体成员找到对应的结构体指针

比如我定义了个结构
struct dev
{
    int a;
    ceev cdev;
   
};
那么当我知道了结构体的一个成员的时候怎么找到对应的结构体呢??特别是在驱动开发中,我们在open函数代开的时候,如果这个驱动用了几个设备,那么怎么找到对应的设备呢?这个函数在这个时候就很有作用了,因为open函数打开的时候,对应的设备是知道的,而我们将这个设备结构封装到自定义结构体中,如上,这样就可以找到这个结构本身了,其他的成员就可以很好调用了。
 如下使用:
struct dev *dev;
dev = container_of(inode->i_cdev,struct dev,cdev);
第一个参数是知道的我们自定义的结构体dev的一个成员,第二个参数是我们要获取的结构体的类型,第三个参数是第一个参数的类型。,使用起来很简单吧。

函数具体定义:
linux/include/linux/kernel.h中,原型为:
311/**
312 * container_of - cast a member of a structure out to the containing structure
313 * @ptr: the pointer to the member.
314 * @type: the type of the container struct this is embedded in.
315 * @member: the name of the member within the struct.
316 *
317 */
318#define container_of(ptr, type, member) ({ \
319 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
320 (type *)( (char *)__mptr - offsetof(type,member) );})
相关阅读 更多 +
排行榜 更多 +
剑术大乱斗

剑术大乱斗

动作格斗 下载
南钛HSSE

南钛HSSE

商务办公 下载
柱子

柱子

动作格斗 下载