Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sts=4 et sw=4 tw=99:
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,
32 : size_t MinInlineCapacity = 0,
33 : typename AllocPolicy = TempAllocPolicy>
34 0 : class TraceableFifo : public js::Fifo<T, MinInlineCapacity, AllocPolicy>
35 : {
36 : using Base = js::Fifo<T, MinInlineCapacity, AllocPolicy>;
37 :
38 : public:
39 0 : explicit TraceableFifo(AllocPolicy alloc = AllocPolicy()) : Base(alloc) {}
40 :
41 : TraceableFifo(TraceableFifo&& rhs) : Base(mozilla::Move(rhs)) { }
42 : TraceableFifo& operator=(TraceableFifo&& rhs) { return Base::operator=(mozilla::Move(rhs)); }
43 :
44 : TraceableFifo(const TraceableFifo&) = delete;
45 : TraceableFifo& operator=(const TraceableFifo&) = delete;
46 :
47 0 : void trace(JSTracer* trc) {
48 0 : for (size_t i = 0; i < this->front_.length(); ++i)
49 0 : JS::GCPolicy<T>::trace(trc, &this->front_[i], "fifo element");
50 0 : for (size_t i = 0; i < this->rear_.length(); ++i)
51 0 : JS::GCPolicy<T>::trace(trc, &this->rear_[i], "fifo element");
52 0 : }
53 : };
54 :
55 : template <typename Wrapper, typename T, size_t Capacity, typename AllocPolicy>
56 : class WrappedPtrOperations<TraceableFifo<T, Capacity, AllocPolicy>, Wrapper>
57 : {
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>, Wrapper>
69 : : public WrappedPtrOperations<TraceableFifo<T, Capacity, AllocPolicy>, Wrapper>
70 : {
71 : using TF = TraceableFifo<T, Capacity, AllocPolicy>;
72 : TF& fifo() { return static_cast<Wrapper*>(this)->get(); }
73 :
74 : public:
75 : T& front() { return fifo().front(); }
76 :
77 : template<typename U> bool pushBack(U&& u) { return fifo().pushBack(mozilla::Forward<U>(u)); }
78 : template<typename... Args> bool emplaceBack(Args&&... args) {
79 : return fifo().emplaceBack(mozilla::Forward<Args...>(args...));
80 : }
81 :
82 : bool popFront() { return fifo().popFront(); }
83 : void clear() { fifo().clear(); }
84 : };
85 :
86 : } // namespace js
87 :
88 : #endif // js_TraceableFifo_h
|