public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Significance of do ... while (0)
@ 2004-12-17  4:09 krishna
  2004-12-17  4:21 ` Dave Jones
  2004-12-17  4:29 ` Mitchell Blank Jr
  0 siblings, 2 replies; 7+ messages in thread
From: krishna @ 2004-12-17  4:09 UTC (permalink / raw)
  To: Linux Kernel

Hi all,

    Can any one explain the importance of do  ... while (0)

Regards,
Krishna Chaitanya

#define STATS_SET_HIGH(x)       do { if ((x)->num_active > (x)->high_mark) \
                                        (x)->high_mark = (x)->num_active; \
                                } while (0)
#define STATS_INC_ERR(x)        ((x)->errors++)
#define STATS_SET_FREEABLE(x, i) \
                                do { if ((x)->max_freeable < i) \
                                        (x)->max_freeable = i; \
                                } while (0)
                                                                                                                              
 
#define STATS_INC_ALLOCHIT(x)   atomic_inc(&(x)->allochit)
#define STATS_INC_ALLOCMISS(x)  atomic_inc(&(x)->allocmiss)
#define STATS_INC_FREEHIT(x)    atomic_inc(&(x)->freehit)
#define STATS_INC_FREEMISS(x)   atomic_inc(&(x)->freemiss)
#else
#define STATS_INC_ACTIVE(x)     do { } while (0)
#define STATS_DEC_ACTIVE(x)     do { } while (0)
#define STATS_INC_ALLOCED(x)    do { } while (0)
#define STATS_INC_GROWN(x)      do { } while (0)
#define STATS_INC_REAPED(x)     do { } while (0)
#define STATS_SET_HIGH(x)       do { } while (0)
#define STATS_INC_ERR(x)        do { } while (0)
#define STATS_SET_FREEABLE(x, i) \
                                do { } while (0)
                                                                                                                              
 
#define STATS_INC_ALLOCHIT(x)   do { } while (0)
#define STATS_INC_ALLOCMISS(x)  do { } while (0)
#define STATS_INC_FREEHIT(x)    do { } while (0)
#define STATS_INC_FREEMISS(x)   do { } while (0)


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

* Re: Significance of do ... while (0)
  2004-12-17  4:09 Significance of do ... while (0) krishna
@ 2004-12-17  4:21 ` Dave Jones
  2004-12-17  4:54   ` krishna
  2004-12-17  4:29 ` Mitchell Blank Jr
  1 sibling, 1 reply; 7+ messages in thread
From: Dave Jones @ 2004-12-17  4:21 UTC (permalink / raw)
  To: krishna; +Cc: Linux Kernel

On Fri, Dec 17, 2004 at 09:39:00AM +0530, krishna wrote:
 > Hi all,
 > 
 >    Can any one explain the importance of do  ... while (0)

http://www.kernelnewbies.org/faq/


		Dave


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

* Re: Significance of do ... while (0)
  2004-12-17  4:09 Significance of do ... while (0) krishna
  2004-12-17  4:21 ` Dave Jones
@ 2004-12-17  4:29 ` Mitchell Blank Jr
  2004-12-17  7:44   ` Jan Engelhardt
  1 sibling, 1 reply; 7+ messages in thread
From: Mitchell Blank Jr @ 2004-12-17  4:29 UTC (permalink / raw)
  To: krishna; +Cc: Linux Kernel

krishna wrote:
>    Can any one explain the importance of do  ... while (0)

It's a standard C programming practice; more info is available from
your local search engine:
  http://www.google.com/search?lr=&c2coff=1&q=%22while%280%29%22

-Mitch

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

* Re: Significance of do ... while (0)
  2004-12-17  4:21 ` Dave Jones
@ 2004-12-17  4:54   ` krishna
  0 siblings, 0 replies; 7+ messages in thread
From: krishna @ 2004-12-17  4:54 UTC (permalink / raw)
  To: Dave Jones; +Cc: Linux Kernel

Thank you very much

Dave Jones wrote:

>On Fri, Dec 17, 2004 at 09:39:00AM +0530, krishna wrote:
> > Hi all,
> > 
> >    Can any one explain the importance of do  ... while (0)
>
>http://www.kernelnewbies.org/faq/
>
>
>		Dave
>
>
>  
>

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

* Re: Significance of do ... while (0)
  2004-12-17  4:29 ` Mitchell Blank Jr
@ 2004-12-17  7:44   ` Jan Engelhardt
  2004-12-17  7:58     ` vlobanov
  0 siblings, 1 reply; 7+ messages in thread
From: Jan Engelhardt @ 2004-12-17  7:44 UTC (permalink / raw)
  To: Mitchell Blank Jr; +Cc: krishna, Linux Kernel

>>    Can any one explain the importance of do  ... while (0)
>
>It's a standard C programming practice; more info is available from
>your local search engine:
>  http://www.google.com/search?lr=&c2coff=1&q=%22while%280%29%22

Why? {} would suffice, no need to do{}while(0)



Jan Engelhardt
-- 
ENOSPC

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

* Re: Significance of do ... while (0)
  2004-12-17  7:44   ` Jan Engelhardt
@ 2004-12-17  7:58     ` vlobanov
  2004-12-17  8:06       ` Jan Engelhardt
  0 siblings, 1 reply; 7+ messages in thread
From: vlobanov @ 2004-12-17  7:58 UTC (permalink / raw)
  To: Jan Engelhardt; +Cc: Mitchell Blank Jr, krishna, Linux Kernel

> >>    Can any one explain the importance of do  ... while (0)
> >
> >It's a standard C programming practice; more info is available from
> >your local search engine:
> >  http://www.google.com/search?lr=&c2coff=1&q=%22while%280%29%22
>
> Why? {} would suffice, no need to do{}while(0)
>
>
>
> Jan Engelhardt
> --
> ENOSPC
> -
> 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/
>

Unless I'm mistaken, it won't work in all cases. Example:

    #define foo(...) { ... /* nefarious macro stuff here */ ... }

    if (some_condition)
        foo();
    else
        // do something nifty

The above would expand to:

    if (some_condition) {
        /* nefarious macro stuff here */
    };
    else
        // do something nifty

And that's not quite right.

-Vadim Lobanov

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

* Re: Significance of do ... while (0)
  2004-12-17  7:58     ` vlobanov
@ 2004-12-17  8:06       ` Jan Engelhardt
  0 siblings, 0 replies; 7+ messages in thread
From: Jan Engelhardt @ 2004-12-17  8:06 UTC (permalink / raw)
  Cc: Linux Kernel

>> >>    Can any one explain the importance of do  ... while (0)
>> >It's a standard C programming practice; more info is available from
>> Why? {} would suffice, no need to do{}while(0)
>
>Unless I'm mistaken, it won't work in all cases. Example:
>    #define foo(...) { ... /* nefarious macro stuff here */ ... }
>    if (some_condition)
>        foo();
>    else
>        // do something nifty
>
>The above would expand to:
>
>    if (some_condition) {
>        /* nefarious macro stuff here */
>    };
>    else
>        // do something nifty
>
>And that's not quite right.

So it's best to always make some braces around ifs :)
Would be consistent with longer-than-one-statement blocks too.

if(some_cond) {
	MY_MACRO
}



Jan Engelhardt
-- 
ENOSPC

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

end of thread, other threads:[~2004-12-17  8:07 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-12-17  4:09 Significance of do ... while (0) krishna
2004-12-17  4:21 ` Dave Jones
2004-12-17  4:54   ` krishna
2004-12-17  4:29 ` Mitchell Blank Jr
2004-12-17  7:44   ` Jan Engelhardt
2004-12-17  7:58     ` vlobanov
2004-12-17  8:06       ` Jan Engelhardt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox