Chinaunix
标题:
list_for_each_entry_safe的safe体现在哪
[打印本页]
作者:
amarant
时间:
2011-03-24 15:30
标题:
list_for_each_entry_safe的safe体现在哪
下面是两个宏,搞不明白为什么第二个是“safe“的
/**
* 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_for_each_entry_safe - iterate over list of given type safe against removal of list entry
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member), \
n = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, typeof(*n), member))
复制代码
作者:
Godbach
时间:
2011-03-24 15:36
这两个宏定义有明显的区别啊。
比如,如果你想一边遍历链表,一边遇到符合条件节点进行删除,你觉得第一个可以满足需求吗
作者:
amarant
时间:
2011-03-24 15:37
是不是第二个是为了在操作完以后可以移去这个表头?
作者:
Godbach
时间:
2011-03-24 16:29
是不是第二个是为了在操作完以后可以移去这个表头?
amarant 发表于 2011-03-24 15:37
如果我用第 1 个遍历链表,中间删除了某个节点,并 free 了,你觉得还能够继续下一个节点的遍历吗?
作者:
amarant
时间:
2011-03-24 17:34
回复
4#
Godbach
恩 明白了~ 怎么刚才看的时候就傻傻的一直没明白呢。(好简单的问题。。)
作者:
Anzyfly
时间:
2011-03-24 21:56
呵呵,这个问题我也一直没有搞明白。正好路过听听{:3_189:}
作者:
Godbach
时间:
2011-03-24 21:58
实际使用一下,印象就会很深刻了
作者:
cluter
时间:
2011-03-25 19:39
貌似 英文注释很清晰啊
作者:
Anzyfly
时间:
2017-10-15 16:50
确实啊,用第一个遍历函数只会将头节点后的第一个节点删除掉,后面的是不会被遍历删除掉的;只有用第二个函数才能够实现循环遍历哈希链表并删除各节点的功能;
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2