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 AlignedTestSingleton() {} | |
117 ~AlignedTestSingleton() {} | |
118 static AlignedTestSingleton* GetInstance() { | |
119 return Singleton<AlignedTestSingleton, | |
120 StaticMemorySingletonTraits<AlignedTestSingleton> >::get(); | |
121 } | |
122 | |
123 Type type_; | |
124 }; | |
125 | |
113 | 126 |
114 void SingletonNoLeak(CallbackFunc CallOnQuit) { | 127 void SingletonNoLeak(CallbackFunc CallOnQuit) { |
115 CallbackSingletonWithNoLeakTrait::GetInstance()->callback_ = CallOnQuit; | 128 CallbackSingletonWithNoLeakTrait::GetInstance()->callback_ = CallOnQuit; |
116 } | 129 } |
117 | 130 |
118 void SingletonLeak(CallbackFunc CallOnQuit) { | 131 void SingletonLeak(CallbackFunc CallOnQuit) { |
119 CallbackSingletonWithLeakTrait::GetInstance()->callback_ = CallOnQuit; | 132 CallbackSingletonWithLeakTrait::GetInstance()->callback_ = CallOnQuit; |
120 } | 133 } |
121 | 134 |
122 CallbackFunc* GetLeakySingleton() { | 135 CallbackFunc* GetLeakySingleton() { |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
243 { | 256 { |
244 // Resurrect the static singleton, and assert that it | 257 // Resurrect the static singleton, and assert that it |
245 // still points to the same (static) memory. | 258 // still points to the same (static) memory. |
246 CallbackSingletonWithStaticTrait::Trait::Resurrect(); | 259 CallbackSingletonWithStaticTrait::Trait::Resurrect(); |
247 EXPECT_EQ(GetStaticSingleton(), static_singleton); | 260 EXPECT_EQ(GetStaticSingleton(), static_singleton); |
248 } | 261 } |
249 } | 262 } |
250 // The leaky singleton shouldn't leak since SingletonLeak has not been called. | 263 // The leaky singleton shouldn't leak since SingletonLeak has not been called. |
251 VerifiesCallbacksNotCalled(); | 264 VerifiesCallbacksNotCalled(); |
252 } | 265 } |
266 | |
267 #define EXPECT_ALIGNED(ptr, align) \ | |
268 EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1)) | |
269 | |
270 TEST_F(SingletonTest, Alignment) { | |
271 using base::RawAlignedMemory; | |
272 | |
273 // Create some static singletons with increasing sizes and alignment | |
274 // requirements. By ordering this way, the linker will need to do some work to | |
275 // ensure proper alignment of the static data. | |
276 AlignedTestSingleton<int32>* align4 = | |
277 AlignedTestSingleton<int32>::GetInstance(); | |
278 AlignedTestSingleton<RawAlignedMemory<32, 32> >* align32 = | |
279 AlignedTestSingleton<RawAlignedMemory<32, 32> >::GetInstance(); | |
280 AlignedTestSingleton<RawAlignedMemory<128, 128> >* align128 = | |
281 AlignedTestSingleton<RawAlignedMemory<128, 128> >::GetInstance(); | |
282 AlignedTestSingleton<RawAlignedMemory<4096, 4096> >* align4096 = | |
283 AlignedTestSingleton<RawAlignedMemory<4096, 4096> >::GetInstance(); | |
284 | |
285 EXPECT_ALIGNED(align4, 4); | |
willchan no longer on Chromium
2012/02/23 01:18:41
Again it's reversed here.
jbates
2012/02/23 02:23:53
The macro is defined above to call EXPECT_EQ prope
| |
286 EXPECT_ALIGNED(align32, 32); | |
287 EXPECT_ALIGNED(align128, 128); | |
288 EXPECT_ALIGNED(align4096, 4096); | |
289 } | |
OLD | NEW |