This was originally published as a side bar to an article I wrote in Linux Journal, issue 110, June 2003. I found it when someone asked about what this macro does on the #kernelnewbies irc channel and dug it up from their site. I've put it here, and updated it a bit, adding more information, as it's good to have around. I need to eventually put up all of my Linux Journal articles on my site to prevent them from ever going stale.)
What exactly does the weird Linux kernel
To help explain pointer mess, let us go through the steps of what it is doing.
and is used in code as:
Replacing the above code with the first macro expansion produces:
Then, the next level of expansion creates:
To simplify this, remember that
So with the type of the
To show this using real numbers, say the i2c_driver structure looks like:
The location of the driver variable usually is 32 bytes into the structure, depending on the packing rules of the compiler at the time. For more information on how to see where variables are located in structures, see my article Writing Portable Device Drivers in the May/June 2002 issue of Embedded Linux Journal.
So, if the
Putting these values into the macro, it then is reduced to:
which is then evaluated at runtime by the processor as a simple subtraction on a pointer, which should be quite fast.
In order to do this kind of pointer manipulation, the code has to know the type of pointer being passed to it. The driver core only passes in the type of driver structure registered with it, so this type of manipulation is safe. This also prevents other parts of the kernel from modifying the unique fields of the structure used to control the subsystem's code.
posted Fri, 18 Feb 2005 in [/linux]
My Linux Stuff