macro _set_base - "do - while(0)" question

Previous thread: [PATCH] sh: sh7712 defconfig by Andrew Murray on Wednesday, January 2, 2008 - 7:43 am. (1 message)

Next thread: Re: macro _set_base - "do - while(0)" question by Bodo Eggert on Wednesday, January 2, 2008 - 8:15 am. (1 message)
To: <linux-kernel@...>
Date: Wednesday, January 2, 2008 - 7:45 am

Hi,

In file include/asm-i386/system.h, _set_base and _set_limit use an
useless do ... while(0)

Why is this needed ?

exemple with _set_base from linux-2.6.23

#define _set_base(addr,base) do { unsigned long __pr; \
__asm__ __volatile__ ("movw %%dx,%1\n\t" \
"rorl $16,%%edx\n\t" \
"movb %%dl,%2\n\t" \
"movb %%dh,%3" \
:"=&d" (__pr) \
:"m" (*((addr)+2)), \
"m" (*((addr)+4)), \
"m" (*((addr)+7)), \
"0" (base) \
); } while(0)

--
Abdel
http://draconux.free.fr/
--

To: Abdel <draconux@...>
Cc: <linux-kernel@...>
Date: Wednesday, January 2, 2008 - 8:42 am

Hi Abdel :)

Google for "do while swallow semicolon". This looks like an useless and
weird construction but it is very useful when it comes to macros (you
can define variables inside the block and you can use your macro as any
other statement, it won't swallow the semicolon).

Raúl Núñez de Arenas Coronado
--
Linux Registered User 88736 | http://www.dervishd.net
It's my PC and I'll cry if I want to... RAmen!
We are waiting for 13 Feb 2009 23:31:30 +0000 ...
--

To: Abdel <draconux@...>
Cc: <linux-kernel@...>
Date: Wednesday, January 2, 2008 - 7:49 am

Without the do {} while (0)

then
1) you could not define the variable __pr
2) You would be fooled when you did:

if (foo != bar)
set_base(addr,base);

Sam
--

Previous thread: [PATCH] sh: sh7712 defconfig by Andrew Murray on Wednesday, January 2, 2008 - 7:43 am. (1 message)

Next thread: Re: macro _set_base - "do - while(0)" question by Bodo Eggert on Wednesday, January 2, 2008 - 8:15 am. (1 message)