]> 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)
committerAki Tuomi <aki.tuomi@dovecot.fi>
Thu, 7 Sep 2017 07:43:55 +0000 (10:43 +0300)
This is a simple but type safe implementation.

src/lib/macros.h

index dc4d66db962d29fdacc8b8988e334fccdf76c4b4..4cbab19320811d13a9c8a817018f491ef896c065 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))