Re: question about __v annotation

2010-01-06 Thread Bernd Petrovitsch
On Mon, 2010-01-04 at 11:46 -0500, Greg Freemyer wrote:
> >
> > On Sat, Jan 2, 2010 at 8:10 AM, Shawn  wrote:
> >>
> >> hello guys,
> >>  I got a newbie confused when I was looking into the source code of
> >> s3c2440's RTC driver.I dont know what is __v excatly means.anyone can
> >> tell?thanks anyway!
> >>
> >> #define readb(c) ({ __u8  __v = __raw_readb(__mem_pci(c)); __v; })
> 
> On Sun, Jan 3, 2010 at 11:49 AM, Anand Arumugam  
> wrote:
> > I think the last __v; inside the macro is to avoid compiler warning or error
> > that the unsigned 8-bit variable __v is not being used inside the scope
> > defined by the macro.
> 
> No,  I believe it is the value assigned to the statement sequence
> wrapped in the {} pair.
> 
> ie. {statement1; statement2; variable} evaluates to the value of the
> variable which is the last statement.
No.
"{statement1; statement2; variable}" is pure (from K&R times ) C (if you
fix the syntax error of the missing ";" before the closing "}") and this
is usually called a "compound statement".
It becomes an expression if you
- use a gcc (young enough to implement it) and
- put the "( and ")" around it.
And the the result of the last statement becomes the result of the
expression

Bernd
-- 
mobil: +43 664 4416156  http://bernd.petrovitsch.priv.at/
Linux Software Entwicklung, Beratung und Dienstleistungen


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



Re: question about __v annotation

2010-01-05 Thread Bernd Petrovitsch
On Mon, 2010-01-04 at 15:47 -0800, Greg KH wrote:
> On Mon, Jan 04, 2010 at 03:32:07PM -0800, Bruce Blinn wrote:
[...]
> > > On Sat, Jan 02, 2010 at 09:10:08PM +0800, Shawn wrote:
> > > > hello guys,
> > > >  I got a newbie confused when I was looking into the source code of 
> > > > s3c2440's RTC driver.I dont know what is __v excatly 
> > > means.anyone can 
> > > > tell?thanks anyway!
> > > > 
> > > > #define readb(c) ({ __u8  __v = __raw_readb(__mem_pci(c)); __v; })
> > > 
> > > __v is an variable that the macro creates and then returns 
> > > the value of.
> > > 
> > > Don't code like this, it's horrible :)
> > 
> > Could you be more specific with what you find wrong with this. The reason I
> > ask is there is code like this all over the kernel.
> 
> Like what specifically?
> 
> Wrapping up a function call in a macro just because someone doesn't want
> to type the whole thing out?  That's the horrible thing.
> 
> Returning a temporary variable isn't a big deal, but an inline function
> is better to use as you will get the proper type safety.
So which is better (in the kernel and for this functions, macros or
whatever they really maybe!)?
a) #define readb(c) ((__u8)__raw_readb(__mem_pci(c)))
   which is more a rephrase of the above
b) static inline __u8 readb(const void *c) { return __raw_readb(__mem_pci(c)); }
Caveat emptor: Not compile-tested.

Bernd
-- 
mobil: +43 664 4416156  http://bernd.petrovitsch.priv.at/
Linux Software Entwicklung, Beratung und Dienstleistungen


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



Re: question about __v annotation

2010-01-05 Thread Greg KH
On Tue, Jan 05, 2010 at 11:00:37PM +0800, Pei Lin wrote:
> i only can think about the reason why not use the inline is that it is
> support by C99 but not support by C89 ANSI C.

No, the Linux kernel supports inline functions, no one builds the kernel
with a C89-only complier, thankfully.

thanks,

greg k-h

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



Re: question about __v annotation

2010-01-05 Thread Robin Randhawa
On Mon, Jan 04, 2010 at 05:08:03PM -0500, Anand Arumugam wrote:
>The purpose of the macro is served even without the last __v; statement.
>But the last statement __v; enables the macro to be used as rvalue (as
>described in many replies) in an expression.
>
>Without the last statement __v; at the end of the macro the compiler will
>issue a warning/error for the unused variable __v within the scope defined
>by { ... } and an error if the macro is used as rvalue.

True. The point is that we _want_ to use the macro as an rvalue and
hence the need to have the last '__v;' statement. Use of the macro as an
lvalue would work fine but result in a warning without this statement,
as you correctly point out.

Robin

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



Re: question about __v annotation

2010-01-05 Thread Greg KH
On Tue, Jan 05, 2010 at 12:42:03PM +0100, Bernd Petrovitsch wrote:
> So which is better (in the kernel and for this functions, macros or
> whatever they really maybe!)?
> a) #define readb(c) ((__u8)__raw_readb(__mem_pci(c)))
>which is more a rephrase of the above
> b) static inline __u8 readb(const void *c) { return 
> __raw_readb(__mem_pci(c)); }
> Caveat emptor: Not compile-tested.

Neither is best, use the proper __iomem marking for the memory space,
and use the built-in kernel functions to read and write data to io
space, they are there for a reason, don't try to roll your own.

thanks,

greg k-h

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



Re: question about __v annotation

2010-01-05 Thread Robin Randhawa
On Sun, Jan 03, 2010 at 11:49:09AM -0500, Anand Arumugam wrote:
>I think the last __v; inside the macro is to avoid compiler warning or
>error that the unsigned 8-bit variable __v is not being used inside the
>scope defined by the macro.

That's incorrect. The GNU C literature states that a compound statement
enclosed in parentheses may appear as an expression.

As a previous poster pointed out, the expansion of the readb macro is a
compound statement which is surrounded by parentheses and is therefore an
expression that evaluates to the value of the last subexpression in the
compound statement : in this case the value of __v.

See  for more
information.

Cheers,
Robin

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



Re: question about __v annotation

2010-01-05 Thread Pei Lin
2010/1/5 Greg KH :
> On Mon, Jan 04, 2010 at 03:32:07PM -0800, Bruce Blinn wrote:
>> > -Original Message-
>> > From: kernelnewbies-bou...@nl.linux.org
>> > [mailto:kernelnewbies-bou...@nl.linux.org] On Behalf Of Greg KH
>> > Sent: Sunday, January 03, 2010 1:27 PM
>> > To: Shawn
>> > Cc: kernelnewbies@nl.linux.org
>> > Subject: Re: question about __v annotation
>> >
>> >
>> > On Sat, Jan 02, 2010 at 09:10:08PM +0800, Shawn wrote:
>> > > hello guys,
>> > >  I got a newbie confused when I was looking into the source code of
>> > > s3c2440's RTC driver.I dont know what is __v excatly
>> > means.anyone can
>> > > tell?thanks anyway!
>> > >
>> > > #define readb(c) ({ __u8  __v = __raw_readb(__mem_pci(c)); __v; })
>> >
>> > __v is an variable that the macro creates and then returns
>> > the value of.
>> >
>> > Don't code like this, it's horrible :)
>> >
>>
>> Could you be more specific with what you find wrong with this. The reason I
>> ask is there is code like this all over the kernel.
>
> Like what specifically?
>
> Wrapping up a function call in a macro just because someone doesn't want
> to type the whole thing out?  That's the horrible thing.
>
> Returning a temporary variable isn't a big deal, but an inline function
> is better to use as you will get the proper type safety.
>
i only can think about the reason why not use the inline is that it is
support by C99 but not support by C89 ANSI C.

Also i get the good reasons why use macro is "bad" in the WIKI.
http://en.wikipedia.org/wiki/Inline_function
"
Use of true inline functions, as are available in C99, provides
several benefits over this approach:
Macro invocations do not perform type checking, or even check that
arguments are well-formed, whereas function calls usually do.
In C, a macro cannot use the return keyword with the same meaning as a
function would do (it would make the function that asked the expansion
terminate, rather than the macro). In other words, you cannot make the
macro return something which is not the result of the last expression
invoked inside it.
Since C macros use mere textual substitution, this may result in
unintended side-effects and inefficiency due to re-evaluation of
arguments and order of operations.
Compiler errors within macros are often difficult to understand,
because they refer to the expanded code, rather than the code the
programmer typed.
Many constructs are awkward or impossible to express using macros, or
use a significantly different syntax. In-line functions use the same
syntax as ordinary functions, and can be inlined and un-inlined at
will with ease.
Debugging information for inlined code is usually more helpful than
that of macro-expanded code.
Many compilers can also inline expand some recursive functions;
recursive macros are typically illegal.
"


> thanks,
>
> greg k-h
>
> --
> To unsubscribe from this list: send an email with
> "unsubscribe kernelnewbies" to ecar...@nl.linux.org
> Please read the FAQ at http://kernelnewbies.org/FAQ
>
>



-- 
Best Regards
Lin

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



Re: question about __v annotation

2010-01-05 Thread Greg KH
On Mon, Jan 04, 2010 at 03:32:07PM -0800, Bruce Blinn wrote:
> > -Original Message-
> > From: kernelnewbies-bou...@nl.linux.org 
> > [mailto:kernelnewbies-bou...@nl.linux.org] On Behalf Of Greg KH
> > Sent: Sunday, January 03, 2010 1:27 PM
> > To: Shawn
> > Cc: kernelnewbies@nl.linux.org
> > Subject: Re: question about __v annotation
> > 
> > 
> > On Sat, Jan 02, 2010 at 09:10:08PM +0800, Shawn wrote:
> > > hello guys,
> > >  I got a newbie confused when I was looking into the source code of 
> > > s3c2440's RTC driver.I dont know what is __v excatly 
> > means.anyone can 
> > > tell?thanks anyway!
> > > 
> > > #define readb(c) ({ __u8  __v = __raw_readb(__mem_pci(c)); __v; })
> > 
> > __v is an variable that the macro creates and then returns 
> > the value of.
> > 
> > Don't code like this, it's horrible :)
> > 
> 
> Could you be more specific with what you find wrong with this. The reason I
> ask is there is code like this all over the kernel.

Like what specifically?

Wrapping up a function call in a macro just because someone doesn't want
to type the whole thing out?  That's the horrible thing.

Returning a temporary variable isn't a big deal, but an inline function
is better to use as you will get the proper type safety.

thanks,

greg k-h

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



RE: question about __v annotation

2010-01-05 Thread Bruce Blinn
> -Original Message-
> From: kernelnewbies-bou...@nl.linux.org 
> [mailto:kernelnewbies-bou...@nl.linux.org] On Behalf Of Greg KH
> Sent: Sunday, January 03, 2010 1:27 PM
> To: Shawn
> Cc: kernelnewbies@nl.linux.org
> Subject: Re: question about __v annotation
> 
> 
> On Sat, Jan 02, 2010 at 09:10:08PM +0800, Shawn wrote:
> > hello guys,
> >  I got a newbie confused when I was looking into the source code of 
> > s3c2440's RTC driver.I dont know what is __v excatly 
> means.anyone can 
> > tell?thanks anyway!
> > 
> > #define readb(c) ({ __u8  __v = __raw_readb(__mem_pci(c)); __v; })
> 
> __v is an variable that the macro creates and then returns 
> the value of.
> 
> Don't code like this, it's horrible :)
> 

Could you be more specific with what you find wrong with this. The reason I
ask is there is code like this all over the kernel.

Bruce  


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



Re: question about __v annotation

2010-01-05 Thread Greg Freemyer
>
> On Sat, Jan 2, 2010 at 8:10 AM, Shawn  wrote:
>>
>> hello guys,
>>  I got a newbie confused when I was looking into the source code of
>> s3c2440's RTC driver.I dont know what is __v excatly means.anyone can
>> tell?thanks anyway!
>>
>> #define readb(c) ({ __u8  __v = __raw_readb(__mem_pci(c)); __v; })

On Sun, Jan 3, 2010 at 11:49 AM, Anand Arumugam  wrote:
> I think the last __v; inside the macro is to avoid compiler warning or error
> that the unsigned 8-bit variable __v is not being used inside the scope
> defined by the macro.

No,  I believe it is the value assigned to the statement sequence
wrapped in the {} pair.

ie. {statement1; statement2; variable} evaluates to the value of the
variable which is the last statement.

Thus c code with:

ret = readb(c);

will assign ret to __v which in turn is the value returned from __raw_readb().

Thus the above macro structure is the standard way to return a value
from a macro.  You will find similar macro usage throughout the
kernel.

Greg

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



Re: question about __v annotation

2010-01-05 Thread Pei Lin
2010/1/3 Manish Katiyar :
> On Sat, Jan 2, 2010 at 6:40 PM, Shawn  wrote:
>> hello guys,
>>  I got a newbie confused when I was looking into the source code of
>> s3c2440's RTC driver.I dont know what is __v excatly means
> A local variable name.
i think he confused for the second __v here.
Because it is a macro define, and want to be used as a function.
So the last __v is used for return value.
u can use this macro define as a function as below;

return_val = readb(c);


> ..anyone can
>> tell?thanks anyway!
>>
>> #define readb(c) ({ __u8  __v = __raw_readb(__mem_pci(c)); __v; })
>>
>> --
>> GNU powered it...
>> GPL protect it...
>> God blessing it...
>>
>> regards
>> HFG--Shawn the R0ck
>>
>
>
>
> --
> Thanks -
> Manish
> ==
> [$\*.^ -- I miss being one of them
> ==
>
> --
> To unsubscribe from this list: send an email with
> "unsubscribe kernelnewbies" to ecar...@nl.linux.org
> Please read the FAQ at http://kernelnewbies.org/FAQ
>
>



-- 
Best Regards
Lin

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



Re: question about __v annotation

2010-01-04 Thread Anand Arumugam
The purpose of the macro is served even without the last __v; statement. But
the last statement __v; enables the macro to be used as rvalue (as described
in many replies) in an expression.

Without the last statement __v; at the end of the macro the compiler will
issue a warning/error for the unused variable __v within the scope defined
by { ... } and an error if the macro is used as rvalue.

cheers,
anand.

On Mon, Jan 4, 2010 at 3:21 PM, Robin Randhawa wrote:

> On Sun, Jan 03, 2010 at 11:49:09AM -0500, Anand Arumugam wrote:
> >I think the last __v; inside the macro is to avoid compiler warning or
> >error that the unsigned 8-bit variable __v is not being used inside
> the
> >scope defined by the macro.
>
> That's incorrect. The GNU C literature states that a compound statement
> enclosed in parentheses may appear as an expression.
>
> As a previous poster pointed out, the expansion of the readb macro is a
> compound statement which is surrounded by parentheses and is therefore an
> expression that evaluates to the value of the last subexpression in the
> compound statement : in this case the value of __v.
>
> See  for more
> information.
>
> Cheers,
> Robin
>


Re: question about __v annotation

2010-01-04 Thread Greg KH
On Sat, Jan 02, 2010 at 09:10:08PM +0800, Shawn wrote:
> hello guys,
>  I got a newbie confused when I was looking into the source code of
> s3c2440's RTC driver.I dont know what is __v excatly means.anyone can
> tell?thanks anyway!
> 
> #define readb(c) ({ __u8  __v = __raw_readb(__mem_pci(c)); __v; })

__v is an variable that the macro creates and then returns the value of.

Don't code like this, it's horrible :)

thanks,

greg k-h

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



Re: question about __v annotation

2010-01-04 Thread Anand Arumugam
I think the last __v; inside the macro is to avoid compiler warning or error
that the unsigned 8-bit variable __v is not being used inside the scope
defined by the macro.

On Sat, Jan 2, 2010 at 8:10 AM, Shawn  wrote:

> hello guys,
>  I got a newbie confused when I was looking into the source code of
> s3c2440's RTC driver.I dont know what is __v excatly means.anyone can
> tell?thanks anyway!
>
> #define readb(c) ({ __u8  __v = __raw_readb(__mem_pci(c)); __v; })
>
> --
> GNU powered it...
> GPL protect it...
> God blessing it...
>
> regards
> HFG--Shawn the R0ck
>


Re: question about __v annotation

2010-01-03 Thread Ryan Wang
2010/1/3 Ryan Wang :
> 2010/1/2 Shawn :
>> hello guys,
>>  I got a newbie confused when I was looking into the source code of
>> s3c2440's RTC driver.I dont know what is __v excatly means.anyone can
>> tell?thanks anyway!
>>
>> #define readb(c) ({ __u8  __v = __raw_readb(__mem_pci(c)); __v; })
>>
>> --
>> GNU powered it...
>> GPL protect it...
>> God blessing it...
>>
>> regards
>> HFG--Shawn the R0ck
>>
>
> notice "{ ... }"
> It's code block, and __v is its last value
> when you use readb link
>        somevar = readb(someport)
> then __v is return to set somevar
>
Oh, sorry
I've made a mistake

{} is a compound statement, () quotes it, and then you get one expression.
And the value of the expression is the last value of the compound statement.
so you can use readb like this:
 alm_en = readb(base + S3C2410_RTCALM);
Here, read(..) is one expression, and you should add ";" after it.

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



Re: question about __v annotation

2010-01-03 Thread Ryan Wang
2010/1/2 Shawn :
> hello guys,
>  I got a newbie confused when I was looking into the source code of
> s3c2440's RTC driver.I dont know what is __v excatly means.anyone can
> tell?thanks anyway!
>
> #define readb(c) ({ __u8  __v = __raw_readb(__mem_pci(c)); __v; })
>
> --
> GNU powered it...
> GPL protect it...
> God blessing it...
>
> regards
> HFG--Shawn the R0ck
>

notice "{ ... }"
It's code block, and __v is its last value
when you use readb link
somevar = readb(someport)
then __v is return to set somevar

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



Re: question about __v annotation

2010-01-03 Thread Manish Katiyar
On Sat, Jan 2, 2010 at 6:40 PM, Shawn  wrote:
> hello guys,
>  I got a newbie confused when I was looking into the source code of
> s3c2440's RTC driver.I dont know what is __v excatly means
A local variable name.
..anyone can
> tell?thanks anyway!
>
> #define readb(c) ({ __u8  __v = __raw_readb(__mem_pci(c)); __v; })
>
> --
> GNU powered it...
> GPL protect it...
> God blessing it...
>
> regards
> HFG--Shawn the R0ck
>



-- 
Thanks -
Manish
==
[$\*.^ -- I miss being one of them
==

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



Re: question about __v annotation

2010-01-03 Thread Tadeusz Struk
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Shawn wrote:
> hello guys,
>  I got a newbie confused when I was looking into the source code of
> s3c2440's RTC driver.I dont know what is __v excatly means.anyone can
> tell?thanks anyway!
> 
> #define readb(c) ({ __u8  __v = __raw_readb(__mem_pci(c)); __v; })
> 
> -- 
> GNU powered it...
> GPL protect it...
> God blessing it...
> 
> regards
> HFG--Shawn the R0ck

Hi,
__v is a variable name.
If you convert this macro to a function it would look something like:

uint8_t f(uint8_t *addr)
{
uint8_t __v = 0;
__v = _raw_readb(__mem_pci(addr));
return __v;
}

Regards
T.
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAktATcoACgkQMWzB+ySr3HLbdwCgkxxEHjFS89YihCIuWyTsjBAx
D/0AoJl+cmsMqWTS54BPadmHA4dx8djt
=sMgA
-END PGP SIGNATURE-

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



question about __v annotation

2010-01-02 Thread Shawn
hello guys,
 I got a newbie confused when I was looking into the source code of
s3c2440's RTC driver.I dont know what is __v excatly means.anyone can
tell?thanks anyway!

#define readb(c) ({ __u8  __v = __raw_readb(__mem_pci(c)); __v; })

-- 
GNU powered it...
GPL protect it...
God blessing it...

regards
HFG--Shawn the R0ck