From 009b4d574e88d558a9a9278cff363cee38b9e041 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Mon, 18 Mar 2013 05:54:44 +0100 Subject: list.h: implement type safety for container_of() Signed-off-by: Felix Fietkau --- list.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'list.h') 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 { -- cgit v1.2.3