public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* macro _set_base - "do - while(0)" question
@ 2008-01-02 11:45 Abdel
  2008-01-02 11:49 ` Sam Ravnborg
  2008-01-02 12:42 ` DervishD
  0 siblings, 2 replies; 5+ messages in thread
From: Abdel @ 2008-01-02 11:45 UTC (permalink / raw)
  To: linux-kernel

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/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: macro _set_base - "do - while(0)" question
  2008-01-02 11:45 macro _set_base - "do - while(0)" question Abdel
@ 2008-01-02 11:49 ` Sam Ravnborg
  2008-01-02 12:42 ` DervishD
  1 sibling, 0 replies; 5+ messages in thread
From: Sam Ravnborg @ 2008-01-02 11:49 UTC (permalink / raw)
  To: Abdel; +Cc: linux-kernel

On Wed, Jan 02, 2008 at 12:45:39PM +0100, Abdel wrote:
> 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)
> 
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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: macro _set_base - "do - while(0)" question
       [not found] <9HagT-66H-21@gated-at.bofh.it>
@ 2008-01-02 12:15 ` Bodo Eggert
  0 siblings, 0 replies; 5+ messages in thread
From: Bodo Eggert @ 2008-01-02 12:15 UTC (permalink / raw)
  To: Abdel, linux-kernel

Abdel <draconux@gmail.com> wrote:

> In file include/asm-i386/system.h,  _set_base and _set_limit use an
> useless do ... while(0)
> 
> Why is this needed ?

http://kernelnewbies.org/FAQ/DoWhile0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: macro _set_base - "do - while(0)" question
  2008-01-02 11:45 macro _set_base - "do - while(0)" question Abdel
  2008-01-02 11:49 ` Sam Ravnborg
@ 2008-01-02 12:42 ` DervishD
  1 sibling, 0 replies; 5+ messages in thread
From: DervishD @ 2008-01-02 12:42 UTC (permalink / raw)
  To: Abdel; +Cc: linux-kernel

Hi Abdel :)

* Abdel <draconux@gmail.com> dixit:
> In file include/asm-i386/system.h,  _set_base and _set_limit use an
> useless do ... while(0)
> 
> Why is this needed ?

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: macro _set_base - "do - while(0)" question
@ 2008-01-02 13:22 Abdel
  0 siblings, 0 replies; 5+ messages in thread
From: Abdel @ 2008-01-02 13:22 UTC (permalink / raw)
  To: linux-kernel

2008/1/2, DervishD <lkml@dervishd.net>:

>    * Abdel <draconux@gmail.com> dixit:
>    > In file include/asm-i386/system.h,  _set_base and _set_limit use an
>    > useless do ... while(0)
>    >
>    > Why is this needed ?
>
>    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).


thanks to all for your responses :)

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2008-01-02 13:22 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-01-02 11:45 macro _set_base - "do - while(0)" question Abdel
2008-01-02 11:49 ` Sam Ravnborg
2008-01-02 12:42 ` DervishD
     [not found] <9HagT-66H-21@gated-at.bofh.it>
2008-01-02 12:15 ` Bodo Eggert
  -- strict thread matches above, loose matches on Subject: below --
2008-01-02 13:22 Abdel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox