Permalink
Browse files

Use GCC's builtin offset_of as the first option

Other than C++ code, the GCC offsetof builtin was never getting used.
The version that did get expanded could not be used for array
declarations on GCC 4.5 and later because it wasn't considered a
constant expression by those later GCC compilers.

The first symptom was seen when building world with GCC 4.6, but the
the offsetof expansion was finally identified as the cause of the
problem while discussion PostgreSQL 9.1 build failures on DragonFly.
Since DragonFly was the only platform exibiting the behavior, the
problem was narrowed down to the offsetof macro or the compiler itself.
Fixing the macro allowed the unpatched pgsql to compile.

Thanks-to: Tom Lane
  • Loading branch information...
John Marino François Tigeot
John Marino authored and François Tigeot committed Oct 7, 2012
1 parent 7b33812 commit 262e0d745238888fc693cd80f79c7ac0c513769f
Showing with 10 additions and 7 deletions.
  1. +6 −5 sys/cpu/i386/include/stdint.h
  2. +4 −2 sys/cpu/x86_64/include/stdint.h
@@ -36,7 +36,6 @@
* POSSIBILITY OF SUCH DAMAGE.
*
* $FreeBSD: src/sys/i386/include/_stdint.h,v 1.1 2002/07/29 17:41:07 mike Exp $
- * $DragonFly: src/sys/cpu/i386/include/stdint.h,v 1.8 2007/07/29 22:20:10 pavalos Exp $
*/
#ifndef _CPU_STDINT_H_
@@ -117,16 +116,18 @@ typedef volatile int __atomic_intr_t;
/*
* Its convenient to put these here rather then create another header file.
*/
+#if __GNUC_PREREQ__(4, 1)
+#define __offsetof(type, field) __builtin_offsetof(type, field)
+#else
#ifndef __cplusplus
#define __offsetof(type, field) ((__size_t)(&((type *)0)->field))
-#elif (__GNUC__ >= 4)
-#define __offsetof(type, field) __builtin_offsetof(type, field)
-#else
+#else
#define __offsetof(type, field) \
(__offsetof__ (reinterpret_cast <__size_t> \
(&reinterpret_cast <const volatile char &> \
(static_cast<type *> (0)->field))))
-#endif
+#endif
+#endif
#define __arysize(ary) (sizeof(ary)/sizeof((ary)[0]))
@@ -132,16 +132,18 @@ typedef volatile int __atomic_intr_t;
/*
* Its convenient to put these here rather then create another header file.
*/
+#if __GNUC_PREREQ__(4, 1)
+#define __offsetof(type, field) __builtin_offsetof(type, field)
+#else
#ifndef __cplusplus
#define __offsetof(type, field) ((__size_t)(&((type *)0)->field))
-#elif (__GNUC__ >= 4)
-#define __offsetof(type, field) __builtin_offsetof(type, field)
#else
#define __offsetof(type, field) \
(__offsetof__ (reinterpret_cast <__size_t> \
(&reinterpret_cast <const volatile char &> \
(static_cast<type *> (0)->field))))
#endif
+#endif
#define __arysize(ary) (sizeof(ary)/sizeof((ary)[0]))

0 comments on commit 262e0d7

Please sign in to comment.