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 | |
113 | 124 |
114 void SingletonNoLeak(CallbackFunc CallOnQuit) { | 125 void SingletonNoLeak(CallbackFunc CallOnQuit) { |
115 CallbackSingletonWithNoLeakTrait::GetInstance()->callback_ = CallOnQuit; | 126 CallbackSingletonWithNoLeakTrait::GetInstance()->callback_ = CallOnQuit; |
116 } | 127 } |
117 | 128 |
118 void SingletonLeak(CallbackFunc CallOnQuit) { | 129 void SingletonLeak(CallbackFunc CallOnQuit) { |
119 CallbackSingletonWithLeakTrait::GetInstance()->callback_ = CallOnQuit; | 130 CallbackSingletonWithLeakTrait::GetInstance()->callback_ = CallOnQuit; |
120 } | 131 } |
121 | 132 |
122 CallbackFunc* GetLeakySingleton() { | 133 CallbackFunc* GetLeakySingleton() { |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
243 { | 254 { |
244 // Resurrect the static singleton, and assert that it | 255 // Resurrect the static singleton, and assert that it |
245 // still points to the same (static) memory. | 256 // still points to the same (static) memory. |
246 CallbackSingletonWithStaticTrait::Trait::Resurrect(); | 257 CallbackSingletonWithStaticTrait::Trait::Resurrect(); |
247 EXPECT_EQ(GetStaticSingleton(), static_singleton); | 258 EXPECT_EQ(GetStaticSingleton(), static_singleton); |
248 } | 259 } |
249 } | 260 } |
250 // The leaky singleton shouldn't leak since SingletonLeak has not been called. | 261 // The leaky singleton shouldn't leak since SingletonLeak has not been called. |
251 VerifiesCallbacksNotCalled(); | 262 VerifiesCallbacksNotCalled(); |
252 } | 263 } |
264 | |
265 TEST_F(SingletonTest, Alignment) { | |
266 using base::RawAlignedMemory; | |
267 AlignedTestSingleton<RawAlignedMemory<32, 32> >* singleton1_align32 = | |
268 AlignedTestSingleton<RawAlignedMemory<32, 32> >::GetInstance(); | |
269 AlignedTestSingleton<int>* singleton2_align4 = | |
270 AlignedTestSingleton<int>::GetInstance(); | |
271 AlignedTestSingleton<RawAlignedMemory<64, 64> >* singleton3_align64 = | |
272 AlignedTestSingleton<RawAlignedMemory<64, 64> >::GetInstance(); | |
273 AlignedTestSingleton<char>* singleton4_align1 = | |
274 AlignedTestSingleton<char>::GetInstance(); | |
275 AlignedTestSingleton<RawAlignedMemory<128, 128> >* singleton5_align128 = | |
276 AlignedTestSingleton<RawAlignedMemory<128, 128> >::GetInstance(); | |
277 AlignedTestSingleton<RawAlignedMemory<4096, 4096> >* singleton6_align4096 = | |
278 AlignedTestSingleton<RawAlignedMemory<4096, 4096> >::GetInstance(); | |
279 intptr_t singleton1_addr = | |
280 reinterpret_cast<intptr_t>(&singleton1_align32->type_); | |
281 intptr_t singleton2_addr = | |
282 reinterpret_cast<intptr_t>(&singleton2_align4->type_); | |
283 intptr_t singleton3_addr = | |
284 reinterpret_cast<intptr_t>(&singleton3_align64->type_); | |
285 (void)singleton4_align1; | |
286 intptr_t singleton5_addr = | |
287 reinterpret_cast<intptr_t>(&singleton5_align128->type_); | |
288 intptr_t singleton6_addr = | |
289 reinterpret_cast<intptr_t>(&singleton6_align4096->type_); | |
290 EXPECT_EQ(intptr_t(0), singleton1_addr & 31); | |
Sigurður Ásgeirsson
2012/02/22 15:31:32
this test would IMHO be more readable if you'd pul
jbates
2012/02/22 19:37:15
Done.
| |
291 EXPECT_EQ(intptr_t(0), singleton2_addr & 3); | |
292 EXPECT_EQ(intptr_t(0), singleton3_addr & 63); | |
293 // singleton4_addr is 1-byte aligned, don't care what the pointer is. | |
294 EXPECT_EQ(intptr_t(0), singleton5_addr & 127); | |
295 EXPECT_EQ(intptr_t(0), singleton6_addr & 4095); | |
296 } | |
OLD | NEW |