Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / js / public / GCVector.h
blob424f689a9103f2b142f0ff079a01df5ff745b7aa
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/. */
7 #ifndef js_GCVector_h
8 #define js_GCVector_h
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"
23 class JSTracer;
24 struct JSContext;
26 namespace JS {
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
31 // the contents.
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>
44 class GCVector {
45 mozilla::Vector<T, MinInlineCapacity, AllocPolicy> vector;
47 public:
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);
57 return *this;
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(); }
89 template <typename U>
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) {
98 vector.eraseIf(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) {
172 mutableEraseIf(
173 [trc](T& elem) { return !GCPolicy<T>::traceWeak(trc, &elem); });
174 return !empty();
177 // Like eraseIf, but may mutate the contents of the vector. Iterates from
178 // |startIndex| to the last element of the vector.
179 template <typename Pred>
180 void mutableEraseIf(Pred pred, size_t startIndex = 0) {
181 MOZ_ASSERT(startIndex <= length());
183 T* src = begin() + startIndex;
184 T* dst = src;
185 while (src != end()) {
186 if (!pred(*src)) {
187 if (src != dst) {
188 *dst = std::move(*src);
190 dst++;
192 src++;
195 MOZ_ASSERT(dst <= end());
196 shrinkBy(end() - dst);
200 // AllocPolicy is optional. It has a default value declared in TypeDecls.h
201 template <typename T, typename AllocPolicy>
202 class MOZ_STACK_CLASS StackGCVector : public GCVector<T, 8, AllocPolicy> {
203 public:
204 using Base = GCVector<T, 8, AllocPolicy>;
206 private:
207 // Inherit constructor from GCVector.
208 using Base::Base;
211 } // namespace JS
213 namespace js {
215 template <typename Wrapper, typename T, size_t Capacity, typename AllocPolicy>
216 class WrappedPtrOperations<JS::GCVector<T, Capacity, AllocPolicy>, Wrapper> {
217 using Vec = JS::GCVector<T, Capacity, AllocPolicy>;
218 const Vec& vec() const { return static_cast<const Wrapper*>(this)->get(); }
220 public:
221 const AllocPolicy& allocPolicy() const { return vec().allocPolicy(); }
222 size_t length() const { return vec().length(); }
223 bool empty() const { return vec().empty(); }
224 size_t capacity() const { return vec().capacity(); }
225 const T* begin() const { return vec().begin(); }
226 const T* end() const { return vec().end(); }
227 const T& back() const { return vec().back(); }
229 JS::Handle<T> operator[](size_t aIndex) const {
230 return JS::Handle<T>::fromMarkedLocation(&vec().operator[](aIndex));
234 template <typename Wrapper, typename T, size_t Capacity, typename AllocPolicy>
235 class MutableWrappedPtrOperations<JS::GCVector<T, Capacity, AllocPolicy>,
236 Wrapper>
237 : public WrappedPtrOperations<JS::GCVector<T, Capacity, AllocPolicy>,
238 Wrapper> {
239 using Vec = JS::GCVector<T, Capacity, AllocPolicy>;
240 const Vec& vec() const { return static_cast<const Wrapper*>(this)->get(); }
241 Vec& vec() { return static_cast<Wrapper*>(this)->get(); }
243 public:
244 const AllocPolicy& allocPolicy() const { return vec().allocPolicy(); }
245 AllocPolicy& allocPolicy() { return vec().allocPolicy(); }
246 const T* begin() const { return vec().begin(); }
247 T* begin() { return vec().begin(); }
248 const T* end() const { return vec().end(); }
249 T* end() { return vec().end(); }
250 const T& back() const { return vec().back(); }
251 T& back() { return vec().back(); }
253 JS::Handle<T> operator[](size_t aIndex) const {
254 return JS::Handle<T>::fromMarkedLocation(&vec().operator[](aIndex));
256 JS::MutableHandle<T> operator[](size_t aIndex) {
257 return JS::MutableHandle<T>::fromMarkedLocation(&vec().operator[](aIndex));
260 [[nodiscard]] bool initCapacity(size_t aRequest) {
261 return vec().initCapacity(aRequest);
263 [[nodiscard]] bool reserve(size_t aRequest) {
264 return vec().reserve(aRequest);
266 void shrinkBy(size_t aIncr) { vec().shrinkBy(aIncr); }
267 [[nodiscard]] bool growBy(size_t aIncr) { return vec().growBy(aIncr); }
268 [[nodiscard]] bool resize(size_t aNewLength) {
269 return vec().resize(aNewLength);
271 void clear() { vec().clear(); }
272 void clearAndFree() { vec().clearAndFree(); }
273 template <typename U>
274 [[nodiscard]] bool append(U&& aU) {
275 return vec().append(std::forward<U>(aU));
277 template <typename... Args>
278 [[nodiscard]] bool emplaceBack(Args&&... aArgs) {
279 return vec().emplaceBack(std::forward<Args>(aArgs)...);
281 template <typename... Args>
282 void infallibleEmplaceBack(Args&&... args) {
283 vec().infallibleEmplaceBack(std::forward<Args>(args)...);
285 template <typename U>
286 [[nodiscard]] bool appendAll(U&& aU) {
287 return vec().appendAll(aU);
289 [[nodiscard]] bool appendN(const T& aT, size_t aN) {
290 return vec().appendN(aT, aN);
292 template <typename U>
293 [[nodiscard]] bool append(const U* aBegin, const U* aEnd) {
294 return vec().append(aBegin, aEnd);
296 template <typename U>
297 [[nodiscard]] bool append(const U* aBegin, size_t aLength) {
298 return vec().append(aBegin, aLength);
300 template <typename U>
301 void infallibleAppend(U&& aU) {
302 vec().infallibleAppend(std::forward<U>(aU));
304 void infallibleAppendN(const T& aT, size_t aN) {
305 vec().infallibleAppendN(aT, aN);
307 template <typename U>
308 void infallibleAppend(const U* aBegin, const U* aEnd) {
309 vec().infallibleAppend(aBegin, aEnd);
311 template <typename U>
312 void infallibleAppend(const U* aBegin, size_t aLength) {
313 vec().infallibleAppend(aBegin, aLength);
315 void popBack() { vec().popBack(); }
316 T popCopy() { return vec().popCopy(); }
317 void erase(T* aT) { vec().erase(aT); }
318 void erase(T* aBegin, T* aEnd) { vec().erase(aBegin, aEnd); }
319 template <typename Pred>
320 void eraseIf(Pred pred) {
321 vec().eraseIf(pred);
323 template <typename U>
324 void eraseIfEqual(const U& u) {
325 vec().eraseIfEqual(u);
329 template <typename Wrapper, typename T, typename AllocPolicy>
330 class WrappedPtrOperations<JS::StackGCVector<T, AllocPolicy>, Wrapper>
331 : public WrappedPtrOperations<
332 typename JS::StackGCVector<T, AllocPolicy>::Base, Wrapper> {};
334 template <typename Wrapper, typename T, typename AllocPolicy>
335 class MutableWrappedPtrOperations<JS::StackGCVector<T, AllocPolicy>, Wrapper>
336 : public MutableWrappedPtrOperations<
337 typename JS::StackGCVector<T, AllocPolicy>::Base, Wrapper> {};
339 } // namespace js
341 namespace JS {
343 // An automatically rooted GCVector for stack use.
344 template <typename T>
345 class RootedVector : public Rooted<StackGCVector<T>> {
346 using Vec = StackGCVector<T>;
347 using Base = Rooted<Vec>;
349 public:
350 explicit RootedVector(JSContext* cx) : Base(cx, Vec(cx)) {}
353 // For use in rust code, an analog to RootedVector that doesn't require
354 // instances to be destroyed in LIFO order.
355 template <typename T>
356 class PersistentRootedVector : public PersistentRooted<StackGCVector<T>> {
357 using Vec = StackGCVector<T>;
358 using Base = PersistentRooted<Vec>;
360 public:
361 explicit PersistentRootedVector(JSContext* cx) : Base(cx, Vec(cx)) {}
364 } // namespace JS
366 #endif // js_GCVector_h