Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/at_exit.h" | 5 #include "base/at_exit.h" |
| 6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/memory/singleton.h" | 7 #include "base/memory/singleton.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 struct CallbackSingletonWithStaticTrait::Trait | 103 struct CallbackSingletonWithStaticTrait::Trait |
| 104 : public StaticMemorySingletonTraits<CallbackSingletonWithStaticTrait> { | 104 : public StaticMemorySingletonTraits<CallbackSingletonWithStaticTrait> { |
| 105 static void Delete(CallbackSingletonWithStaticTrait* instance) { | 105 static void Delete(CallbackSingletonWithStaticTrait* instance) { |
| 106 if (instance->callback_) | 106 if (instance->callback_) |
| 107 (instance->callback_)(); | 107 (instance->callback_)(); |
| 108 StaticMemorySingletonTraits<CallbackSingletonWithStaticTrait>::Delete( | 108 StaticMemorySingletonTraits<CallbackSingletonWithStaticTrait>::Delete( |
| 109 instance); | 109 instance); |
| 110 } | 110 } |
| 111 }; | 111 }; |
| 112 | 112 |
| 113 template <class Type> | |
| 114 class AlignedTestSingleton { | |
| 115 public: | |
| 116 static AlignedTestSingleton* GetInstance() { | |
| 117 return Singleton<AlignedTestSingleton, | |
| 118 StaticMemorySingletonTraits<AlignedTestSingleton> >::get(); | |
| 119 } | |
| 120 | |
| 121 Type type_; | |
| 122 }; | |
| 123 | |
| 124 class ALIGNED(32) Aligned32Type { | |
| 125 public: | |
| 126 Aligned32Type() { | |
| 127 memset(data_, 32, sizeof(data_)); | |
| 128 } | |
| 129 private: | |
| 130 int32 data_[32 / 4]; | |
| 131 }; | |
| 132 | |
| 113 | 133 |
| 114 void SingletonNoLeak(CallbackFunc CallOnQuit) { | 134 void SingletonNoLeak(CallbackFunc CallOnQuit) { |
| 115 CallbackSingletonWithNoLeakTrait::GetInstance()->callback_ = CallOnQuit; | 135 CallbackSingletonWithNoLeakTrait::GetInstance()->callback_ = CallOnQuit; |
| 116 } | 136 } |
| 117 | 137 |
| 118 void SingletonLeak(CallbackFunc CallOnQuit) { | 138 void SingletonLeak(CallbackFunc CallOnQuit) { |
| 119 CallbackSingletonWithLeakTrait::GetInstance()->callback_ = CallOnQuit; | 139 CallbackSingletonWithLeakTrait::GetInstance()->callback_ = CallOnQuit; |
| 120 } | 140 } |
| 121 | 141 |
| 122 CallbackFunc* GetLeakySingleton() { | 142 CallbackFunc* GetLeakySingleton() { |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 243 { | 263 { |
| 244 // Resurrect the static singleton, and assert that it | 264 // Resurrect the static singleton, and assert that it |
| 245 // still points to the same (static) memory. | 265 // still points to the same (static) memory. |
| 246 CallbackSingletonWithStaticTrait::Trait::Resurrect(); | 266 CallbackSingletonWithStaticTrait::Trait::Resurrect(); |
| 247 EXPECT_EQ(GetStaticSingleton(), static_singleton); | 267 EXPECT_EQ(GetStaticSingleton(), static_singleton); |
| 248 } | 268 } |
| 249 } | 269 } |
| 250 // The leaky singleton shouldn't leak since SingletonLeak has not been called. | 270 // The leaky singleton shouldn't leak since SingletonLeak has not been called. |
| 251 VerifiesCallbacksNotCalled(); | 271 VerifiesCallbacksNotCalled(); |
| 252 } | 272 } |
| 273 | |
| 274 TEST_F(SingletonTest, Alignment) { | |
| 275 AlignedTestSingleton<Aligned32Type>* singleton1_align32 = | |
| 276 AlignedTestSingleton<Aligned32Type>::GetInstance(); | |
| 277 AlignedTestSingleton<int>* singleton2_align4 = | |
| 278 AlignedTestSingleton<int>::GetInstance(); | |
| 279 AlignedTestSingleton<Aligned32Type>* singleton3_align32 = | |
| 280 AlignedTestSingleton<Aligned32Type>::GetInstance(); | |
| 281 intptr_t singleton1_addr = | |
| 282 reinterpret_cast<intptr_t>(&singleton1_align32->type_); | |
| 283 intptr_t singleton2_addr = | |
| 284 reinterpret_cast<intptr_t>(&singleton2_align4->type_); | |
| 285 intptr_t singleton3_addr = | |
| 286 reinterpret_cast<intptr_t>(&singleton3_align32->type_); | |
| 287 EXPECT_EQ(intptr_t(0), singleton1_addr & 31); | |
| 288 EXPECT_EQ(intptr_t(0), singleton2_addr & 3); | |
| 289 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
| |
| 290 } | |
| OLD | NEW |