| 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 CC_BASE_CONTIGUOUS_CONTAINER_H_ | 5 #ifndef CC_BASE_CONTIGUOUS_CONTAINER_H_ |
| 6 #define CC_BASE_CONTIGUOUS_CONTAINER_H_ | 6 #define CC_BASE_CONTIGUOUS_CONTAINER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 const BaseElementType& last() const { return *rbegin(); } | 163 const BaseElementType& last() const { return *rbegin(); } |
| 164 BaseElementType& operator[](size_t index) { return *(begin() + index); } | 164 BaseElementType& operator[](size_t index) { return *(begin() + index); } |
| 165 const BaseElementType& operator[](size_t index) const { | 165 const BaseElementType& operator[](size_t index) const { |
| 166 return *(begin() + index); | 166 return *(begin() + index); |
| 167 } | 167 } |
| 168 | 168 |
| 169 template <class DerivedElementType, typename... Args> | 169 template <class DerivedElementType, typename... Args> |
| 170 DerivedElementType& AllocateAndConstruct(Args&&... args) { | 170 DerivedElementType& AllocateAndConstruct(Args&&... args) { |
| 171 static_assert(alignment % ALIGNOF(DerivedElementType) == 0, | 171 static_assert(alignment % ALIGNOF(DerivedElementType) == 0, |
| 172 "Derived type requires stronger alignment."); | 172 "Derived type requires stronger alignment."); |
| 173 size_t alloc_size = Align(sizeof(DerivedElementType)); | 173 return *new (AlignedAllocate(sizeof(DerivedElementType))) |
| 174 return *new (Allocate(alloc_size)) | |
| 175 DerivedElementType(std::forward<Args>(args)...); | 174 DerivedElementType(std::forward<Args>(args)...); |
| 176 } | 175 } |
| 177 | 176 |
| 178 void RemoveLast() { | 177 void RemoveLast() { |
| 179 DCHECK(!empty()); | 178 DCHECK(!empty()); |
| 180 last().~BaseElementType(); | 179 last().~BaseElementType(); |
| 181 ContiguousContainerBase::RemoveLast(); | 180 ContiguousContainerBase::RemoveLast(); |
| 182 } | 181 } |
| 183 | 182 |
| 184 void Clear() { | 183 void Clear() { |
| 185 for (auto& element : *this) { | 184 for (auto& element : *this) { |
| 186 // MSVC incorrectly reports this variable as unused. | 185 // MSVC incorrectly reports this variable as unused. |
| 187 (void)element; | 186 (void)element; |
| 188 element.~BaseElementType(); | 187 element.~BaseElementType(); |
| 189 } | 188 } |
| 190 ContiguousContainerBase::Clear(); | 189 ContiguousContainerBase::Clear(); |
| 191 } | 190 } |
| 192 | 191 |
| 193 void Swap(ContiguousContainer& other) { | 192 void Swap(ContiguousContainer& other) { |
| 194 ContiguousContainerBase::Swap(other); | 193 ContiguousContainerBase::Swap(other); |
| 195 } | 194 } |
| 196 | 195 |
| 197 // Appends a new element using memcpy, then default-constructs a base | 196 // Appends a new element using memcpy, then default-constructs a base |
| 198 // element in its place. Use with care. | 197 // element in its place. Use with care. |
| 199 BaseElementType& AppendByMoving(BaseElementType* item, size_t size) { | 198 BaseElementType& AppendByMoving(BaseElementType* item, size_t size) { |
| 200 DCHECK_GE(size, sizeof(BaseElementType)); | 199 DCHECK_GE(size, sizeof(BaseElementType)); |
| 201 void* new_item = Allocate(size); | 200 void* new_item = AlignedAllocate(size); |
| 202 memcpy(new_item, static_cast<void*>(item), size); | 201 memcpy(new_item, static_cast<void*>(item), size); |
| 203 new (item) BaseElementType; | 202 new (item) BaseElementType; |
| 204 return *static_cast<BaseElementType*>(new_item); | 203 return *static_cast<BaseElementType*>(new_item); |
| 205 } | 204 } |
| 206 | 205 |
| 207 private: | 206 private: |
| 208 static size_t Align(size_t size) { | 207 void* AlignedAllocate(size_t size) { |
| 208 void* result = ContiguousContainerBase::Allocate(Align(size)); |
| 209 DCHECK_EQ(reinterpret_cast<intptr_t>(result) & (alignment - 1), 0u); |
| 210 return result; |
| 211 } |
| 212 |
| 213 size_t Align(size_t size) { |
| 209 size_t aligned_size = alignment * ((size + alignment - 1) / alignment); | 214 size_t aligned_size = alignment * ((size + alignment - 1) / alignment); |
| 210 DCHECK_EQ(aligned_size % alignment, 0u); | 215 DCHECK_EQ(aligned_size % alignment, 0u); |
| 211 DCHECK_GE(aligned_size, size); | 216 DCHECK_GE(aligned_size, size); |
| 212 DCHECK_LT(aligned_size, size + alignment); | 217 DCHECK_LT(aligned_size, size + alignment); |
| 213 return aligned_size; | 218 return aligned_size; |
| 214 } | 219 } |
| 215 }; | 220 }; |
| 216 | 221 |
| 217 } // namespace cc | 222 } // namespace cc |
| 218 | 223 |
| 219 #endif // CC_BASE_CONTIGUOUS_CONTAINER_H_ | 224 #endif // CC_BASE_CONTIGUOUS_CONTAINER_H_ |
| OLD | NEW |