Re: asm/unistd.h

Bart Trojanowski (bart@jukie.net)
Thu, 5 Apr 2001 09:06:20 -0400 (EDT)


On Thu, 5 Apr 2001, Sardaņons, Eliel wrote:

> I'm taking a look at the linux code and I don't understand how do you
> programm...mmm (?) may be i'm a stupid why in include/asm/unistd.h in some
> macros you use this:
>
> do {
> ...
> } while (0)

This is a very useful trick.

If you define a macro such as:

#define foo(x) do_one(x); do_two(x); do_three(x)

you may later be compelled to call it in an if statement:

if(condition)
foo(something);

note that it would not do what you want as it will only execute do_one(x)
conditionally but do_two(x) and do_three(x) would always be done.

However a do{ ... } while(0) is a nice convenient block which is
guaranteed to execute once. The compiler (with a -O flag) will not
generate any extra code for the do{}while(0) so its just a macro building
aid.

So you ask: "why not just use a { ... } to define a macro". I don't
remember the case for this but I know it's there. It has to do with a
complicated if/else structure where a simple {} breaks.

Cheers,
Bart.

-- 
	WebSig: http://www.jukie.net/~bart/sig/

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