From: Josef 'Jeff' Sipek Date: Tue, 13 Sep 2016 19:01:56 +0000 (-0400) Subject: lib: introduce container_of X-Git-Tag: 2.3.0.rc1~1097 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1c238d4b71b43ba80cf4b9b3c3e797f2b25ec23c;p=thirdparty%2Fdovecot%2Fcore.git lib: introduce container_of This is a simple but type safe implementation. --- diff --git a/src/lib/macros.h b/src/lib/macros.h index dc4d66db96..4cbab19320 100644 --- a/src/lib/macros.h +++ b/src/lib/macros.h @@ -26,6 +26,10 @@ #define CONST_PTR_OFFSET(ptr, offset) \ ((const void *) (((const unsigned char *) (ptr)) + (offset))) +#define container_of(ptr, type, name) \ + (type *)((uintptr_t)(ptr) - (uintptr_t)offsetof(type, name) + \ + COMPILE_ERROR_IF_TYPES_NOT_COMPATIBLE(ptr, &((type *) 0)->name)) + /* Don't use simply MIN/MAX, as they're often defined elsewhere in include files that are included after this file generating tons of warnings. */ #define I_MIN(a, b) (((a) < (b)) ? (a) : (b))