list_for_each_entry
PATCH 1/1 list_for_each_entry
Using two variables all the time is pissing me off:
struct list_head *i;
list_for_each(i, &head) {
struct foo *f = list_entry(i, struct foo, list);
...
}Much nicer is:
struct foo *f;
list_for_each_entry(f, &head, list) {
...
}Asm code produced is identical.
Cheers,
Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
Name: list_for_each_entry patch
Author: Rusty Russell
Status: Trivial
D: This adds list_for_each_entry, which is the equivalent of
D: list_for_each and list_entry, except only one variable is needed.
include/linux/list.h +12 -0
@@ -171,6 +171,18 @@ static __inline__ void list_splice(strucfor (pos = (head)->prev,prefetch (pos->prev); pos != (head); \ pos = pos->prev,prefetch (pos->prev)) +/** +* list_for_each_entry - iterate over list of given type +* @pos: the type * to use as a loop counter. +* @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), \ + prefetch(pos->member.next))#endif /* __KERNEL__ || _LVM_H_INCLUDE */