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* newItem = alignedAllocate(size); | 229 void* newItem = alignedAllocate(size); |
230 memcpy(newItem, static_cast<void*>(&item), size); | 230 memcpy(newItem, static_cast<void*>(&item), size); |
231 new (&item) BaseElementType; | 231 new (&item) BaseElementType; |
232 return *static_cast<BaseElementType*>(newItem); | 232 return *static_cast<BaseElementType*>(newItem); |
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 alignedSize = alignment * ((size + alignment - 1) / alignment); | 244 size_t alignedSize = alignment * ((size + alignment - 1) / alignment); |
245 DCHECK_EQ(alignedSize % alignment, 0u); | 245 DCHECK_EQ(alignedSize % alignment, 0u); |
246 DCHECK_GE(alignedSize, size); | 246 DCHECK_GE(alignedSize, size); |
247 DCHECK_LT(alignedSize, size + alignment); | 247 DCHECK_LT(alignedSize, size + alignment); |
248 return alignedSize; | 248 return alignedSize; |
249 } | 249 } |
250 }; | 250 }; |
251 | 251 |
252 } // namespace blink | 252 } // namespace blink |
253 | 253 |
254 #endif // ContiguousContainer_h | 254 #endif // ContiguousContainer_h |
OLD | NEW |