Significance of do ... while (0)
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)Quoted 3 lines · krishna
Hi all,
Can any one explain the importance of do ... while (0)
http://www.kernelnewbies.org/faq/
Dave
Quoted 1 line · krishna
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
Thank you very much
Quoted 13 lines · Dave Jones
Quoted 3 lines · krishna
Hi all,
Can any one explain the importance of do ... while (0)
http://www.kernelnewbies.org/faq/
Dave
Quoted 5 lines
Quoted 1 line
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
Signature · 1 line
ENOSPC
Quoted 10 lines
Quoted 5 lines
Quoted 1 line
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%22Why? {} would suffice, no need to do{}while(0)
Quoted 9 lines
Jan Engelhardt
--
ENOSPC
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
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 niftyThe above would expand to:
if (some_condition) {
/* nefarious macro stuff here */
};
else
// do something niftyAnd that's not quite right.
-Vadim Lobanov
Quoted 13 lines
Quoted 3 lines
Quoted 2 lines
Quoted 1 line
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 niftyThe above would expand to:
Quoted 7 lines
if (some_condition) { /* nefarious macro stuff here */ }; else // do something niftyAnd 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
Signature · 1 line
ENOSPC