首页 > 运维 > linux运维 > 正文

Linux内核基础篇——container_of原理和实际应用

发布: 2023-07-31 15:46:13
转载
1045 人浏览过

Linux内核中经常可见container_of的身影,它在实际驱动的编写中也是广泛应用。container_of的身影,它在实际驱动的编写中也是广泛应用。

container_of原理

作用:通过结构体的某个成员变量地址找到该结构体的首地址

定义如下:

/**
 * 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) );})
登录后复制
  • ptr:结构体成员变量的指针
  • type:结构体类型
  • member:结构体成员变量的名字

换句话说,叫:已知结构体type的成员member的地址ptr,求解结构体type

container_of原理

作用

:通过结构体的某个成员变量地址🎜找到该结构体的首地址🎜。🎜🎜定义如下:🎜
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
登录后复制
登录后复制
  • ptr:结构体成员变量的指针
  • type:结构体类型
  • member:结构体成员变量的名字
🎜换句话说,叫:已知结构体type的成员member的地址ptr,求解结构体type的起始地址🎜。🎜

计算公式为:类型的起始地址 = ptr -size (size 为成员的大小)type的起始地址 = ptr -size (size为member的大小)

以一幅图说明ptrtypemember的关系:

Linux内核基础篇——container_of原理和实际应用
  • 原理简述:

container_of的妙处就在于0作为成员变量member的基址

其中定义了一个中间变量__mptr,"__"代表内部使用,“m”代表middle

以一幅图说明ptr、<代码样式=“字体大小:14px;填充:2px 4px;边框半径:4px;右边距:2px;左边距:2px;背景颜色:rgba(27,31,35,0.05);font- family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: 全部分解;color: rgb(239, 112, 96);">类型、成员的关系:🎜
Linux内核基础篇——container_of原理和实际应用
  • 原理简述:
🎜container_of的妙处就在🎜以0成员作为变量成员的基址
。🎜🎜其中定义了一个中间变量__mptr,"__"代表内部使用,“m”代表middle 。🎜
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
登录后复制
登录后复制

typeof( ((type *)0)->member )是获取member的类型,__mptr = (ptr)判断ptrmember是否为同一类型,offsetof计算成员member的大小size

驱动中的实际例子

例如内核的pwm驱动,通过成员变量chip,找到结构体bcm2835_pwm

struct bcm2835_pwm {
 struct pwm_chip chip;
 struct device *dev;
 void __iomem *base;
 struct clk *clk;
};

static inline struct bcm2835_pwm *to_bcm2835_pwm(struct pwm_chip *chip_ptr)
{
 return container_of(chip_ptr, struct bcm2835_pwm, chip);
}
登录后复制

使用container_of通常都会定义一个函数,并且命名为to_xxx或者to_find_xxx,代表要找xxx这个结构体,传参则传入成员变量指针,另外函数也会声明为inline

以上是Linux内核基础篇——container_of原理和实际应用的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:嵌入式Linux充电站
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!