From 1c238d4b71b43ba80cf4b9b3c3e797f2b25ec23c Mon Sep 17 00:00:00 2001 From: Josef 'Jeff' Sipek Date: Tue, 13 Sep 2016 15:01:56 -0400 Subject: [PATCH] lib: introduce container_of This is a simple but type safe implementation. --- src/lib/macros.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib/macros.h b/src/lib/macros.h index dc4d66db96..4cbab19320 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)) -- 2.47.3