Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2
* vim: set ts=8 sts=2 et sw=2 tw=80:
3
* This Source Code Form is subject to the terms of the Mozilla Public
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef js_GCVector_h
8
#define js_GCVector_h
9
10
#include "mozilla/Vector.h"
11
12
#include "js/GCPolicyAPI.h"
13
#include "js/RootingAPI.h"
14
#include "js/TracingAPI.h"
15
#include "js/Vector.h"
16
17
namespace JS {
18
19
// A GCVector is a Vector with an additional trace method that knows how
20
// to visit all of the items stored in the Vector. For vectors that contain GC
21
// things, this is usually more convenient than manually iterating and marking
22
// the contents.
23
//
24
// Most types of GC pointers as keys and values can be traced with no extra
25
// infrastructure. For structs and non-gc-pointer members, ensure that there is
26
// a specialization of GCPolicy<T> with an appropriate trace method available
27
// to handle the custom type. Generic helpers can be found in
28
// js/public/TracingAPI.h.
29
//
30
// Note that although this Vector's trace will deal correctly with moved items,
31
// it does not itself know when to barrier or trace items. To function properly
32
// it must either be used with Rooted, or barriered and traced manually.
33
template <typename T, size_t MinInlineCapacity = 0,
34
typename AllocPolicy = js::TempAllocPolicy>
35
class GCVector {
36
mozilla::Vector<T, MinInlineCapacity, AllocPolicy> vector;
37
38
public:
39
using ElementType = T;
40
41
explicit GCVector(AllocPolicy alloc = AllocPolicy())
42
: vector(std::move(alloc)) {}
43
44
GCVector(GCVector&& vec) : vector(std::move(vec.vector)) {}
45
46
GCVector& operator=(GCVector&& vec) {
47
vector = std::move(vec.vector);
48
return *this;
49
}
50
51
size_t length() const { return vector.length(); }
52
bool empty() const { return vector.empty(); }
53
size_t capacity() const { return vector.capacity(); }
54
55
T* begin() { return vector.begin(); }
56
const T* begin() const { return vector.begin(); }
57
58
T* end() { return vector.end(); }
59
const T* end() const { return vector.end(); }
60
61
T& operator[](size_t i) { return vector[i]; }
62
const T& operator[](size_t i) const { return vector[i]; }
63
64
T& back() { return vector.back(); }
65
const T& back() const { return vector.back(); }
66
67
bool initCapacity(size_t cap) { return vector.initCapacity(cap); }
68
MOZ_MUST_USE bool reserve(size_t req) { return vector.reserve(req); }
69
void shrinkBy(size_t amount) { return vector.shrinkBy(amount); }
70
MOZ_MUST_USE bool growBy(size_t amount) { return vector.growBy(amount); }
71
MOZ_MUST_USE bool resize(size_t newLen) { return vector.resize(newLen); }
72
73
void clear() { return vector.clear(); }
74
void clearAndFree() { return vector.clearAndFree(); }
75
76
template <typename U>
77
bool append(U&& item) {
78
return vector.append(std::forward<U>(item));
79
}
80
81
void erase(T* it) { vector.erase(it); }
82
void erase(T* begin, T* end) { vector.erase(begin, end); }
83
template <typename Pred>
84
void eraseIf(Pred pred) {
85
vector.eraseIf(pred);
86
}
87
template <typename U>
88
void eraseIfEqual(const U& u) {
89
vector.eraseIfEqual(u);
90
}
91
92
template <typename... Args>
93
MOZ_MUST_USE bool emplaceBack(Args&&... args) {
94
return vector.emplaceBack(std::forward<Args>(args)...);
95
}
96
97
template <typename U>
98
void infallibleAppend(U&& aU) {
99
return vector.infallibleAppend(std::forward<U>(aU));
100
}
101
void infallibleAppendN(const T& aT, size_t aN) {
102
return vector.infallibleAppendN(aT, aN);
103
}
104
template <typename U>
105
void infallibleAppend(const U* aBegin, const U* aEnd) {
106
return vector.infallibleAppend(aBegin, aEnd);
107
}
108
template <typename U>
109
void infallibleAppend(const U* aBegin, size_t aLength) {
110
return vector.infallibleAppend(aBegin, aLength);
111
}
112
113
template <typename U>
114
MOZ_MUST_USE bool appendAll(const U& aU) {
115
return vector.append(aU.begin(), aU.end());
116
}
117
118
MOZ_MUST_USE bool appendN(const T& val, size_t count) {
119
return vector.appendN(val, count);
120
}
121
122
template <typename U>
123
MOZ_MUST_USE bool append(const U* aBegin, const U* aEnd) {
124
return vector.append(aBegin, aEnd);
125
}
126
template <typename U>
127
MOZ_MUST_USE bool append(const U* aBegin, size_t aLength) {
128
return vector.append(aBegin, aLength);
129
}
130
131
void popBack() { return vector.popBack(); }
132
T popCopy() { return vector.popCopy(); }
133
134
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
135
return vector.sizeOfExcludingThis(mallocSizeOf);
136
}
137
138
size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
139
return vector.sizeOfIncludingThis(mallocSizeOf);
140
}
141
142
void trace(JSTracer* trc) {
143
for (auto& elem : vector) {
144
GCPolicy<T>::trace(trc, &elem, "vector element");
145
}
146
}
147
148
bool needsSweep() const { return !this->empty(); }
149
150
void sweep() {
151
T* src = begin();
152
T* dst = begin();
153
while (src != end()) {
154
if (!GCPolicy<T>::needsSweep(src)) {
155
if (src != dst) {
156
*dst = std::move(*src);
157
}
158
dst++;
159
}
160
src++;
161
}
162
163
MOZ_ASSERT(dst <= end());
164
shrinkBy(end() - dst);
165
}
166
};
167
168
// AllocPolicy is optional. It has a default value declared in TypeDecls.h
169
template <typename T, typename AllocPolicy>
170
class MOZ_STACK_CLASS StackGCVector : public GCVector<T, 8, AllocPolicy> {
171
public:
172
using Base = GCVector<T, 8, AllocPolicy>;
173
174
private:
175
// Inherit constructor from GCVector.
176
using Base::Base;
177
};
178
179
} // namespace JS
180
181
namespace js {
182
183
template <typename Wrapper, typename T, size_t Capacity, typename AllocPolicy>
184
class WrappedPtrOperations<JS::GCVector<T, Capacity, AllocPolicy>, Wrapper> {
185
using Vec = JS::GCVector<T, Capacity, AllocPolicy>;
186
const Vec& vec() const { return static_cast<const Wrapper*>(this)->get(); }
187
188
public:
189
const AllocPolicy& allocPolicy() const { return vec().allocPolicy(); }
190
size_t length() const { return vec().length(); }
191
bool empty() const { return vec().empty(); }
192
size_t capacity() const { return vec().capacity(); }
193
const T* begin() const { return vec().begin(); }
194
const T* end() const { return vec().end(); }
195
const T& back() const { return vec().back(); }
196
197
JS::Handle<T> operator[](size_t aIndex) const {
198
return JS::Handle<T>::fromMarkedLocation(&vec().operator[](aIndex));
199
}
200
};
201
202
template <typename Wrapper, typename T, size_t Capacity, typename AllocPolicy>
203
class MutableWrappedPtrOperations<JS::GCVector<T, Capacity, AllocPolicy>,
204
Wrapper>
205
: public WrappedPtrOperations<JS::GCVector<T, Capacity, AllocPolicy>,
206
Wrapper> {
207
using Vec = JS::GCVector<T, Capacity, AllocPolicy>;
208
const Vec& vec() const { return static_cast<const Wrapper*>(this)->get(); }
209
Vec& vec() { return static_cast<Wrapper*>(this)->get(); }
210
211
public:
212
const AllocPolicy& allocPolicy() const { return vec().allocPolicy(); }
213
AllocPolicy& allocPolicy() { return vec().allocPolicy(); }
214
const T* begin() const { return vec().begin(); }
215
T* begin() { return vec().begin(); }
216
const T* end() const { return vec().end(); }
217
T* end() { return vec().end(); }
218
const T& back() const { return vec().back(); }
219
T& back() { return vec().back(); }
220
221
JS::Handle<T> operator[](size_t aIndex) const {
222
return JS::Handle<T>::fromMarkedLocation(&vec().operator[](aIndex));
223
}
224
JS::MutableHandle<T> operator[](size_t aIndex) {
225
return JS::MutableHandle<T>::fromMarkedLocation(&vec().operator[](aIndex));
226
}
227
228
MOZ_MUST_USE bool initCapacity(size_t aRequest) {
229
return vec().initCapacity(aRequest);
230
}
231
MOZ_MUST_USE bool reserve(size_t aRequest) { return vec().reserve(aRequest); }
232
void shrinkBy(size_t aIncr) { vec().shrinkBy(aIncr); }
233
MOZ_MUST_USE bool growBy(size_t aIncr) { return vec().growBy(aIncr); }
234
MOZ_MUST_USE bool resize(size_t aNewLength) {
235
return vec().resize(aNewLength);
236
}
237
MOZ_MUST_USE bool growByUninitialized(size_t aIncr) {
238
return vec().growByUninitialized(aIncr);
239
}
240
void infallibleGrowByUninitialized(size_t aIncr) {
241
vec().infallibleGrowByUninitialized(aIncr);
242
}
243
MOZ_MUST_USE bool resizeUninitialized(size_t aNewLength) {
244
return vec().resizeUninitialized(aNewLength);
245
}
246
void clear() { vec().clear(); }
247
void clearAndFree() { vec().clearAndFree(); }
248
template <typename U>
249
MOZ_MUST_USE bool append(U&& aU) {
250
return vec().append(std::forward<U>(aU));
251
}
252
template <typename... Args>
253
MOZ_MUST_USE bool emplaceBack(Args&&... aArgs) {
254
return vec().emplaceBack(std::forward<Args>(aArgs)...);
255
}
256
template <typename U>
257
MOZ_MUST_USE bool appendAll(const U& aU) {
258
return vec().appendAll(aU);
259
}
260
MOZ_MUST_USE bool appendN(const T& aT, size_t aN) {
261
return vec().appendN(aT, aN);
262
}
263
template <typename U>
264
MOZ_MUST_USE bool append(const U* aBegin, const U* aEnd) {
265
return vec().append(aBegin, aEnd);
266
}
267
template <typename U>
268
MOZ_MUST_USE bool append(const U* aBegin, size_t aLength) {
269
return vec().append(aBegin, aLength);
270
}
271
template <typename U>
272
void infallibleAppend(U&& aU) {
273
vec().infallibleAppend(std::forward<U>(aU));
274
}
275
void infallibleAppendN(const T& aT, size_t aN) {
276
vec().infallibleAppendN(aT, aN);
277
}
278
template <typename U>
279
void infallibleAppend(const U* aBegin, const U* aEnd) {
280
vec().infallibleAppend(aBegin, aEnd);
281
}
282
template <typename U>
283
void infallibleAppend(const U* aBegin, size_t aLength) {
284
vec().infallibleAppend(aBegin, aLength);
285
}
286
void popBack() { vec().popBack(); }
287
T popCopy() { return vec().popCopy(); }
288
template <typename U>
289
T* insert(T* aP, U&& aVal) {
290
return vec().insert(aP, std::forward<U>(aVal));
291
}
292
void erase(T* aT) { vec().erase(aT); }
293
void erase(T* aBegin, T* aEnd) { vec().erase(aBegin, aEnd); }
294
template <typename Pred>
295
void eraseIf(Pred pred) {
296
vec().eraseIf(pred);
297
}
298
template <typename U>
299
void eraseIfEqual(const U& u) {
300
vec().eraseIfEqual(u);
301
}
302
};
303
304
template <typename Wrapper, typename T, typename AllocPolicy>
305
class WrappedPtrOperations<JS::StackGCVector<T, AllocPolicy>, Wrapper>
306
: public WrappedPtrOperations<
307
typename JS::StackGCVector<T, AllocPolicy>::Base, Wrapper> {};
308
309
template <typename Wrapper, typename T, typename AllocPolicy>
310
class MutableWrappedPtrOperations<JS::StackGCVector<T, AllocPolicy>, Wrapper>
311
: public MutableWrappedPtrOperations<
312
typename JS::StackGCVector<T, AllocPolicy>::Base, Wrapper> {};
313
314
} // namespace js
315
316
namespace JS {
317
318
// An automatically rooted GCVector for stack use.
319
template <typename T>
320
class RootedVector : public Rooted<StackGCVector<T>> {
321
using Vec = StackGCVector<T>;
322
using Base = Rooted<Vec>;
323
324
public:
325
explicit RootedVector(JSContext* cx) : Base(cx, Vec(cx)) {}
326
};
327
328
// For use in rust code, an analog to RootedVector that doesn't require
329
// instances to be destroyed in LIFO order.
330
template <typename T>
331
class PersistentRootedVector : public PersistentRooted<StackGCVector<T>> {
332
using Vec = StackGCVector<T>;
333
using Base = PersistentRooted<Vec>;
334
335
public:
336
explicit PersistentRootedVector(JSContext* cx) : Base(cx, Vec(cx)) {}
337
};
338
339
} // namespace JS
340
341
#endif // js_GCVector_h