2015-12-19 01:50:20 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=8 sts=4 et sw=4 tw=99:
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef js_SweepingAPI_h
|
|
|
|
#define js_SweepingAPI_h
|
|
|
|
|
|
|
|
#include "js/HeapAPI.h"
|
|
|
|
|
|
|
|
namespace JS {
|
2017-06-14 12:35:16 +03:00
|
|
|
namespace detail {
|
|
|
|
class WeakCacheBase;
|
|
|
|
} // namespace detail
|
2015-12-19 01:50:20 +03:00
|
|
|
|
|
|
|
namespace shadow {
|
|
|
|
JS_PUBLIC_API(void)
|
2017-06-14 12:35:16 +03:00
|
|
|
RegisterWeakCache(JS::Zone* zone, JS::detail::WeakCacheBase* cachep);
|
2017-03-07 23:55:37 +03:00
|
|
|
JS_PUBLIC_API(void)
|
2017-06-14 12:35:16 +03:00
|
|
|
RegisterWeakCache(JSRuntime* rt, JS::detail::WeakCacheBase* cachep);
|
2015-12-19 01:50:20 +03:00
|
|
|
} // namespace shadow
|
|
|
|
|
2017-06-14 12:35:16 +03:00
|
|
|
namespace detail {
|
|
|
|
class WeakCacheBase : public mozilla::LinkedListElement<WeakCacheBase>
|
|
|
|
{
|
|
|
|
WeakCacheBase() = delete;
|
2017-06-14 12:52:18 +03:00
|
|
|
explicit WeakCacheBase(const WeakCacheBase&) = delete;
|
2017-06-14 12:35:16 +03:00
|
|
|
|
|
|
|
public:
|
2017-06-14 12:52:18 +03:00
|
|
|
explicit WeakCacheBase(Zone* zone) {
|
2017-06-14 12:35:16 +03:00
|
|
|
shadow::RegisterWeakCache(zone, this);
|
|
|
|
}
|
2017-06-14 12:52:18 +03:00
|
|
|
explicit WeakCacheBase(JSRuntime* rt) {
|
2017-06-14 12:35:16 +03:00
|
|
|
shadow::RegisterWeakCache(rt, this);
|
|
|
|
}
|
|
|
|
WeakCacheBase(WeakCacheBase&& other) = default;
|
|
|
|
virtual ~WeakCacheBase() {}
|
|
|
|
|
|
|
|
virtual void sweep() = 0;
|
|
|
|
};
|
|
|
|
} // namespace detail
|
|
|
|
|
2015-12-19 01:50:20 +03:00
|
|
|
// A WeakCache stores the given Sweepable container and links itself into a
|
2017-03-07 23:55:37 +03:00
|
|
|
// list of such caches that are swept during each GC. A WeakCache can be
|
|
|
|
// specific to a zone, or across a whole runtime, depending on which
|
|
|
|
// constructor is used.
|
2015-12-19 01:50:20 +03:00
|
|
|
template <typename T>
|
2017-06-14 12:35:16 +03:00
|
|
|
class WeakCache : protected detail::WeakCacheBase,
|
|
|
|
public js::MutableWrappedPtrOperations<T, WeakCache<T>>
|
2015-12-19 01:50:20 +03:00
|
|
|
{
|
|
|
|
T cache;
|
|
|
|
|
|
|
|
public:
|
2016-04-21 01:17:25 +03:00
|
|
|
using Type = T;
|
|
|
|
|
2017-06-14 12:35:16 +03:00
|
|
|
template <typename... Args>
|
2017-06-14 12:52:18 +03:00
|
|
|
explicit WeakCache(Zone* zone, Args&&... args)
|
2017-06-14 12:35:16 +03:00
|
|
|
: WeakCacheBase(zone), cache(mozilla::Forward<Args>(args)...)
|
|
|
|
{}
|
|
|
|
template <typename... Args>
|
2017-06-14 12:52:18 +03:00
|
|
|
explicit WeakCache(JSRuntime* rt, Args&&... args)
|
2017-06-14 12:35:16 +03:00
|
|
|
: WeakCacheBase(rt), cache(mozilla::Forward<Args>(args)...)
|
|
|
|
{}
|
2015-12-19 01:50:20 +03:00
|
|
|
|
|
|
|
const T& get() const { return cache; }
|
|
|
|
T& get() { return cache; }
|
|
|
|
|
2017-06-14 12:35:16 +03:00
|
|
|
void sweep() override {
|
|
|
|
GCPolicy<T>::sweep(&cache);
|
|
|
|
}
|
2015-12-19 01:50:20 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace JS
|
|
|
|
|
|
|
|
#endif // js_SweepingAPI_h
|