PDA

View Full Version : return value from a function



jamesbon
November 15th, 2010, 11:47 AM
On this link http://lxr.ncu.cc/source/kernel/timer.c#094 a return type is defined

return ((unsigned int)(unsigned long)base & TBASE_DEFERRABLE_FLAG);

What is the above function returning.I am not clear with definition of what is being returned in the above code.

Tony Flury
November 15th, 2010, 12:11 PM
I think it is returning an unsigned int.

the function declaration will tell you (assuming this is C/C++) :



static inline unsigned int tbase_get_deferrable(struct tvec_base *base)


I think base is cast to an unsigned long and then cast to a unsigned int (presumably truncating it depending on the actual size of int and long) and then bitwise AND with TBASE_DEFERRABLE_FLAG, so it will return either a zero or non-zero value depending if that flag is unset or set.

jamesbon
November 16th, 2010, 03:38 AM
Is the same thing happening here
http://www.mjmwired.net/kernel/Documentation/prctl/disable-tsc-ctxt-sw-stress-test.c#34

worksofcraft
November 16th, 2010, 04:00 AM
#define TBASE_DEFERRABLE_FLAG (0x1)
/* Functions below help us manage 'deferrable' flag */
static inline unsigned int tbase_get_deferrable(struct tvec_base *base)
{
return ((unsigned int)(unsigned long)base & TBASE_DEFERRABLE_FLAG);
}


Sheesh... :shock: it's really not a good example to learn from IMHO what are you hoping to gain from reading that?

nvteighen
November 16th, 2010, 08:24 AM
That's highly optimized C... as I would expect from the Linux kernel source... There are a couple of things going on there: using bitmasking as a way to express stuff and then two castings that I guess/hope mainpulate the result of the bitmask in such a way that it's useful. I'm not a kernel hacker, so I can't tell you what the hell is going on there, but what I can tell you is that, if interested in reading others' source code, you should rather take a look to applications written in C and not the kernel.