Chromium Code Reviews| Index: base/memory/singleton_unittest.cc |
| diff --git a/base/memory/singleton_unittest.cc b/base/memory/singleton_unittest.cc |
| index 068148ff76b75e5e25f9f02ff1ddc802e93591d8..59527774440da4ec944a7f2f311c65c6ba5a1999 100644 |
| --- a/base/memory/singleton_unittest.cc |
| +++ b/base/memory/singleton_unittest.cc |
| @@ -110,6 +110,26 @@ struct CallbackSingletonWithStaticTrait::Trait |
| } |
| }; |
| +template <class Type> |
| +class AlignedTestSingleton { |
| + public: |
| + static AlignedTestSingleton* GetInstance() { |
| + return Singleton<AlignedTestSingleton, |
| + StaticMemorySingletonTraits<AlignedTestSingleton> >::get(); |
| + } |
| + |
| + Type type_; |
| +}; |
| + |
| +class ALIGNED(32) Aligned32Type { |
| + public: |
| + Aligned32Type() { |
| + memset(data_, 32, sizeof(data_)); |
| + } |
| + private: |
| + int32 data_[32 / 4]; |
| +}; |
| + |
| void SingletonNoLeak(CallbackFunc CallOnQuit) { |
| CallbackSingletonWithNoLeakTrait::GetInstance()->callback_ = CallOnQuit; |
| @@ -250,3 +270,21 @@ TEST_F(SingletonTest, Basic) { |
| // The leaky singleton shouldn't leak since SingletonLeak has not been called. |
| VerifiesCallbacksNotCalled(); |
| } |
| + |
| +TEST_F(SingletonTest, Alignment) { |
| + AlignedTestSingleton<Aligned32Type>* singleton1_align32 = |
| + AlignedTestSingleton<Aligned32Type>::GetInstance(); |
| + AlignedTestSingleton<int>* singleton2_align4 = |
| + AlignedTestSingleton<int>::GetInstance(); |
| + AlignedTestSingleton<Aligned32Type>* singleton3_align32 = |
| + AlignedTestSingleton<Aligned32Type>::GetInstance(); |
| + intptr_t singleton1_addr = |
| + reinterpret_cast<intptr_t>(&singleton1_align32->type_); |
| + intptr_t singleton2_addr = |
| + reinterpret_cast<intptr_t>(&singleton2_align4->type_); |
| + intptr_t singleton3_addr = |
| + reinterpret_cast<intptr_t>(&singleton3_align32->type_); |
| + EXPECT_EQ(intptr_t(0), singleton1_addr & 31); |
| + EXPECT_EQ(intptr_t(0), singleton2_addr & 3); |
| + EXPECT_EQ(intptr_t(0), singleton3_addr & 31); |
|
Jeffrey Yasskin (google)
2012/02/15 22:27:19
Isn't a better test to check that singleton1_addr
jbates
2012/02/15 22:51:33
Yes! In fact, I had a nightmare about how broken t
|
| +} |