Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)"
Now that most of the sizeof(array)/sizeof(array[0]) conversions have been done (there are about 800 done and about another 130 left), perhaps it could be useful to change the code to use a define similar to the list_for_each
#define list_for_each(pos, head) \
for (pos = (head)->next; prefetch(pos->next), pos != (head); \
pos = pos->next)perhaps
#define array_for_each(index, array) \ for ((index) = 0; (index) < ARRAY_SIZE((array)); (index)++)
Quoted 6 lines · Joe Perches
Now that most of the sizeof(array)/sizeof(array[0]) conversions have been done (there are about 800 done and about another 130 left), perhaps it could be useful to change the code to use a define similar to the
list_for_each
Quoted 9 lines
#define list_for_each(pos, head) \ for (pos = (head)->next; prefetch(pos->next), pos != (head); \ pos = pos->next)perhaps
#define array_for_each(index, array) \ for ((index) = 0; (index) < ARRAY_SIZE((array)); (index)++)
I like the idea, my only concern would be potential confusion. That is, the list_for_each macro sets pos to each list_head in turn where array_for_each just sets the index /in to/ the array. While I think the way you have is nicer, for compatibility between the two styles maybe something more like
#define array_for_each(element, array) \ for (int __idx = 0; __idx < ARRAY_SIZE((array)); \ __idx++, (element) = &(array[__idx]))
would help. Of course the other option is to name array_for_each something different to avoid comparisons with list_for_each.
--Ben.
Quoted 3 lines · Ben Nizette
#define array_for_each(element, array) \ for (int __idx = 0; __idx < ARRAY_SIZE((array)); \ __idx++, (element) = &(array[__idx]))
This requires all interior loop code be changed.
Quoted 8 lines · Joe Perches
Quoted 3 lines · Ben Nizette
#define array_for_each(element, array) \ for (int __idx = 0; __idx < ARRAY_SIZE((array)); \ __idx++, (element) = &(array[__idx]))This requires all interior loop code be changed.
Ben is right though. Making this thing confusing to use is going to be worse than sticking with the very simple and unconfusing loops.
If you really wanted to introduce your loop, then please call it array_for_each_idx, or something to distinguish it.
Signature · 2 lines
SUSE Labs, Novell Inc. Send instant messages to your online friends http://au.messenger.yahoo.com
Quoted 5 lines · Nick Piggin
Quoted 3 lines
Quoted 3 lines
#define array_for_each(element, array) \ for (int __idx = 0; __idx < ARRAY_SIZE((array)); \ __idx++, (element) = &(array[__idx]))If you really wanted to introduce your loop, then please call it
array_for_each_idx, or something to distinguish it.
perhaps:
#define array_for_each(element, array) \ for ((element) = (array); \ (element) < ((array) + ARRAY_SIZE((array))); \ (element)++)
#define array_for_each_index(index, array) \ for ((index) = 0; (index) < ARRAY_SIZE((array)); (index)++)
Quoted 5 lines · Joe Perches
Now that most of the sizeof(array)/sizeof(array[0]) conversions have been done (there are about 800 done and about another 130 left), perhaps it could be useful to change the code to use a define similar to the
list_for_each
Quoted 8 lines
#define list_for_each(pos, head) \ for (pos = (head)->next; prefetch(pos->next), pos != (head); \ pos = pos->next)perhaps
#define array_for_each(index, array) \ for ((index) = 0; (index) < ARRAY_SIZE((array)); (index)++)
Could we please stop "improving" the C language? it has served us fine so far.
Cheers,
Muli
Quoted 10 lines · Joe Perches
Quoted 6 lines · Nick Piggin
Quoted 3 lines
Quoted 3 lines
#define array_for_each(element, array) \ for (int __idx = 0; __idx < ARRAY_SIZE((array)); \ __idx++, (element) = &(array[__idx]))If you really wanted to introduce your loop, then please call it
array_for_each_idx, or something to distinguish it.
Quoted 6 lines
perhaps:
#define array_for_each(element, array) \ for ((element) = (array); \ (element) < ((array) + ARRAY_SIZE((array))); \ (element)++)
If you're going for consistency, then shouldn't this be array_for_each_entry()?
Quoted 2 lines
#define array_for_each_index(index, array) \ for ((index) = 0; (index) < ARRAY_SIZE((array)); (index)++)
Signature · 2 lines
SUSE Labs, Novell Inc. Send instant messages to your online friends http://au.messenger.yahoo.com
In article <1171324070.1528.25.camel@localhost> (at Mon, 12 Feb 2007 15:47:50 -0800), Joe Perches <[email protected]> says:
Quoted 6 lines
Now that most of the sizeof(array)/sizeof(array[0]) conversions have been done (there are about 800 done and about another 130 left), perhaps it could be useful to change the code to use a define similar to the
list_for_each
Quoted 8 lines
#define list_for_each(pos, head) \ for (pos = (head)->next; prefetch(pos->next), pos != (head); \ pos = pos->next)perhaps
#define array_for_each(index, array) \ for ((index) = 0; (index) < ARRAY_SIZE((array)); (index)++)
I dislike this; it is overkill.
list_for_each etc. are for list_head etc., of structures. On the other hand, arrays are not.
It is very, very obvious how to access its members.
--yoshfuji
Quoted 1 line · Nick Piggin
Joe Perches wrote:
[...]
Quoted 9 lines
Quoted 6 lines
perhaps:
#define array_for_each(element, array) \ for ((element) = (array); \ (element) < ((array) + ARRAY_SIZE((array))); \ (element)++)If you're going for consistency, then shouldn't this be
array_for_each_entry()?
That depends on the decision between consistency to array_for_each_index or consistency to list_for_each.
Quoted 2 lines
Quoted 2 lines
#define array_for_each_index(index, array) \ for ((index) = 0; (index) < ARRAY_SIZE((array)); (index)++)
Bernd
Signature · 3 lines
Firmix Software GmbH http://www.firmix.at/
mobil: +43 664 4416156 fax: +43 1 7890849-55
Embedded Linux Development and ServicesQuoted 13 lines · Bernd Petrovitsch
Quoted 1 line · Nick Piggin
Joe Perches wrote:
[...]
Quoted 7 lines
Quoted 6 lines
perhaps:
#define array_for_each(element, array) \ for ((element) = (array); \ (element) < ((array) + ARRAY_SIZE((array))); \ (element)++)
Quoted 6 lines
Quoted 2 lines
If you're going for consistency, then shouldn't this be
array_for_each_entry()?That depends on the decision between consistency to
array_for_each_indexor consistency tolist_for_each.
I don't follow.
list_for_each gives you a list_head.list_for_each_entry gives you a pointer to an entry in the list, which is equivalent to the above loop which gives a pointer to an entry in the array. Accordingly, it should be called array_for_each_entry. What sort of logic leads to another conclusion?
array_for_each_index gives an index into the array.
I offer no opinion on the merit of such macros, just their names.
Signature · 2 lines
SUSE Labs, Novell Inc. Send instant messages to your online friends http://au.messenger.yahoo.com
Quoted 14 lines · Nick Piggin
Quoted 13 lines · Bernd Petrovitsch
Quoted 1 line · Nick Piggin
Joe Perches wrote:
[...]
Quoted 7 lines
Quoted 6 lines
perhaps:
#define array_for_each(element, array) \ for ((element) = (array); \ (element) < ((array) + ARRAY_SIZE((array))); \ (element)++)
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
If you're going for consistency, then shouldn't this be
array_for_each_entry()?That depends on the decision between consistency to
array_for_each_indexor consistency tolist_for_each.I don't follow.
Yes, thinko on my side. Sorry.
Quoted 5 lines
list_for_eachgives you alist_head.list_for_each_entrygives you a pointer to an entry in the list, which is equivalent to the above loop which gives a pointer to an entry in the array. Accordingly, it should be calledarray_for_each_entry. What sort of logic leads to another conclusion?
The wrong logic that list_for_each gives an entry. Sorry f.t. confusion.
Bernd
Signature · 3 lines
Firmix Software GmbH http://www.firmix.at/
mobil: +43 664 4416156 fax: +43 1 7890849-55
Embedded Linux Development and ServicesQuoted 11 lines · On Tue, 13 Feb 2007 09:37:38 +0200 Muli Ben-Yehuda wrote:
Quoted 9 lines · Joe Perches
Now that most of the sizeof(array)/sizeof(array[0]) conversions have been done (there are about 800 done and about another 130 left), perhaps it could be useful to change the code to use a define similar to the
list_for_each#define list_for_each(pos, head) \ for (pos = (head)->next; prefetch(pos->next), pos != (head); \ pos = pos->next)
Quoted 7 lines
Quoted 4 lines
perhaps
#define array_for_each(index, array) \ for ((index) = 0; (index) < ARRAY_SIZE((array)); (index)++)Could we please stop "improving" the C language? it has served us fine so far.
I'm with Muli. The open-coded for loop is fine (using ARRAY_SIZE).
---
~Randy
*** Remember to use Documentation/SubmitChecklist when testing your code ***