]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: introduce container_of
authorJosef 'Jeff' Sipek <jeff.sipek@dovecot.fi>
Tue, 13 Sep 2016 19:01:56 +0000 (15:01 -0400)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Mon, 30 Apr 2018 12:53:10 +0000 (15:53 +0300)
This is a simple but type safe implementation.

src/lib/macros.h

index 35826866bbe487bfa0f75de91a20ec37addfd0de..14ba1beea1653731b7f8348df3f2eb9e5e55b6b8 100644 (file)
 #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))