linux 内核常用数据结构及算法——container_of

container_of是linux内核中常用的一个宏函数,其用途是通过结构体的某个成员变量的指针倒推出结构体变量的指针。在内核代码中,container_of的实现如下:

#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif

#ifndef 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)); })
#endif
首先,宏函数offsetof是用来计算结构体的起始到成员类型MEMBER的偏移量,其参数为:

TYPE为结构体的类型

MEMBER为结构体成员的类型

((TYPE *)0)表示虚拟的结构体变量指针,其起始地址为0,((TYPE *)0)->MEMBER表示MEMER类型的成员,&((TYPE *)0)->MEMBER则取MEMBER成员的地址,如果考虑结构体变量的起始地址为0,则MEMBER的地址,就是结构体指针0到MEMBER成员的偏移量


container_of的是三个参数是:

ptr:成员变量的指针

type:结构体的类型

member:与ptr对应的结构体成员的类型

其宏函数可以分解为以下几个部分

typeof(((type *)0)->member) * __mptr;   //typeof是GNU C对标准C的扩展,它的作用是根据变量获取变量的类型,表示定义了一个结构体成员member类型的变量__mptr

__mptr = (ptr);  //将结构体成员变量ptr的指针赋值给__mptr

通过offsetof(type, member)计算出结构体变量起始指针到member类型变量的偏移长度,

用__mptr即结构体成员变量的指针减去偏移长度,就可以得到结构体变量的地址指针


以下是一个例子

#include <stdio.h>

#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif

#ifndef 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)); })
#endif

struct  cat{
	int  age;
	int  weight;
	char name[10];       
};

int main(int argc, char** argv)
{
	struct cat  tom;
	tom.age = 3;
	tom.weight = 4;
	strcpy(tom.name, "Tom Cat");

	struct cat *who;
	who = (struct cat*)container_of(&tom.name, struct cat, name);
	if (who ==  &tom) {
		  printf("who is tom\n");
	}

	who = NULL;
	who = (struct cat*)container_of(&tom.weight, struct cat, weight);
	if (who ==  &tom) {
		  printf("who is tom\n");
	}
	return 0;
}


知行后,输出结果为:

#who is tom

#who is tom


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值