2001-11-22 17:07:15

by Kristian Hogsberg

[permalink] [raw]
Subject: Combining list_for_each and list_entry

Hi,

Everywhere in the kernel where list_for_each is used, you typically
find a list_entry as the first statement in the loop body, e.g.:

list_for_each(tmp, &runqueue_head) {
p = list_entry(tmp, struct task_struct, run_list);
if (can_schedule(p, this_cpu)) {
int weight = goodness(p, this_cpu, prev->active_mm);
if (weight > c)
c = weight, next = p;
}
}

I came up with the following idea to combine the two:

#define list_iterate(lh, head, elm, link) \
for (lh = (head)->next; \
lh != (head) && \
(elm = list_entry(lh, typeof(*elm), link), lh = lh->next, 1);)

Now you can say

list_iterate(tmp, &runqueue_head, p, run_list) {
if (can_schedule(p, this_cpu)) {
int weight = goodness(p, this_cpu, prev->active_mm);
if (weight > c)
c = weight, next = p;
}
}

and since lh is update at the beginning of the loop, list_iterate is
automatically safe, i.e. you can remove the current element while
looping.

How about it?

regards,
Kristian