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_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 1333 : explicit WeakCacheBase(Zone* zone) {
32 1333 : shadow::RegisterWeakCache(zone, this);
33 1333 : }
34 : explicit WeakCacheBase(JSRuntime* rt) {
35 : shadow::RegisterWeakCache(rt, this);
36 : }
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 : // Derived classes do not support incremental barriers by default.
45 0 : return false;
46 : }
47 0 : virtual bool needsIncrementalBarrier() const {
48 : // Derived classes do not support incremental barriers by default.
49 0 : return false;
50 : }
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 0 : 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 315 : explicit WeakCache(Zone* zone, Args&&... args)
69 315 : : WeakCacheBase(zone), cache(mozilla::Forward<Args>(args)...)
70 315 : {}
71 : template <typename... Args>
72 : explicit WeakCache(JSRuntime* rt, Args&&... args)
73 : : WeakCacheBase(rt), cache(mozilla::Forward<Args>(args)...)
74 : {}
75 :
76 : const T& get() const { return cache; }
77 2415 : T& get() { return cache; }
78 :
79 0 : size_t sweep() override {
80 0 : GCPolicy<T>::sweep(&cache);
81 0 : return 0;
82 : }
83 :
84 0 : bool needsSweep() override {
85 0 : return cache.needsSweep();
86 : }
87 : };
88 :
89 : } // namespace JS
90 :
91 : #endif // js_SweepingAPI_h
|