/*refer to http://blog.csdn.net/yinkaizhong/article/details/4093795 */ [root@localhost chapter15]# cat test.c #include<stdio.h> struct student{ char name[20]; char sex; }stu={"zhangsan",'m'}; main() { struct student *stu_ptr; //存储container_of宏的返回值 int offset; //存储offsetof宏的返回值 //下面三行代码等同于 container_of(&stu.sex,struct student, sex )参数带入的情形 const typeof(((struct student*)0)->sex) *_mptr = &stu.sex; //首先定义一个 _mptr指针, 类型为struct student结构体中sex成员的类型 //typeof 为获取(((struct student*)0)->sex)的类型,此处此类型为char //((struct student*)0)在offsetof处讲解 offset = (int)(&((struct student *)0)->sex); /*((struct student*)0)为 把 0地址 强制转化为指向student结构体类型的指针 该指针从地址 0 开始的 21个字节用来存放name 与 sex(char name〔20〕与 char sex共21字节) sex存放在第20个字节出(从0字节开始) &((struct student *)0)->sex 取出sex地址(此处即为20) 并强制转化为整形 所以offset为20,后面的printf结果将证明这一点*/ stu_ptr = (struct student *)((char*)_mptr - offset); /*((char*)_mptr - offset)此处先把_mptr指针转化为字符形指针 (为什么这么做呢? 如果_mptr为整形指针 _mptr - offset 相当于减去 sizeof(int)*offset个字节) 减去 offset值 相当于 得到_mptr所在结构体的首地址(即stu的地址) 然后我们把 该地址 强制转化为 struct student类型即可正常使用了*/ printf("offset of stu.sex = %d\n",offset); printf("stu_ptr->name:%s\nstu_ptr->sex:%c\n", stu_ptr->name, stu_ptr->sex); return 0; } [root@localhost chapter15]#
[root@localhost chapter15]# ./test offset of stu.sex = 20 stu_ptr->name:zhangsan stu_ptr->sex:m
/**********include/linux/kernel.h******************/ #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );}) /**********include/linux/stddef.h*****************/ #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)主要就是取offset这一步,利用了借助将0强制转化为student指针类型,
给定
结构体的一个成员的地址,
此成员在此结构体中名字,
此结构体类型
可以找到此结构体的地址
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(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );})
/* * Simple doubly linked list implementation. * * Some of the internal functions ("__xxx") are useful when * manipulating whole lists rather than single entries, as * sometimes we already know the next/prev entries and we can * generate better code by using them directly rather than * using the generic single-entry routines. */ struct list_head { struct list_head *next, *prev; };
/** * list_entry - get the struct for this entry * @ptr: the &struct list_head pointer. * @type: the type of the struct this is embedded in. * @member: the name of the list_struct within the struct. */ #define list_entry(ptr, type, member) \ container_of(ptr, type, member)
/** * list_for_each - iterate over a list * @pos: the &struct list_head to use as a loop cursor. * @head: the head for your list. */ #define list_for_each(pos, head) \ for (pos = (head)->next; prefetch(pos->next), pos != (head); \ pos = pos->next)
/** * list_for_each_entry - iterate over list of given type * @pos: the type * to use as a loop cursor. * @head: the head for your list. * @member: the name of the list_struct within the struct. */ #define list_for_each_entry(pos, head, member) \ for (pos = list_entry((head)->next, typeof(*pos), member); \ prefetch(pos->member.next), &pos->member != (head); \ pos = list_entry(pos->member.next, typeof(*pos), member))