Re: fake loop

Crutcher Dunnavant (crutcher@datastacks.com)
Fri, 3 Aug 2001 12:13:28 -0400


++ 03/08/01 09:57 -0600 - Andrey Ilinykh:
> Hi!
> Very often I see in kernel such code as
> do {
> dosomthing();
> } while(0);

This is a fake function. Macros often use this to give themselves an environment
to allocate stack variables in. Example:

#define swap(A, B) \
{ \
int C; \
C = (A); (A) = (B); (B) = C; \
} while(0)

swap will now act almost exactly like a function. Please note that this macro
does NOT have a semicolon on the end, as that would cause bad things if I did:

if (test)
swap(a,b);
else
do_something();

> or even
> #define prepare_to_switch() do { } while(0)

This is most often found in situations where a code block becomes trivial when
a config option is turned off. The spinlock functions mostly become this when
SMP is not turned on, so they get optimized out of the code.

-- 
Crutcher        <crutcher@datastacks.com>
GCS d--- s+:>+:- a-- C++++$ UL++++$ L+++$>++++ !E PS+++ PE Y+ PGP+>++++
    R-(+++) !tv(+++) b+(++++) G+ e>++++ h+>++ r* y+>*$
-
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/