Reviewers: Sven Panne, Jakob,

Message:
Another trivial cleanup, PTAL.

Description:
Add V8_ALIGNOF() and use that in lazy-instance.h.

Also fix the inconsistent attribute naming.

Please review this at https://codereview.chromium.org/22888008/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files:
  M include/v8config.h
  M src/lazy-instance.h


Index: include/v8config.h
diff --git a/include/v8config.h b/include/v8config.h
index aa24eebff50bb193543295723c9195ece613b8ec..72366fff85f99bc58ebf794e5f92a330e23e3f26 100644
--- a/include/v8config.h
+++ b/include/v8config.h
@@ -97,6 +97,7 @@
 // C++11 feature detection
 //
 //  V8_HAS_CXX11_ALIGNAS        - alignas specifier supported
+//  V8_HAS_CXX11_ALIGNOF        - alignas operator supported
 //  V8_HAS_CXX11_STATIC_ASSERT  - static_assert() supported
 //  V8_HAS_CXX11_DELETE         - deleted functions supported
 //  V8_HAS_CXX11_FINAL          - final marker supported
@@ -104,7 +105,9 @@
 //
 // Compiler-specific feature detection
 //
-// V8_HAS_ATTRIBUTE___ALIGNED__ - __attribute__((__aligned__(n))) supported
+//  V8_HAS___ALIGNOF                - __alignof(t) operator supported
+//  V8_HAS___ALIGNOF__              - __alignof__(t) operator supported
+//  V8_HAS_ATTRIBUTE_ALIGNED        - __attribute__((aligned(n))) supported
// V8_HAS_ATTRIBUTE_ALWAYS_INLINE - __attribute__((always_inline)) supported
 //  V8_HAS_ATTRIBUTE_DEPRECATED     - __attribute__((deprecated)) supported
 //  V8_HAS_ATTRIBUTE_VISIBILITY     - __attribute__((visibility)) supported
@@ -122,7 +125,13 @@

 # define V8_CC_CLANG 1

-# define V8_HAS_ATTRIBUTE___ALIGNED__ (__has_attribute(__aligned__))
+// If we pretend to be GCC 2.95 or newer, then we have to support
+// the GCC __alignof__ operator as well.
+#if __GNUC__ > 3 || (__GNUC__ >= 2 && __GNUC_MINOR__ >= 95)
+# define V8_HAS___ALIGNOF__ 1
+#endif
+
+# define V8_HAS_ATTRIBUTE_ALIGNED (__has_attribute(aligned))
 # define V8_HAS_ATTRIBUTE_ALWAYS_INLINE (__has_attribute(always_inline))
 # define V8_HAS_ATTRIBUTE_DEPRECATED (__has_attribute(deprecated))
 # define V8_HAS_ATTRIBUTE_VISIBILITY (__has_attribute(visibility))
@@ -146,7 +155,9 @@
 #  define V8_CC_MINGW 1
 # endif

-# define V8_HAS_ATTRIBUTE___ALIGNED__ (V8_GNUC_PREREQ(2, 95, 0))
+# define V8_HAS___ALIGNOF__ (V8_GNUC_PREREQ(2, 95, 0))
+
+# define V8_HAS_ATTRIBUTE_ALIGNED (V8_GNUC_PREREQ(2, 95, 0))
 // always_inline is available in gcc 4.0 but not very reliable until 4.4.
 // Works around "sorry, unimplemented: inlining failed" build errors with
 // older compilers.
@@ -164,6 +175,7 @@
 // both for forward compatibility.
 # if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
 #  define V8_HAS_CXX11_ALIGNAS (V8_GNUC_PREREQ(4, 8, 0))
+#  define V8_HAS_CXX11_ALIGNOF (V8_GNUC_PREREQ(4, 5, 0))
 #  define V8_HAS_CXX11_STATIC_ASSERT (V8_GNUC_PREREQ(4, 3, 0))
 #  define V8_HAS_CXX11_DELETE (V8_GNUC_PREREQ(4, 4, 0))
 #  define V8_HAS_CXX11_OVERRIDE (V8_GNUC_PREREQ(4, 7, 0))
@@ -188,6 +200,7 @@
 # define V8_HAS_DECLSPEC_ALIGN 1
 # define V8_HAS_DECLSPEC_DEPRECATED (_MSC_VER >= 1300)

+# define V8_HAS___ALIGNOF 1
 # define V8_HAS___FORCEINLINE 1

 #endif
@@ -279,12 +292,28 @@
 //   V8_ALIGNAS(32) int array[42];
 #if V8_HAS_CXX11_ALIGNAS
 # define V8_ALIGNAS(n) alignas(n)
-#elif V8_HAS_ATTRIBUTE___ALIGNED__
-# define V8_ALIGNAS(n) __attribute__((__aligned__(n)))
+#elif V8_HAS_ATTRIBUTE_ALIGNED
+# define V8_ALIGNAS(n) __attribute__((aligned(n)))
 #elif V8_HAS_DECLSPEC_ALIGN
 # define V8_ALIGNAS(n) __declspec(align(n))
 #else
 # define V8_ALIGNAS(n) /* NOT SUPPORTED */
 #endif

+// This macro takes a type and returns the power of 2 byte boundary on which
+// the type instances must be allocated.
+// Use like:
+//   size_t alignment = V8_ALIGNOF(double);
+//   V8_ALIGNAS(V8_ALIGNOF(void*)) int x;
+#if V8_HAS_CXX11_ALIGNOF
+# define V8_ALIGNOF(t) alignof(t)
+#elif V8_HAS___ALIGNOF
+# define V8_ALIGNOF(t) __alignof(t)
+#elif V8_HAS___ALIGNOF__
+# define V8_ALIGNOF(t) __alignof__(t)
+#else
+namespace v8 { template <typename T> struct AlignOfHelper { char x; T t; }; }
+# define V8_ALIGNOF(t) (sizeof(::v8::AlignOfHelper<t>) - sizeof(t))
+#endif
+
 #endif  // V8CONFIG_H_
Index: src/lazy-instance.h
diff --git a/src/lazy-instance.h b/src/lazy-instance.h
index 9d68b8cacce0f0f4815f6a5940716df0ddf83bbb..cdde2255ff19be0ef92c00f40916249769449665 100644
--- a/src/lazy-instance.h
+++ b/src/lazy-instance.h
@@ -111,17 +111,9 @@ struct LeakyInstanceTrait {

 // Traits that define how an instance is allocated and accessed.

-// TODO(kalmard): __alignof__ is only defined for GCC > 4.2. Fix alignment issue
-// on MIPS with other compilers.
-#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__
2))
-#define LAZY_ALIGN(x) __attribute__((aligned(__alignof__(x))))
-#else
-#define LAZY_ALIGN(x)
-#endif
-
 template <typename T>
 struct StaticallyAllocatedInstanceTrait {
-  typedef char StorageType[sizeof(T)] LAZY_ALIGN(T);
+  typedef char StorageType[sizeof(T)] V8_ALIGNAS(V8_ALIGNOF(T));

   static T* MutableInstance(StorageType* storage) {
     return reinterpret_cast<T*>(storage);
@@ -133,8 +125,6 @@ struct StaticallyAllocatedInstanceTrait {
   }
 };

-#undef LAZY_ALIGN
-

 template <typename T>
 struct DynamicallyAllocatedInstanceTrait {


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to