| 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 "base/at_exit.h" | 5 #include "base/at_exit.h" |
| 6 #include "base/atomic_sequence_num.h" | 6 #include "base/atomic_sequence_num.h" |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/memory/aligned_memory.h" |
| 8 #include "base/threading/simple_thread.h" | 9 #include "base/threading/simple_thread.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 11 |
| 11 namespace { | 12 namespace { |
| 12 | 13 |
| 13 base::AtomicSequenceNumber constructed_seq_(base::LINKER_INITIALIZED); | 14 base::AtomicSequenceNumber constructed_seq_(base::LINKER_INITIALIZED); |
| 14 base::AtomicSequenceNumber destructed_seq_(base::LINKER_INITIALIZED); | 15 base::AtomicSequenceNumber destructed_seq_(base::LINKER_INITIALIZED); |
| 15 | 16 |
| 16 class ConstructAndDestructLogger { | 17 class ConstructAndDestructLogger { |
| 17 public: | 18 public: |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 // when the AtExitManager finishes. | 133 // when the AtExitManager finishes. |
| 133 bool deleted2 = false; | 134 bool deleted2 = false; |
| 134 { | 135 { |
| 135 base::ShadowingAtExitManager shadow; | 136 base::ShadowingAtExitManager shadow; |
| 136 static base::LazyInstance<DeleteLogger>::Leaky | 137 static base::LazyInstance<DeleteLogger>::Leaky |
| 137 test = LAZY_INSTANCE_INITIALIZER; | 138 test = LAZY_INSTANCE_INITIALIZER; |
| 138 test.Get().SetDeletedPtr(&deleted2); | 139 test.Get().SetDeletedPtr(&deleted2); |
| 139 } | 140 } |
| 140 EXPECT_FALSE(deleted2); | 141 EXPECT_FALSE(deleted2); |
| 141 } | 142 } |
| 143 |
| 144 namespace { |
| 145 |
| 146 template <size_t alignment> |
| 147 class AlignedData { |
| 148 public: |
| 149 AlignedData() {} |
| 150 ~AlignedData() {} |
| 151 base::AlignedMemory<alignment, alignment> data_; |
| 152 }; |
| 153 |
| 154 } // anonymous namespace |
| 155 |
| 156 #define EXPECT_ALIGNED(ptr, align) \ |
| 157 EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1)) |
| 158 |
| 159 TEST(LazyInstanceTest, Alignment) { |
| 160 using base::LazyInstance; |
| 161 |
| 162 // Create some static instances with increasing sizes and alignment |
| 163 // requirements. By ordering this way, the linker will need to do some work to |
| 164 // ensure proper alignment of the static data. |
| 165 static LazyInstance<AlignedData<4> > align4 = LAZY_INSTANCE_INITIALIZER; |
| 166 static LazyInstance<AlignedData<32> > align32 = LAZY_INSTANCE_INITIALIZER; |
| 167 static LazyInstance<AlignedData<4096> > align4096 = LAZY_INSTANCE_INITIALIZER; |
| 168 |
| 169 EXPECT_ALIGNED(align4.Pointer(), 4); |
| 170 EXPECT_ALIGNED(align32.Pointer(), 32); |
| 171 EXPECT_ALIGNED(align4096.Pointer(), 4096); |
| 172 } |
| OLD | NEW |