| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <stdint.h> | 5 #include <stdint.h> |
| 6 | 6 |
| 7 #include "base/at_exit.h" | 7 #include "base/at_exit.h" |
| 8 #include "base/memory/singleton.h" | 8 #include "base/memory/singleton.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| 11 namespace base { | 11 namespace base { |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 static_assert(DefaultSingletonTraits<int>::kRegisterAtExit == true, | 14 static_assert(DefaultSingletonTraits<int>::kRegisterAtExit == true, |
| 15 "object must be deleted on process exit"); | 15 "object must be deleted on process exit"); |
| 16 | 16 |
| 17 typedef void (*CallbackFunc)(); | 17 typedef void (*CallbackFunc)(); |
| 18 | 18 |
| 19 template <size_t alignment> |
| 20 class AlignedData { |
| 21 public: |
| 22 AlignedData() {} |
| 23 ~AlignedData() {} |
| 24 alignas(alignment) char data_[alignment]; |
| 25 }; |
| 26 |
| 19 class IntSingleton { | 27 class IntSingleton { |
| 20 public: | 28 public: |
| 21 static IntSingleton* GetInstance() { | 29 static IntSingleton* GetInstance() { |
| 22 return Singleton<IntSingleton>::get(); | 30 return Singleton<IntSingleton>::get(); |
| 23 } | 31 } |
| 24 | 32 |
| 25 int value_; | 33 int value_; |
| 26 }; | 34 }; |
| 27 | 35 |
| 28 class Init5Singleton { | 36 class Init5Singleton { |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 } | 270 } |
| 263 } | 271 } |
| 264 // The leaky singleton shouldn't leak since SingletonLeak has not been called. | 272 // The leaky singleton shouldn't leak since SingletonLeak has not been called. |
| 265 VerifiesCallbacksNotCalled(); | 273 VerifiesCallbacksNotCalled(); |
| 266 } | 274 } |
| 267 | 275 |
| 268 #define EXPECT_ALIGNED(ptr, align) \ | 276 #define EXPECT_ALIGNED(ptr, align) \ |
| 269 EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1)) | 277 EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1)) |
| 270 | 278 |
| 271 TEST_F(SingletonTest, Alignment) { | 279 TEST_F(SingletonTest, Alignment) { |
| 272 using base::AlignedMemory; | |
| 273 | |
| 274 // Create some static singletons with increasing sizes and alignment | 280 // Create some static singletons with increasing sizes and alignment |
| 275 // requirements. By ordering this way, the linker will need to do some work to | 281 // requirements. By ordering this way, the linker will need to do some work to |
| 276 // ensure proper alignment of the static data. | 282 // ensure proper alignment of the static data. |
| 277 AlignedTestSingleton<int32_t>* align4 = | 283 AlignedTestSingleton<int32_t>* align4 = |
| 278 AlignedTestSingleton<int32_t>::GetInstance(); | 284 AlignedTestSingleton<int32_t>::GetInstance(); |
| 279 AlignedTestSingleton<AlignedMemory<32, 32> >* align32 = | 285 AlignedTestSingleton<AlignedData<32>>* align32 = |
| 280 AlignedTestSingleton<AlignedMemory<32, 32> >::GetInstance(); | 286 AlignedTestSingleton<AlignedData<32>>::GetInstance(); |
| 281 AlignedTestSingleton<AlignedMemory<128, 128> >* align128 = | 287 AlignedTestSingleton<AlignedData<128>>* align128 = |
| 282 AlignedTestSingleton<AlignedMemory<128, 128> >::GetInstance(); | 288 AlignedTestSingleton<AlignedData<128>>::GetInstance(); |
| 283 AlignedTestSingleton<AlignedMemory<4096, 4096> >* align4096 = | 289 AlignedTestSingleton<AlignedData<4096>>* align4096 = |
| 284 AlignedTestSingleton<AlignedMemory<4096, 4096> >::GetInstance(); | 290 AlignedTestSingleton<AlignedData<4096>>::GetInstance(); |
| 285 | 291 |
| 286 EXPECT_ALIGNED(align4, 4); | 292 EXPECT_ALIGNED(align4, 4); |
| 287 EXPECT_ALIGNED(align32, 32); | 293 EXPECT_ALIGNED(align32, 32); |
| 288 EXPECT_ALIGNED(align128, 128); | 294 EXPECT_ALIGNED(align128, 128); |
| 289 EXPECT_ALIGNED(align4096, 4096); | 295 EXPECT_ALIGNED(align4096, 4096); |
| 290 } | 296 } |
| 291 | 297 |
| 292 } // namespace | 298 } // namespace |
| 293 } // namespace base | 299 } // namespace base |
| OLD | NEW |