Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(309)

Side by Side Diff: cc/base/contiguous_container.h

Issue 2119033003: Fix alignment issue of ContiguousContainer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
22 namespace internal {
23 template <size_t input>
24 struct Log2 {};
25 template <>
26 struct Log2<1> {
27 static const unsigned value = 0;
28 };
29 template <>
30 struct Log2<2> {
31 static const unsigned value = 1;
32 };
33 template <>
34 struct Log2<4> {
35 static const unsigned value = 2;
36 };
37 template <>
38 struct Log2<8> {
39 static const unsigned value = 3;
40 };
41 template <>
42 struct Log2<16> {
43 static const unsigned value = 4;
44 };
45 } // namespace internal
46
47 template <typename T>
48 unsigned Log2Alignment() {
49 return internal::Log2<ALIGNOF(T)>::value;
50 }
51
21 // ContiguousContainer is a container which stores a list of heterogeneous 52 // ContiguousContainer is a container which stores a list of heterogeneous
22 // objects (in particular, of varying sizes), packed next to one another in 53 // 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 54 // memory. Objects are never relocated, so it is safe to store pointers to them
24 // for the lifetime of the container (unless the object is removed). 55 // for the lifetime of the container (unless the object is removed).
25 // 56 //
26 // Memory is allocated in a series of buffers (with exponential growth). When an 57 // Memory is allocated in a series of buffers (with exponential growth). When an
27 // object is allocated, it is given only the space it requires (possibly with 58 // object is allocated, it is given only the space it requires (possibly with
28 // enough padding to preserve alignment), rather than the maximum possible size. 59 // enough padding to preserve alignment), rather than the maximum possible size.
29 // This allows small and large objects to coexist without wasting much space. 60 // This allows small and large objects to coexist without wasting much space.
30 // 61 //
(...skipping 11 matching lines...) Expand all
42 ContiguousContainerBase(size_t max_object_size, size_t initial_size_bytes); 73 ContiguousContainerBase(size_t max_object_size, size_t initial_size_bytes);
43 ~ContiguousContainerBase(); 74 ~ContiguousContainerBase();
44 75
45 size_t size() const { return elements_.size(); } 76 size_t size() const { return elements_.size(); }
46 bool empty() const { return !size(); } 77 bool empty() const { return !size(); }
47 size_t GetCapacityInBytes() const; 78 size_t GetCapacityInBytes() const;
48 size_t UsedCapacityInBytes() const; 79 size_t UsedCapacityInBytes() const;
49 size_t MemoryUsageInBytes() const; 80 size_t MemoryUsageInBytes() const;
50 81
51 // These do not invoke constructors or destructors. 82 // These do not invoke constructors or destructors.
52 void* Allocate(size_t object_size); 83 void* Allocate(size_t object_size, unsigned log2_alignment);
53 void Clear(); 84 void Clear();
54 void RemoveLast(); 85 void RemoveLast();
55 void Swap(ContiguousContainerBase& other); 86 void Swap(ContiguousContainerBase& other);
56 87
57 std::vector<void*> elements_; 88 std::vector<void*> elements_;
58 89
59 private: 90 private:
60 class Buffer; 91 class Buffer;
61 92
62 Buffer* AllocateNewBufferForNextAllocation(size_t buffer_size); 93 Buffer* AllocateNewBufferForNextAllocation(size_t buffer_size);
63 94
64 std::vector<std::unique_ptr<Buffer>> buffers_; 95 std::vector<std::unique_ptr<Buffer>> buffers_;
65 size_t end_index_; 96 size_t end_index_;
66 size_t max_object_size_; 97 size_t max_object_size_;
67 98
68 DISALLOW_COPY_AND_ASSIGN(ContiguousContainerBase); 99 DISALLOW_COPY_AND_ASSIGN(ContiguousContainerBase);
69 }; 100 };
70 101
71 // For most cases, no alignment stricter than pointer alignment is required. If 102 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 { 103 class ContiguousContainer : public ContiguousContainerBase {
79 private: 104 private:
105 FRIEND_TEST_ALL_PREFIXES(ContiguousContainerTest, Alignment);
106
80 // Declares itself as a forward iterator, but also supports a few more 107 // Declares itself as a forward iterator, but also supports a few more
81 // things. The whole random access iterator interface is a bit much. 108 // things. The whole random access iterator interface is a bit much.
82 template <typename BaseIterator, typename ValueType> 109 template <typename BaseIterator, typename ValueType>
83 class IteratorWrapper 110 class IteratorWrapper
84 : public std::iterator<std::forward_iterator_tag, ValueType> { 111 : public std::iterator<std::forward_iterator_tag, ValueType> {
85 public: 112 public:
86 IteratorWrapper() {} 113 IteratorWrapper() {}
87 bool operator==(const IteratorWrapper& other) const { 114 bool operator==(const IteratorWrapper& other) const {
88 return it_ == other.it_; 115 return it_ == other.it_;
89 } 116 }
(...skipping 29 matching lines...) Expand all
119 IteratorWrapper<std::vector<void*>::iterator, BaseElementType>; 146 IteratorWrapper<std::vector<void*>::iterator, BaseElementType>;
120 using const_iterator = IteratorWrapper<std::vector<void*>::const_iterator, 147 using const_iterator = IteratorWrapper<std::vector<void*>::const_iterator,
121 const BaseElementType>; 148 const BaseElementType>;
122 using reverse_iterator = 149 using reverse_iterator =
123 IteratorWrapper<std::vector<void*>::reverse_iterator, BaseElementType>; 150 IteratorWrapper<std::vector<void*>::reverse_iterator, BaseElementType>;
124 using const_reverse_iterator = 151 using const_reverse_iterator =
125 IteratorWrapper<std::vector<void*>::const_reverse_iterator, 152 IteratorWrapper<std::vector<void*>::const_reverse_iterator,
126 const BaseElementType>; 153 const BaseElementType>;
127 154
128 explicit ContiguousContainer(size_t max_object_size) 155 explicit ContiguousContainer(size_t max_object_size)
129 : ContiguousContainerBase(Align(max_object_size)) {} 156 : ContiguousContainerBase(max_object_size) {}
130 ContiguousContainer(size_t max_object_size, size_t initial_size_bytes) 157 ContiguousContainer(size_t max_object_size, size_t initial_size_bytes)
131 : ContiguousContainerBase(Align(max_object_size), initial_size_bytes) {} 158 : ContiguousContainerBase(max_object_size, initial_size_bytes) {}
132 159
133 ~ContiguousContainer() { 160 ~ContiguousContainer() {
134 for (auto& element : *this) { 161 for (auto& element : *this) {
135 // MSVC incorrectly reports this variable as unused. 162 // MSVC incorrectly reports this variable as unused.
136 (void)element; 163 (void)element;
137 element.~BaseElementType(); 164 element.~BaseElementType();
138 } 165 }
139 } 166 }
140 167
141 using ContiguousContainerBase::size; 168 using ContiguousContainerBase::size;
(...skipping 19 matching lines...) Expand all
161 const BaseElementType& first() const { return *begin(); } 188 const BaseElementType& first() const { return *begin(); }
162 BaseElementType& last() { return *rbegin(); } 189 BaseElementType& last() { return *rbegin(); }
163 const BaseElementType& last() const { return *rbegin(); } 190 const BaseElementType& last() const { return *rbegin(); }
164 BaseElementType& operator[](size_t index) { return *(begin() + index); } 191 BaseElementType& operator[](size_t index) { return *(begin() + index); }
165 const BaseElementType& operator[](size_t index) const { 192 const BaseElementType& operator[](size_t index) const {
166 return *(begin() + index); 193 return *(begin() + index);
167 } 194 }
168 195
169 template <class DerivedElementType, typename... Args> 196 template <class DerivedElementType, typename... Args>
170 DerivedElementType& AllocateAndConstruct(Args&&... args) { 197 DerivedElementType& AllocateAndConstruct(Args&&... args) {
171 static_assert(alignment % ALIGNOF(DerivedElementType) == 0, 198 size_t alloc_size = sizeof(DerivedElementType);
172 "Derived type requires stronger alignment."); 199 return *new (Allocate(alloc_size, Log2Alignment<DerivedElementType>()))
173 size_t alloc_size = Align(sizeof(DerivedElementType));
174 return *new (Allocate(alloc_size))
175 DerivedElementType(std::forward<Args>(args)...); 200 DerivedElementType(std::forward<Args>(args)...);
176 } 201 }
177 202
178 void RemoveLast() { 203 void RemoveLast() {
179 DCHECK(!empty()); 204 DCHECK(!empty());
180 last().~BaseElementType(); 205 last().~BaseElementType();
181 ContiguousContainerBase::RemoveLast(); 206 ContiguousContainerBase::RemoveLast();
182 } 207 }
183 208
184 void Clear() { 209 void Clear() {
185 for (auto& element : *this) { 210 for (auto& element : *this) {
186 // MSVC incorrectly reports this variable as unused. 211 // MSVC incorrectly reports this variable as unused.
187 (void)element; 212 (void)element;
188 element.~BaseElementType(); 213 element.~BaseElementType();
189 } 214 }
190 ContiguousContainerBase::Clear(); 215 ContiguousContainerBase::Clear();
191 } 216 }
192 217
193 void Swap(ContiguousContainer& other) { 218 void Swap(ContiguousContainer& other) {
194 ContiguousContainerBase::Swap(other); 219 ContiguousContainerBase::Swap(other);
195 } 220 }
196 221
197 // Appends a new element using memcpy, then default-constructs a base 222 // Appends a new element using memcpy, then default-constructs a base
198 // element in its place. Use with care. 223 // element in its place. Use with care.
199 BaseElementType& AppendByMoving(BaseElementType* item, size_t size) { 224 BaseElementType& AppendByMoving(BaseElementType* item,
225 size_t size,
226 unsigned log2_alignment) {
200 DCHECK_GE(size, sizeof(BaseElementType)); 227 DCHECK_GE(size, sizeof(BaseElementType));
201 void* new_item = Allocate(size); 228 void* new_item = Allocate(size, log2_alignment);
202 memcpy(new_item, static_cast<void*>(item), size); 229 memcpy(new_item, static_cast<void*>(item), size);
203 new (item) BaseElementType; 230 new (item) BaseElementType;
204 return *static_cast<BaseElementType*>(new_item); 231 return *static_cast<BaseElementType*>(new_item);
205 } 232 }
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 }; 233 };
216 234
217 } // namespace cc 235 } // namespace cc
218 236
219 #endif // CC_BASE_CONTIGUOUS_CONTAINER_H_ 237 #endif // CC_BASE_CONTIGUOUS_CONTAINER_H_
OLDNEW
« no previous file with comments | « no previous file | cc/base/contiguous_container.cc » ('j') | third_party/WebKit/Source/platform/graphics/ContiguousContainer.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698