Linux-ext4 Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH 1/2] libext2fs: use statement-expression for container_of only on GNU-compatible compilers
@ 2021-04-14  7:41 Michael Forney
  2021-04-14  7:41 ` [PATCH 2/2] libext2fs: use offsetof() from stddef.h Michael Forney
  0 siblings, 1 reply; 2+ messages in thread
From: Michael Forney @ 2021-04-14  7:41 UTC (permalink / raw)
  To: linux-ext4

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>
---
 lib/ext2fs/compiler.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/lib/ext2fs/compiler.h b/lib/ext2fs/compiler.h
index 9aa9b4ec..03c35ab8 100644
--- a/lib/ext2fs/compiler.h
+++ b/lib/ext2fs/compiler.h
@@ -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 */
-- 
2.30.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH 2/2] libext2fs: use offsetof() from stddef.h
  2021-04-14  7:41 [PATCH 1/2] libext2fs: use statement-expression for container_of only on GNU-compatible compilers Michael Forney
@ 2021-04-14  7:41 ` Michael Forney
  0 siblings, 0 replies; 2+ messages in thread
From: Michael Forney @ 2021-04-14  7:41 UTC (permalink / raw)
  To: linux-ext4

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

Signed-off-by: Michael Forney <mforney@mforney.org>
---
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(-)

diff --git a/lib/ext2fs/compiler.h b/lib/ext2fs/compiler.h
index 03c35ab8..42faa61c 100644
--- a/lib/ext2fs/compiler.h
+++ b/lib/ext2fs/compiler.h
@@ -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) ({			\
-- 
2.30.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-14  7:41 [PATCH 1/2] libext2fs: use statement-expression for container_of only on GNU-compatible compilers Michael Forney
2021-04-14  7:41 ` [PATCH 2/2] libext2fs: use offsetof() from stddef.h Michael Forney

Linux-ext4 Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-ext4/0 linux-ext4/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-ext4 linux-ext4/ https://lore.kernel.org/linux-ext4 \
		linux-ext4@vger.kernel.org
	public-inbox-index linux-ext4

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-ext4


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git