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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include "base/at_exit.h" | 7 #include "base/at_exit.h" |
8 #include "base/atomic_sequence_num.h" | 8 #include "base/atomic_sequence_num.h" |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/memory/aligned_memory.h" | |
11 #include "base/threading/simple_thread.h" | 10 #include "base/threading/simple_thread.h" |
| 11 #include "build/build_config.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 base::StaticAtomicSequenceNumber constructed_seq_; | 16 base::StaticAtomicSequenceNumber constructed_seq_; |
17 base::StaticAtomicSequenceNumber destructed_seq_; | 17 base::StaticAtomicSequenceNumber destructed_seq_; |
18 | 18 |
19 class ConstructAndDestructLogger { | 19 class ConstructAndDestructLogger { |
20 public: | 20 public: |
21 ConstructAndDestructLogger() { | 21 ConstructAndDestructLogger() { |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 EXPECT_FALSE(deleted2); | 145 EXPECT_FALSE(deleted2); |
146 } | 146 } |
147 | 147 |
148 namespace { | 148 namespace { |
149 | 149 |
150 template <size_t alignment> | 150 template <size_t alignment> |
151 class AlignedData { | 151 class AlignedData { |
152 public: | 152 public: |
153 AlignedData() {} | 153 AlignedData() {} |
154 ~AlignedData() {} | 154 ~AlignedData() {} |
155 base::AlignedMemory<alignment, alignment> data_; | 155 alignas(alignment) char data_[alignment]; |
156 }; | 156 }; |
157 | 157 |
158 } // anonymous namespace | 158 } // namespace |
159 | 159 |
160 #define EXPECT_ALIGNED(ptr, align) \ | 160 #define EXPECT_ALIGNED(ptr, align) \ |
161 EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1)) | 161 EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1)) |
162 | 162 |
163 TEST(LazyInstanceTest, Alignment) { | 163 TEST(LazyInstanceTest, Alignment) { |
164 using base::LazyInstance; | 164 using base::LazyInstance; |
165 | 165 |
166 // Create some static instances with increasing sizes and alignment | 166 // Create some static instances with increasing sizes and alignment |
167 // requirements. By ordering this way, the linker will need to do some work to | 167 // requirements. By ordering this way, the linker will need to do some work to |
168 // ensure proper alignment of the static data. | 168 // ensure proper alignment of the static data. |
169 static LazyInstance<AlignedData<4>>::DestructorAtExit align4 = | 169 static LazyInstance<AlignedData<4>>::DestructorAtExit align4 = |
170 LAZY_INSTANCE_INITIALIZER; | 170 LAZY_INSTANCE_INITIALIZER; |
171 static LazyInstance<AlignedData<32>>::DestructorAtExit align32 = | 171 static LazyInstance<AlignedData<32>>::DestructorAtExit align32 = |
172 LAZY_INSTANCE_INITIALIZER; | 172 LAZY_INSTANCE_INITIALIZER; |
173 static LazyInstance<AlignedData<4096>>::DestructorAtExit align4096 = | 173 static LazyInstance<AlignedData<4096>>::DestructorAtExit align4096 = |
174 LAZY_INSTANCE_INITIALIZER; | 174 LAZY_INSTANCE_INITIALIZER; |
175 | 175 |
176 EXPECT_ALIGNED(align4.Pointer(), 4); | 176 EXPECT_ALIGNED(align4.Pointer(), 4); |
177 EXPECT_ALIGNED(align32.Pointer(), 32); | 177 EXPECT_ALIGNED(align32.Pointer(), 32); |
178 EXPECT_ALIGNED(align4096.Pointer(), 4096); | 178 EXPECT_ALIGNED(align4096.Pointer(), 4096); |
179 } | 179 } |
OLD | NEW |