2012-01-20 19:49:33 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2013-04-16 13:47:10 -07:00
|
|
|
* vim: set ts=8 sts=4 et sw=4 tw=99:
|
2012-05-21 04:12:37 -07:00
|
|
|
* 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/. */
|
2012-01-20 19:49:33 -08:00
|
|
|
|
2013-06-19 17:59:09 -07:00
|
|
|
#ifndef js_RootingAPI_h
|
|
|
|
#define js_RootingAPI_h
|
2012-01-20 19:49:33 -08:00
|
|
|
|
2013-11-22 12:05:04 -08:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-07-31 14:43:45 -07:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2012-09-10 10:31:58 -07:00
|
|
|
#include "mozilla/GuardObjects.h"
|
2013-11-04 13:35:08 -08:00
|
|
|
#include "mozilla/LinkedList.h"
|
2012-12-27 09:20:22 -08:00
|
|
|
#include "mozilla/TypeTraits.h"
|
2012-06-19 15:01:58 -07:00
|
|
|
|
2012-09-10 10:31:58 -07:00
|
|
|
#include "jspubtd.h"
|
2012-04-12 18:15:26 -07:00
|
|
|
|
2014-06-16 11:59:45 -07:00
|
|
|
#include "js/GCAPI.h"
|
2014-05-01 09:26:12 -07:00
|
|
|
#include "js/HeapAPI.h"
|
2013-08-27 19:59:14 -07:00
|
|
|
#include "js/TypeDecls.h"
|
2013-08-13 15:34:12 -07:00
|
|
|
#include "js/Utility.h"
|
|
|
|
|
2012-01-20 19:49:33 -08:00
|
|
|
/*
|
|
|
|
* Moving GC Stack Rooting
|
|
|
|
*
|
|
|
|
* A moving GC may change the physical location of GC allocated things, even
|
|
|
|
* when they are rooted, updating all pointers to the thing to refer to its new
|
|
|
|
* location. The GC must therefore know about all live pointers to a thing,
|
|
|
|
* not just one of them, in order to behave correctly.
|
|
|
|
*
|
2013-04-27 12:27:53 -07:00
|
|
|
* The |Rooted| and |Handle| classes below are used to root stack locations
|
2012-11-14 16:06:27 -08:00
|
|
|
* whose value may be held live across a call that can trigger GC. For a
|
|
|
|
* code fragment such as:
|
2012-01-20 19:49:33 -08:00
|
|
|
*
|
2012-11-14 16:06:27 -08:00
|
|
|
* JSObject *obj = NewObject(cx);
|
|
|
|
* DoSomething(cx);
|
2012-01-20 19:49:33 -08:00
|
|
|
* ... = obj->lastProperty();
|
|
|
|
*
|
2012-11-14 16:06:27 -08:00
|
|
|
* If |DoSomething()| can trigger a GC, the stack location of |obj| must be
|
|
|
|
* rooted to ensure that the GC does not move the JSObject referred to by
|
|
|
|
* |obj| without updating |obj|'s location itself. This rooting must happen
|
|
|
|
* regardless of whether there are other roots which ensure that the object
|
|
|
|
* itself will not be collected.
|
2012-01-20 19:49:33 -08:00
|
|
|
*
|
2012-11-14 16:06:27 -08:00
|
|
|
* If |DoSomething()| cannot trigger a GC, and the same holds for all other
|
|
|
|
* calls made between |obj|'s definitions and its last uses, then no rooting
|
2013-03-05 10:32:17 -08:00
|
|
|
* is required.
|
2012-01-20 19:49:33 -08:00
|
|
|
*
|
2012-11-14 16:06:27 -08:00
|
|
|
* SpiderMonkey can trigger a GC at almost any time and in ways that are not
|
|
|
|
* always clear. For example, the following innocuous-looking actions can
|
|
|
|
* cause a GC: allocation of any new GC thing; JSObject::hasProperty;
|
|
|
|
* JS_ReportError and friends; and ToNumber, among many others. The following
|
|
|
|
* dangerous-looking actions cannot trigger a GC: js_malloc, cx->malloc_,
|
|
|
|
* rt->malloc_, and friends and JS_ReportOutOfMemory.
|
|
|
|
*
|
2013-04-12 01:31:43 -07:00
|
|
|
* The following family of three classes will exactly root a stack location.
|
2012-11-14 16:06:27 -08:00
|
|
|
* Incorrect usage of these classes will result in a compile error in almost
|
|
|
|
* all cases. Therefore, it is very hard to be incorrectly rooted if you use
|
|
|
|
* these classes exclusively. These classes are all templated on the type T of
|
|
|
|
* the value being rooted.
|
2012-01-20 19:49:33 -08:00
|
|
|
*
|
2012-05-24 16:05:18 -07:00
|
|
|
* - Rooted<T> declares a variable of type T, whose value is always rooted.
|
|
|
|
* Rooted<T> may be automatically coerced to a Handle<T>, below. Rooted<T>
|
|
|
|
* should be used whenever a local variable's value may be held live across a
|
2013-04-12 01:31:43 -07:00
|
|
|
* call which can trigger a GC.
|
2012-01-20 19:49:33 -08:00
|
|
|
*
|
2012-05-24 16:05:18 -07:00
|
|
|
* - Handle<T> is a const reference to a Rooted<T>. Functions which take GC
|
2012-05-24 08:52:21 -07:00
|
|
|
* things or values as arguments and need to root those arguments should
|
|
|
|
* generally use handles for those arguments and avoid any explicit rooting.
|
|
|
|
* This has two benefits. First, when several such functions call each other
|
|
|
|
* then redundant rooting of multiple copies of the GC thing can be avoided.
|
|
|
|
* Second, if the caller does not pass a rooted value a compile error will be
|
|
|
|
* generated, which is quicker and easier to fix than when relying on a
|
|
|
|
* separate rooting analysis.
|
2012-11-14 16:06:27 -08:00
|
|
|
*
|
|
|
|
* - MutableHandle<T> is a non-const reference to Rooted<T>. It is used in the
|
|
|
|
* same way as Handle<T> and includes a |set(const T &v)| method to allow
|
|
|
|
* updating the value of the referenced Rooted<T>. A MutableHandle<T> can be
|
|
|
|
* created from a Rooted<T> by using |Rooted<T>::operator&()|.
|
|
|
|
*
|
2013-04-27 12:27:53 -07:00
|
|
|
* In some cases the small performance overhead of exact rooting (measured to
|
|
|
|
* be a few nanoseconds on desktop) is too much. In these cases, try the
|
|
|
|
* following:
|
2012-11-14 16:06:27 -08:00
|
|
|
*
|
|
|
|
* - Move all Rooted<T> above inner loops: this allows you to re-use the root
|
|
|
|
* on each iteration of the loop.
|
|
|
|
*
|
|
|
|
* - Pass Handle<T> through your hot call stack to avoid re-rooting costs at
|
|
|
|
* every invocation.
|
|
|
|
*
|
2012-12-04 11:10:53 -08:00
|
|
|
* The following diagram explains the list of supported, implicit type
|
|
|
|
* conversions between classes of this family:
|
|
|
|
*
|
2013-04-12 01:31:43 -07:00
|
|
|
* Rooted<T> ----> Handle<T>
|
|
|
|
* | ^
|
|
|
|
* | |
|
|
|
|
* | |
|
|
|
|
* +---> MutableHandle<T>
|
|
|
|
* (via &)
|
2012-12-04 11:10:53 -08:00
|
|
|
*
|
2013-04-27 12:27:53 -07:00
|
|
|
* All of these types have an implicit conversion to raw pointers.
|
2012-01-20 19:49:33 -08:00
|
|
|
*/
|
|
|
|
|
2012-09-24 18:08:22 -07:00
|
|
|
namespace js {
|
|
|
|
|
2012-04-12 09:23:51 -07:00
|
|
|
template <typename T>
|
2013-06-19 03:32:26 -07:00
|
|
|
struct GCMethods {};
|
2012-04-12 09:23:51 -07:00
|
|
|
|
2012-11-14 16:06:27 -08:00
|
|
|
template <typename T>
|
|
|
|
class RootedBase {};
|
|
|
|
|
2012-09-24 18:08:22 -07:00
|
|
|
template <typename T>
|
|
|
|
class HandleBase {};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class MutableHandleBase {};
|
|
|
|
|
2013-05-27 04:51:25 -07:00
|
|
|
template <typename T>
|
|
|
|
class HeapBase {};
|
|
|
|
|
2015-01-23 02:23:56 -08:00
|
|
|
template <typename T>
|
|
|
|
class PersistentRootedBase {};
|
|
|
|
|
2013-03-20 03:41:20 -07:00
|
|
|
/*
|
2013-09-19 12:24:53 -07:00
|
|
|
* js::NullPtr acts like a nullptr pointer in contexts that require a Handle.
|
2013-03-20 03:41:20 -07:00
|
|
|
*
|
|
|
|
* Handle provides an implicit constructor for js::NullPtr so that, given:
|
|
|
|
* foo(Handle<JSObject*> h);
|
|
|
|
* callers can simply write:
|
|
|
|
* foo(js::NullPtr());
|
2013-09-19 12:24:53 -07:00
|
|
|
* which avoids creating a Rooted<JSObject*> just to pass nullptr.
|
2013-03-20 03:41:20 -07:00
|
|
|
*
|
|
|
|
* This is the SpiderMonkey internal variant. js::NullPtr should be used in
|
|
|
|
* preference to JS::NullPtr to avoid the GOT access required for JS_PUBLIC_API
|
|
|
|
* symbols.
|
|
|
|
*/
|
|
|
|
struct NullPtr
|
|
|
|
{
|
|
|
|
static void * const constNullValue;
|
|
|
|
};
|
|
|
|
|
2013-05-27 04:51:25 -07:00
|
|
|
namespace gc {
|
|
|
|
struct Cell;
|
2014-01-23 01:53:42 -08:00
|
|
|
template<typename T>
|
2014-01-22 03:28:06 -08:00
|
|
|
struct PersistentRootedMarker;
|
2013-05-27 04:51:25 -07:00
|
|
|
} /* namespace gc */
|
|
|
|
|
2015-02-04 15:27:00 -08:00
|
|
|
#define DECLARE_POINTER_COMPARISON_OPS(T) \
|
|
|
|
bool operator==(const T &other) const { return get() == other; } \
|
|
|
|
bool operator!=(const T &other) const { return get() != other; }
|
|
|
|
|
|
|
|
// Important: Return a reference so passing a Rooted<T>, etc. to
|
|
|
|
// something that takes a |const T&| is not a GC hazard.
|
|
|
|
#define DECLARE_POINTER_CONSTREF_OPS(T) \
|
|
|
|
operator const T &() const { return get(); } \
|
|
|
|
const T &operator->() const { return get(); }
|
|
|
|
|
|
|
|
// Assignment operators on a base class are hidden by the implicitly defined
|
|
|
|
// operator= on the derived class. Thus, define the operator= directly on the
|
|
|
|
// class as we would need to manually pass it through anyway.
|
|
|
|
#define DECLARE_POINTER_ASSIGN_OPS(Wrapper, T) \
|
|
|
|
Wrapper<T> &operator=(const T &p) { \
|
|
|
|
set(p); \
|
|
|
|
return *this; \
|
|
|
|
} \
|
|
|
|
Wrapper<T> &operator=(const Wrapper<T> &other) { \
|
|
|
|
set(other.get()); \
|
|
|
|
return *this; \
|
|
|
|
} \
|
|
|
|
|
|
|
|
#define DELETE_ASSIGNMENT_OPS(Wrapper, T) \
|
|
|
|
template <typename S> Wrapper<T> &operator=(S) = delete; \
|
|
|
|
Wrapper<T> &operator=(const Wrapper<T> &) = delete;
|
|
|
|
|
|
|
|
#define DECLARE_NONPOINTER_ACCESSOR_METHODS(ptr) \
|
|
|
|
const T *address() const { return &(ptr); } \
|
|
|
|
const T &get() const { return (ptr); } \
|
|
|
|
|
|
|
|
#define DECLARE_NONPOINTER_MUTABLE_ACCESSOR_METHODS(ptr) \
|
|
|
|
T *address() { return &(ptr); } \
|
|
|
|
T &get() { return (ptr); } \
|
|
|
|
|
2012-09-24 18:08:22 -07:00
|
|
|
} /* namespace js */
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2013-03-06 08:41:38 -08:00
|
|
|
template <typename T> class Rooted;
|
2013-11-04 13:35:08 -08:00
|
|
|
template <typename T> class PersistentRooted;
|
2013-03-06 08:41:38 -08:00
|
|
|
|
2013-03-06 10:10:35 -08:00
|
|
|
/* This is exposing internal state of the GC for inlining purposes. */
|
2012-11-22 16:09:14 -08:00
|
|
|
JS_FRIEND_API(bool) isGCEnabled();
|
2012-09-26 11:13:20 -07:00
|
|
|
|
2012-07-08 03:00:11 -07:00
|
|
|
/*
|
2013-09-19 12:24:53 -07:00
|
|
|
* JS::NullPtr acts like a nullptr pointer in contexts that require a Handle.
|
2013-03-20 03:41:20 -07:00
|
|
|
*
|
|
|
|
* Handle provides an implicit constructor for JS::NullPtr so that, given:
|
2012-07-08 03:00:11 -07:00
|
|
|
* foo(Handle<JSObject*> h);
|
|
|
|
* callers can simply write:
|
2013-03-20 03:41:20 -07:00
|
|
|
* foo(JS::NullPtr());
|
2013-09-19 12:24:53 -07:00
|
|
|
* which avoids creating a Rooted<JSObject*> just to pass nullptr.
|
2012-07-08 03:00:11 -07:00
|
|
|
*/
|
2013-03-20 03:41:20 -07:00
|
|
|
struct JS_PUBLIC_API(NullPtr)
|
2012-07-08 03:00:11 -07:00
|
|
|
{
|
|
|
|
static void * const constNullValue;
|
|
|
|
};
|
|
|
|
|
2014-07-31 14:43:45 -07:00
|
|
|
JS_FRIEND_API(void) HeapCellPostBarrier(js::gc::Cell **cellp);
|
|
|
|
JS_FRIEND_API(void) HeapCellRelocate(js::gc::Cell **cellp);
|
|
|
|
|
|
|
|
#ifdef JS_DEBUG
|
|
|
|
/*
|
|
|
|
* For generational GC, assert that an object is in the tenured generation as
|
|
|
|
* opposed to being in the nursery.
|
|
|
|
*/
|
|
|
|
extern JS_FRIEND_API(void)
|
|
|
|
AssertGCThingMustBeTenured(JSObject* obj);
|
|
|
|
#else
|
|
|
|
inline void
|
|
|
|
AssertGCThingMustBeTenured(JSObject *obj) {}
|
|
|
|
#endif
|
|
|
|
|
2013-05-27 04:51:25 -07:00
|
|
|
/*
|
2013-11-01 03:20:50 -07:00
|
|
|
* The Heap<T> class is a heap-stored reference to a JS GC thing. All members of
|
|
|
|
* heap classes that refer to GC things should use Heap<T> (or possibly
|
|
|
|
* TenuredHeap<T>, described below).
|
2013-05-27 04:51:25 -07:00
|
|
|
*
|
2013-11-01 03:20:50 -07:00
|
|
|
* Heap<T> is an abstraction that hides some of the complexity required to
|
|
|
|
* maintain GC invariants for the contained reference. It uses operator
|
|
|
|
* overloading to provide a normal pointer interface, but notifies the GC every
|
|
|
|
* time the value it contains is updated. This is necessary for generational GC,
|
|
|
|
* which keeps track of all pointers into the nursery.
|
2013-06-21 06:12:45 -07:00
|
|
|
*
|
2013-11-01 03:20:50 -07:00
|
|
|
* Heap<T> instances must be traced when their containing object is traced to
|
|
|
|
* keep the pointed-to GC thing alive.
|
2013-06-21 06:12:45 -07:00
|
|
|
*
|
2013-11-01 03:20:50 -07:00
|
|
|
* Heap<T> objects should only be used on the heap. GC references stored on the
|
|
|
|
* C/C++ stack must use Rooted/Handle/MutableHandle instead.
|
|
|
|
*
|
|
|
|
* Type T must be one of: JS::Value, jsid, JSObject*, JSString*, JSScript*
|
2013-05-27 04:51:25 -07:00
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
class Heap : public js::HeapBase<T>
|
|
|
|
{
|
|
|
|
public:
|
2013-06-13 15:14:44 -07:00
|
|
|
Heap() {
|
2013-07-18 10:59:53 -07:00
|
|
|
static_assert(sizeof(T) == sizeof(Heap<T>),
|
|
|
|
"Heap<T> must be binary compatible with T.");
|
2013-06-19 03:32:26 -07:00
|
|
|
init(js::GCMethods<T>::initial());
|
2013-06-13 15:14:44 -07:00
|
|
|
}
|
2013-06-18 03:00:37 -07:00
|
|
|
explicit Heap(T p) { init(p); }
|
2014-02-07 10:03:21 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* For Heap, move semantics are equivalent to copy semantics. In C++, a
|
|
|
|
* copy constructor taking const-ref is the way to get a single function
|
|
|
|
* that will be used for both lvalue and rvalue copies, so we can simply
|
|
|
|
* omit the rvalue variant.
|
|
|
|
*/
|
2013-06-18 03:00:37 -07:00
|
|
|
explicit Heap(const Heap<T> &p) { init(p.ptr); }
|
2013-05-27 04:51:25 -07:00
|
|
|
|
|
|
|
~Heap() {
|
2013-06-19 03:32:26 -07:00
|
|
|
if (js::GCMethods<T>::needsPostBarrier(ptr))
|
2013-05-27 04:51:25 -07:00
|
|
|
relocate();
|
|
|
|
}
|
|
|
|
|
2015-02-04 15:27:00 -08:00
|
|
|
DECLARE_POINTER_CONSTREF_OPS(T);
|
|
|
|
DECLARE_POINTER_ASSIGN_OPS(Heap, T);
|
|
|
|
DECLARE_NONPOINTER_ACCESSOR_METHODS(ptr);
|
2013-05-27 04:51:25 -07:00
|
|
|
|
|
|
|
T *unsafeGet() { return &ptr; }
|
|
|
|
|
2013-11-09 02:20:22 -08:00
|
|
|
/*
|
|
|
|
* Set the pointer to a value which will cause a crash if it is
|
|
|
|
* dereferenced.
|
|
|
|
*/
|
|
|
|
void setToCrashOnTouch() {
|
|
|
|
ptr = reinterpret_cast<T>(crashOnTouchPointer);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isSetToCrashOnTouch() {
|
|
|
|
return ptr == crashOnTouchPointer;
|
|
|
|
}
|
|
|
|
|
2013-05-27 04:51:25 -07:00
|
|
|
private:
|
2013-06-18 03:00:37 -07:00
|
|
|
void init(T newPtr) {
|
2014-02-17 22:24:15 -08:00
|
|
|
MOZ_ASSERT(!js::GCMethods<T>::poisoned(newPtr));
|
2013-06-18 03:00:37 -07:00
|
|
|
ptr = newPtr;
|
2013-06-19 03:32:26 -07:00
|
|
|
if (js::GCMethods<T>::needsPostBarrier(ptr))
|
2013-06-18 03:00:37 -07:00
|
|
|
post();
|
|
|
|
}
|
|
|
|
|
2015-02-04 15:27:00 -08:00
|
|
|
void set(T newPtr) {
|
|
|
|
MOZ_ASSERT(!js::GCMethods<T>::poisoned(newPtr));
|
|
|
|
if (js::GCMethods<T>::needsPostBarrier(newPtr)) {
|
|
|
|
ptr = newPtr;
|
|
|
|
post();
|
|
|
|
} else if (js::GCMethods<T>::needsPostBarrier(ptr)) {
|
|
|
|
relocate(); /* Called before overwriting ptr. */
|
|
|
|
ptr = newPtr;
|
|
|
|
} else {
|
|
|
|
ptr = newPtr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-27 04:51:25 -07:00
|
|
|
void post() {
|
2014-02-17 22:24:15 -08:00
|
|
|
MOZ_ASSERT(js::GCMethods<T>::needsPostBarrier(ptr));
|
2013-06-19 03:32:26 -07:00
|
|
|
js::GCMethods<T>::postBarrier(&ptr);
|
2013-05-27 04:51:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void relocate() {
|
2013-06-19 03:32:26 -07:00
|
|
|
js::GCMethods<T>::relocate(&ptr);
|
2013-05-27 04:51:25 -07:00
|
|
|
}
|
|
|
|
|
2013-11-09 02:20:22 -08:00
|
|
|
enum {
|
|
|
|
crashOnTouchPointer = 1
|
|
|
|
};
|
|
|
|
|
2013-05-27 04:51:25 -07:00
|
|
|
T ptr;
|
|
|
|
};
|
|
|
|
|
2013-07-23 02:58:26 -07:00
|
|
|
/*
|
|
|
|
* The TenuredHeap<T> class is similar to the Heap<T> class above in that it
|
|
|
|
* encapsulates the GC concerns of an on-heap reference to a JS object. However,
|
|
|
|
* it has two important differences:
|
|
|
|
*
|
|
|
|
* 1) Pointers which are statically known to only reference "tenured" objects
|
|
|
|
* can avoid the extra overhead of SpiderMonkey's write barriers.
|
|
|
|
*
|
|
|
|
* 2) Objects in the "tenured" heap have stronger alignment restrictions than
|
|
|
|
* those in the "nursery", so it is possible to store flags in the lower
|
|
|
|
* bits of pointers known to be tenured. TenuredHeap wraps a normal tagged
|
|
|
|
* pointer with a nice API for accessing the flag bits and adds various
|
|
|
|
* assertions to ensure that it is not mis-used.
|
|
|
|
*
|
|
|
|
* GC things are said to be "tenured" when they are located in the long-lived
|
|
|
|
* heap: e.g. they have gained tenure as an object by surviving past at least
|
|
|
|
* one GC. For performance, SpiderMonkey allocates some things which are known
|
|
|
|
* to normally be long lived directly into the tenured generation; for example,
|
|
|
|
* global objects. Additionally, SpiderMonkey does not visit individual objects
|
|
|
|
* when deleting non-tenured objects, so object with finalizers are also always
|
|
|
|
* tenured; for instance, this includes most DOM objects.
|
|
|
|
*
|
|
|
|
* The considerations to keep in mind when using a TenuredHeap<T> vs a normal
|
|
|
|
* Heap<T> are:
|
|
|
|
*
|
|
|
|
* - It is invalid for a TenuredHeap<T> to refer to a non-tenured thing.
|
|
|
|
* - It is however valid for a Heap<T> to refer to a tenured thing.
|
|
|
|
* - It is not possible to store flag bits in a Heap<T>.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
class TenuredHeap : public js::HeapBase<T>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TenuredHeap() : bits(0) {
|
2013-07-18 10:59:53 -07:00
|
|
|
static_assert(sizeof(T) == sizeof(TenuredHeap<T>),
|
|
|
|
"TenuredHeap<T> must be binary compatible with T.");
|
2013-07-23 02:58:26 -07:00
|
|
|
}
|
|
|
|
explicit TenuredHeap(T p) : bits(0) { setPtr(p); }
|
2014-03-03 08:53:42 -08:00
|
|
|
explicit TenuredHeap(const TenuredHeap<T> &p) : bits(0) { setPtr(p.getPtr()); }
|
2013-07-23 02:58:26 -07:00
|
|
|
|
|
|
|
bool operator==(const TenuredHeap<T> &other) { return bits == other.bits; }
|
|
|
|
bool operator!=(const TenuredHeap<T> &other) { return bits != other.bits; }
|
|
|
|
|
|
|
|
void setPtr(T newPtr) {
|
2014-02-17 22:24:15 -08:00
|
|
|
MOZ_ASSERT((reinterpret_cast<uintptr_t>(newPtr) & flagsMask) == 0);
|
|
|
|
MOZ_ASSERT(!js::GCMethods<T>::poisoned(newPtr));
|
2013-07-23 02:58:26 -07:00
|
|
|
if (newPtr)
|
|
|
|
AssertGCThingMustBeTenured(newPtr);
|
|
|
|
bits = (bits & flagsMask) | reinterpret_cast<uintptr_t>(newPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setFlags(uintptr_t flagsToSet) {
|
2014-02-17 22:24:15 -08:00
|
|
|
MOZ_ASSERT((flagsToSet & ~flagsMask) == 0);
|
2013-07-23 02:58:26 -07:00
|
|
|
bits |= flagsToSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unsetFlags(uintptr_t flagsToUnset) {
|
2014-02-17 22:24:15 -08:00
|
|
|
MOZ_ASSERT((flagsToUnset & ~flagsMask) == 0);
|
2013-07-23 02:58:26 -07:00
|
|
|
bits &= ~flagsToUnset;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasFlag(uintptr_t flag) const {
|
2014-02-17 22:24:15 -08:00
|
|
|
MOZ_ASSERT((flag & ~flagsMask) == 0);
|
2013-07-23 02:58:26 -07:00
|
|
|
return (bits & flag) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
T getPtr() const { return reinterpret_cast<T>(bits & ~flagsMask); }
|
|
|
|
uintptr_t getFlags() const { return bits & flagsMask; }
|
|
|
|
|
|
|
|
operator T() const { return getPtr(); }
|
|
|
|
T operator->() const { return getPtr(); }
|
|
|
|
|
|
|
|
TenuredHeap<T> &operator=(T p) {
|
|
|
|
setPtr(p);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-11-18 09:21:44 -08:00
|
|
|
TenuredHeap<T> &operator=(const TenuredHeap<T>& other) {
|
|
|
|
bits = other.bits;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-07-23 02:58:26 -07:00
|
|
|
private:
|
|
|
|
enum {
|
|
|
|
maskBits = 3,
|
|
|
|
flagsMask = (1 << maskBits) - 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
uintptr_t bits;
|
|
|
|
};
|
|
|
|
|
2012-04-12 09:23:51 -07:00
|
|
|
/*
|
2012-04-27 16:40:10 -07:00
|
|
|
* Reference to a T that has been rooted elsewhere. This is most useful
|
|
|
|
* as a parameter type, which guarantees that the T lvalue is properly
|
|
|
|
* rooted. See "Move GC Stack Rooting" above.
|
2012-07-12 03:01:59 -07:00
|
|
|
*
|
|
|
|
* If you want to add additional methods to Handle for a specific
|
|
|
|
* specialization, define a HandleBase<T> specialization containing them.
|
2012-04-12 09:23:51 -07:00
|
|
|
*/
|
|
|
|
template <typename T>
|
2013-05-27 14:05:02 -07:00
|
|
|
class MOZ_NONHEAP_CLASS Handle : public js::HandleBase<T>
|
2012-04-12 22:07:43 -07:00
|
|
|
{
|
2013-10-10 15:22:35 -07:00
|
|
|
friend class JS::MutableHandle<T>;
|
2012-11-14 16:06:27 -08:00
|
|
|
|
2012-04-12 09:23:51 -07:00
|
|
|
public:
|
2012-06-19 15:01:58 -07:00
|
|
|
/* Creates a handle from a handle of a type convertible to T. */
|
|
|
|
template <typename S>
|
|
|
|
Handle(Handle<S> handle,
|
|
|
|
typename mozilla::EnableIf<mozilla::IsConvertible<S, T>::value, int>::Type dummy = 0)
|
|
|
|
{
|
2013-07-18 10:59:53 -07:00
|
|
|
static_assert(sizeof(Handle<T>) == sizeof(T *),
|
|
|
|
"Handle must be binary compatible with T*.");
|
2012-04-12 09:23:51 -07:00
|
|
|
ptr = reinterpret_cast<const T *>(handle.address());
|
|
|
|
}
|
2012-01-20 19:49:33 -08:00
|
|
|
|
2013-09-19 12:24:53 -07:00
|
|
|
/* Create a handle for a nullptr pointer. */
|
2014-05-25 18:46:24 -07:00
|
|
|
MOZ_IMPLICIT Handle(js::NullPtr) {
|
2013-07-18 10:59:53 -07:00
|
|
|
static_assert(mozilla::IsPointer<T>::value,
|
|
|
|
"js::NullPtr overload not valid for non-pointer types");
|
2013-03-20 03:41:20 -07:00
|
|
|
ptr = reinterpret_cast<const T *>(&js::NullPtr::constNullValue);
|
|
|
|
}
|
|
|
|
|
2013-09-19 12:24:53 -07:00
|
|
|
/* Create a handle for a nullptr pointer. */
|
2014-05-25 18:46:24 -07:00
|
|
|
MOZ_IMPLICIT Handle(JS::NullPtr) {
|
2013-07-18 10:59:53 -07:00
|
|
|
static_assert(mozilla::IsPointer<T>::value,
|
|
|
|
"JS::NullPtr overload not valid for non-pointer types");
|
2013-03-20 03:41:20 -07:00
|
|
|
ptr = reinterpret_cast<const T *>(&JS::NullPtr::constNullValue);
|
2012-07-08 03:00:11 -07:00
|
|
|
}
|
|
|
|
|
2014-05-25 18:46:24 -07:00
|
|
|
MOZ_IMPLICIT Handle(MutableHandle<T> handle) {
|
2012-07-18 14:31:24 -07:00
|
|
|
ptr = handle.address();
|
|
|
|
}
|
|
|
|
|
2012-04-13 18:06:40 -07:00
|
|
|
/*
|
2013-07-23 02:58:28 -07:00
|
|
|
* Take care when calling this method!
|
2012-04-27 16:40:10 -07:00
|
|
|
*
|
2013-07-23 02:58:28 -07:00
|
|
|
* This creates a Handle from the raw location of a T.
|
|
|
|
*
|
|
|
|
* It should be called only if the following conditions hold:
|
|
|
|
*
|
|
|
|
* 1) the location of the T is guaranteed to be marked (for some reason
|
|
|
|
* other than being a Rooted), e.g., if it is guaranteed to be reachable
|
|
|
|
* from an implicit root.
|
|
|
|
*
|
|
|
|
* 2) the contents of the location are immutable, or at least cannot change
|
|
|
|
* for the lifetime of the handle, as its users may not expect its value
|
|
|
|
* to change underneath them.
|
2012-04-13 18:06:40 -07:00
|
|
|
*/
|
2013-11-22 12:05:04 -08:00
|
|
|
static MOZ_CONSTEXPR Handle fromMarkedLocation(const T *p) {
|
|
|
|
return Handle(p, DeliberatelyChoosingThisOverload,
|
|
|
|
ImUsingThisOnlyInFromFromMarkedLocation);
|
2012-04-13 18:06:40 -07:00
|
|
|
}
|
|
|
|
|
2012-04-27 16:40:10 -07:00
|
|
|
/*
|
|
|
|
* Construct a handle from an explicitly rooted location. This is the
|
2012-05-24 08:52:21 -07:00
|
|
|
* normal way to create a handle, and normally happens implicitly.
|
2012-04-27 16:40:10 -07:00
|
|
|
*/
|
2012-06-19 15:01:58 -07:00
|
|
|
template <typename S>
|
|
|
|
inline
|
2013-05-16 09:36:56 -07:00
|
|
|
Handle(const Rooted<S> &root,
|
2012-06-19 15:01:58 -07:00
|
|
|
typename mozilla::EnableIf<mozilla::IsConvertible<S, T>::value, int>::Type dummy = 0);
|
2012-04-12 09:23:51 -07:00
|
|
|
|
2013-11-04 13:35:08 -08:00
|
|
|
template <typename S>
|
|
|
|
inline
|
|
|
|
Handle(const PersistentRooted<S> &root,
|
|
|
|
typename mozilla::EnableIf<mozilla::IsConvertible<S, T>::value, int>::Type dummy = 0);
|
|
|
|
|
2012-07-30 04:19:09 -07:00
|
|
|
/* Construct a read only handle from a mutable handle. */
|
|
|
|
template <typename S>
|
|
|
|
inline
|
|
|
|
Handle(MutableHandle<S> &root,
|
|
|
|
typename mozilla::EnableIf<mozilla::IsConvertible<S, T>::value, int>::Type dummy = 0);
|
|
|
|
|
2015-02-04 15:27:00 -08:00
|
|
|
DECLARE_POINTER_COMPARISON_OPS(T);
|
|
|
|
DECLARE_POINTER_CONSTREF_OPS(T);
|
|
|
|
DECLARE_NONPOINTER_ACCESSOR_METHODS(*ptr);
|
2012-11-29 10:22:12 -08:00
|
|
|
|
2012-04-12 09:23:51 -07:00
|
|
|
private:
|
2012-04-13 18:06:40 -07:00
|
|
|
Handle() {}
|
2015-02-04 15:27:00 -08:00
|
|
|
DELETE_ASSIGNMENT_OPS(Handle, T);
|
2012-04-13 18:06:40 -07:00
|
|
|
|
2013-11-22 12:05:04 -08:00
|
|
|
enum Disambiguator { DeliberatelyChoosingThisOverload = 42 };
|
|
|
|
enum CallerIdentity { ImUsingThisOnlyInFromFromMarkedLocation = 17 };
|
|
|
|
MOZ_CONSTEXPR Handle(const T *p, Disambiguator, CallerIdentity) : ptr(p) {}
|
|
|
|
|
2012-04-12 09:23:51 -07:00
|
|
|
const T *ptr;
|
2012-04-12 22:07:43 -07:00
|
|
|
};
|
2012-04-12 09:23:51 -07:00
|
|
|
|
2012-07-04 11:12:16 -07:00
|
|
|
/*
|
|
|
|
* Similar to a handle, but the underlying storage can be changed. This is
|
|
|
|
* useful for outparams.
|
2012-07-12 03:01:59 -07:00
|
|
|
*
|
|
|
|
* If you want to add additional methods to MutableHandle for a specific
|
|
|
|
* specialization, define a MutableHandleBase<T> specialization containing
|
|
|
|
* them.
|
2012-07-04 11:12:16 -07:00
|
|
|
*/
|
|
|
|
template <typename T>
|
2013-04-25 03:55:30 -07:00
|
|
|
class MOZ_STACK_CLASS MutableHandle : public js::MutableHandleBase<T>
|
2012-07-04 11:12:16 -07:00
|
|
|
{
|
|
|
|
public:
|
2014-05-25 18:46:24 -07:00
|
|
|
inline MOZ_IMPLICIT MutableHandle(Rooted<T> *root);
|
|
|
|
inline MOZ_IMPLICIT MutableHandle(PersistentRooted<T> *root);
|
2012-07-04 11:12:16 -07:00
|
|
|
|
2014-01-02 15:27:41 -08:00
|
|
|
private:
|
2015-01-14 09:09:11 -08:00
|
|
|
// Disallow nullptr for overloading purposes.
|
|
|
|
MutableHandle(decltype(nullptr)) = delete;
|
2014-01-02 15:27:41 -08:00
|
|
|
|
|
|
|
public:
|
2012-11-14 16:06:27 -08:00
|
|
|
void set(T v) {
|
2014-02-17 22:24:15 -08:00
|
|
|
MOZ_ASSERT(!js::GCMethods<T>::poisoned(v));
|
2012-07-10 18:17:29 -07:00
|
|
|
*ptr = v;
|
|
|
|
}
|
2012-07-04 11:12:16 -07:00
|
|
|
|
2012-07-18 14:31:24 -07:00
|
|
|
/*
|
|
|
|
* This may be called only if the location of the T is guaranteed
|
|
|
|
* to be marked (for some reason other than being a Rooted),
|
|
|
|
* e.g., if it is guaranteed to be reachable from an implicit root.
|
|
|
|
*
|
|
|
|
* Create a MutableHandle from a raw location of a T.
|
|
|
|
*/
|
|
|
|
static MutableHandle fromMarkedLocation(T *p) {
|
|
|
|
MutableHandle h;
|
|
|
|
h.ptr = p;
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2015-02-04 15:27:00 -08:00
|
|
|
DECLARE_POINTER_CONSTREF_OPS(T);
|
|
|
|
DECLARE_NONPOINTER_ACCESSOR_METHODS(*ptr);
|
|
|
|
DECLARE_NONPOINTER_MUTABLE_ACCESSOR_METHODS(*ptr);
|
2012-07-04 11:12:16 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
MutableHandle() {}
|
2015-02-04 15:27:00 -08:00
|
|
|
DELETE_ASSIGNMENT_OPS(MutableHandle, T);
|
2012-07-04 11:12:16 -07:00
|
|
|
|
|
|
|
T *ptr;
|
|
|
|
};
|
|
|
|
|
2012-12-04 11:10:53 -08:00
|
|
|
} /* namespace JS */
|
|
|
|
|
|
|
|
namespace js {
|
|
|
|
|
2012-09-10 11:26:46 -07:00
|
|
|
/*
|
|
|
|
* InternalHandle is a handle to an internal pointer into a gcthing. Use
|
|
|
|
* InternalHandle when you have a pointer to a direct field of a gcthing, or
|
|
|
|
* when you need a parameter type for something that *may* be a pointer to a
|
|
|
|
* direct field of a gcthing.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2012-11-14 16:06:27 -08:00
|
|
|
class InternalHandle {};
|
2012-09-10 11:26:46 -07:00
|
|
|
|
|
|
|
template <typename T>
|
2012-10-05 13:16:39 -07:00
|
|
|
class InternalHandle<T*>
|
2012-09-10 11:26:46 -07:00
|
|
|
{
|
|
|
|
void * const *holder;
|
|
|
|
size_t offset;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/*
|
|
|
|
* Create an InternalHandle using a Handle to the gcthing containing the
|
|
|
|
* field in question, and a pointer to the field.
|
|
|
|
*/
|
|
|
|
template<typename H>
|
2012-09-24 18:08:22 -07:00
|
|
|
InternalHandle(const JS::Handle<H> &handle, T *field)
|
2012-09-10 11:26:46 -07:00
|
|
|
: holder((void**)handle.address()), offset(uintptr_t(field) - uintptr_t(handle.get()))
|
2012-11-14 16:06:27 -08:00
|
|
|
{}
|
2012-09-10 11:26:46 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create an InternalHandle to a field within a Rooted<>.
|
|
|
|
*/
|
|
|
|
template<typename R>
|
2013-03-19 03:35:41 -07:00
|
|
|
InternalHandle(const JS::Rooted<R> &root, T *field)
|
2012-09-10 11:26:46 -07:00
|
|
|
: holder((void**)root.address()), offset(uintptr_t(field) - uintptr_t(root.get()))
|
2012-11-14 16:06:27 -08:00
|
|
|
{}
|
2012-09-10 11:26:46 -07:00
|
|
|
|
2013-11-18 09:21:44 -08:00
|
|
|
InternalHandle(const InternalHandle<T*>& other)
|
|
|
|
: holder(other.holder), offset(other.offset) {}
|
|
|
|
|
2012-09-10 11:26:46 -07:00
|
|
|
T *get() const { return reinterpret_cast<T*>(uintptr_t(*holder) + offset); }
|
|
|
|
|
2012-11-14 16:06:27 -08:00
|
|
|
const T &operator*() const { return *get(); }
|
|
|
|
T *operator->() const { return get(); }
|
2012-09-10 11:26:46 -07:00
|
|
|
|
|
|
|
static InternalHandle<T*> fromMarkedLocation(T *fieldPtr) {
|
|
|
|
return InternalHandle(fieldPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/*
|
|
|
|
* Create an InternalHandle to something that is not a pointer to a
|
|
|
|
* gcthing, and so does not need to be rooted in the first place. Use these
|
|
|
|
* InternalHandles to pass pointers into functions that also need to accept
|
|
|
|
* regular InternalHandles to gcthing fields.
|
|
|
|
*
|
|
|
|
* Make this private to prevent accidental misuse; this is only for
|
|
|
|
* fromMarkedLocation().
|
|
|
|
*/
|
2014-05-25 18:46:24 -07:00
|
|
|
explicit InternalHandle(T *field)
|
2013-03-20 03:41:20 -07:00
|
|
|
: holder(reinterpret_cast<void * const *>(&js::NullPtr::constNullValue)),
|
2012-09-10 11:26:46 -07:00
|
|
|
offset(uintptr_t(field))
|
2012-11-14 16:06:27 -08:00
|
|
|
{}
|
2013-11-18 09:21:44 -08:00
|
|
|
|
2015-01-06 15:35:02 -08:00
|
|
|
void operator=(InternalHandle<T*> other) = delete;
|
2012-09-26 11:13:20 -07:00
|
|
|
};
|
|
|
|
|
2012-07-18 14:32:39 -07:00
|
|
|
/*
|
2014-06-05 07:38:00 -07:00
|
|
|
* By default, things should use the inheritance hierarchy to find their
|
2012-07-18 14:32:39 -07:00
|
|
|
* ThingRootKind. Some pointer types are explicitly set in jspubtd.h so that
|
|
|
|
* Rooted<T> may be used without the class definition being available.
|
|
|
|
*/
|
2014-06-05 07:38:00 -07:00
|
|
|
template <typename T>
|
|
|
|
struct RootKind
|
|
|
|
{
|
|
|
|
static ThingRootKind rootKind() { return T::rootKind(); }
|
|
|
|
};
|
|
|
|
|
2012-07-18 14:32:39 -07:00
|
|
|
template <typename T>
|
2012-11-14 16:06:27 -08:00
|
|
|
struct RootKind<T *>
|
|
|
|
{
|
|
|
|
static ThingRootKind rootKind() { return T::rootKind(); }
|
|
|
|
};
|
2012-07-18 14:32:39 -07:00
|
|
|
|
2012-01-20 19:49:33 -08:00
|
|
|
template <typename T>
|
2013-06-19 03:32:26 -07:00
|
|
|
struct GCMethods<T *>
|
2012-01-20 19:49:33 -08:00
|
|
|
{
|
2013-09-19 12:24:53 -07:00
|
|
|
static T *initial() { return nullptr; }
|
2013-03-19 03:35:41 -07:00
|
|
|
static bool poisoned(T *v) { return JS::IsPoisonedPtr(v); }
|
2014-04-11 14:29:14 -07:00
|
|
|
static bool needsPostBarrier(T *v) { return false; }
|
|
|
|
static void postBarrier(T **vp) {}
|
|
|
|
static void relocate(T **vp) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct GCMethods<JSObject *>
|
|
|
|
{
|
|
|
|
static JSObject *initial() { return nullptr; }
|
|
|
|
static bool poisoned(JSObject *v) { return JS::IsPoisonedPtr(v); }
|
2014-07-31 14:43:45 -07:00
|
|
|
static gc::Cell *asGCThingOrNull(JSObject *v) {
|
|
|
|
if (!v)
|
|
|
|
return nullptr;
|
2014-10-01 10:17:51 -07:00
|
|
|
MOZ_ASSERT(uintptr_t(v) > 32);
|
2014-07-31 14:43:45 -07:00
|
|
|
return reinterpret_cast<gc::Cell *>(v);
|
|
|
|
}
|
2014-05-01 09:26:12 -07:00
|
|
|
static bool needsPostBarrier(JSObject *v) {
|
|
|
|
return v != nullptr && gc::IsInsideNursery(reinterpret_cast<gc::Cell *>(v));
|
|
|
|
}
|
2014-04-11 14:29:14 -07:00
|
|
|
static void postBarrier(JSObject **vp) {
|
2013-05-27 04:51:25 -07:00
|
|
|
JS::HeapCellPostBarrier(reinterpret_cast<js::gc::Cell **>(vp));
|
|
|
|
}
|
2014-04-11 14:29:14 -07:00
|
|
|
static void relocate(JSObject **vp) {
|
2013-05-27 04:51:25 -07:00
|
|
|
JS::HeapCellRelocate(reinterpret_cast<js::gc::Cell **>(vp));
|
|
|
|
}
|
2012-01-20 19:49:33 -08:00
|
|
|
};
|
|
|
|
|
2014-06-23 12:36:36 -07:00
|
|
|
template <>
|
|
|
|
struct GCMethods<JSFunction *>
|
|
|
|
{
|
|
|
|
static JSFunction *initial() { return nullptr; }
|
|
|
|
static bool poisoned(JSFunction *v) { return JS::IsPoisonedPtr(v); }
|
|
|
|
static bool needsPostBarrier(JSFunction *v) {
|
|
|
|
return v != nullptr && gc::IsInsideNursery(reinterpret_cast<gc::Cell *>(v));
|
|
|
|
}
|
|
|
|
static void postBarrier(JSFunction **vp) {
|
|
|
|
JS::HeapCellPostBarrier(reinterpret_cast<js::gc::Cell **>(vp));
|
|
|
|
}
|
|
|
|
static void relocate(JSFunction **vp) {
|
|
|
|
JS::HeapCellRelocate(reinterpret_cast<js::gc::Cell **>(vp));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-03-06 08:41:38 -08:00
|
|
|
} /* namespace js */
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2012-01-20 19:49:33 -08:00
|
|
|
/*
|
2012-05-24 08:52:21 -07:00
|
|
|
* Local variable of type T whose value is always rooted. This is typically
|
|
|
|
* used for local variables, or for non-rooted values being passed to a
|
|
|
|
* function that requires a handle, e.g. Foo(Root<T>(cx, x)).
|
2012-07-12 03:01:59 -07:00
|
|
|
*
|
2012-07-12 16:42:26 -07:00
|
|
|
* If you want to add additional methods to Rooted for a specific
|
|
|
|
* specialization, define a RootedBase<T> specialization containing them.
|
2012-01-20 19:49:33 -08:00
|
|
|
*/
|
|
|
|
template <typename T>
|
2013-04-25 03:55:30 -07:00
|
|
|
class MOZ_STACK_CLASS Rooted : public js::RootedBase<T>
|
2012-01-20 19:49:33 -08:00
|
|
|
{
|
2013-07-10 08:29:52 -07:00
|
|
|
/* Note: CX is a subclass of either ContextFriendFields or PerThreadDataFriendFields. */
|
|
|
|
template <typename CX>
|
|
|
|
void init(CX *cx) {
|
2014-06-07 02:34:57 -07:00
|
|
|
js::ThingRootKind kind = js::RootKind<T>::rootKind();
|
2013-07-10 08:29:52 -07:00
|
|
|
this->stack = &cx->thingGCRooters[kind];
|
|
|
|
this->prev = *stack;
|
|
|
|
*stack = reinterpret_cast<Rooted<void*>*>(this);
|
2012-01-20 19:49:33 -08:00
|
|
|
|
2014-02-17 22:24:15 -08:00
|
|
|
MOZ_ASSERT(!js::GCMethods<T>::poisoned(ptr));
|
2013-02-07 11:34:43 -08:00
|
|
|
}
|
|
|
|
|
2012-05-24 08:52:21 -07:00
|
|
|
public:
|
2014-05-25 18:46:24 -07:00
|
|
|
explicit Rooted(JSContext *cx
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
2013-06-19 03:32:26 -07:00
|
|
|
: ptr(js::GCMethods<T>::initial())
|
2012-09-11 14:58:47 -07:00
|
|
|
{
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
2013-07-10 08:29:52 -07:00
|
|
|
init(js::ContextFriendFields::get(cx));
|
2012-09-11 14:58:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Rooted(JSContext *cx, T initial
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
2012-11-27 13:28:51 -08:00
|
|
|
: ptr(initial)
|
2013-07-10 08:29:52 -07:00
|
|
|
{
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
init(js::ContextFriendFields::get(cx));
|
|
|
|
}
|
|
|
|
|
2014-05-25 18:46:24 -07:00
|
|
|
explicit Rooted(js::ContextFriendFields *cx
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
2013-07-10 08:29:52 -07:00
|
|
|
: ptr(js::GCMethods<T>::initial())
|
2012-09-11 14:58:47 -07:00
|
|
|
{
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
init(cx);
|
|
|
|
}
|
2012-05-24 08:52:21 -07:00
|
|
|
|
2013-07-10 08:29:52 -07:00
|
|
|
Rooted(js::ContextFriendFields *cx, T initial
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
|
|
|
: ptr(initial)
|
|
|
|
{
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
init(cx);
|
|
|
|
}
|
|
|
|
|
2014-05-25 18:46:24 -07:00
|
|
|
explicit Rooted(js::PerThreadDataFriendFields *pt
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
2013-06-19 03:32:26 -07:00
|
|
|
: ptr(js::GCMethods<T>::initial())
|
2013-02-07 11:34:43 -08:00
|
|
|
{
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
2013-07-10 08:29:52 -07:00
|
|
|
init(pt);
|
2013-02-07 11:34:43 -08:00
|
|
|
}
|
|
|
|
|
2013-07-10 08:29:52 -07:00
|
|
|
Rooted(js::PerThreadDataFriendFields *pt, T initial
|
2013-02-07 11:34:43 -08:00
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
|
|
|
: ptr(initial)
|
|
|
|
{
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
2013-07-10 08:29:52 -07:00
|
|
|
init(pt);
|
2013-06-12 14:17:54 -07:00
|
|
|
}
|
|
|
|
|
2014-05-25 18:46:24 -07:00
|
|
|
explicit Rooted(JSRuntime *rt
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
2013-06-19 03:32:26 -07:00
|
|
|
: ptr(js::GCMethods<T>::initial())
|
2013-06-12 14:17:54 -07:00
|
|
|
{
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
init(js::PerThreadDataFriendFields::getMainThread(rt));
|
|
|
|
}
|
|
|
|
|
|
|
|
Rooted(JSRuntime *rt, T initial
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
|
|
|
: ptr(initial)
|
|
|
|
{
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
init(js::PerThreadDataFriendFields::getMainThread(rt));
|
2013-02-07 11:34:43 -08:00
|
|
|
}
|
|
|
|
|
2013-09-23 12:28:08 -07:00
|
|
|
~Rooted() {
|
2014-02-17 22:24:15 -08:00
|
|
|
MOZ_ASSERT(*stack == reinterpret_cast<Rooted<void*>*>(this));
|
2012-01-20 19:49:33 -08:00
|
|
|
*stack = prev;
|
|
|
|
}
|
|
|
|
|
2014-06-05 07:38:00 -07:00
|
|
|
Rooted<T> *previous() { return reinterpret_cast<Rooted<T>*>(prev); }
|
2012-01-20 19:49:33 -08:00
|
|
|
|
2013-05-24 19:38:09 -07:00
|
|
|
/*
|
2015-02-04 15:27:00 -08:00
|
|
|
* This method is public for Rooted so that Codegen.py can use a Rooted
|
|
|
|
* interchangeably with a MutableHandleValue.
|
2013-05-24 19:38:09 -07:00
|
|
|
*/
|
2013-06-07 19:45:45 -07:00
|
|
|
void set(T value) {
|
2014-02-17 22:24:15 -08:00
|
|
|
MOZ_ASSERT(!js::GCMethods<T>::poisoned(value));
|
2013-06-07 19:45:45 -07:00
|
|
|
ptr = value;
|
|
|
|
}
|
|
|
|
|
2015-02-04 15:27:00 -08:00
|
|
|
DECLARE_POINTER_COMPARISON_OPS(T);
|
|
|
|
DECLARE_POINTER_CONSTREF_OPS(T);
|
|
|
|
DECLARE_POINTER_ASSIGN_OPS(Rooted, T);
|
|
|
|
DECLARE_NONPOINTER_ACCESSOR_METHODS(ptr);
|
|
|
|
DECLARE_NONPOINTER_MUTABLE_ACCESSOR_METHODS(ptr);
|
2012-12-31 12:40:21 -08:00
|
|
|
|
2012-01-20 19:49:33 -08:00
|
|
|
private:
|
2014-06-05 07:38:00 -07:00
|
|
|
/*
|
|
|
|
* These need to be templated on void* to avoid aliasing issues between, for
|
|
|
|
* example, Rooted<JSObject> and Rooted<JSFunction>, which use the same
|
|
|
|
* stack head pointer for different classes.
|
|
|
|
*/
|
|
|
|
Rooted<void *> **stack, *prev;
|
2012-11-19 14:20:12 -08:00
|
|
|
|
2013-01-10 17:46:06 -08:00
|
|
|
/*
|
|
|
|
* |ptr| must be the last field in Rooted because the analysis treats all
|
|
|
|
* Rooted as Rooted<void*> during the analysis. See bug 829372.
|
|
|
|
*/
|
|
|
|
T ptr;
|
|
|
|
|
|
|
|
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
|
|
|
|
|
2015-01-06 15:35:02 -08:00
|
|
|
Rooted(const Rooted &) = delete;
|
2012-01-20 19:49:33 -08:00
|
|
|
};
|
|
|
|
|
2013-03-06 08:41:38 -08:00
|
|
|
} /* namespace JS */
|
|
|
|
|
|
|
|
namespace js {
|
|
|
|
|
2014-02-13 09:17:43 -08:00
|
|
|
/*
|
|
|
|
* Augment the generic Rooted<T> interface when T = JSObject* with
|
|
|
|
* class-querying and downcasting operations.
|
|
|
|
*
|
|
|
|
* Given a Rooted<JSObject*> obj, one can view
|
|
|
|
* Handle<StringObject*> h = obj.as<StringObject*>();
|
|
|
|
* as an optimization of
|
|
|
|
* Rooted<StringObject*> rooted(cx, &obj->as<StringObject*>());
|
|
|
|
* Handle<StringObject*> h = rooted;
|
|
|
|
*/
|
|
|
|
template <>
|
|
|
|
class RootedBase<JSObject*>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
template <class U>
|
|
|
|
JS::Handle<U*> as() const;
|
|
|
|
};
|
|
|
|
|
2014-10-02 19:32:39 -07:00
|
|
|
/*
|
|
|
|
* Augment the generic Handle<T> interface when T = JSObject* with
|
|
|
|
* downcasting operations.
|
|
|
|
*
|
|
|
|
* Given a Handle<JSObject*> obj, one can view
|
|
|
|
* Handle<StringObject*> h = obj.as<StringObject*>();
|
|
|
|
* as an optimization of
|
|
|
|
* Rooted<StringObject*> rooted(cx, &obj->as<StringObject*>());
|
|
|
|
* Handle<StringObject*> h = rooted;
|
|
|
|
*/
|
|
|
|
template <>
|
|
|
|
class HandleBase<JSObject*>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
template <class U>
|
|
|
|
JS::Handle<U*> as() const;
|
|
|
|
};
|
|
|
|
|
2013-01-23 14:22:10 -08:00
|
|
|
/* Interface substitute for Rooted<T> which does not root the variable's memory. */
|
|
|
|
template <typename T>
|
|
|
|
class FakeRooted : public RootedBase<T>
|
|
|
|
{
|
|
|
|
public:
|
2013-07-10 08:29:52 -07:00
|
|
|
template <typename CX>
|
|
|
|
FakeRooted(CX *cx
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
2013-06-19 03:32:26 -07:00
|
|
|
: ptr(GCMethods<T>::initial())
|
2013-01-23 14:22:10 -08:00
|
|
|
{
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
}
|
|
|
|
|
2013-07-10 08:29:52 -07:00
|
|
|
template <typename CX>
|
|
|
|
FakeRooted(CX *cx, T initial
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
2013-01-23 14:22:10 -08:00
|
|
|
: ptr(initial)
|
|
|
|
{
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
}
|
|
|
|
|
2015-02-04 15:27:00 -08:00
|
|
|
DECLARE_POINTER_COMPARISON_OPS(T);
|
|
|
|
DECLARE_POINTER_CONSTREF_OPS(T);
|
|
|
|
DECLARE_POINTER_ASSIGN_OPS(FakeRooted, T);
|
|
|
|
DECLARE_NONPOINTER_ACCESSOR_METHODS(ptr);
|
|
|
|
DECLARE_NONPOINTER_MUTABLE_ACCESSOR_METHODS(ptr);
|
2013-01-23 14:22:10 -08:00
|
|
|
|
2015-02-04 15:27:00 -08:00
|
|
|
private:
|
|
|
|
T ptr;
|
|
|
|
|
|
|
|
void set(const T &value) {
|
2014-02-17 22:24:15 -08:00
|
|
|
MOZ_ASSERT(!GCMethods<T>::poisoned(value));
|
2013-01-23 14:22:10 -08:00
|
|
|
ptr = value;
|
2013-11-18 09:21:44 -08:00
|
|
|
}
|
|
|
|
|
2013-01-23 14:22:10 -08:00
|
|
|
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
|
|
|
|
|
2015-01-06 15:35:02 -08:00
|
|
|
FakeRooted(const FakeRooted &) = delete;
|
2013-01-23 14:22:10 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Interface substitute for MutableHandle<T> which is not required to point to rooted memory. */
|
|
|
|
template <typename T>
|
|
|
|
class FakeMutableHandle : public js::MutableHandleBase<T>
|
|
|
|
{
|
|
|
|
public:
|
2014-05-25 18:46:24 -07:00
|
|
|
MOZ_IMPLICIT FakeMutableHandle(T *t) {
|
2013-01-23 14:22:10 -08:00
|
|
|
ptr = t;
|
|
|
|
}
|
|
|
|
|
2014-05-25 18:46:24 -07:00
|
|
|
MOZ_IMPLICIT FakeMutableHandle(FakeRooted<T> *root) {
|
2013-01-24 19:18:34 -08:00
|
|
|
ptr = root->address();
|
|
|
|
}
|
|
|
|
|
2013-01-23 14:22:10 -08:00
|
|
|
void set(T v) {
|
2014-02-17 22:24:15 -08:00
|
|
|
MOZ_ASSERT(!js::GCMethods<T>::poisoned(v));
|
2013-01-23 14:22:10 -08:00
|
|
|
*ptr = v;
|
|
|
|
}
|
|
|
|
|
2015-02-04 15:27:00 -08:00
|
|
|
DECLARE_POINTER_CONSTREF_OPS(T);
|
|
|
|
DECLARE_NONPOINTER_ACCESSOR_METHODS(*ptr);
|
|
|
|
DECLARE_NONPOINTER_MUTABLE_ACCESSOR_METHODS(*ptr);
|
2013-01-23 14:22:10 -08:00
|
|
|
|
|
|
|
private:
|
|
|
|
FakeMutableHandle() {}
|
2015-02-04 15:27:00 -08:00
|
|
|
DELETE_ASSIGNMENT_OPS(FakeMutableHandle, T);
|
2013-01-23 14:22:10 -08:00
|
|
|
|
|
|
|
T *ptr;
|
|
|
|
};
|
|
|
|
|
2013-01-18 19:35:08 -08:00
|
|
|
/*
|
|
|
|
* Types for a variable that either should or shouldn't be rooted, depending on
|
2014-02-01 12:04:03 -08:00
|
|
|
* the template parameter allowGC. Used for implementing functions that can
|
2013-01-18 19:35:08 -08:00
|
|
|
* operate on either rooted or unrooted data.
|
2013-01-23 14:22:10 -08:00
|
|
|
*
|
|
|
|
* The toHandle() and toMutableHandle() functions are for calling functions
|
2013-01-24 19:18:34 -08:00
|
|
|
* which require handle types and are only called in the CanGC case. These
|
2013-01-23 14:22:10 -08:00
|
|
|
* allow the calling code to type check.
|
2013-01-18 19:35:08 -08:00
|
|
|
*/
|
|
|
|
enum AllowGC {
|
2013-01-24 19:18:34 -08:00
|
|
|
NoGC = 0,
|
|
|
|
CanGC = 1
|
2013-01-18 19:35:08 -08:00
|
|
|
};
|
|
|
|
template <typename T, AllowGC allowGC>
|
|
|
|
class MaybeRooted
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2013-01-24 19:18:34 -08:00
|
|
|
template <typename T> class MaybeRooted<T, CanGC>
|
2013-01-18 19:35:08 -08:00
|
|
|
{
|
|
|
|
public:
|
2013-03-19 03:35:41 -07:00
|
|
|
typedef JS::Handle<T> HandleType;
|
|
|
|
typedef JS::Rooted<T> RootType;
|
|
|
|
typedef JS::MutableHandle<T> MutableHandleType;
|
2013-01-23 14:22:10 -08:00
|
|
|
|
2013-03-19 03:35:41 -07:00
|
|
|
static inline JS::Handle<T> toHandle(HandleType v) {
|
2013-01-23 14:22:10 -08:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2013-03-19 03:35:41 -07:00
|
|
|
static inline JS::MutableHandle<T> toMutableHandle(MutableHandleType v) {
|
2013-01-23 14:22:10 -08:00
|
|
|
return v;
|
|
|
|
}
|
2014-10-02 19:32:39 -07:00
|
|
|
|
|
|
|
template <typename T2>
|
|
|
|
static inline JS::Handle<T2*> downcastHandle(HandleType v) {
|
|
|
|
return v.template as<T2>();
|
|
|
|
}
|
2013-01-18 19:35:08 -08:00
|
|
|
};
|
|
|
|
|
2013-01-24 19:18:34 -08:00
|
|
|
template <typename T> class MaybeRooted<T, NoGC>
|
2013-01-18 19:35:08 -08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef T HandleType;
|
2013-01-23 14:22:10 -08:00
|
|
|
typedef FakeRooted<T> RootType;
|
|
|
|
typedef FakeMutableHandle<T> MutableHandleType;
|
|
|
|
|
2014-06-29 23:05:34 -07:00
|
|
|
static JS::Handle<T> toHandle(HandleType v) {
|
|
|
|
MOZ_CRASH("Bad conversion");
|
2013-01-23 14:22:10 -08:00
|
|
|
}
|
|
|
|
|
2014-06-29 23:05:34 -07:00
|
|
|
static JS::MutableHandle<T> toMutableHandle(MutableHandleType v) {
|
|
|
|
MOZ_CRASH("Bad conversion");
|
2013-01-23 14:22:10 -08:00
|
|
|
}
|
2014-10-02 19:32:39 -07:00
|
|
|
|
|
|
|
template <typename T2>
|
|
|
|
static inline T2* downcastHandle(HandleType v) {
|
|
|
|
return &v->template as<T2>();
|
|
|
|
}
|
2013-01-18 19:35:08 -08:00
|
|
|
};
|
|
|
|
|
2012-09-24 18:08:22 -07:00
|
|
|
} /* namespace js */
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2012-11-14 16:06:27 -08:00
|
|
|
template <typename T> template <typename S>
|
2012-09-24 18:08:22 -07:00
|
|
|
inline
|
2013-05-16 09:36:56 -07:00
|
|
|
Handle<T>::Handle(const Rooted<S> &root,
|
2012-09-24 18:08:22 -07:00
|
|
|
typename mozilla::EnableIf<mozilla::IsConvertible<S, T>::value, int>::Type dummy)
|
|
|
|
{
|
|
|
|
ptr = reinterpret_cast<const T *>(root.address());
|
|
|
|
}
|
|
|
|
|
2013-11-04 13:35:08 -08:00
|
|
|
template <typename T> template <typename S>
|
|
|
|
inline
|
|
|
|
Handle<T>::Handle(const PersistentRooted<S> &root,
|
|
|
|
typename mozilla::EnableIf<mozilla::IsConvertible<S, T>::value, int>::Type dummy)
|
|
|
|
{
|
|
|
|
ptr = reinterpret_cast<const T *>(root.address());
|
|
|
|
}
|
|
|
|
|
2012-11-14 16:06:27 -08:00
|
|
|
template <typename T> template <typename S>
|
2012-09-24 18:08:22 -07:00
|
|
|
inline
|
|
|
|
Handle<T>::Handle(MutableHandle<S> &root,
|
|
|
|
typename mozilla::EnableIf<mozilla::IsConvertible<S, T>::value, int>::Type dummy)
|
|
|
|
{
|
|
|
|
ptr = reinterpret_cast<const T *>(root.address());
|
|
|
|
}
|
|
|
|
|
2013-01-04 17:32:01 -08:00
|
|
|
template <typename T>
|
2012-09-24 18:08:22 -07:00
|
|
|
inline
|
2013-03-06 08:41:38 -08:00
|
|
|
MutableHandle<T>::MutableHandle(Rooted<T> *root)
|
2012-09-24 18:08:22 -07:00
|
|
|
{
|
2013-07-18 10:59:53 -07:00
|
|
|
static_assert(sizeof(MutableHandle<T>) == sizeof(T *),
|
|
|
|
"MutableHandle must be binary compatible with T*.");
|
2012-09-24 18:08:22 -07:00
|
|
|
ptr = root->address();
|
|
|
|
}
|
|
|
|
|
2013-11-04 13:35:08 -08:00
|
|
|
template <typename T>
|
|
|
|
inline
|
|
|
|
MutableHandle<T>::MutableHandle(PersistentRooted<T> *root)
|
|
|
|
{
|
|
|
|
static_assert(sizeof(MutableHandle<T>) == sizeof(T *),
|
|
|
|
"MutableHandle must be binary compatible with T*.");
|
|
|
|
ptr = root->address();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A copyable, assignable global GC root type with arbitrary lifetime, an
|
|
|
|
* infallible constructor, and automatic unrooting on destruction.
|
|
|
|
*
|
|
|
|
* These roots can be used in heap-allocated data structures, so they are not
|
|
|
|
* associated with any particular JSContext or stack. They are registered with
|
|
|
|
* the JSRuntime itself, without locking, so they require a full JSContext to be
|
2015-01-23 02:23:56 -08:00
|
|
|
* initialized, not one of its more restricted superclasses. Initialization may
|
|
|
|
* take place on construction, or in two phases if the no-argument constructor
|
|
|
|
* is called followed by init().
|
2013-11-04 13:35:08 -08:00
|
|
|
*
|
|
|
|
* Note that you must not use an PersistentRooted in an object owned by a JS
|
|
|
|
* object:
|
|
|
|
*
|
|
|
|
* Whenever one object whose lifetime is decided by the GC refers to another
|
|
|
|
* such object, that edge must be traced only if the owning JS object is traced.
|
|
|
|
* This applies not only to JS objects (which obviously are managed by the GC)
|
|
|
|
* but also to C++ objects owned by JS objects.
|
|
|
|
*
|
|
|
|
* If you put a PersistentRooted in such a C++ object, that is almost certainly
|
|
|
|
* a leak. When a GC begins, the referent of the PersistentRooted is treated as
|
|
|
|
* live, unconditionally (because a PersistentRooted is a *root*), even if the
|
|
|
|
* JS object that owns it is unreachable. If there is any path from that
|
|
|
|
* referent back to the JS object, then the C++ object containing the
|
|
|
|
* PersistentRooted will not be destructed, and the whole blob of objects will
|
|
|
|
* not be freed, even if there are no references to them from the outside.
|
|
|
|
*
|
|
|
|
* In the context of Firefox, this is a severe restriction: almost everything in
|
|
|
|
* Firefox is owned by some JS object or another, so using PersistentRooted in
|
|
|
|
* such objects would introduce leaks. For these kinds of edges, Heap<T> or
|
|
|
|
* TenuredHeap<T> would be better types. It's up to the implementor of the type
|
|
|
|
* containing Heap<T> or TenuredHeap<T> members to make sure their referents get
|
|
|
|
* marked when the object itself is marked.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
2015-01-23 02:23:56 -08:00
|
|
|
class PersistentRooted : public js::PersistentRootedBase<T>,
|
|
|
|
private mozilla::LinkedListElement<PersistentRooted<T>>
|
|
|
|
{
|
|
|
|
typedef mozilla::LinkedListElement<PersistentRooted<T>> ListBase;
|
2015-01-23 02:23:56 -08:00
|
|
|
|
2014-01-22 03:28:06 -08:00
|
|
|
friend class mozilla::LinkedList<PersistentRooted>;
|
|
|
|
friend class mozilla::LinkedListElement<PersistentRooted>;
|
2014-01-23 01:53:42 -08:00
|
|
|
|
2014-06-18 17:57:51 -07:00
|
|
|
friend struct js::gc::PersistentRootedMarker<T>;
|
2013-11-04 13:35:08 -08:00
|
|
|
|
2015-01-23 02:23:56 -08:00
|
|
|
friend void js::gc::FinishPersistentRootedChains(JSRuntime *rt);
|
|
|
|
|
2013-11-04 13:35:08 -08:00
|
|
|
void registerWithRuntime(JSRuntime *rt) {
|
2015-01-23 02:23:56 -08:00
|
|
|
MOZ_ASSERT(!initialized());
|
2013-11-04 13:35:08 -08:00
|
|
|
JS::shadow::Runtime *srt = JS::shadow::Runtime::asShadowRuntime(rt);
|
|
|
|
srt->getPersistentRootedList<T>().insertBack(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2015-01-23 02:23:56 -08:00
|
|
|
PersistentRooted() : ptr(js::GCMethods<T>::initial()) {}
|
|
|
|
|
|
|
|
explicit PersistentRooted(JSContext *cx) {
|
|
|
|
init(cx);
|
2013-11-04 13:35:08 -08:00
|
|
|
}
|
|
|
|
|
2015-01-23 02:23:56 -08:00
|
|
|
PersistentRooted(JSContext *cx, T initial) {
|
|
|
|
init(cx, initial);
|
2013-11-04 13:35:08 -08:00
|
|
|
}
|
|
|
|
|
2015-01-23 02:23:56 -08:00
|
|
|
explicit PersistentRooted(JSRuntime *rt) {
|
|
|
|
init(rt);
|
2013-11-04 13:35:08 -08:00
|
|
|
}
|
|
|
|
|
2015-01-23 02:23:56 -08:00
|
|
|
PersistentRooted(JSRuntime *rt, T initial) {
|
|
|
|
init(rt, initial);
|
2013-11-04 13:35:08 -08:00
|
|
|
}
|
|
|
|
|
2014-06-23 12:59:19 -07:00
|
|
|
PersistentRooted(const PersistentRooted &rhs)
|
2015-01-23 02:23:56 -08:00
|
|
|
: mozilla::LinkedListElement<PersistentRooted<T>>(),
|
2014-05-22 11:15:06 -07:00
|
|
|
ptr(rhs.ptr)
|
2013-11-04 13:35:08 -08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Copy construction takes advantage of the fact that the original
|
|
|
|
* is already inserted, and simply adds itself to whatever list the
|
|
|
|
* original was on - no JSRuntime pointer needed.
|
2014-06-23 12:59:19 -07:00
|
|
|
*
|
|
|
|
* This requires mutating rhs's links, but those should be 'mutable'
|
|
|
|
* anyway. C++ doesn't let us declare mutable base classes.
|
2013-11-04 13:35:08 -08:00
|
|
|
*/
|
2014-06-23 12:59:19 -07:00
|
|
|
const_cast<PersistentRooted &>(rhs).setNext(this);
|
2013-11-04 13:35:08 -08:00
|
|
|
}
|
|
|
|
|
2015-01-23 02:23:56 -08:00
|
|
|
bool initialized() {
|
2015-01-23 02:23:56 -08:00
|
|
|
return ListBase::isInList();
|
2015-01-23 02:23:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void init(JSContext *cx) {
|
|
|
|
init(cx, js::GCMethods<T>::initial());
|
|
|
|
}
|
|
|
|
|
2015-02-04 15:27:00 -08:00
|
|
|
void init(JSContext *cx, T initial) {
|
2015-01-23 02:23:56 -08:00
|
|
|
ptr = initial;
|
|
|
|
registerWithRuntime(js::GetRuntime(cx));
|
|
|
|
}
|
|
|
|
|
|
|
|
void init(JSRuntime *rt) {
|
|
|
|
init(rt, js::GCMethods<T>::initial());
|
|
|
|
}
|
|
|
|
|
2015-02-04 15:27:00 -08:00
|
|
|
void init(JSRuntime *rt, T initial) {
|
2015-01-23 02:23:56 -08:00
|
|
|
ptr = initial;
|
|
|
|
registerWithRuntime(rt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset() {
|
|
|
|
if (initialized()) {
|
|
|
|
set(js::GCMethods<T>::initial());
|
2015-01-23 02:23:56 -08:00
|
|
|
ListBase::remove();
|
2015-01-23 02:23:56 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-04 15:27:00 -08:00
|
|
|
DECLARE_POINTER_COMPARISON_OPS(T);
|
|
|
|
DECLARE_POINTER_CONSTREF_OPS(T);
|
|
|
|
DECLARE_POINTER_ASSIGN_OPS(PersistentRooted, T);
|
|
|
|
DECLARE_NONPOINTER_ACCESSOR_METHODS(ptr);
|
|
|
|
DECLARE_NONPOINTER_MUTABLE_ACCESSOR_METHODS(ptr);
|
2013-11-04 13:35:08 -08:00
|
|
|
|
2015-02-04 15:27:00 -08:00
|
|
|
private:
|
2013-11-04 13:35:08 -08:00
|
|
|
void set(T value) {
|
2015-01-23 02:23:56 -08:00
|
|
|
MOZ_ASSERT(initialized());
|
2014-02-17 22:24:15 -08:00
|
|
|
MOZ_ASSERT(!js::GCMethods<T>::poisoned(value));
|
2013-11-04 13:35:08 -08:00
|
|
|
ptr = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
T ptr;
|
|
|
|
};
|
|
|
|
|
2014-06-16 11:59:45 -07:00
|
|
|
class JS_PUBLIC_API(ObjectPtr)
|
|
|
|
{
|
|
|
|
Heap<JSObject *> value;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ObjectPtr() : value(nullptr) {}
|
|
|
|
|
|
|
|
explicit ObjectPtr(JSObject *obj) : value(obj) {}
|
|
|
|
|
|
|
|
/* Always call finalize before the destructor. */
|
|
|
|
~ObjectPtr() { MOZ_ASSERT(!value); }
|
|
|
|
|
|
|
|
void finalize(JSRuntime *rt) {
|
|
|
|
if (IsIncrementalBarrierNeeded(rt))
|
|
|
|
IncrementalObjectBarrier(value);
|
|
|
|
value = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void init(JSObject *obj) { value = obj; }
|
|
|
|
|
|
|
|
JSObject *get() const { return value; }
|
|
|
|
|
|
|
|
void writeBarrierPre(JSRuntime *rt) {
|
|
|
|
IncrementalObjectBarrier(value);
|
|
|
|
}
|
|
|
|
|
2014-09-24 04:54:11 -07:00
|
|
|
void updateWeakPointerAfterGC();
|
2014-06-16 11:59:45 -07:00
|
|
|
|
|
|
|
ObjectPtr &operator=(JSObject *obj) {
|
|
|
|
IncrementalObjectBarrier(value);
|
|
|
|
value = obj;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void trace(JSTracer *trc, const char *name);
|
|
|
|
|
|
|
|
JSObject &operator*() const { return *value; }
|
|
|
|
JSObject *operator->() const { return value; }
|
|
|
|
operator JSObject *() const { return value; }
|
|
|
|
};
|
|
|
|
|
2012-09-24 18:08:22 -07:00
|
|
|
} /* namespace JS */
|
|
|
|
|
|
|
|
namespace js {
|
2014-07-31 14:43:45 -07:00
|
|
|
namespace gc {
|
|
|
|
|
|
|
|
template <typename T, typename TraceCallbacks>
|
|
|
|
void
|
|
|
|
CallTraceCallbackOnNonHeap(T *v, const TraceCallbacks &aCallbacks, const char *aName, void *aClosure)
|
|
|
|
{
|
|
|
|
static_assert(sizeof(T) == sizeof(JS::Heap<T>), "T and Heap<T> must be compatible.");
|
|
|
|
MOZ_ASSERT(v);
|
|
|
|
mozilla::DebugOnly<Cell *> cell = GCMethods<T>::asGCThingOrNull(*v);
|
|
|
|
MOZ_ASSERT(cell);
|
|
|
|
MOZ_ASSERT(!IsInsideNursery(cell));
|
|
|
|
JS::Heap<T> *asHeapT = reinterpret_cast<JS::Heap<T>*>(v);
|
|
|
|
aCallbacks.Trace(asHeapT, aName, aClosure);
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace gc */
|
|
|
|
} /* namespace js */
|
2012-04-12 09:23:51 -07:00
|
|
|
|
2015-02-04 15:27:00 -08:00
|
|
|
#undef DELETE_ASSIGNMENT_OPS
|
|
|
|
#undef DECLARE_NONPOINTER_MUTABLE_ACCESSOR_METHODS
|
|
|
|
#undef DECLARE_NONPOINTER_ACCESSOR_METHODS
|
|
|
|
|
2013-06-19 17:59:09 -07:00
|
|
|
#endif /* js_RootingAPI_h */
|