概述
在内核编程中经常会遇到list_for_each_entry(pos, head, member),今天深入分析一下list_for_each_entry在内核中是如何定义的。
list_for_each_entry主要作用是遍历给定类型的链表,被定义在include/linux/list.h中:
/**
* 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);
&pos->member != (head);
pos = list_entry(pos->member.next, typeof(*pos), member))
接着需要理解list_entry,list_entry的作用是获取条目的结构类型,同样被定义在include/linux/list.h中:
/**
* 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)
container_of作用是通过结构体成员变量的地址和结构体成员变量名以及结构体类型获取该结构体变量的地址,被定义在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) );})
const typeof( ((type *)0)->member ) *__mptr = (ptr);
typeof是c 语言的一个新拓展,有多重用法,主要用于得到其参数的类型,参数可以是表达式,函数,类型等,这里表示获取成员member的类型。
这里定义的局部指针变量__mptr被赋值为member的地址。
(type )( (char )__mptr - offsetof(type,member) );
(char *)__mptr:将__mptr转换为字节类型地址。
offsetof(type, member)的作用是返回member在type中的偏移,其在include/linux/stddef.h中的定义如下:
#ifdef __compiler_offsetof
#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
#else
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif
用实际的结构体成员变量的地址减去成员变量在结构体中的偏移地址,自然就得到该结构体变量的起始地址,最终得到(type*)类型的指针。
最后
以上就是甜蜜蛋挞为你收集整理的list_for_each_entry深入理解的全部内容,希望文章能够帮你解决list_for_each_entry深入理解所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复