forked from crosswalk-project/chromium-crosswalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontiguous_container.h
219 lines (188 loc) · 7.62 KB
/
contiguous_container.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CC_BASE_CONTIGUOUS_CONTAINER_H_
#define CC_BASE_CONTIGUOUS_CONTAINER_H_
#include <stddef.h>
#include <memory>
#include <utility>
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/stl_util.h"
#include "cc/base/cc_export.h"
namespace cc {
// ContiguousContainer is a container which stores a list of heterogeneous
// objects (in particular, of varying sizes), packed next to one another in
// memory. Objects are never relocated, so it is safe to store pointers to them
// for the lifetime of the container (unless the object is removed).
//
// Memory is allocated in a series of buffers (with exponential growth). When an
// object is allocated, it is given only the space it requires (possibly with
// enough padding to preserve alignment), rather than the maximum possible size.
// This allows small and large objects to coexist without wasting much space.
//
// Since it stores pointers to all of the objects it allocates in a vector, it
// supports efficient iteration and indexing. However, for mutation the
// supported operations are limited to appending to, and removing from, the end
// of the list.
//
// Clients should instantiate ContiguousContainer; ContiguousContainerBase is an
// artifact of the implementation.
class CC_EXPORT ContiguousContainerBase {
protected:
explicit ContiguousContainerBase(size_t max_object_size);
ContiguousContainerBase(size_t max_object_size, size_t initial_size_bytes);
~ContiguousContainerBase();
size_t size() const { return elements_.size(); }
bool empty() const { return !size(); }
size_t GetCapacityInBytes() const;
size_t UsedCapacityInBytes() const;
size_t MemoryUsageInBytes() const;
// These do not invoke constructors or destructors.
void* Allocate(size_t object_size);
void Clear();
void RemoveLast();
void Swap(ContiguousContainerBase& other);
std::vector<void*> elements_;
private:
class Buffer;
Buffer* AllocateNewBufferForNextAllocation(size_t buffer_size);
std::vector<std::unique_ptr<Buffer>> buffers_;
size_t end_index_;
size_t max_object_size_;
DISALLOW_COPY_AND_ASSIGN(ContiguousContainerBase);
};
// For most cases, no alignment stricter than pointer alignment is required. If
// one of the derived classes has stronger alignment requirements (and the
// static_assert fires), set alignment to the least common multiple of the
// derived class alignments. For small structs without pointers, it may be
// possible to reduce alignment for tighter packing.
template <class BaseElementType, unsigned alignment = sizeof(void*)>
class ContiguousContainer : public ContiguousContainerBase {
private:
// Declares itself as a forward iterator, but also supports a few more
// things. The whole random access iterator interface is a bit much.
template <typename BaseIterator, typename ValueType>
class IteratorWrapper
: public std::iterator<std::forward_iterator_tag, ValueType> {
public:
IteratorWrapper() {}
bool operator==(const IteratorWrapper& other) const {
return it_ == other.it_;
}
bool operator!=(const IteratorWrapper& other) const {
return it_ != other.it_;
}
ValueType& operator*() const { return *static_cast<ValueType*>(*it_); }
ValueType* operator->() const { return &operator*(); }
IteratorWrapper operator+(std::ptrdiff_t n) const {
return IteratorWrapper(it_ + n);
}
IteratorWrapper operator++(int) {
IteratorWrapper tmp = *this;
++it_;
return tmp;
}
std::ptrdiff_t operator-(const IteratorWrapper& other) const {
return it_ - other.it_;
}
IteratorWrapper& operator++() {
++it_;
return *this;
}
private:
explicit IteratorWrapper(const BaseIterator& it) : it_(it) {}
BaseIterator it_;
friend class ContiguousContainer;
};
public:
using iterator =
IteratorWrapper<std::vector<void*>::iterator, BaseElementType>;
using const_iterator = IteratorWrapper<std::vector<void*>::const_iterator,
const BaseElementType>;
using reverse_iterator =
IteratorWrapper<std::vector<void*>::reverse_iterator, BaseElementType>;
using const_reverse_iterator =
IteratorWrapper<std::vector<void*>::const_reverse_iterator,
const BaseElementType>;
explicit ContiguousContainer(size_t max_object_size)
: ContiguousContainerBase(Align(max_object_size)) {}
ContiguousContainer(size_t max_object_size, size_t initial_size_bytes)
: ContiguousContainerBase(Align(max_object_size), initial_size_bytes) {}
~ContiguousContainer() {
for (auto& element : *this) {
// MSVC incorrectly reports this variable as unused.
(void)element;
element.~BaseElementType();
}
}
using ContiguousContainerBase::size;
using ContiguousContainerBase::empty;
using ContiguousContainerBase::GetCapacityInBytes;
using ContiguousContainerBase::UsedCapacityInBytes;
using ContiguousContainerBase::MemoryUsageInBytes;
iterator begin() { return iterator(elements_.begin()); }
iterator end() { return iterator(elements_.end()); }
const_iterator begin() const { return const_iterator(elements_.begin()); }
const_iterator end() const { return const_iterator(elements_.end()); }
reverse_iterator rbegin() { return reverse_iterator(elements_.rbegin()); }
reverse_iterator rend() { return reverse_iterator(elements_.rend()); }
const_reverse_iterator rbegin() const {
return const_reverse_iterator(elements_.rbegin());
}
const_reverse_iterator rend() const {
return const_reverse_iterator(elements_.rend());
}
BaseElementType& first() { return *begin(); }
const BaseElementType& first() const { return *begin(); }
BaseElementType& last() { return *rbegin(); }
const BaseElementType& last() const { return *rbegin(); }
BaseElementType& operator[](size_t index) { return *(begin() + index); }
const BaseElementType& operator[](size_t index) const {
return *(begin() + index);
}
template <class DerivedElementType, typename... Args>
DerivedElementType& AllocateAndConstruct(Args&&... args) {
static_assert(alignment % ALIGNOF(DerivedElementType) == 0,
"Derived type requires stronger alignment.");
size_t alloc_size = Align(sizeof(DerivedElementType));
return *new (Allocate(alloc_size))
DerivedElementType(std::forward<Args>(args)...);
}
void RemoveLast() {
DCHECK(!empty());
last().~BaseElementType();
ContiguousContainerBase::RemoveLast();
}
void Clear() {
for (auto& element : *this) {
// MSVC incorrectly reports this variable as unused.
(void)element;
element.~BaseElementType();
}
ContiguousContainerBase::Clear();
}
void Swap(ContiguousContainer& other) {
ContiguousContainerBase::Swap(other);
}
// Appends a new element using memcpy, then default-constructs a base
// element in its place. Use with care.
BaseElementType& AppendByMoving(BaseElementType* item, size_t size) {
DCHECK_GE(size, sizeof(BaseElementType));
void* new_item = Allocate(size);
memcpy(new_item, static_cast<void*>(item), size);
new (item) BaseElementType;
return *static_cast<BaseElementType*>(new_item);
}
private:
static size_t Align(size_t size) {
size_t aligned_size = alignment * ((size + alignment - 1) / alignment);
DCHECK_EQ(aligned_size % alignment, 0u);
DCHECK_GE(aligned_size, size);
DCHECK_LT(aligned_size, size + alignment);
return aligned_size;
}
};
} // namespace cc
#endif // CC_BASE_CONTIGUOUS_CONTAINER_H_