diff mbox series

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

Message ID 20210414074128.31268-2-mforney@mforney.org
State Accepted
Headers show
Series [1/2] libext2fs: use statement-expression for container_of only on GNU-compatible compilers | expand

Commit Message

Michael Forney April 14, 2021, 7:41 a.m. UTC
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(-)

Comments

Theodore Ts'o July 6, 2021, 2:55 a.m. UTC | #1
On Wed, Apr 14, 2021 at 12:41:28AM -0700, Michael Forney wrote:
> 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>

Thanks, applied.

> 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
diff mbox series

Patch

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) ({			\