Re: fake loop

Richard B. Johnson (root@chaos.analogic.com)
Fri, 3 Aug 2001 12:44:00 -0400 (EDT)


On 3 Aug 2001, Andrey Ilinykh wrote:

> Hi!
> Very often I see in kernel such code as
> do {
> dosomthing();
> } while(0);
>
> or even
>
> #define prepare_to_switch() do { } while(0)
>
> Who can explain me a reason for these fake loops?
> Thank you,
> Andrey
>

Yes! This is neat! The { } defines a new program unit. Therefore,
you can allocate temporary variables within it, like:

do {
int i;
for(i=0; i< MAX; i++)
do_something(i);
} while (0);

The new "i" doesn't interfere with any previous one because it is
an independent program-unit and it executes just once.

Then, the second reason is also real neat.

#define foo do {printk(...);do_something;do_something_else;} while (0)

In the code, you can now do:

if(event)
foo;
else
bar;

Since the program unit contains its own {}, you don't have to worry
about what the "else" refers to, it always refers to if(event) even
if the "do while {} (0)" contains conditional statements.

FYI, it is an ISO C compatible construct, not a "gnuism". Therefore,
it is a safe and effective way to even make statements disappear
under certain conditions:

#ifdef __SMP__
#define foo something_that_exists_only_under_these_conditions()
#else
#define foo do { } while(0)
#

Cheers,
Dick Johnson

Penguin : Linux version 2.4.1 on an i686 machine (799.53 BogoMips).

I was going to compile a list of innovations that could be
attributed to Microsoft. Once I realized that Ctrl-Alt-Del
was handled in the BIOS, I found that there aren't any.

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