From: Felix Fietkau Date: Mon, 18 Mar 2013 04:54:44 +0000 (+0100) Subject: list.h: implement type safety for container_of() X-Git-Url: http://git.archive.openwrt.org/?p=project%2Flibubox.git;a=commitdiff_plain;h=009b4d574e88d558a9a9278cff363cee38b9e041 list.h: implement type safety for container_of() Signed-off-by: Felix Fietkau --- diff --git a/list.h b/list.h index 47ce33b..ab52acf 100644 --- a/list.h +++ b/list.h @@ -35,8 +35,11 @@ #define prefetch(x) #ifndef container_of -#define container_of(ptr, type, member) ( \ - (type *)( (char *)ptr - offsetof(type,member) )) +#define container_of(ptr, type, member) \ + ({ \ + const typeof(((type *) NULL)->member) *__mptr = (ptr); \ + (type *) ((char *) __mptr - offsetof(type, member)); \ + }) #endif struct list_head {