From 1e0e840ee5ddd92f6e8ec3fb9bb5a4e995043649 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Thu, 27 Jun 2013 20:58:43 +0200 Subject: [PATCH] Get rid of aligment warning on CONTAINER_OF in clang --- include/ucore/utils.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/ucore/utils.h b/include/ucore/utils.h index 9d1e86c..aad8379 100644 --- a/include/ucore/utils.h +++ b/include/ucore/utils.h @@ -48,12 +48,14 @@ //struct foo *f = CONTAINER_OF(p, struct foo, zap); #define CONTAINER_OF(ptr, type, member) ({ \ typeof( ((type *)0)->member ) *__mptr = (ptr); \ - (type *)( (unsigned char *)__mptr - offsetof(type, member) ); }) + (type *)((void *)(unsigned char *)__mptr - offsetof(type, member) ); }) + +//the void* cast is to suppress alignment warnings //Same as CONTAINER_OF, but for a const pointer to avoid gcc warnings.. #define CONST_CONTAINER_OF(ptr, type, member) ({ \ const typeof( ((const type *)0)->member ) *__mptr = (ptr); \ - (const type *)( (const unsigned char *)__mptr - offsetof(const type, member) ); }) + (const type *)((const void *)(const unsigned char *)__mptr - offsetof(const type, member) ); }) /** Align a value. * @param val value to align