| 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_TraceableFifo_h |
| 8 |
#define js_TraceableFifo_h |
| 9 |
|
| 10 |
#include "ds/Fifo.h" |
| 11 |
#include "js/RootingAPI.h" |
| 12 |
#include "js/TracingAPI.h" |
| 13 |
|
| 14 |
namespace js { |
| 15 |
|
| 16 |
// A TraceableFifo is a Fifo with an additional trace method that knows how to |
| 17 |
// visit all of the items stored in the Fifo. For Fifos that contain GC things, |
| 18 |
// this is usually more convenient than manually iterating and marking the |
| 19 |
// contents. |
| 20 |
// |
| 21 |
// Most types of GC pointers as keys and values can be traced with no extra |
| 22 |
// infrastructure. For structs and non-gc-pointer members, ensure that there is |
| 23 |
// a specialization of GCPolicy<T> with an appropriate trace method available |
| 24 |
// to handle the custom type. Generic helpers can be found in |
| 25 |
// js/public/TracingAPI.h. Generic helpers can be found in |
| 26 |
// js/public/TracingAPI.h. |
| 27 |
// |
| 28 |
// Note that although this Fifo's trace will deal correctly with moved items, it |
| 29 |
// does not itself know when to barrier or trace items. To function properly it |
| 30 |
// must either be used with Rooted, or barriered and traced manually. |
| 31 |
template <typename T, size_t MinInlineCapacity = 0, |
| 32 |
typename AllocPolicy = TempAllocPolicy> |
| 33 |
class TraceableFifo : public js::Fifo<T, MinInlineCapacity, AllocPolicy> { |
| 34 |
using Base = js::Fifo<T, MinInlineCapacity, AllocPolicy>; |
| 35 |
|
| 36 |
public: |
| 37 |
explicit TraceableFifo(AllocPolicy alloc = AllocPolicy()) |
| 38 |
: Base(std::move(alloc)) {} |
| 39 |
|
| 40 |
TraceableFifo(TraceableFifo&& rhs) : Base(std::move(rhs)) {} |
| 41 |
TraceableFifo& operator=(TraceableFifo&& rhs) = default; |
| 42 |
|
| 43 |
TraceableFifo(const TraceableFifo&) = delete; |
| 44 |
TraceableFifo& operator=(const TraceableFifo&) = delete; |
| 45 |
|
| 46 |
void trace(JSTracer* trc) { |
| 47 |
for (size_t i = 0; i < this->front_.length(); ++i) { |
| 48 |
JS::GCPolicy<T>::trace(trc, &this->front_[i], "fifo element"); |
| 49 |
} |
| 50 |
for (size_t i = 0; i < this->rear_.length(); ++i) { |
| 51 |
JS::GCPolicy<T>::trace(trc, &this->rear_[i], "fifo element"); |
| 52 |
} |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
template <typename Wrapper, typename T, size_t Capacity, typename AllocPolicy> |
| 57 |
class WrappedPtrOperations<TraceableFifo<T, Capacity, AllocPolicy>, Wrapper> { |
| 58 |
using TF = TraceableFifo<T, Capacity, AllocPolicy>; |
| 59 |
const TF& fifo() const { return static_cast<const Wrapper*>(this)->get(); } |
| 60 |
|
| 61 |
public: |
| 62 |
size_t length() const { return fifo().length(); } |
| 63 |
bool empty() const { return fifo().empty(); } |
| 64 |
const T& front() const { return fifo().front(); } |
| 65 |
}; |
| 66 |
|
| 67 |
template <typename Wrapper, typename T, size_t Capacity, typename AllocPolicy> |
| 68 |
class MutableWrappedPtrOperations<TraceableFifo<T, Capacity, AllocPolicy>, |
| 69 |
Wrapper> |
| 70 |
: public WrappedPtrOperations<TraceableFifo<T, Capacity, AllocPolicy>, |
| 71 |
Wrapper> { |
| 72 |
using TF = TraceableFifo<T, Capacity, AllocPolicy>; |
| 73 |
TF& fifo() { return static_cast<Wrapper*>(this)->get(); } |
| 74 |
|
| 75 |
public: |
| 76 |
T& front() { return fifo().front(); } |
| 77 |
|
| 78 |
template <typename U> |
| 79 |
bool pushBack(U&& u) { |
| 80 |
return fifo().pushBack(std::forward<U>(u)); |
| 81 |
} |
| 82 |
template <typename... Args> |
| 83 |
bool emplaceBack(Args&&... args) { |
| 84 |
return fifo().emplaceBack(std::forward<Args...>(args...)); |
| 85 |
} |
| 86 |
|
| 87 |
void popFront() { fifo().popFront(); } |
| 88 |
void clear() { fifo().clear(); } |
| 89 |
}; |
| 90 |
|
| 91 |
} // namespace js |
| 92 |
|
| 93 |
#endif // js_TraceableFifo_h |