Re: generic swap()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 1/21/09, debian developer <debiandev@xxxxxxxxx> wrote:
> I don't seem to understand this patch. What is swap(a, b) returning here?
> and where is it returning from?

It is not "returning" but substituting, cause it's a macro, not a
function. It swaps the values of "a" and "b".

>
> What advantage does using a do{}while(0); have on this?

Because the macro is formed by more than a line, the do-while(0)
thing, prevents from errors when using something like this:

if(condition)
  swap(a,b)

With do-while, even in the case of the missing "{" and "}" _all_ the
lines in the macro will be executed if "condition" is true.

>
> On Fri, Jan 16, 2009 at 3:15 PM, Peter Zijlstra <peterz@xxxxxxxxxxxxx>
> wrote:
> >
> > On Fri, 2009-01-16 at 01:38 -0800, Andrew Morton wrote:
> > > On Fri, 16 Jan 2009 10:26:34 +0100 Peter Zijlstra <peterz@xxxxxxxxxxxxx>
> wrote:
> > >
> > > > Hi,
> > > >
> > > > Where did this patch come from? -- I can't seem to find it on lkml at
> all..
> > >
> > > That's a damn good question.  I received:
> >
> > Hehe, one of the mysteries of life then :-)
> >
> >
> > > > The reason I ask it that I wonder why swap() has a return value?
> > >
> > > Accident, I guess.
> > >
> > > > +#define swap(a, b) ({ typeof(a) __tmp = (a); (a) = (b); (b) = __tmp;
> })
> > >
> > > You think it should use the do{}while(0) thing?
> >
> > That was what I was thinking indeed. Non of the current users appear to
> > make use of the (somewhat arbitrary) return value, so lets change it
> > before someone gets creative ;-)
> >
> > Signed-off-by: Peter Zijlstra <a.p.zijlstra@xxxxxxxxx>
> > ---
> >
> > diff --git a/include/linux/kernel.h b/include/linux/kernel.h
> > index 343df9e..7fa3718 100644
> >
> > --- a/include/linux/kernel.h
> > +++ b/include/linux/kernel.h
> > @@ -480,7 +480,8 @@ static inline char *pack_hex_byte(char *buf, u8 byte)
> >  /*
> >
> >  * swap - swap value of @a and @b
> >  */
> >
> > -#define swap(a, b) ({ typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; })
> > +#define swap(a, b) \
> > +       do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
> >
> >
> >  /**
> >  * container_of - cast a member of a structure out to the containing
> structure
> >
> >
> >
> >
> >
> > --
> > To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> > the body of a message to majordomo@xxxxxxxxxxxxxxx
> > More majordomo info at
> http://vger.kernel.org/majordomo-info.html
> > Please read the FAQ at  http://www.tux.org/lkml/
> >
>
>

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux