container of()函数简介
Typeof关键字介绍
前言
typeof() 是GUN C提供的一种特性,可参考C-Extensions,它可以取得变量的类型,或者表达式的类型。
本文总结了typeof()关键字的常见用法,并给出了相应的例子,以加深理解
typeof()关键字常见用法:
typeof关键字常见用法一共有以下几种:
1.不知道函数返回什么类型,可以使用typeof()定义一个接收该函数返回值的变量
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct apple{
int weight;
int color;
};
struct apple *get_apple_info()
{
struct apple *a1;
a1 = malloc(sizeof(struct apple));
if(a1 == NULL)
{
printf("malloc error.\n");
return NULL;
}
a1->weight = 2;
a1->color = 1;
return a1;
}
int main(int argc, char *argv[])
{
typeof(get_apple_info()) r1;//定义一个变量r1,用于接收函数get_apple_info()返回的值,由于该函数返回的类型是:struct apple *,所以变量r1也是该类型。注意,函数不会执行。
r1 = get_apple_info();
printf("apple weight:%d\n", r1->weight);
printf("apple color:%d\n", r1->color);
return 0;
}
2.在宏定义中动态获取相关结构体成员的类型
如下代码,定义一个和变量x相同类型的临时变量_max1,定义一个和变量y相同类型的临时变量_max2,再判断两者类型是否一致,不一致给出一个警告,最后比较两者。
#define max(x, y) ({ \
typeof(x) _max1 = (x); \
typeof(y) _max2 = (y); \
(void) (&_max1 == &_max2); \//如果调用者传参时,两者类型不一致,在编译时就会发出警告。
_max1 > _max2 ? _max1 : _max2; })
int main(int argc, char *argv[])
{
int a=3;
float b = 4.0;
int r = max(a, b);
printf("r:%d\n", r);
return 0;
}
3.也可以直接取得已知类型
如下代码,定义了一个int类型的指针P, 像这种用法没有什么太大的意义
int a = 2;
typeof (int *) p;
p = &a;
printf("%d\n", *p);
4.其他用法
//其它用法1
char *p1;
typeof (*p1) ch = 'a'; //ch为char类型,不是char *类型。
printf("%d, %c\n", sizeof(ch), ch);//1, a
//其它用法2
char *p2;
typeof (p2) p = "hello world";//此时的p才是char *类型,由于在64位机器上,所以指针大小为8字节
printf("%d, %s\n", sizeof(p), p);//8, hello world
参考:https://blog.csdn.net/rosetta/article/details/90741468
Typeof(((type *)0)->member)和offset_of()
前言
本文讲解typeof(((type *)0)->member)的含义,并在此基础上学习offset_of()
typeof(((type *)0)->member)
ANSI C标准允许值为0的常量被强制转换成任何一种类型的指针,并且转换的结果是个NULL,因此((type *)0)
的结果就是一个类型为type *
的NULL指针.
如果利用这个NULL指针来访问type的成员当然是非法的,但typeof( ((type *)0)->member )
是想取该成员的类型(有关typeof()的用法可参考这里),所有编译器不会生成访问type成员的代码,类似的代码&( ((type *)0)->member )
在最前面有个取地址符&
,它的意图是想取member的地址,所以编译器同样会优化为直接取地址。
struct apple{
int weight;
int color;
};
int main(int argc, char *argv[])
{
int weight = 5;
typeof(((struct apple *)0)->weight) *ap1 = &weight;//定义一个指针变量ap1, ap1的类型为apple成员weight的类型。
printf("ap1's value:%d\n", *ap1);
}
这段代码运行结果为:
[root@xxx c_base]# ./a.out
ap1's value:5
Offset_of()
理解了以上内容后再看offset_of(),它的作用是获取结构体中某个成员相对于该结构体首元素地址的偏移量
#define offset_of(type, memb) \
((unsigned long)(&((type *)0)->memb))
比如想知道sruct apple
中的color是在结构体中的哪个位置,可以这么做
printf("colore offset addr:%d\n", offset_of(struct apple, color));
[root@xxx c_base]# ./a.out
colore offset addr:4
参考: https://blog.csdn.net/rosetta/article/details/90746936
container of函数
在linux内核编程中,会经常碰到container_of(ptr,type,member),
/**
* 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) );})
要看懂上述代码,需要知道三个关键点:
- typeof(),取变量或表达式类型
- typeof( ((type *)0)->member ),用typeof( ((struct apple *)0)->color ) 解释更易理解,它的作用是获取结构体apple中变量color的类型
- offsetof(),获取结构体中某个成员相对于该结构体首元素地址的偏移量
好了,再开始解释container_of(),其三个入参含义:
- ptr:结构体变量中某个成员的地址
- type:结构体类型
- member:该结构体变量的具体名字
比如如下结构体struct ipstore
,假设已经有一个变量struct ipstore *ist1;
,并给ist1分配好了内存且进行了初始化,在已知结构体成员list的地址的情况下,获取list所在的结构体变量ist1的首地址。此时有人就会问了,这里ist1明明是已知的,这么做不是自己给自己找麻烦吗?这是个好问题,但是不要忘记,本文到目前为止只是讲解container_of()的含义,并没有说它适合用在什么样的场景下,因为有一种使用场景,当链表是通过list串起来的时候,此时并不知道ist1的首地址,反而是知道list的地址,这时container_of()就非常合适了,内核中的链表就是这么做的。
struct list_head {
struct list_head *next;
struct list_head *prev;
};
struct ipstore{
unsigned long time;
__u32 addr[4];
struct list_head list;
};
所以,调用container_of()时具体的传参如下所示,其返回的结果是ist1的地址。
container_of(ist1->list, struct ipstore, list)
如下测试代码,通过container_of()取得了ip1的地址。
void container_of_test()
{
struct ipstore ip1;
struct ipstore *p1;
p1 = container_of(&ip1.list, struct ipstore, list);
printf("ip1's addr:0x%0x\n", &ip1);
printf("p1's addr:0x%0x\n", p1);
}
[root@xxx c_base]# ./a.out
ip1's addr:0xa5fe1fe0
p1's addr:0xa5fe1fe0
最后测试一下0指针的使用
#include<stdio.h>
struct test
{
char i ;
int j;
char k;
};
int main()
{
struct test temp;
printf("&temp = %p\n",&temp);
printf("&temp.k = %p\n",&temp.k);
printf("&((struct test *)0)->k = %d\n",((int)&((struct test *)0)->k));
}
编译运行,可以得到如下结果:
&temp = 0xbf9815b4
&temp.k = 0xbf9815bc
&((struct test *)0)->k = 8
什么意思看到了吧,自定义的结构体有三个变量:i,j,k。 因为有字节对齐要求,所以该结构体大小为4bytes * 3 =12 bytes. 而&((struct test *)0)->k 的作用就是求 k到结构体temp起始地址的字节数大小(就是我们的size)。在这里0被强制转化为struct test *型, 它的作用就是作为指向该结构体起始地址的指针,就是作为指向该结构体起始地址的指针,就是作为指向该结构体起始地址的指针, 而&((struct test *)0)->k 的作用便是**求k到该起始指针的字节数。。。****其实是求相对地址,起始地址为0,则&k的值便是size大小(注:打印时因为需要整型,所以有个int强转)**所以我们便可以求我们需要的 size 了 。
参考:https://blog.csdn.net/s2603898260/article/details/79371024