OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #ifndef ContiguousContainer_h | 5 #ifndef ContiguousContainer_h |
6 #define ContiguousContainer_h | 6 #define ContiguousContainer_h |
7 | 7 |
8 #include <cstddef> | 8 #include <cstddef> |
9 #include <iterator> | 9 #include <iterator> |
10 #include <memory> | 10 #include <memory> |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 DerivedElementType& AllocateAndConstruct(Args&&... args) { | 197 DerivedElementType& AllocateAndConstruct(Args&&... args) { |
198 static_assert(WTF::IsSubclass<DerivedElementType, BaseElementType>::value, | 198 static_assert(WTF::IsSubclass<DerivedElementType, BaseElementType>::value, |
199 "Must use subclass of BaseElementType."); | 199 "Must use subclass of BaseElementType."); |
200 static_assert(alignment % WTF_ALIGN_OF(DerivedElementType) == 0, | 200 static_assert(alignment % WTF_ALIGN_OF(DerivedElementType) == 0, |
201 "Derived type requires stronger alignment."); | 201 "Derived type requires stronger alignment."); |
202 return *new (AlignedAllocate(sizeof(DerivedElementType))) | 202 return *new (AlignedAllocate(sizeof(DerivedElementType))) |
203 DerivedElementType(std::forward<Args>(args)...); | 203 DerivedElementType(std::forward<Args>(args)...); |
204 } | 204 } |
205 | 205 |
206 void RemoveLast() { | 206 void RemoveLast() { |
207 ASSERT(!IsEmpty()); | 207 DCHECK(!IsEmpty()); |
208 Last().~BaseElementType(); | 208 Last().~BaseElementType(); |
209 ContiguousContainerBase::RemoveLast(); | 209 ContiguousContainerBase::RemoveLast(); |
210 } | 210 } |
211 | 211 |
212 DISABLE_CFI_PERF | 212 DISABLE_CFI_PERF |
213 void Clear() { | 213 void Clear() { |
214 for (auto& element : *this) { | 214 for (auto& element : *this) { |
215 (void)element; // MSVC incorrectly reports this variable as unused. | 215 (void)element; // MSVC incorrectly reports this variable as unused. |
216 element.~BaseElementType(); | 216 element.~BaseElementType(); |
217 } | 217 } |
218 ContiguousContainerBase::Clear(); | 218 ContiguousContainerBase::Clear(); |
219 } | 219 } |
220 | 220 |
221 void Swap(ContiguousContainer& other) { | 221 void Swap(ContiguousContainer& other) { |
222 ContiguousContainerBase::Swap(other); | 222 ContiguousContainerBase::Swap(other); |
223 } | 223 } |
224 | 224 |
225 // Appends a new element using memcpy, then default-constructs a base | 225 // Appends a new element using memcpy, then default-constructs a base |
226 // element in its place. Use with care. | 226 // element in its place. Use with care. |
227 BaseElementType& AppendByMoving(BaseElementType& item, size_t size) { | 227 BaseElementType& AppendByMoving(BaseElementType& item, size_t size) { |
228 ASSERT(size >= sizeof(BaseElementType)); | 228 DCHECK_GE(size, sizeof(BaseElementType)); |
229 void* new_item = AlignedAllocate(size); | 229 void* new_item = AlignedAllocate(size); |
230 memcpy(new_item, static_cast<void*>(&item), size); | 230 memcpy(new_item, static_cast<void*>(&item), size); |
231 new (&item) BaseElementType; | 231 new (&item) BaseElementType; |
232 return *static_cast<BaseElementType*>(new_item); | 232 return *static_cast<BaseElementType*>(new_item); |
233 } | 233 } |
234 | 234 |
235 private: | 235 private: |
236 void* AlignedAllocate(size_t size) { | 236 void* AlignedAllocate(size_t size) { |
237 void* result = ContiguousContainerBase::Allocate( | 237 void* result = ContiguousContainerBase::Allocate( |
238 Align(size), WTF_HEAP_PROFILER_TYPE_NAME(BaseElementType)); | 238 Align(size), WTF_HEAP_PROFILER_TYPE_NAME(BaseElementType)); |
239 DCHECK_EQ(reinterpret_cast<intptr_t>(result) & (alignment - 1), 0u); | 239 DCHECK_EQ(reinterpret_cast<intptr_t>(result) & (alignment - 1), 0u); |
240 return result; | 240 return result; |
241 } | 241 } |
242 | 242 |
243 static size_t Align(size_t size) { | 243 static size_t Align(size_t size) { |
244 size_t aligned_size = alignment * ((size + alignment - 1) / alignment); | 244 size_t aligned_size = alignment * ((size + alignment - 1) / alignment); |
245 DCHECK_EQ(aligned_size % alignment, 0u); | 245 DCHECK_EQ(aligned_size % alignment, 0u); |
246 DCHECK_GE(aligned_size, size); | 246 DCHECK_GE(aligned_size, size); |
247 DCHECK_LT(aligned_size, size + alignment); | 247 DCHECK_LT(aligned_size, size + alignment); |
248 return aligned_size; | 248 return aligned_size; |
249 } | 249 } |
250 }; | 250 }; |
251 | 251 |
252 } // namespace blink | 252 } // namespace blink |
253 | 253 |
254 #endif // ContiguousContainer_h | 254 #endif // ContiguousContainer_h |
OLD | NEW |