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/. */
10 #include "mozilla/Assertions.h" // MOZ_ASSERT
11 #include "mozilla/Attributes.h" // MOZ_STACK_CLASS
12 #include "mozilla/MemoryReporting.h" // MallocSizeOf
13 #include "mozilla/Span.h"
14 #include "mozilla/Vector.h"
16 #include <stddef.h> // size_t
17 #include <utility> // forward, move
19 #include "js/AllocPolicy.h"
20 #include "js/GCPolicyAPI.h"
21 #include "js/RootingAPI.h"
28 // A GCVector is a Vector with an additional trace method that knows how
29 // to visit all of the items stored in the Vector. For vectors that contain GC
30 // things, this is usually more convenient than manually iterating and marking
33 // Most types of GC pointers as keys and values can be traced with no extra
34 // infrastructure. For structs and non-gc-pointer members, ensure that there is
35 // a specialization of GCPolicy<T> with an appropriate trace method available
36 // to handle the custom type. Generic helpers can be found in
37 // js/public/TracingAPI.h.
39 // Note that although this Vector's trace will deal correctly with moved items,
40 // it does not itself know when to barrier or trace items. To function properly
41 // it must either be used with Rooted, or barriered and traced manually.
42 template <typename T
, size_t MinInlineCapacity
= 0,
43 typename AllocPolicy
= js::TempAllocPolicy
>
45 mozilla::Vector
<T
, MinInlineCapacity
, AllocPolicy
> vector
;
48 using ElementType
= T
;
50 explicit GCVector(AllocPolicy alloc
) : vector(std::move(alloc
)) {}
51 GCVector() : GCVector(AllocPolicy()) {}
53 GCVector(GCVector
&& vec
) : vector(std::move(vec
.vector
)) {}
55 GCVector
& operator=(GCVector
&& vec
) {
56 vector
= std::move(vec
.vector
);
60 size_t length() const { return vector
.length(); }
61 bool empty() const { return vector
.empty(); }
62 size_t capacity() const { return vector
.capacity(); }
64 T
* begin() { return vector
.begin(); }
65 const T
* begin() const { return vector
.begin(); }
67 T
* end() { return vector
.end(); }
68 const T
* end() const { return vector
.end(); }
70 T
& operator[](size_t i
) { return vector
[i
]; }
71 const T
& operator[](size_t i
) const { return vector
[i
]; }
73 T
& back() { return vector
.back(); }
74 const T
& back() const { return vector
.back(); }
76 operator mozilla::Span
<T
>() { return vector
; }
77 operator mozilla::Span
<const T
>() const { return vector
; }
79 bool initCapacity(size_t cap
) { return vector
.initCapacity(cap
); }
80 [[nodiscard
]] bool reserve(size_t req
) { return vector
.reserve(req
); }
81 void shrinkBy(size_t amount
) { return vector
.shrinkBy(amount
); }
82 void shrinkTo(size_t newLen
) { return vector
.shrinkTo(newLen
); }
83 [[nodiscard
]] bool growBy(size_t amount
) { return vector
.growBy(amount
); }
84 [[nodiscard
]] bool resize(size_t newLen
) { return vector
.resize(newLen
); }
86 void clear() { return vector
.clear(); }
87 void clearAndFree() { return vector
.clearAndFree(); }
90 bool append(U
&& item
) {
91 return vector
.append(std::forward
<U
>(item
));
94 void erase(T
* it
) { vector
.erase(it
); }
95 void erase(T
* begin
, T
* end
) { vector
.erase(begin
, end
); }
96 template <typename Pred
>
97 void eraseIf(Pred pred
) {
100 template <typename U
>
101 void eraseIfEqual(const U
& u
) {
102 vector
.eraseIfEqual(u
);
105 template <typename
... Args
>
106 [[nodiscard
]] bool emplaceBack(Args
&&... args
) {
107 return vector
.emplaceBack(std::forward
<Args
>(args
)...);
110 template <typename
... Args
>
111 void infallibleEmplaceBack(Args
&&... args
) {
112 vector
.infallibleEmplaceBack(std::forward
<Args
>(args
)...);
115 template <typename U
>
116 void infallibleAppend(U
&& aU
) {
117 return vector
.infallibleAppend(std::forward
<U
>(aU
));
119 void infallibleAppendN(const T
& aT
, size_t aN
) {
120 return vector
.infallibleAppendN(aT
, aN
);
122 template <typename U
>
123 void infallibleAppend(const U
* aBegin
, const U
* aEnd
) {
124 return vector
.infallibleAppend(aBegin
, aEnd
);
126 template <typename U
>
127 void infallibleAppend(const U
* aBegin
, size_t aLength
) {
128 return vector
.infallibleAppend(aBegin
, aLength
);
131 template <typename U
>
132 [[nodiscard
]] bool appendAll(const U
& aU
) {
133 return vector
.append(aU
.begin(), aU
.end());
135 template <typename T2
, size_t MinInlineCapacity2
, typename AllocPolicy2
>
136 [[nodiscard
]] bool appendAll(
137 GCVector
<T2
, MinInlineCapacity2
, AllocPolicy2
>&& aU
) {
138 return vector
.appendAll(aU
.begin(), aU
.end());
141 [[nodiscard
]] bool appendN(const T
& val
, size_t count
) {
142 return vector
.appendN(val
, count
);
145 template <typename U
>
146 [[nodiscard
]] bool append(const U
* aBegin
, const U
* aEnd
) {
147 return vector
.append(aBegin
, aEnd
);
149 template <typename U
>
150 [[nodiscard
]] bool append(const U
* aBegin
, size_t aLength
) {
151 return vector
.append(aBegin
, aLength
);
154 void popBack() { return vector
.popBack(); }
155 T
popCopy() { return vector
.popCopy(); }
157 size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf
) const {
158 return vector
.sizeOfExcludingThis(mallocSizeOf
);
161 size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf
) const {
162 return vector
.sizeOfIncludingThis(mallocSizeOf
);
165 void trace(JSTracer
* trc
) {
166 for (auto& elem
: vector
) {
167 GCPolicy
<T
>::trace(trc
, &elem
, "vector element");
171 bool traceWeak(JSTracer
* trc
) {
173 [trc
](T
& elem
) { return !GCPolicy
<T
>::traceWeak(trc
, &elem
); });
177 // Like eraseIf, but may mutate the contents of the vector.
178 template <typename Pred
>
179 void mutableEraseIf(Pred pred
) {
182 while (src
!= end()) {
185 *dst
= std::move(*src
);
192 MOZ_ASSERT(dst
<= end());
193 shrinkBy(end() - dst
);
197 // AllocPolicy is optional. It has a default value declared in TypeDecls.h
198 template <typename T
, typename AllocPolicy
>
199 class MOZ_STACK_CLASS StackGCVector
: public GCVector
<T
, 8, AllocPolicy
> {
201 using Base
= GCVector
<T
, 8, AllocPolicy
>;
204 // Inherit constructor from GCVector.
212 template <typename Wrapper
, typename T
, size_t Capacity
, typename AllocPolicy
>
213 class WrappedPtrOperations
<JS::GCVector
<T
, Capacity
, AllocPolicy
>, Wrapper
> {
214 using Vec
= JS::GCVector
<T
, Capacity
, AllocPolicy
>;
215 const Vec
& vec() const { return static_cast<const Wrapper
*>(this)->get(); }
218 const AllocPolicy
& allocPolicy() const { return vec().allocPolicy(); }
219 size_t length() const { return vec().length(); }
220 bool empty() const { return vec().empty(); }
221 size_t capacity() const { return vec().capacity(); }
222 const T
* begin() const { return vec().begin(); }
223 const T
* end() const { return vec().end(); }
224 const T
& back() const { return vec().back(); }
226 JS::Handle
<T
> operator[](size_t aIndex
) const {
227 return JS::Handle
<T
>::fromMarkedLocation(&vec().operator[](aIndex
));
231 template <typename Wrapper
, typename T
, size_t Capacity
, typename AllocPolicy
>
232 class MutableWrappedPtrOperations
<JS::GCVector
<T
, Capacity
, AllocPolicy
>,
234 : public WrappedPtrOperations
<JS::GCVector
<T
, Capacity
, AllocPolicy
>,
236 using Vec
= JS::GCVector
<T
, Capacity
, AllocPolicy
>;
237 const Vec
& vec() const { return static_cast<const Wrapper
*>(this)->get(); }
238 Vec
& vec() { return static_cast<Wrapper
*>(this)->get(); }
241 const AllocPolicy
& allocPolicy() const { return vec().allocPolicy(); }
242 AllocPolicy
& allocPolicy() { return vec().allocPolicy(); }
243 const T
* begin() const { return vec().begin(); }
244 T
* begin() { return vec().begin(); }
245 const T
* end() const { return vec().end(); }
246 T
* end() { return vec().end(); }
247 const T
& back() const { return vec().back(); }
248 T
& back() { return vec().back(); }
250 JS::Handle
<T
> operator[](size_t aIndex
) const {
251 return JS::Handle
<T
>::fromMarkedLocation(&vec().operator[](aIndex
));
253 JS::MutableHandle
<T
> operator[](size_t aIndex
) {
254 return JS::MutableHandle
<T
>::fromMarkedLocation(&vec().operator[](aIndex
));
257 [[nodiscard
]] bool initCapacity(size_t aRequest
) {
258 return vec().initCapacity(aRequest
);
260 [[nodiscard
]] bool reserve(size_t aRequest
) {
261 return vec().reserve(aRequest
);
263 void shrinkBy(size_t aIncr
) { vec().shrinkBy(aIncr
); }
264 [[nodiscard
]] bool growBy(size_t aIncr
) { return vec().growBy(aIncr
); }
265 [[nodiscard
]] bool resize(size_t aNewLength
) {
266 return vec().resize(aNewLength
);
268 void clear() { vec().clear(); }
269 void clearAndFree() { vec().clearAndFree(); }
270 template <typename U
>
271 [[nodiscard
]] bool append(U
&& aU
) {
272 return vec().append(std::forward
<U
>(aU
));
274 template <typename
... Args
>
275 [[nodiscard
]] bool emplaceBack(Args
&&... aArgs
) {
276 return vec().emplaceBack(std::forward
<Args
>(aArgs
)...);
278 template <typename
... Args
>
279 void infallibleEmplaceBack(Args
&&... args
) {
280 vec().infallibleEmplaceBack(std::forward
<Args
>(args
)...);
282 template <typename U
>
283 [[nodiscard
]] bool appendAll(U
&& aU
) {
284 return vec().appendAll(aU
);
286 [[nodiscard
]] bool appendN(const T
& aT
, size_t aN
) {
287 return vec().appendN(aT
, aN
);
289 template <typename U
>
290 [[nodiscard
]] bool append(const U
* aBegin
, const U
* aEnd
) {
291 return vec().append(aBegin
, aEnd
);
293 template <typename U
>
294 [[nodiscard
]] bool append(const U
* aBegin
, size_t aLength
) {
295 return vec().append(aBegin
, aLength
);
297 template <typename U
>
298 void infallibleAppend(U
&& aU
) {
299 vec().infallibleAppend(std::forward
<U
>(aU
));
301 void infallibleAppendN(const T
& aT
, size_t aN
) {
302 vec().infallibleAppendN(aT
, aN
);
304 template <typename U
>
305 void infallibleAppend(const U
* aBegin
, const U
* aEnd
) {
306 vec().infallibleAppend(aBegin
, aEnd
);
308 template <typename U
>
309 void infallibleAppend(const U
* aBegin
, size_t aLength
) {
310 vec().infallibleAppend(aBegin
, aLength
);
312 void popBack() { vec().popBack(); }
313 T
popCopy() { return vec().popCopy(); }
314 void erase(T
* aT
) { vec().erase(aT
); }
315 void erase(T
* aBegin
, T
* aEnd
) { vec().erase(aBegin
, aEnd
); }
316 template <typename Pred
>
317 void eraseIf(Pred pred
) {
320 template <typename U
>
321 void eraseIfEqual(const U
& u
) {
322 vec().eraseIfEqual(u
);
326 template <typename Wrapper
, typename T
, typename AllocPolicy
>
327 class WrappedPtrOperations
<JS::StackGCVector
<T
, AllocPolicy
>, Wrapper
>
328 : public WrappedPtrOperations
<
329 typename
JS::StackGCVector
<T
, AllocPolicy
>::Base
, Wrapper
> {};
331 template <typename Wrapper
, typename T
, typename AllocPolicy
>
332 class MutableWrappedPtrOperations
<JS::StackGCVector
<T
, AllocPolicy
>, Wrapper
>
333 : public MutableWrappedPtrOperations
<
334 typename
JS::StackGCVector
<T
, AllocPolicy
>::Base
, Wrapper
> {};
340 // An automatically rooted GCVector for stack use.
341 template <typename T
>
342 class RootedVector
: public Rooted
<StackGCVector
<T
>> {
343 using Vec
= StackGCVector
<T
>;
344 using Base
= Rooted
<Vec
>;
347 explicit RootedVector(JSContext
* cx
) : Base(cx
, Vec(cx
)) {}
350 // For use in rust code, an analog to RootedVector that doesn't require
351 // instances to be destroyed in LIFO order.
352 template <typename T
>
353 class PersistentRootedVector
: public PersistentRooted
<StackGCVector
<T
>> {
354 using Vec
= StackGCVector
<T
>;
355 using Base
= PersistentRooted
<Vec
>;
358 explicit PersistentRootedVector(JSContext
* cx
) : Base(cx
, Vec(cx
)) {}
363 #endif // js_GCVector_h