Skip to content

Commit 957b614

Browse files
committed
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 <[email protected]>
1 parent a5b7e41 commit 957b614

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

include/kerncompat.h

+33-3
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,39 @@ do { \
456456

457457
#define IS_ENABLED(c) 0
458458

459-
#define container_of(ptr, type, member) ({ \
460-
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
461-
(type *)( (char *)__mptr - offsetof(type,member) );})
459+
/* Are two types/vars the same type (ignoring qualifiers)? */
460+
#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
461+
462+
#define typeof_member(T, m) typeof(((T*)0)->m)
463+
464+
/**
465+
* container_of - cast a member of a structure out to the containing structure
466+
* @ptr: the pointer to the member.
467+
* @type: the type of the container struct this is embedded in.
468+
* @member: the name of the member within the struct.
469+
*
470+
* WARNING: any const qualifier of @ptr is lost.
471+
*/
472+
#define container_of(ptr, type, member) ({ \
473+
void *__mptr = (void *)(ptr); \
474+
static_assert(__same_type(*(ptr), ((type *)0)->member) || \
475+
__same_type(*(ptr), void), \
476+
"pointer type mismatch in container_of()"); \
477+
((type *)(__mptr - offsetof(type, member))); })
478+
479+
/**
480+
* container_of_const - cast a member of a structure out to the containing
481+
* structure and preserve the const-ness of the pointer
482+
* @ptr: the pointer to the member
483+
* @type: the type of the container struct this is embedded in.
484+
* @member: the name of the member within the struct.
485+
*/
486+
#define container_of_const(ptr, type, member) \
487+
_Generic(ptr, \
488+
const typeof(*(ptr)) *: ((const type *)container_of(ptr, type, member)),\
489+
default: ((type *)container_of(ptr, type, member)) \
490+
)
491+
462492
#ifndef __bitwise
463493
#ifdef __CHECKER__
464494
#define __bitwise __bitwise__

0 commit comments

Comments
 (0)