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 #include <type_traits> | |
10 | 11 |
11 #include "base/memory/aligned_memory.h" | |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 namespace base { | 16 namespace base { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 class Dummy : public base::RefCounted<Dummy> { | 20 class Dummy : public base::RefCounted<Dummy> { |
21 public: | 21 public: |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 EXPECT_EQ(alive, 0); | 100 EXPECT_EQ(alive, 0); |
101 | 101 |
102 // Shouldn't crash at exit. | 102 // Shouldn't crash at exit. |
103 } | 103 } |
104 | 104 |
105 namespace { | 105 namespace { |
106 | 106 |
107 template <size_t alignment> | 107 template <size_t alignment> |
108 class AlignedData { | 108 class AlignedData { |
109 public: | 109 public: |
110 AlignedData() { memset(data_.void_data(), 0, alignment); } | 110 AlignedData() { memset(&data_, 0, alignment); } |
111 ~AlignedData() {} | 111 ~AlignedData() {} |
112 base::AlignedMemory<alignment, alignment> data_; | 112 typename std::aligned_storage<alignment, alignment>::type data_; |
danakj
2017/06/12 18:32:10
C++14 adds this (http://en.cppreference.com/w/cpp/
brettw
2017/06/12 22:22:15
I just replaced these all with char arrays. This s
danakj
2017/06/12 22:23:10
Me too, that is A+
| |
113 }; | 113 }; |
114 | 114 |
115 } // anonymous namespace | 115 } // anonymous namespace |
116 | 116 |
117 #define EXPECT_ALIGNED(ptr, align) \ | 117 #define EXPECT_ALIGNED(ptr, align) \ |
118 EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1)) | 118 EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1)) |
119 | 119 |
120 TEST(StackContainer, BufferAlignment) { | 120 TEST(StackContainer, BufferAlignment) { |
121 StackVector<wchar_t, 16> text; | 121 StackVector<wchar_t, 16> text; |
122 text->push_back(L'A'); | 122 text->push_back(L'A'); |
123 EXPECT_ALIGNED(&text[0], ALIGNOF(wchar_t)); | 123 EXPECT_ALIGNED(&text[0], alignof(wchar_t)); |
124 | 124 |
125 StackVector<double, 1> doubles; | 125 StackVector<double, 1> doubles; |
126 doubles->push_back(0.0); | 126 doubles->push_back(0.0); |
127 EXPECT_ALIGNED(&doubles[0], ALIGNOF(double)); | 127 EXPECT_ALIGNED(&doubles[0], alignof(double)); |
128 | 128 |
129 StackVector<AlignedData<16>, 1> aligned16; | 129 StackVector<AlignedData<16>, 1> aligned16; |
130 aligned16->push_back(AlignedData<16>()); | 130 aligned16->push_back(AlignedData<16>()); |
131 EXPECT_ALIGNED(&aligned16[0], 16); | 131 EXPECT_ALIGNED(&aligned16[0], 16); |
132 | 132 |
133 #if !defined(__GNUC__) || defined(ARCH_CPU_X86_FAMILY) | 133 #if !defined(__GNUC__) || defined(ARCH_CPU_X86_FAMILY) |
134 // It seems that non-X86 gcc doesn't respect greater than 16 byte alignment. | 134 // 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. | 135 // 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. | 136 // TODO(sbc):re-enable this if GCC starts respecting higher alignments. |
137 StackVector<AlignedData<256>, 1> aligned256; | 137 StackVector<AlignedData<256>, 1> aligned256; |
138 aligned256->push_back(AlignedData<256>()); | 138 aligned256->push_back(AlignedData<256>()); |
139 EXPECT_ALIGNED(&aligned256[0], 256); | 139 EXPECT_ALIGNED(&aligned256[0], 256); |
140 #endif | 140 #endif |
141 } | 141 } |
142 | 142 |
143 template class StackVector<int, 2>; | 143 template class StackVector<int, 2>; |
144 template class StackVector<scoped_refptr<Dummy>, 2>; | 144 template class StackVector<scoped_refptr<Dummy>, 2>; |
145 | 145 |
146 } // namespace base | 146 } // namespace base |
OLD | NEW |