From 957b61408854d006f45b732f68db2efb354cfe9e Mon Sep 17 00:00:00 2001 From: David Sterba Date: Wed, 7 Aug 2024 15:24:58 +0200 Subject: [PATCH] btrfs-progs: kerncompat: update definition of container_of Copy linux.git/include/linux/container_of.h definition of container_of and the const variant (currently unused) Signed-off-by: David Sterba --- include/kerncompat.h | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/include/kerncompat.h b/include/kerncompat.h index b6499d7f..8040ce47 100644 --- a/include/kerncompat.h +++ b/include/kerncompat.h @@ -456,9 +456,39 @@ do { \ #define IS_ENABLED(c) 0 -#define container_of(ptr, type, member) ({ \ - const typeof( ((type *)0)->member ) *__mptr = (ptr); \ - (type *)( (char *)__mptr - offsetof(type,member) );}) +/* Are two types/vars the same type (ignoring qualifiers)? */ +#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b)) + +#define typeof_member(T, m) typeof(((T*)0)->m) + +/** + * container_of - cast a member of a structure out to the containing structure + * @ptr: the pointer to the member. + * @type: the type of the container struct this is embedded in. + * @member: the name of the member within the struct. + * + * WARNING: any const qualifier of @ptr is lost. + */ +#define container_of(ptr, type, member) ({ \ + void *__mptr = (void *)(ptr); \ + static_assert(__same_type(*(ptr), ((type *)0)->member) || \ + __same_type(*(ptr), void), \ + "pointer type mismatch in container_of()"); \ + ((type *)(__mptr - offsetof(type, member))); }) + +/** + * container_of_const - cast a member of a structure out to the containing + * structure and preserve the const-ness of the pointer + * @ptr: the pointer to the member + * @type: the type of the container struct this is embedded in. + * @member: the name of the member within the struct. + */ +#define container_of_const(ptr, type, member) \ + _Generic(ptr, \ + const typeof(*(ptr)) *: ((const type *)container_of(ptr, type, member)),\ + default: ((type *)container_of(ptr, type, member)) \ + ) + #ifndef __bitwise #ifdef __CHECKER__ #define __bitwise __bitwise__