When you begin with the kernel, and you start to look around and read the code, you will eventually come across this magical preprocessor construct. What does it do? Well, precisely what its name indicates. It takes three arguments – a pointer, type of the container, and the name of the member the pointer refers to. The macro will then expand to a new address pointing to the container which accommodates the respective member. It is indeed a particularly clever macro, but how the hell can this possibly work? Let me illustrate… The first diagram illustrates the principle of the Bellow is the actual implementation of the macro from Linux Kernel:
At first glance, this might look like a whole lot of magic, but it isn’t quite so. Let’s take it step by step. Statements in ExpressionsThe first thing to gain your attention might be the structure of the whole
expression. The statement should return a pointer, right? But there is just
some kind of weird
typeof()This is a non-standard GNU C extension. It takes one argument and returns its type. Its exact semantics is throughly described in gcc documentation.
Zero Pointer DereferenceBut what about the zero pointer dereference? Well, it’s a little pointer magic to get the type of the member. It won’t crash, because the expression itself will never be evaluated. All the compiler cares for is its type. The same situation occurs in case we ask back for the address. The compiler again doesn’t care for the value, it will simply add the offset of the member to the address of the structure, in this particular case 0, and return the new address.
Also note that the following two definitions are equivalent:
offsetof(st, m)This macro will return a byte offset of a member to the beginning of the
structure. It is even part of the standard library (available in
It returns an address of a member called MEMBER of a structure of type TYPE that is stored in memory from address 0 (which happens to be the offset we’re looking for). Putting It All Together
When you look more closely at the original definition from the beginning of this post, you will start wondering if the first line is really good for anything. You will be right. The first line is not intrinsically important for the result of the macro, but it is there for type checking purposes. And what the second line really does? It subtracts the offset of the structure’s member from its address yielding the address of the container structure. That’s it! After you strip all the magical operators, constructs and tricks, it is that simple :-). References |
|