Linux内核中的container_of浅析

本文详细解析了Linux内核中container_of宏的作用,它能根据结构体成员地址反向获取整个结构体的地址。文章介绍了container_of的定义,并在toolsperfutilincludelinuxKernel.h中找到其源码。还通过一个简单的实例展示了如何使用container_of进行结构体成员的访问。
摘要由CSDN通过智能技术生成

本文的主要内容:

一、container_of的作用

二、container_of的定义

三、container_of的简单实现


一、container_of的作用

一般的我们通过结构体变量的地址可以找到其成员的地址,但是反过来一般是行不通的。在linux内核中就有这样的一个宏:container_of,它可以实现根据结构体成员的地址,找到这个结构体变量的地址,从而对结构体中的其他成员进行访问。


二、container_of的定义

container_of这个宏在Linux内核的tools\perf\util\include\linux\Kernel.h文件中,它的具体定义如下:

/**
 * 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(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值