From 5dd41b50fc7d24c103522171ec2b3b89b7c0d37f Mon Sep 17 00:00:00 2001 From: Paul Lind Date: Tue, 7 Aug 2012 22:27:52 -0700 Subject: Fix lazy-instance template to preserve object alignment on MIPS. The alignment attribute of an object was discarded when allocating storage for the lazy-instance. Alignment is important for MIPS, where doubles (and objects containing them) must be aligned on 8-byte boundary. The alignment attributes are included via a macro that is only defined under GCC, so that this change does not affect other build enviromments. Change-Id: I99047cf610618de2e7e29e5de85835dc45d99231 Signed-off-by: Paul Lind --- base/lazy_instance.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/base/lazy_instance.h b/base/lazy_instance.h index 7b1bdc4..a8ff0e8 100644 --- a/base/lazy_instance.h +++ b/base/lazy_instance.h @@ -108,6 +108,14 @@ class BASE_API LazyInstanceHelper { DISALLOW_COPY_AND_ASSIGN(LazyInstanceHelper); }; +// Allow preservation of object alignment in the lazy instance when using GCC. +// __alignof__ is only defined for GCC > 4.2. +#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2)) +#define LAZY_ALIGN(T) __attribute__((aligned(__alignof__(T)))) +#else +#define LAZY_ALIGN(T) +#endif + template > class LazyInstance : public LazyInstanceHelper { public: @@ -167,12 +175,15 @@ class LazyInstance : public LazyInstanceHelper { base::subtle::Release_Store(&me->state_, STATE_EMPTY); } - int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. + // Preallocate the space for the Type instance, and preserve alignment. + int8 buf_[sizeof(Type)] LAZY_ALIGN(Type); Type *instance_; DISALLOW_COPY_AND_ASSIGN(LazyInstance); }; +#undef LAZY_ALIGN + } // namespace base #endif // BASE_LAZY_INSTANCE_H_ -- cgit v1.1