lkml.org 
[lkml]   [2001]   [Nov]   [22]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
SubjectCombining list_for_each and list_entry
From
Date
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/

\
 
 \ /
  Last update: 2005-03-22 13:13    [W:0.044 / U:0.808 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site