/work/obj-fuzz/dist/include/js/SweepingAPI.h
Line | Count | Source (jump to first uncovered line) |
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_SweepingAPI_h |
8 | | #define js_SweepingAPI_h |
9 | | |
10 | | #include "js/HeapAPI.h" |
11 | | |
12 | | namespace JS { |
13 | | namespace detail { |
14 | | class WeakCacheBase; |
15 | | } // namespace detail |
16 | | |
17 | | namespace shadow { |
18 | | JS_PUBLIC_API(void) |
19 | | RegisterWeakCache(JS::Zone* zone, JS::detail::WeakCacheBase* cachep); |
20 | | JS_PUBLIC_API(void) |
21 | | RegisterWeakCache(JSRuntime* rt, JS::detail::WeakCacheBase* cachep); |
22 | | } // namespace shadow |
23 | | |
24 | | namespace detail { |
25 | | class WeakCacheBase : public mozilla::LinkedListElement<WeakCacheBase> |
26 | | { |
27 | | WeakCacheBase() = delete; |
28 | | explicit WeakCacheBase(const WeakCacheBase&) = delete; |
29 | | |
30 | | public: |
31 | 67 | explicit WeakCacheBase(Zone* zone) { |
32 | 67 | shadow::RegisterWeakCache(zone, this); |
33 | 67 | } |
34 | 0 | explicit WeakCacheBase(JSRuntime* rt) { |
35 | 0 | shadow::RegisterWeakCache(rt, this); |
36 | 0 | } |
37 | | WeakCacheBase(WeakCacheBase&& other) = default; |
38 | 0 | virtual ~WeakCacheBase() {} |
39 | | |
40 | | virtual size_t sweep() = 0; |
41 | | virtual bool needsSweep() = 0; |
42 | | |
43 | 0 | virtual bool setNeedsIncrementalBarrier(bool needs) { |
44 | 0 | // Derived classes do not support incremental barriers by default. |
45 | 0 | return false; |
46 | 0 | } |
47 | 36 | virtual bool needsIncrementalBarrier() const { |
48 | 36 | // Derived classes do not support incremental barriers by default. |
49 | 36 | return false; |
50 | 36 | } |
51 | | }; |
52 | | } // namespace detail |
53 | | |
54 | | // A WeakCache stores the given Sweepable container and links itself into a |
55 | | // list of such caches that are swept during each GC. A WeakCache can be |
56 | | // specific to a zone, or across a whole runtime, depending on which |
57 | | // constructor is used. |
58 | | template <typename T> |
59 | | class WeakCache : protected detail::WeakCacheBase, |
60 | | public js::MutableWrappedPtrOperations<T, WeakCache<T>> |
61 | | { |
62 | | T cache; |
63 | | |
64 | | public: |
65 | | using Type = T; |
66 | | |
67 | | template <typename... Args> |
68 | | explicit WeakCache(Zone* zone, Args&&... args) |
69 | | : WeakCacheBase(zone), cache(std::forward<Args>(args)...) |
70 | 9 | {} |
71 | | template <typename... Args> |
72 | | explicit WeakCache(JSRuntime* rt, Args&&... args) |
73 | | : WeakCacheBase(rt), cache(std::forward<Args>(args)...) |
74 | | {} |
75 | | |
76 | | const T& get() const { return cache; } |
77 | 3 | T& get() { return cache; } |
78 | | |
79 | 0 | size_t sweep() override { |
80 | 0 | GCPolicy<T>::sweep(&cache); |
81 | 0 | return 0; |
82 | 0 | } |
83 | | |
84 | 36 | bool needsSweep() override { |
85 | 36 | return cache.needsSweep(); |
86 | 36 | } |
87 | | }; |
88 | | |
89 | | } // namespace JS |
90 | | |
91 | | #endif // js_SweepingAPI_h |