On Thu, Feb 10, 2005 at 12:22:26PM -0000, rajesh ms wrote: > ? > Hi all, > > I have got a question. > > 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) );}) > > > > 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) ); > > }); Yeah, fun isn't it :) > 1)What value will be stored in drv? Is there any rule to find this, It will be a pointer to the pci_driver structure. > 2)We are declaring const struct device_driver pointer and substracting > the offset value (struct device_driver * is a member of struct > pci_driver )and getting the address of struct pci_driver which may > contain some data that is irrelevent to struct pci_driver. If this is > the return value whether this will cause problems? How this is dealt > in the code. Look at the definition of struct pci_driver. It contains a struct device_driver. When passed a pointer to a struct device_driver, the code "knows" that it is safe to treat it as a part of the struct pci_driver. So it moves backwards in memory to get the real pointer to the struct pci_driver and everyone is happy. This convention is used a _lot_ in the driver model code in the kernel to allow for "inheritance" in C. Hope this helps, greg k-h -- Kernelnewbies: Help each other learn about the Linux kernel. Archive: http://mail.nl.linux.org/kernelnewbies/ FAQ: http://kernelnewbies.org/faq/