Combining list_for_each and list_entry

From: Kristian Hogsberg (hogsberg@users.sourceforge.net)
Date: Thu Nov 22 2001 - 12:06:29 EST


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

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Fri Nov 23 2001 - 21:00:30 EST