Skip to contentSkip to conversation

Philippe Gerum : boilerplate: add offsetof() shorthand

Xenomai Gilles Chanteperdrix 2 participants 6 messages Jul 24, 2015 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw
Quoted 22 lines · git repository hosting

Module: xenomai-3
Branch: next
Commit: 2a584d6763c64f9e566b5f53094d57dd72688857
URL:
http://git.xenomai.org/?p=xenomai-3.git;a=commit;h=2a584d6763c64f9e566b5f53094d57dd72688857

Author: Philippe Gerum <[email protected]>
Date:   Thu Jul 23 19:13:29 2015 +0200

boilerplate: add offsetof() shorthand

---

 include/boilerplate/compiler.h |    4 ++++
 1 file changed, 4 insertions(+)
include/boilerplate/compiler.h +0 -0
index e27d165..876e004 100644

include/boilerplate/compiler.h line 59

Quoted 6 lines
include/boilerplate/compiler.h +3 -0
@@ -54,4 +54,8 @@
 #define __aligned(__n)	__attribute__((aligned (__n)))
 #endif
 
+#ifndef offsetof
+#define offsetof(__type, __member)  __builtin_offsetof(__type, __member)
+#endif

offsetof should be defined by glibc headers. By adding a definition, you risk a compiler warning about multiple definitions if someone uses the proper glibc header.

Signature · 2 lines
                                            Gilles.
https://click-hack.org
#2 lore raw
Quoted 27 lines · Gilles Chanteperdrix
Quoted 25 lines · git repository hosting

Module: xenomai-3
Branch: next
Commit: 2a584d6763c64f9e566b5f53094d57dd72688857
URL:
http://git.xenomai.org/?p=xenomai-3.git;a=commit;h=2a584d6763c64f9e566b5f53094d57dd72688857

Author: Philippe Gerum <[email protected]>
Date:   Thu Jul 23 19:13:29 2015 +0200

boilerplate: add offsetof() shorthand

---

 include/boilerplate/compiler.h |    4 ++++
 1 file changed, 4 insertions(+)
include/boilerplate/compiler.h +0 -0
index e27d165..876e004 100644
@@ -54,4 +54,8 @@
 #define __aligned(__n)	__attribute__((aligned (__n)))
 #endif
 
Quoted 9 lines

include/boilerplate/compiler.h line 59

Quoted 3 lines
include/boilerplate/compiler.h +3 -0
+#ifndef offsetof
+#define offsetof(__type, __member)  __builtin_offsetof(__type, __member)
+#endif

offsetof should be defined by glibc headers. By adding a definition, you risk a compiler warning about multiple definitions if someone uses the proper glibc header.

There is hardly any risk of that kind when using the gcc builtin.

Signature · 1 line
Philippe.
#3 lore raw
Quoted 39 lines · Philippe Gerum
Quoted 27 lines · Gilles Chanteperdrix
Quoted 25 lines · git repository hosting

Module: xenomai-3
Branch: next
Commit: 2a584d6763c64f9e566b5f53094d57dd72688857
URL:
http://git.xenomai.org/?p=xenomai-3.git;a=commit;h=2a584d6763c64f9e566b5f53094d57dd72688857

Author: Philippe Gerum <[email protected]>
Date:   Thu Jul 23 19:13:29 2015 +0200

boilerplate: add offsetof() shorthand

---

 include/boilerplate/compiler.h |    4 ++++
 1 file changed, 4 insertions(+)
include/boilerplate/compiler.h +0 -0
index e27d165..876e004 100644
@@ -54,4 +54,8 @@
 #define __aligned(__n)	__attribute__((aligned (__n)))
 #endif
 
Quoted 10 lines

include/boilerplate/compiler.h line 60

Quoted 4 lines
include/boilerplate/compiler.h +3 -0
+#ifndef offsetof
+#define offsetof(__type, __member)  __builtin_offsetof(__type,
 __member)
+#endif

offsetof should be defined by glibc headers. By adding a definition, you risk a compiler warning about multiple definitions if someone uses the proper glibc header.

Quoted 2 lines

There is hardly any risk of that kind when using the gcc builtin.

builtin or not, if I compile the following program:

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

#define offsetof(__type, __member) __builtin_offsetof(__type,__member)

int main(void)

{
	printf("Hello\n");
	return EXIT_SUCCESS;
}

I get the following warning:

$ gcc -Wall -W -o foo foo.c
foo.c:5:0: warning: "offsetof" redefined [enabled by default]
 #define offsetof(__type, __member)  __builtin_offsetof(__type,__member)
 ^
In file included from /usr/include/alloca.h:24:0,
                 from /usr/include/stdlib.h:491,
                 from foo.c:3:
/usr/lib64/gcc/x86_64-slackware-linux/4.8.2/include/stddef.h:413:0: note:
this is the location of the previous definition
 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
 ^
Signature · 2 lines
                                            Gilles.
https://click-hack.org
#4 lore raw
Quoted 44 lines · Gilles Chanteperdrix
Quoted 39 lines · Philippe Gerum
Quoted 27 lines · Gilles Chanteperdrix
Quoted 25 lines · git repository hosting

Module: xenomai-3
Branch: next
Commit: 2a584d6763c64f9e566b5f53094d57dd72688857
URL:
http://git.xenomai.org/?p=xenomai-3.git;a=commit;h=2a584d6763c64f9e566b5f53094d57dd72688857

Author: Philippe Gerum <[email protected]>
Date:   Thu Jul 23 19:13:29 2015 +0200

boilerplate: add offsetof() shorthand

---

 include/boilerplate/compiler.h |    4 ++++
 1 file changed, 4 insertions(+)
include/boilerplate/compiler.h +0 -0
index e27d165..876e004 100644
@@ -54,4 +54,8 @@
 #define __aligned(__n)	__attribute__((aligned (__n)))
 #endif
 
Quoted 10 lines

include/boilerplate/compiler.h line 60

Quoted 4 lines
include/boilerplate/compiler.h +3 -0
+#ifndef offsetof
+#define offsetof(__type, __member)  __builtin_offsetof(__type,
 __member)
+#endif

offsetof should be defined by glibc headers. By adding a definition, you risk a compiler warning about multiple definitions if someone uses the proper glibc header.

Quoted 2 lines

There is hardly any risk of that kind when using the gcc builtin.

Quoted 7 lines

builtin or not, if I compile the following program:

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

#define offsetof(__type, __member) __builtin_offsetof(__type,__member)

Your program is wrong in the first place, since offsetof() is defined by stddef.h. Given that 3.x assumes a modern compiler with offsetof() available from there, you are just asking for troubles, especially since you explicitly omit the guard, which is not the best way to get things working either.

What can be done is including stddef.h from compiler.h, and provide a placeholder for offsetof() based on the null cast trick. That would make more sense.

Quoted 22 lines

int main(void)

{
	printf("Hello\n");
	return EXIT_SUCCESS;
}

I get the following warning:

$ gcc -Wall -W -o foo foo.c
foo.c:5:0: warning: "offsetof" redefined [enabled by default]
 #define offsetof(__type, __member)  __builtin_offsetof(__type,__member)
 ^
In file included from /usr/include/alloca.h:24:0,
                 from /usr/include/stdlib.h:491,
                 from foo.c:3:
/usr/lib64/gcc/x86_64-slackware-linux/4.8.2/include/stddef.h:413:0: note:
this is the location of the previous definition
 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
 ^
Signature · 1 line
Philippe.
#5 lore raw
Quoted 52 lines · Philippe Gerum
Quoted 51 lines · Gilles Chanteperdrix
Quoted 40 lines · Philippe Gerum
Quoted 27 lines · Gilles Chanteperdrix
Quoted 25 lines · git repository hosting

Module: xenomai-3
Branch: next
Commit: 2a584d6763c64f9e566b5f53094d57dd72688857
URL:
http://git.xenomai.org/?p=xenomai-3.git;a=commit;h=2a584d6763c64f9e566b5f53094d57dd72688857

Author: Philippe Gerum <[email protected]>
Date:   Thu Jul 23 19:13:29 2015 +0200

boilerplate: add offsetof() shorthand

---

 include/boilerplate/compiler.h |    4 ++++
 1 file changed, 4 insertions(+)
include/boilerplate/compiler.h +0 -0
index e27d165..876e004 100644
@@ -54,4 +54,8 @@
 #define __aligned(__n)	__attribute__((aligned (__n)))
 #endif
 
Quoted 11 lines

include/boilerplate/compiler.h line 60

Quoted 4 lines
include/boilerplate/compiler.h +3 -0
+#ifndef offsetof
+#define offsetof(__type, __member)  __builtin_offsetof(__type,
 __member)
+#endif

offsetof should be defined by glibc headers. By adding a definition, you
risk a compiler warning about multiple definitions if someone uses the proper glibc header.

Quoted 2 lines

There is hardly any risk of that kind when using the gcc builtin.

builtin or not, if I compile the following program:

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

Quoted 8 lines
Quoted 1 line

#define offsetof(__type, __member) __builtin_offsetof(__type,__member)

Your program is wrong in the first place, since offsetof() is defined by stddef.h. Given that 3.x assumes a modern compiler with offsetof() available from there, you are just asking for troubles, especially since you explicitly omit the guard, which is not the best way to get things working either.

I did that on purpose to illustrate the case that would happen if stddef was included after compiler.h, because the compiler stddef.h does not contain any guard. The guard in compiler.h does not work if compiler.h is included first.

Quoted 4 lines

What can be done is including stddef.h from compiler.h, and provide a placeholder for offsetof() based on the null cast trick. That would make more sense.

Not really, stddef.h has been defining offetof for ages.

Signature · 2 lines
                                            Gilles.
https://click-hack.org
#6 lore raw
Quoted 69 lines · Gilles Chanteperdrix
Quoted 52 lines · Philippe Gerum
Quoted 51 lines · Gilles Chanteperdrix
Quoted 40 lines · Philippe Gerum
Quoted 27 lines · Gilles Chanteperdrix
Quoted 25 lines · git repository hosting

Module: xenomai-3
Branch: next
Commit: 2a584d6763c64f9e566b5f53094d57dd72688857
URL:
http://git.xenomai.org/?p=xenomai-3.git;a=commit;h=2a584d6763c64f9e566b5f53094d57dd72688857

Author: Philippe Gerum <[email protected]>
Date:   Thu Jul 23 19:13:29 2015 +0200

boilerplate: add offsetof() shorthand

---

 include/boilerplate/compiler.h |    4 ++++
 1 file changed, 4 insertions(+)
include/boilerplate/compiler.h +0 -0
index e27d165..876e004 100644
@@ -54,4 +54,8 @@
 #define __aligned(__n)	__attribute__((aligned (__n)))
 #endif
 
Quoted 11 lines

include/boilerplate/compiler.h line 60

Quoted 4 lines
include/boilerplate/compiler.h +3 -0
+#ifndef offsetof
+#define offsetof(__type, __member)  __builtin_offsetof(__type,
 __member)
+#endif

offsetof should be defined by glibc headers. By adding a definition, you
risk a compiler warning about multiple definitions if someone uses the proper glibc header.

Quoted 2 lines

There is hardly any risk of that kind when using the gcc builtin.

builtin or not, if I compile the following program:

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

Quoted 8 lines
Quoted 1 line

#define offsetof(__type, __member) __builtin_offsetof(__type,__member)

Your program is wrong in the first place, since offsetof() is defined by stddef.h. Given that 3.x assumes a modern compiler with offsetof() available from there, you are just asking for troubles, especially since you explicitly omit the guard, which is not the best way to get things working either.

I did that on purpose to illustrate the case that would happen if stddef was included after compiler.h, because the compiler stddef.h does not contain any guard. The guard in compiler.h does not work if compiler.h is included first.

Quoted 1 line
Quoted 8 lines
Quoted 3 lines

What can be done is including stddef.h from compiler.h, and provide a placeholder for offsetof() based on the null cast trick. That would make more sense.

Not really, stddef.h has been defining offetof for ages.

That is not the point, including stddef.h first in compiler.h before trying the fallback definition would prevent the issue you mentioned a sentence above. Whether the fallback implementation would ever be picked or not due to stddef.h defining offsetof() most of the time is not really an urgent issue.

Signature · 1 line
Philippe.