// @ptr : the list head to take the element from. #define list_first_entry(ptr, type, member) \ list_entry((ptr)->next, type, member)
// @ptr : the list head to take the element from. #define list_last_entry(ptr, type, member) \ list_entry((ptr)->prev, type, member)
/** * list_next_entry - get the next element in list * @pos: the type * to cursor * @member: the name of the list_head within the struct. */ #define list_next_entry(pos, member) \ list_entry((pos)->member.next, typeof(*(pos)), member)
替换
遍历
直接遍历struct list_head 节点:
1 2 3 4 5 6 7 8 9 10 11 12
/** * 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)
// @n: another &struct list_head to use as temporary storage #define list_for_each_safe(pos, n, head) \ for (pos = (head)->next, n = pos->next; pos != (head); \ pos = n, n = 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_head within the struct. */ #define list_for_each_entry(pos, head, member) \ for (pos = list_first_entry(head, typeof(*pos), member); \ &pos->member != (head); \ pos = list_next_entry(pos, member))