# HG changeset patch # User Josef 'Jeff' Sipek # Date 1473793316 14400 # Node ID 31856a2337519036553e7a58c96ce371fab82270 # Parent bfeda12bb54a2bca4f53119566dca09418bdc223 lib: introduce container_of This is a simple but type safe implementation. diff -r bfeda12bb54a -r 31856a233751 src/lib/macros.h --- 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))