今天看Linux内核代码,看到了list_for_each_safe()函数,想仔细的研究一下。
下面是源代码:
list_for_each()的定义:
/**
* 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; pos != (head); pos = pos->next)
list_for_each_safe()的定义:
/**
* list_for_each_safe - iterate over a list safe against removal of list entry
* @pos: the &struct list_head to use as a loop cursor.
* @n: another &struct list_head to use as temporary storage
* @head: the head for your list.
*/
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
下面主要是介绍一下他们的区别:
我们