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 {
|
|
|
|
template <typename T> class WeakCache;
|
|
|
|
|
|
|
|
namespace shadow {
|
|
|
|
JS_PUBLIC_API(void)
|
|
|
|
RegisterWeakCache(JS::Zone* zone, JS::WeakCache<void*>* cachep);
|
2017-03-07 23:55:37 +03:00
|
|
|
JS_PUBLIC_API(void)
|
|
|
|
RegisterWeakCache(JSRuntime* rt, JS::WeakCache<void*>* cachep);
|
2015-12-19 01:50:20 +03:00
|
|
|
} // namespace shadow
|
|
|
|
|
|
|
|
// 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-01-10 13:12:14 +03:00
|
|
|
class WeakCache : public js::MutableWrappedPtrOperations<T, WeakCache<T>>,
|
2015-12-19 01:50:20 +03:00
|
|
|
private mozilla::LinkedListElement<WeakCache<T>>
|
|
|
|
{
|
|
|
|
friend class mozilla::LinkedListElement<WeakCache<T>>;
|
|
|
|
friend class mozilla::LinkedList<WeakCache<T>>;
|
|
|
|
|
2016-04-21 01:17:25 +03:00
|
|
|
WeakCache() = delete;
|
2015-12-19 01:50:20 +03:00
|
|
|
WeakCache(const WeakCache&) = delete;
|
|
|
|
|
|
|
|
using SweepFn = void (*)(T*);
|
|
|
|
SweepFn sweeper;
|
|
|
|
T cache;
|
|
|
|
|
|
|
|
public:
|
2016-04-21 01:17:25 +03:00
|
|
|
using Type = T;
|
|
|
|
|
2015-12-19 01:50:20 +03:00
|
|
|
template <typename U>
|
|
|
|
WeakCache(Zone* zone, U&& initial)
|
|
|
|
: cache(mozilla::Forward<U>(initial))
|
|
|
|
{
|
2016-04-26 19:18:48 +03:00
|
|
|
sweeper = GCPolicy<T>::sweep;
|
2015-12-19 01:50:20 +03:00
|
|
|
shadow::RegisterWeakCache(zone, reinterpret_cast<WeakCache<void*>*>(this));
|
|
|
|
}
|
2017-03-07 23:55:37 +03:00
|
|
|
template <typename U>
|
|
|
|
WeakCache(JSRuntime* rt, U&& initial)
|
|
|
|
: cache(mozilla::Forward<U>(initial))
|
|
|
|
{
|
|
|
|
sweeper = GCPolicy<T>::sweep;
|
|
|
|
shadow::RegisterWeakCache(rt, reinterpret_cast<WeakCache<void*>*>(this));
|
|
|
|
}
|
2015-12-19 01:50:20 +03:00
|
|
|
WeakCache(WeakCache&& other)
|
|
|
|
: sweeper(other.sweeper),
|
2016-09-13 02:35:27 +03:00
|
|
|
cache(mozilla::Move(other.cache))
|
2015-12-19 01:50:20 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const T& get() const { return cache; }
|
|
|
|
T& get() { return cache; }
|
|
|
|
|
2016-09-24 01:59:55 +03:00
|
|
|
void sweep() { sweeper(&cache); }
|
2015-12-19 01:50:20 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace JS
|
|
|
|
|
|
|
|
#endif // js_SweepingAPI_h
|