linux内核学习笔记之——list_for_each_entry
2012-02-27 19:32
441 查看
在Linux内核源码中,经常要对链表进行操作,其中一个很重要的宏是list_for_each_entry:
意思大体如下:
假设只有两个结点,则第一个member代表head,
list_for_each_entry的作用就是循环遍历每一个pos中的member子项。
如下图所示:
宏list_for_each_entry:
list_entry((head)->next, typeof(*pos), member)返回(head)->next物理指针所处位置向前减去offsetof()个字节数据之后, 其父变量pos的物理地址,父变量的类型在编译时由typeof(*pos)自动返回.
所以list_for_each_entry遍历head下面挂接的类型为typeof(*pos)的childs结构体们,当然每个child结构体包含struct list_head node之类相似的双向链表list_head类型项,就这样通过循环pos将依次指向双向链表上的各个child.(member就是child类型中被定义的变量名)
其中用到了函数list_entry():
这个函数的作用在图1中表示就是可以通过已知的指向member子项的指针,获得整个结构体的指针(地址)
和函数prefetch:
prefetch的含义是告诉cpu那些元素有可能马上就要用到,告诉cpu预取一下,这样可以提高速度
其中用到了函数container_of():
转自:http://bbs.chinaunix.net/thread-1981115-1-1.html
意思大体如下:
假设只有两个结点,则第一个member代表head,
list_for_each_entry的作用就是循环遍历每一个pos中的member子项。
如下图所示:
宏list_for_each_entry:
/** * 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))
list_entry((head)->next, typeof(*pos), member)返回(head)->next物理指针所处位置向前减去offsetof()个字节数据之后, 其父变量pos的物理地址,父变量的类型在编译时由typeof(*pos)自动返回.
所以list_for_each_entry遍历head下面挂接的类型为typeof(*pos)的childs结构体们,当然每个child结构体包含struct list_head node之类相似的双向链表list_head类型项,就这样通过循环pos将依次指向双向链表上的各个child.(member就是child类型中被定义的变量名)
其中用到了函数list_entry():
这个函数的作用在图1中表示就是可以通过已知的指向member子项的指针,获得整个结构体的指针(地址)
/** * 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)
和函数prefetch:
#define prefetch(x) __builtin_prefetch(x)其中用到了builtin_prefetch:
prefetch的含义是告诉cpu那些元素有可能马上就要用到,告诉cpu预取一下,这样可以提高速度
其中用到了函数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) );})
转自:http://bbs.chinaunix.net/thread-1981115-1-1.html
相关文章推荐
- linux内核学习笔记之——list_for_each_entry
- linux内核学习笔记之——list_for_each_entry
- linux内核学习笔记之——list_for_each_entry
- linux内核学习笔记之——list_for_each_entry
- linux内核学习list_for_each_entry(转)
- linux内核学习笔记之——list_for_each_entry
- Linux内核中的list_for_each_entry
- Linux内核中list_head、list_for_each、list_entry、container_of之间的关系
- C++ for_each学习笔记
- list_for_each_entry_continue()与list_for_each_entry_from()的区别
- Java学习笔记:for-each增强for循环
- list_for_each_entry
- 关于container_of和list_for_each_entry 及其相关函数的分析
- list_entry和list_for_each_entry
- list_for_each_entry分析
- Linux内核学习之list_entry --- 用C语言实现泛型编程
- linux核list_for_each_entry(pos, head, member)分析
- List for each entry
- 详解Linux内核之双向循环链表(2) list_for_each/list_entry/list_for_each_entry
- 关于内核中此宏list_for_each_entry的说明