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 <dsterba@suse.com>
This commit is contained in:
David Sterba 2024-08-07 15:24:58 +02:00
parent a5b7e414da
commit 957b614088
1 changed files with 33 additions and 3 deletions

View File

@ -456,9 +456,39 @@ do { \
#define IS_ENABLED(c) 0 #define IS_ENABLED(c) 0
#define container_of(ptr, type, member) ({ \ /* Are two types/vars the same type (ignoring qualifiers)? */
const typeof( ((type *)0)->member ) *__mptr = (ptr); \ #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
(type *)( (char *)__mptr - offsetof(type,member) );})
#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 #ifndef __bitwise
#ifdef __CHECKER__ #ifdef __CHECKER__
#define __bitwise __bitwise__ #define __bitwise __bitwise__