Re: Question abt piece of code in pci_Driver.c

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

 



On Thu, Feb 10, 2005 at 12:22:26PM -0000, rajesh  ms hit keys to express the following:
>   
> 
> I was going through pci_Driver.c. Then i found
> 
> 
> drv = to_pci_driver(dev->driver);
> 
> and
> drv is of type struct pci_driver*
> 
> #define	to_pci_driver(drv) container_of(drv,struct pci_driver, driver)
> 
> #define container_of(ptr, type, member)
> ({			\
>         const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
>         (type *)( (char *)__mptr - offsetof(type,member) );})
One question here,
Why is it not
#define container_of(ptr, type, member) ({			\
        const typeof( ((type *)offsetof(type,member)) *__mptr = (ptr);	\
        (type *)( (char *)__mptr - offsetof(type,member) );})
( offsetof() is a macro defined in include/linux/stddef.h, gcc does not support
it ). Is there any reason?

> Then i substituted for all parameters and got this
> 
> drv = ({
> 
>         const struct device_driver *__mptr = (dev->driver);
>         (struct pci_driver*)( (char *)__mptr - offsetof(struct pci_driver,driver) );
> 	
> 	});

> 
> 1)What value will be stored in drv? Is there any rule to find this,   ( Like last statement ----- My Guess )
This is a gcc extension.
>From info page of gcc,
A compound statement enclosed in parentheses may appear as an expression
in GNU C.  This allows you to use loops, switches, and local variables
within an expression.
....
#define max(a,b) ((a) > (b) ? (a) : (b))

But this definition computes either A or B twice, with bad results if
the operand has side effects.  In GNU C, if you know the type of the
operands (here let's assume `int'), you can define the macro safely as
follows:

#define maxint(a,b) \
   ({int _a = (a), _b = (b); _a > _b ? _a : _b; })
For more info, look in the info page of gcc, Statements and Declarations in
Expressions secion.

HTH,
Om.
-- 
You will gain money by an immoral action.

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           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