MIPS: Ensure proper alignment of LazyInstance objects.

The template system converts the actual struct type to an array of chars.
Make sure the alignment is kept by the compiler.

This fixes a lot of serialization-related HW tests, for example cctest test-serialize/Serialize.

BUG=
TEST=cctest test-serialize

Review URL: https://chromiumcodereview.appspot.com/9702114
Patch from Daniel Kalmar <kalmard@homejinni.com>.

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@11240 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
danno@chromium.org 2012-04-05 15:12:28 +00:00
parent db86043160
commit 17db8efe0c

View File

@ -111,9 +111,17 @@ struct LeakyInstanceTrait {
// Traits that define how an instance is allocated and accessed. // 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> template <typename T>
struct StaticallyAllocatedInstanceTrait { struct StaticallyAllocatedInstanceTrait {
typedef char StorageType[sizeof(T)]; typedef char StorageType[sizeof(T)] LAZY_ALIGN(T);
static T* MutableInstance(StorageType* storage) { static T* MutableInstance(StorageType* storage) {
return reinterpret_cast<T*>(storage); return reinterpret_cast<T*>(storage);
@ -125,6 +133,8 @@ struct StaticallyAllocatedInstanceTrait {
} }
}; };
#undef LAZY_ALIGN
template <typename T> template <typename T>
struct DynamicallyAllocatedInstanceTrait { struct DynamicallyAllocatedInstanceTrait {
@ -212,7 +222,8 @@ struct LazyInstanceImpl {
mutable OnceType once_; mutable OnceType once_;
// Note that the previous field, OnceType, is an AtomicWord which guarantees // Note that the previous field, OnceType, is an AtomicWord which guarantees
// the correct alignment of the storage field below. // 4-byte alignment of the storage field below. If compiling with GCC (>4.2),
// the LAZY_ALIGN macro above will guarantee correctness for any alignment.
mutable StorageType storage_; mutable StorageType storage_;
}; };