| 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/containers/stack_container.h" | 5 #include "base/containers/stack_container.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| 11 #include "base/memory/aligned_memory.h" | |
| 12 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 13 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 14 |
| 16 namespace base { | 15 namespace base { |
| 17 | 16 |
| 18 namespace { | 17 namespace { |
| 19 | 18 |
| 20 class Dummy : public base::RefCounted<Dummy> { | 19 class Dummy : public base::RefCounted<Dummy> { |
| 21 public: | 20 public: |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 EXPECT_EQ(alive, 0); | 99 EXPECT_EQ(alive, 0); |
| 101 | 100 |
| 102 // Shouldn't crash at exit. | 101 // Shouldn't crash at exit. |
| 103 } | 102 } |
| 104 | 103 |
| 105 namespace { | 104 namespace { |
| 106 | 105 |
| 107 template <size_t alignment> | 106 template <size_t alignment> |
| 108 class AlignedData { | 107 class AlignedData { |
| 109 public: | 108 public: |
| 110 AlignedData() { memset(data_.void_data(), 0, alignment); } | 109 AlignedData() { memset(data_, 0, alignment); } |
| 111 ~AlignedData() {} | 110 ~AlignedData() {} |
| 112 base::AlignedMemory<alignment, alignment> data_; | 111 alignas(alignment) char data_[alignment]; |
| 113 }; | 112 }; |
| 114 | 113 |
| 115 } // anonymous namespace | 114 } // anonymous namespace |
| 116 | 115 |
| 117 #define EXPECT_ALIGNED(ptr, align) \ | 116 #define EXPECT_ALIGNED(ptr, align) \ |
| 118 EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1)) | 117 EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1)) |
| 119 | 118 |
| 120 TEST(StackContainer, BufferAlignment) { | 119 TEST(StackContainer, BufferAlignment) { |
| 121 StackVector<wchar_t, 16> text; | 120 StackVector<wchar_t, 16> text; |
| 122 text->push_back(L'A'); | 121 text->push_back(L'A'); |
| 123 EXPECT_ALIGNED(&text[0], ALIGNOF(wchar_t)); | 122 EXPECT_ALIGNED(&text[0], alignof(wchar_t)); |
| 124 | 123 |
| 125 StackVector<double, 1> doubles; | 124 StackVector<double, 1> doubles; |
| 126 doubles->push_back(0.0); | 125 doubles->push_back(0.0); |
| 127 EXPECT_ALIGNED(&doubles[0], ALIGNOF(double)); | 126 EXPECT_ALIGNED(&doubles[0], alignof(double)); |
| 128 | 127 |
| 129 StackVector<AlignedData<16>, 1> aligned16; | 128 StackVector<AlignedData<16>, 1> aligned16; |
| 130 aligned16->push_back(AlignedData<16>()); | 129 aligned16->push_back(AlignedData<16>()); |
| 131 EXPECT_ALIGNED(&aligned16[0], 16); | 130 EXPECT_ALIGNED(&aligned16[0], 16); |
| 132 | 131 |
| 133 #if !defined(__GNUC__) || defined(ARCH_CPU_X86_FAMILY) | 132 #if !defined(__GNUC__) || defined(ARCH_CPU_X86_FAMILY) |
| 134 // It seems that non-X86 gcc doesn't respect greater than 16 byte alignment. | 133 // It seems that non-X86 gcc doesn't respect greater than 16 byte alignment. |
| 135 // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33721 for details. | 134 // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33721 for details. |
| 136 // TODO(sbc):re-enable this if GCC starts respecting higher alignments. | 135 // TODO(sbc):re-enable this if GCC starts respecting higher alignments. |
| 137 StackVector<AlignedData<256>, 1> aligned256; | 136 StackVector<AlignedData<256>, 1> aligned256; |
| 138 aligned256->push_back(AlignedData<256>()); | 137 aligned256->push_back(AlignedData<256>()); |
| 139 EXPECT_ALIGNED(&aligned256[0], 256); | 138 EXPECT_ALIGNED(&aligned256[0], 256); |
| 140 #endif | 139 #endif |
| 141 } | 140 } |
| 142 | 141 |
| 143 template class StackVector<int, 2>; | 142 template class StackVector<int, 2>; |
| 144 template class StackVector<scoped_refptr<Dummy>, 2>; | 143 template class StackVector<scoped_refptr<Dummy>, 2>; |
| 145 | 144 |
| 146 } // namespace base | 145 } // namespace base |
| OLD | NEW |