From: Josef 'Jeff' Sipek Date: Tue, 13 Sep 2016 19:01:56 +0000 (-0400) Subject: lib: introduce container_of X-Git-Tag: 2.2.36.rc1~17 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c8aef12d23719c7224f3c0112d06e135ce87ea2a;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 35826866bb..14ba1beea1 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))