Whamcloud - gitweb
libext2fs: use statement-expression for container_of only on GNU-compatible compilers
authorMichael Forney <mforney@mforney.org>
Wed, 14 Apr 2021 07:41:27 +0000 (00:41 -0700)
committerTheodore Ts'o <tytso@mit.edu>
Tue, 6 Jul 2021 00:22:57 +0000 (20:22 -0400)
Functionally, the statement-expression is not necessary here; it
just gives a bit of type-safety to make sure the pointer really
does have a compatible type with the specified member of the struct.

When statement expressions are not available, we can just use a
portable fallback macro that skips this member type check.

Signed-off-by: Michael Forney <mforney@mforney.org>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
lib/ext2fs/compiler.h

index 9aa9b4e..03c35ab 100644 (file)
 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
 #endif
 
+#ifdef __GNUC__
 #define container_of(ptr, type, member) ({                     \
        const __typeof__( ((type *)0)->member ) *__mptr = (ptr);        \
        (type *)( (char *)__mptr - offsetof(type,member) );})
+#else
+#define container_of(ptr, type, member)                                \
+       ((type *)((char *)(ptr) - offsetof(type, member)))
+#endif
 
 
 #endif /* _EXT2FS_COMPILER_H */