changeset 22931:31856a233751

lib: introduce container_of This is a simple but type safe implementation.
author Josef 'Jeff' Sipek <jeff.sipek@dovecot.fi>
date Tue, 13 Sep 2016 15:01:56 -0400
parents bfeda12bb54a
children ffa75273f796
files src/lib/macros.h
diffstat 1 files changed, 4 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/macros.h	Sun Apr 29 12:31:23 2018 +0300
+++ b/src/lib/macros.h	Tue Sep 13 15:01:56 2016 -0400
@@ -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))