While looking at Linux kernel's implementation of doubly linked circular lists, I've found following macro:
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
The way this works is that it returns pointer to structure given only address of one of its members:
struct blabla
{
int value;
struct list_head *list;
}
Thus you can get pointer to blabla (and get to "value") given only pointer to list. To my question, how would I make this as portable as possible (best case conforming to C89/C99?). Due to usage of typeof(), this is gcc only.
This is what I've got so far:
#define container_of(ptr, type, member) ( \
(type *) (char *)(ptr)-offsetof(type,member)\
)
Is this snippet conforming to ISO standards (and thus should be able to be compiled on any conforming compiler)?
The macro is written the way it is to perfom a type check on
ptr
. It's possible to use a compound literal instead of the statement expression and fall back to a simple check for pointers instead of using__typeof__
if the compiler is not gcc-compatible:ISO C90 compatible version with type check. (However, caveat: two evaluations of
ptr
!)Test:
We get the
distinct pointer types
warning for 26, but not 25. That is our diagnostic about pointers being misused.I first tried placing the type check into the left hand side of a comma operator, gcc complains about that having no effect, which is a nuisance. But by making it an operand, we ensure that it is used.
The
&((type *) 0)->member
trick isn't well defined by ISO C, but it's widely used for definingoffsetof
. If your compiler uses this null pointer trick foroffsetof
, it will almost certainly behave itself in your own macro.As Ouah commented, the
({ ... })
statement expression is a GNU extension; you won't be able to use that. Your core expression is close to what's required, but doesn't have enough parentheses:That looks clean to me. It's only spread across two lines for SO.