Skip to contentSkip to conversation

libext2fs: use statement-expression for container_of only on GNU-compatible compilers

PATCH Applied Michael Forney 2 participants 4 messages Apr 14, 2021 – Jul 6, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 libext2fs: use statement-expression for container_of only on GNU-compatible compilers

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.

---
 lib/ext2fs/compiler.h | 5 +++++
 1 file changed, 5 insertions(+)
lib/ext2fs/compiler.h +5 -0
index 9aa9b4ec..03c35ab8 100644
@@ -14,9 +14,14 @@
 #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 */
Signature · 1 line
2.30.1
#1 lore raw

PATCH 2/2 libext2fs: use offsetof() from stddef.h

offsetof is a standard C feature available from stddef.h, going back all the way to ANSI C.

---
Perhaps there is some reason to prefer compiler builtins over libc that I'm not seeing?

 lib/ext2fs/compiler.h | 13 +------------
 1 file changed, 1 insertion(+), 12 deletions(-)
lib/ext2fs/compiler.h +1 -12
index 03c35ab8..42faa61c 100644
@@ -1,18 +1,7 @@
 #ifndef _EXT2FS_COMPILER_H
 #define _EXT2FS_COMPILER_H
 
-#ifndef __has_builtin
-#define __has_builtin(x) 0
-#endif
-
-#undef offsetof
-#if __has_builtin(__builtin_offsetof)
-#define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER)
-#elif defined(__compiler_offsetof)
-#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
-#else
-#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
-#endif
+#include <stddef.h>
 
 #ifdef __GNUC__
 #define container_of(ptr, type, member) ({			\
Signature · 1 line
2.30.1
#2 lore raw

Re: [PATCH 2/2] libext2fs: use offsetof() from stddef.h

Quoted 4 lines · Michael Forney

offsetof is a standard C feature available from stddef.h, going back all the way to ANSI C.

Thanks, applied.

Quoted 2 lines

Perhaps there is some reason to prefer compiler builtins over libc that I'm not seeing?

It's because I pulled container_of from the kernel, and the kernel header files has to provide offsetof since we don't use the standard header files --- and it has to work across a bunch of compilers and architectures.

- Ted

#3 lore raw
In reply to Michael Forney
Quoted 8 lines · Michael Forney

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.

Applied, thanks.

- Ted