offsetof与container_of宏[总结]
来源:互联网 发布:jquery ajax 返回json 编辑:程序博客网 时间:2024/06/07 00:36
offsetof与container_of宏[总结]
1、前言
今天在看代码时,遇到offsetof和container_of两个宏,觉得很有意思,功能很强大。offsetof是用来判断结构体中成员的偏移位置,container_of宏用来根据成员的地址来获取结构体的地址。两个宏设计的很巧妙,值得学习。linux内核中有着两个宏的定义,并在链表结构中得到应用。不得不提一下linux内核中的链表,设计的如此之妙,只需要两个指针就搞定了。后续认真研究一下这个链表结构。
2、offsetof宏
使用offsetof宏需要包含stddef.h头文件,实例可以参考:http://www.cplusplus.com/reference/cstddef/offsetof/。
offsetof宏的定义如下:
#define offsetof(type, member) (size_t)&(((type*)0)->member)
巧妙之处在于将地址0强制转换为type类型的指针,从而定位到member在结构体中偏移位置。编译器认为0是一个有效的地址,从而认为0是type指针的起始地址。
3、container_of宏
使用container_of宏需要包含linux/kernel.h头文件,container_of宏的定义如下所示:
#define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );})
container_of宏分为两部分,
第一部分:const typeof( ((type *)0)->member ) *__mptr = (ptr);
通过typeof定义一个member指针类型的指针变量__mptr,(即__mptr是指向member类型的指针),并将__mptr赋值为ptr。
第二部分: (type *)( (char *)__mptr - offsetof(type,member) ),通过offsetof宏计算出member在type中的偏移,然后用member的实际地址__mptr减去偏移,得到type的起始地址,即指向type类型的指针。
第一部分的目的是为了将统一转换为member类型指针。
4、测试程序
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define NAME_STR_LEN 32 5 6 #define offsetof(type, member) (size_t)&(((type*)0)->member) 7 8 #define container_of(ptr, type, member) ({ \ 9 const typeof( ((type *)0)->member ) *__mptr = (ptr); \10 (type *)( (char *)__mptr - offsetof(type,member) );})11 12 typedef struct student_info13 {14 int id;15 char name[NAME_STR_LEN];16 int age;17 }student_info;18 19 20 int main()21 {22 size_t off_set = 0;23 off_set = offsetof(student_info, id);24 printf("id offset: %u\n",off_set);25 off_set = offsetof(student_info, name);26 printf("name offset: %u\n",off_set);27 off_set = offsetof(student_info, age);28 printf("age offset: %u\n",off_set);29 student_info *stu = (student_info *)malloc(sizeof(student_info));30 stu->age = 10;31 student_info *ptr = container_of(&(stu->age), student_info, age);32 printf("age:%d\n", ptr->age);33 printf("stu address:%p\n", stu);34 printf("ptr address:%p\n", ptr);35 return 0;36 }
测试结果:
root@ubuntu:/test/linux/20160109# gcc offset.c
root@ubuntu:/test/linux/20160109# ./a.out
id offset: 0
name offset: 4
age offset: 36
age:10
stu address:0x82e5008
ptr address:0x82e5008
root@ubuntu:/test/linux/20160109#
1、前言
今天在看代码时,遇到offsetof和container_of两个宏,觉得很有意思,功能很强大。offsetof是用来判断结构体中成员的偏移位置,container_of宏用来根据成员的地址来获取结构体的地址。两个宏设计的很巧妙,值得学习。linux内核中有着两个宏的定义,并在链表结构中得到应用。不得不提一下linux内核中的链表,设计的如此之妙,只需要两个指针就搞定了。后续认真研究一下这个链表结构。
2、offsetof宏
使用offsetof宏需要包含stddef.h头文件,实例可以参考:http://www.cplusplus.com/reference/cstddef/offsetof/。
offsetof宏的定义如下:
#define offsetof(type, member) (size_t)&(((type*)0)->member)
巧妙之处在于将地址0强制转换为type类型的指针,从而定位到member在结构体中偏移位置。编译器认为0是一个有效的地址,从而认为0是type指针的起始地址。
3、container_of宏
使用container_of宏需要包含linux/kernel.h头文件,container_of宏的定义如下所示:
#define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );})
container_of宏分为两部分,
第一部分:const typeof( ((type *)0)->member ) *__mptr = (ptr);
通过typeof定义一个member指针类型的指针变量__mptr,(即__mptr是指向member类型的指针),并将__mptr赋值为ptr。
第二部分: (type *)( (char *)__mptr - offsetof(type,member) ),通过offsetof宏计算出member在type中的偏移,然后用member的实际地址__mptr减去偏移,得到type的起始地址,即指向type类型的指针。
第一部分的目的是为了将统一转换为member类型指针。
4、测试程序
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define NAME_STR_LEN 32 5 6 #define offsetof(type, member) (size_t)&(((type*)0)->member) 7 8 #define container_of(ptr, type, member) ({ \ 9 const typeof( ((type *)0)->member ) *__mptr = (ptr); \10 (type *)( (char *)__mptr - offsetof(type,member) );})11 12 typedef struct student_info13 {14 int id;15 char name[NAME_STR_LEN];16 int age;17 }student_info;18 19 20 int main()21 {22 size_t off_set = 0;23 off_set = offsetof(student_info, id);24 printf("id offset: %u\n",off_set);25 off_set = offsetof(student_info, name);26 printf("name offset: %u\n",off_set);27 off_set = offsetof(student_info, age);28 printf("age offset: %u\n",off_set);29 student_info *stu = (student_info *)malloc(sizeof(student_info));30 stu->age = 10;31 student_info *ptr = container_of(&(stu->age), student_info, age);32 printf("age:%d\n", ptr->age);33 printf("stu address:%p\n", stu);34 printf("ptr address:%p\n", ptr);35 return 0;36 }
测试结果:
- offsetof与container_of宏[总结]
- offsetof与container_of宏[总结]
- offsetof与container_of宏[总结]
- offsetof与container_of宏[总结]
- offsetof与container_of宏[总结]
- offsetof与container_of宏[总结]
- offsetof与container_of宏[总结]
- Offsetof与container_of宏总结
- offsetof与container_of宏[总结]
- (转)offsetof与container_of宏[总结]
- offsetof与container_of宏
- offsetof与container_of宏
- offsetof宏与container_of宏
- [转载]offsetof与container_of宏
- offsetof和container_of总结
- 31.offsetof宏与container_of宏
- offsetof与container_of宏(个人理解)
- container_of,offsetof,与typeof
- Java并发编程:线程池的使用
- 【Android】BroadcastReceiver补充笔记
- 搭建PHP开发环境Windows环境(不包括PHP开发的IDE)
- 初接触Kinect v2
- Neural Network Sort
- offsetof与container_of宏[总结]
- MyBatis_输入映射
- Android活动中的启动模式
- BZOJ 1221: [HNOI2001] 软件开发|费用流
- Git简明教程
- C开源hash代码uthash的用法总结
- 数据库外键可以为空
- 深入分析JavaWeb Item46 -- Struts2数据校验与国际化
- linux空格符号