| 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> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/gtest_prod_util.h" |
| 14 #include "base/logging.h" | 15 #include "base/logging.h" |
| 15 #include "base/macros.h" | 16 #include "base/macros.h" |
| 16 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
| 17 #include "cc/base/cc_export.h" | 18 #include "cc/base/cc_export.h" |
| 18 | 19 |
| 19 namespace cc { | 20 namespace cc { |
| 20 | 21 |
| 21 // ContiguousContainer is a container which stores a list of heterogeneous | 22 // ContiguousContainer is a container which stores a list of heterogeneous |
| 22 // objects (in particular, of varying sizes), packed next to one another in | 23 // objects (in particular, of varying sizes), packed next to one another in |
| 23 // memory. Objects are never relocated, so it is safe to store pointers to them | 24 // memory. Objects are never relocated, so it is safe to store pointers to them |
| (...skipping 18 matching lines...) Expand all Loading... |
| 42 ContiguousContainerBase(size_t max_object_size, size_t initial_size_bytes); | 43 ContiguousContainerBase(size_t max_object_size, size_t initial_size_bytes); |
| 43 ~ContiguousContainerBase(); | 44 ~ContiguousContainerBase(); |
| 44 | 45 |
| 45 size_t size() const { return elements_.size(); } | 46 size_t size() const { return elements_.size(); } |
| 46 bool empty() const { return !size(); } | 47 bool empty() const { return !size(); } |
| 47 size_t GetCapacityInBytes() const; | 48 size_t GetCapacityInBytes() const; |
| 48 size_t UsedCapacityInBytes() const; | 49 size_t UsedCapacityInBytes() const; |
| 49 size_t MemoryUsageInBytes() const; | 50 size_t MemoryUsageInBytes() const; |
| 50 | 51 |
| 51 // These do not invoke constructors or destructors. | 52 // These do not invoke constructors or destructors. |
| 52 void* Allocate(size_t object_size); | 53 void* Allocate(size_t object_size, size_t alignment); |
| 53 void Clear(); | 54 void Clear(); |
| 54 void RemoveLast(); | 55 void RemoveLast(); |
| 55 void Swap(ContiguousContainerBase& other); | 56 void Swap(ContiguousContainerBase& other); |
| 56 | 57 |
| 57 std::vector<void*> elements_; | 58 std::vector<void*> elements_; |
| 58 | 59 |
| 59 private: | 60 private: |
| 60 class Buffer; | 61 class Buffer; |
| 61 | 62 |
| 62 Buffer* AllocateNewBufferForNextAllocation(size_t buffer_size); | 63 Buffer* AllocateNewBufferForNextAllocation(size_t buffer_size); |
| 63 | 64 |
| 64 std::vector<std::unique_ptr<Buffer>> buffers_; | 65 std::vector<std::unique_ptr<Buffer>> buffers_; |
| 65 size_t end_index_; | 66 size_t end_index_; |
| 66 size_t max_object_size_; | 67 size_t max_object_size_; |
| 67 | 68 |
| 68 DISALLOW_COPY_AND_ASSIGN(ContiguousContainerBase); | 69 DISALLOW_COPY_AND_ASSIGN(ContiguousContainerBase); |
| 69 }; | 70 }; |
| 70 | 71 |
| 71 // For most cases, no alignment stricter than pointer alignment is required. If | 72 template <class BaseElementType> |
| 72 // one of the derived classes has stronger alignment requirements (and the | |
| 73 // static_assert fires), set alignment to the least common multiple of the | |
| 74 // derived class alignments. For small structs without pointers, it may be | |
| 75 // possible to reduce alignment for tighter packing. | |
| 76 | |
| 77 template <class BaseElementType, unsigned alignment = sizeof(void*)> | |
| 78 class ContiguousContainer : public ContiguousContainerBase { | 73 class ContiguousContainer : public ContiguousContainerBase { |
| 79 private: | 74 private: |
| 75 FRIEND_TEST_ALL_PREFIXES(ContiguousContainerTest, Alignment); |
| 76 |
| 80 // Declares itself as a forward iterator, but also supports a few more | 77 // Declares itself as a forward iterator, but also supports a few more |
| 81 // things. The whole random access iterator interface is a bit much. | 78 // things. The whole random access iterator interface is a bit much. |
| 82 template <typename BaseIterator, typename ValueType> | 79 template <typename BaseIterator, typename ValueType> |
| 83 class IteratorWrapper | 80 class IteratorWrapper |
| 84 : public std::iterator<std::forward_iterator_tag, ValueType> { | 81 : public std::iterator<std::forward_iterator_tag, ValueType> { |
| 85 public: | 82 public: |
| 86 IteratorWrapper() {} | 83 IteratorWrapper() {} |
| 87 bool operator==(const IteratorWrapper& other) const { | 84 bool operator==(const IteratorWrapper& other) const { |
| 88 return it_ == other.it_; | 85 return it_ == other.it_; |
| 89 } | 86 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 119 IteratorWrapper<std::vector<void*>::iterator, BaseElementType>; | 116 IteratorWrapper<std::vector<void*>::iterator, BaseElementType>; |
| 120 using const_iterator = IteratorWrapper<std::vector<void*>::const_iterator, | 117 using const_iterator = IteratorWrapper<std::vector<void*>::const_iterator, |
| 121 const BaseElementType>; | 118 const BaseElementType>; |
| 122 using reverse_iterator = | 119 using reverse_iterator = |
| 123 IteratorWrapper<std::vector<void*>::reverse_iterator, BaseElementType>; | 120 IteratorWrapper<std::vector<void*>::reverse_iterator, BaseElementType>; |
| 124 using const_reverse_iterator = | 121 using const_reverse_iterator = |
| 125 IteratorWrapper<std::vector<void*>::const_reverse_iterator, | 122 IteratorWrapper<std::vector<void*>::const_reverse_iterator, |
| 126 const BaseElementType>; | 123 const BaseElementType>; |
| 127 | 124 |
| 128 explicit ContiguousContainer(size_t max_object_size) | 125 explicit ContiguousContainer(size_t max_object_size) |
| 129 : ContiguousContainerBase(Align(max_object_size)) {} | 126 : ContiguousContainerBase(max_object_size) {} |
| 130 ContiguousContainer(size_t max_object_size, size_t initial_size_bytes) | 127 ContiguousContainer(size_t max_object_size, size_t initial_size_bytes) |
| 131 : ContiguousContainerBase(Align(max_object_size), initial_size_bytes) {} | 128 : ContiguousContainerBase(max_object_size, initial_size_bytes) {} |
| 132 | 129 |
| 133 ~ContiguousContainer() { | 130 ~ContiguousContainer() { |
| 134 for (auto& element : *this) { | 131 for (auto& element : *this) { |
| 135 // MSVC incorrectly reports this variable as unused. | 132 // MSVC incorrectly reports this variable as unused. |
| 136 (void)element; | 133 (void)element; |
| 137 element.~BaseElementType(); | 134 element.~BaseElementType(); |
| 138 } | 135 } |
| 139 } | 136 } |
| 140 | 137 |
| 141 using ContiguousContainerBase::size; | 138 using ContiguousContainerBase::size; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 161 const BaseElementType& first() const { return *begin(); } | 158 const BaseElementType& first() const { return *begin(); } |
| 162 BaseElementType& last() { return *rbegin(); } | 159 BaseElementType& last() { return *rbegin(); } |
| 163 const BaseElementType& last() const { return *rbegin(); } | 160 const BaseElementType& last() const { return *rbegin(); } |
| 164 BaseElementType& operator[](size_t index) { return *(begin() + index); } | 161 BaseElementType& operator[](size_t index) { return *(begin() + index); } |
| 165 const BaseElementType& operator[](size_t index) const { | 162 const BaseElementType& operator[](size_t index) const { |
| 166 return *(begin() + index); | 163 return *(begin() + index); |
| 167 } | 164 } |
| 168 | 165 |
| 169 template <class DerivedElementType, typename... Args> | 166 template <class DerivedElementType, typename... Args> |
| 170 DerivedElementType& AllocateAndConstruct(Args&&... args) { | 167 DerivedElementType& AllocateAndConstruct(Args&&... args) { |
| 171 static_assert(alignment % ALIGNOF(DerivedElementType) == 0, | 168 size_t alloc_size = sizeof(DerivedElementType); |
| 172 "Derived type requires stronger alignment."); | 169 return *new (Allocate(alloc_size, ALIGNOF(DerivedElementType))) |
| 173 size_t alloc_size = Align(sizeof(DerivedElementType)); | |
| 174 return *new (Allocate(alloc_size)) | |
| 175 DerivedElementType(std::forward<Args>(args)...); | 170 DerivedElementType(std::forward<Args>(args)...); |
| 176 } | 171 } |
| 177 | 172 |
| 178 void RemoveLast() { | 173 void RemoveLast() { |
| 179 DCHECK(!empty()); | 174 DCHECK(!empty()); |
| 180 last().~BaseElementType(); | 175 last().~BaseElementType(); |
| 181 ContiguousContainerBase::RemoveLast(); | 176 ContiguousContainerBase::RemoveLast(); |
| 182 } | 177 } |
| 183 | 178 |
| 184 void Clear() { | 179 void Clear() { |
| 185 for (auto& element : *this) { | 180 for (auto& element : *this) { |
| 186 // MSVC incorrectly reports this variable as unused. | 181 // MSVC incorrectly reports this variable as unused. |
| 187 (void)element; | 182 (void)element; |
| 188 element.~BaseElementType(); | 183 element.~BaseElementType(); |
| 189 } | 184 } |
| 190 ContiguousContainerBase::Clear(); | 185 ContiguousContainerBase::Clear(); |
| 191 } | 186 } |
| 192 | 187 |
| 193 void Swap(ContiguousContainer& other) { | 188 void Swap(ContiguousContainer& other) { |
| 194 ContiguousContainerBase::Swap(other); | 189 ContiguousContainerBase::Swap(other); |
| 195 } | 190 } |
| 196 | 191 |
| 197 // Appends a new element using memcpy, then default-constructs a base | 192 // Appends a new element using memcpy, then default-constructs a base |
| 198 // element in its place. Use with care. | 193 // element in its place. Use with care. |
| 199 BaseElementType& AppendByMoving(BaseElementType* item, size_t size) { | 194 BaseElementType& AppendByMoving(BaseElementType* item, |
| 195 size_t size, |
| 196 size_t alignment) { |
| 200 DCHECK_GE(size, sizeof(BaseElementType)); | 197 DCHECK_GE(size, sizeof(BaseElementType)); |
| 201 void* new_item = Allocate(size); | 198 void* new_item = Allocate(size, alignment); |
| 202 memcpy(new_item, static_cast<void*>(item), size); | 199 memcpy(new_item, static_cast<void*>(item), size); |
| 203 new (item) BaseElementType; | 200 new (item) BaseElementType; |
| 204 return *static_cast<BaseElementType*>(new_item); | 201 return *static_cast<BaseElementType*>(new_item); |
| 205 } | 202 } |
| 206 | |
| 207 private: | |
| 208 static size_t Align(size_t size) { | |
| 209 size_t aligned_size = alignment * ((size + alignment - 1) / alignment); | |
| 210 DCHECK_EQ(aligned_size % alignment, 0u); | |
| 211 DCHECK_GE(aligned_size, size); | |
| 212 DCHECK_LT(aligned_size, size + alignment); | |
| 213 return aligned_size; | |
| 214 } | |
| 215 }; | 203 }; |
| 216 | 204 |
| 217 } // namespace cc | 205 } // namespace cc |
| 218 | 206 |
| 219 #endif // CC_BASE_CONTIGUOUS_CONTAINER_H_ | 207 #endif // CC_BASE_CONTIGUOUS_CONTAINER_H_ |
| OLD | NEW |