LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)"
@ 2007-02-12 23:47 Joe Perches
  2007-02-13  0:20 ` Ben Nizette
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Joe Perches @ 2007-02-12 23:47 UTC (permalink / raw)
  To: linux-kernel

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)++)



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)"
  2007-02-12 23:47 Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)" Joe Perches
@ 2007-02-13  0:20 ` Ben Nizette
  2007-02-13  0:47   ` Joe Perches
  2007-02-13  7:37 ` Muli Ben-Yehuda
  2007-02-13  7:48 ` YOSHIFUJI Hideaki / 吉藤英明
  2 siblings, 1 reply; 12+ messages in thread
From: Ben Nizette @ 2007-02-13  0:20 UTC (permalink / raw)
  To: Joe Perches; +Cc: linux-kernel

Joe Perches wrote:
> 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)++)
> 

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.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)"
  2007-02-13  0:20 ` Ben Nizette
@ 2007-02-13  0:47   ` Joe Perches
  2007-02-13  4:19     ` Nick Piggin
  0 siblings, 1 reply; 12+ messages in thread
From: Joe Perches @ 2007-02-13  0:47 UTC (permalink / raw)
  To: Ben Nizette; +Cc: linux-kernel

On Tue, 2007-02-13 at 11:20 +1100, Ben Nizette wrote:
>   #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.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)"
  2007-02-13  0:47   ` Joe Perches
@ 2007-02-13  4:19     ` Nick Piggin
  2007-02-13  7:34       ` Joe Perches
  0 siblings, 1 reply; 12+ messages in thread
From: Nick Piggin @ 2007-02-13  4:19 UTC (permalink / raw)
  To: Joe Perches; +Cc: Ben Nizette, linux-kernel

Joe Perches wrote:
> On Tue, 2007-02-13 at 11:20 +1100, Ben Nizette wrote:
> 
>>  #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.

-- 
SUSE Labs, Novell Inc.
Send instant messages to your online friends http://au.messenger.yahoo.com 

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)"
  2007-02-13  4:19     ` Nick Piggin
@ 2007-02-13  7:34       ` Joe Perches
  2007-02-13  7:42         ` Nick Piggin
  0 siblings, 1 reply; 12+ messages in thread
From: Joe Perches @ 2007-02-13  7:34 UTC (permalink / raw)
  To: Nick Piggin; +Cc: Ben Nizette, linux-kernel

On Tue, 2007-02-13 at 15:19 +1100, Nick Piggin wrote:
> >>  #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)++)



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)"
  2007-02-12 23:47 Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)" Joe Perches
  2007-02-13  0:20 ` Ben Nizette
@ 2007-02-13  7:37 ` Muli Ben-Yehuda
  2007-02-13 16:28   ` Randy Dunlap
  2007-02-13  7:48 ` YOSHIFUJI Hideaki / 吉藤英明
  2 siblings, 1 reply; 12+ messages in thread
From: Muli Ben-Yehuda @ 2007-02-13  7:37 UTC (permalink / raw)
  To: Joe Perches; +Cc: linux-kernel

On Mon, Feb 12, 2007 at 03:47:50PM -0800, Joe Perches wrote:

> 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)++)

Could we please stop "improving" the C language? it has served us fine
so far.

Cheers,
Muli

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)"
  2007-02-13  7:34       ` Joe Perches
@ 2007-02-13  7:42         ` Nick Piggin
  2007-02-13 10:36           ` Bernd Petrovitsch
  0 siblings, 1 reply; 12+ messages in thread
From: Nick Piggin @ 2007-02-13  7:42 UTC (permalink / raw)
  To: Joe Perches; +Cc: Ben Nizette, linux-kernel

Joe Perches wrote:
> On Tue, 2007-02-13 at 15:19 +1100, Nick Piggin wrote:
> 
>>>> #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)++)

If you're going for consistency, then shouldn't this be
array_for_each_entry()?

> #define array_for_each_index(index, array) \
> 	for ((index) = 0; (index) < ARRAY_SIZE((array)); (index)++)

-- 
SUSE Labs, Novell Inc.
Send instant messages to your online friends http://au.messenger.yahoo.com 

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)"
  2007-02-12 23:47 Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)" Joe Perches
  2007-02-13  0:20 ` Ben Nizette
  2007-02-13  7:37 ` Muli Ben-Yehuda
@ 2007-02-13  7:48 ` YOSHIFUJI Hideaki / 吉藤英明
  2 siblings, 0 replies; 12+ messages in thread
From: YOSHIFUJI Hideaki / 吉藤英明 @ 2007-02-13  7:48 UTC (permalink / raw)
  To: joe; +Cc: linux-kernel, yoshfuji

In article <1171324070.1528.25.camel@localhost> (at Mon, 12 Feb 2007 15:47:50 -0800), Joe Perches <joe@perches.com> says:

> 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)++)

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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)"
  2007-02-13  7:42         ` Nick Piggin
@ 2007-02-13 10:36           ` Bernd Petrovitsch
  2007-02-13 10:54             ` Nick Piggin
  0 siblings, 1 reply; 12+ messages in thread
From: Bernd Petrovitsch @ 2007-02-13 10:36 UTC (permalink / raw)
  To: Nick Piggin; +Cc: Joe Perches, Ben Nizette, linux-kernel

On Tue, 2007-02-13 at 18:42 +1100, Nick Piggin wrote:
> Joe Perches wrote:
[...]
> > 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.

> > #define array_for_each_index(index, array) \
> > 	for ((index) = 0; (index) < ARRAY_SIZE((array)); (index)++)

	Bernd
-- 
Firmix Software GmbH                   http://www.firmix.at/
mobil: +43 664 4416156                 fax: +43 1 7890849-55
          Embedded Linux Development and Services


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)"
  2007-02-13 10:36           ` Bernd Petrovitsch
@ 2007-02-13 10:54             ` Nick Piggin
  2007-02-13 11:07               ` Bernd Petrovitsch
  0 siblings, 1 reply; 12+ messages in thread
From: Nick Piggin @ 2007-02-13 10:54 UTC (permalink / raw)
  To: Bernd Petrovitsch; +Cc: Joe Perches, Ben Nizette, linux-kernel

Bernd Petrovitsch wrote:
> On Tue, 2007-02-13 at 18:42 +1100, Nick Piggin wrote:
> 
>>Joe Perches wrote:
> 
> [...]
> 
>>>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.

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.

-- 
SUSE Labs, Novell Inc.
Send instant messages to your online friends http://au.messenger.yahoo.com 

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)"
  2007-02-13 10:54             ` Nick Piggin
@ 2007-02-13 11:07               ` Bernd Petrovitsch
  0 siblings, 0 replies; 12+ messages in thread
From: Bernd Petrovitsch @ 2007-02-13 11:07 UTC (permalink / raw)
  To: Nick Piggin; +Cc: Joe Perches, Ben Nizette, linux-kernel

On Tue, 2007-02-13 at 21:54 +1100, Nick Piggin wrote:
> Bernd Petrovitsch wrote:
> > On Tue, 2007-02-13 at 18:42 +1100, Nick Piggin wrote:
> > 
> >>Joe Perches wrote:
> > 
> > [...]
> > 
> >>>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.
> 
> I don't follow.

Yes, thinko on my side. Sorry.

> 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?

The wrong logic that list_for_each gives an entry. Sorry f.t. confusion.

	Bernd
-- 
Firmix Software GmbH                   http://www.firmix.at/
mobil: +43 664 4416156                 fax: +43 1 7890849-55
          Embedded Linux Development and Services


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)"
  2007-02-13  7:37 ` Muli Ben-Yehuda
@ 2007-02-13 16:28   ` Randy Dunlap
  0 siblings, 0 replies; 12+ messages in thread
From: Randy Dunlap @ 2007-02-13 16:28 UTC (permalink / raw)
  To: Muli Ben-Yehuda; +Cc: Joe Perches, linux-kernel

On Tue, 13 Feb 2007 09:37:38 +0200 Muli Ben-Yehuda wrote:

> On Mon, Feb 12, 2007 at 03:47:50PM -0800, Joe Perches wrote:
> 
> > 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)++)
> 
> 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 ***

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2007-02-13 16:33 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-02-12 23:47 Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)" Joe Perches
2007-02-13  0:20 ` Ben Nizette
2007-02-13  0:47   ` Joe Perches
2007-02-13  4:19     ` Nick Piggin
2007-02-13  7:34       ` Joe Perches
2007-02-13  7:42         ` Nick Piggin
2007-02-13 10:36           ` Bernd Petrovitsch
2007-02-13 10:54             ` Nick Piggin
2007-02-13 11:07               ` Bernd Petrovitsch
2007-02-13  7:37 ` Muli Ben-Yehuda
2007-02-13 16:28   ` Randy Dunlap
2007-02-13  7:48 ` YOSHIFUJI Hideaki / 吉藤英明

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).