From 93375aa60733caf9d30d45f4a0cff0c0394e46e9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Thu, 4 May 2017 22:56:30 -0400 Subject: [PATCH] Add the container_of() macro to macro.h MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérémie Galarneau --- src/common/macros.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/common/macros.h b/src/common/macros.h index 90849ed30..7eaf27cdf 100644 --- a/src/common/macros.h +++ b/src/common/macros.h @@ -20,6 +20,7 @@ #define _MACROS_H #include +#include #include #include @@ -58,6 +59,14 @@ void *zmalloc(size_t len) #define ARRAY_SIZE(array) (sizeof(array) / (sizeof((array)[0]))) #endif +#ifndef container_of +#define container_of(ptr, type, member) \ + ({ \ + const typeof(((type *)NULL)->member) * __ptr = (ptr); \ + (type *)((char *)__ptr - offsetof(type, member)); \ + }) +#endif + #ifndef max #define max(a, b) ((a) > (b) ? (a) : (b)) #endif -- 2.34.1