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
|
|
|
|
2012-09-10 10:31:58 -07:00
|
|
|
#include "mozilla/GuardObjects.h"
|
2012-12-27 09:20:22 -08:00
|
|
|
#include "mozilla/TypeTraits.h"
|
2012-06-19 15:01:58 -07:00
|
|
|
|
2012-11-29 10:22:12 -08:00
|
|
|
#include "js/Utility.h"
|
2012-07-08 03:00:11 -07:00
|
|
|
#include "js/TemplateLib.h"
|
2012-09-10 10:31:58 -07:00
|
|
|
|
|
|
|
#include "jspubtd.h"
|
2012-04-12 18:15:26 -07:00
|
|
|
|
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 {
|
|
|
|
|
2013-01-18 05:18:07 -08:00
|
|
|
class Module;
|
2013-05-22 16:06:54 -07:00
|
|
|
class ScriptSourceObject;
|
2013-01-18 05:18:07 -08:00
|
|
|
|
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 {};
|
|
|
|
|
2013-03-20 03:41:20 -07:00
|
|
|
/*
|
|
|
|
* js::NullPtr acts like a NULL pointer in contexts that require a Handle.
|
|
|
|
*
|
|
|
|
* Handle provides an implicit constructor for js::NullPtr so that, given:
|
|
|
|
* foo(Handle<JSObject*> h);
|
|
|
|
* callers can simply write:
|
|
|
|
* foo(js::NullPtr());
|
|
|
|
* which avoids creating a Rooted<JSObject*> just to pass NULL.
|
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
} /* namespace gc */
|
|
|
|
|
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;
|
|
|
|
|
2012-11-14 16:06:27 -08:00
|
|
|
template <typename T> class Handle;
|
2012-09-24 18:08:22 -07:00
|
|
|
template <typename T> class MutableHandle;
|
|
|
|
|
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
|
|
|
|
2013-01-10 17:46:06 -08:00
|
|
|
#if defined(DEBUG) && defined(JS_GC_ZEAL) && defined(JSGC_ROOT_ANALYSIS) && !defined(JS_THREADSAFE)
|
|
|
|
extern void
|
|
|
|
CheckStackRoots(JSContext *cx);
|
|
|
|
#endif
|
|
|
|
|
2012-07-08 03:00:11 -07:00
|
|
|
/*
|
2013-03-20 03:41:20 -07:00
|
|
|
* JS::NullPtr acts like a NULL pointer in contexts that require a Handle.
|
|
|
|
*
|
|
|
|
* 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());
|
2012-07-08 03:00:11 -07:00
|
|
|
* which avoids creating a Rooted<JSObject*> just to pass NULL.
|
|
|
|
*/
|
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;
|
|
|
|
};
|
|
|
|
|
2013-05-27 04:51:25 -07:00
|
|
|
/*
|
2013-06-21 06:12:45 -07:00
|
|
|
* An encapsulated pointer class for heap based GC thing pointers.
|
2013-05-27 04:51:25 -07:00
|
|
|
*
|
2013-06-21 06:12:45 -07:00
|
|
|
* This implements post-barriers for GC thing pointers stored on the heap. It is
|
|
|
|
* designed to be used for all heap-based GC thing pointers outside the JS
|
|
|
|
* engine.
|
|
|
|
*
|
|
|
|
* The template parameter T must be a JS GC thing pointer, masked pointer or
|
|
|
|
* possible pointer, such as a JS::Value or jsid.
|
|
|
|
*
|
|
|
|
* The class must be used to declare data members of heap classes only.
|
|
|
|
* Stack-based GC thing pointers should used Rooted<T>.
|
|
|
|
*
|
|
|
|
* Write barriers are implemented by overloading the assingment operator.
|
|
|
|
* Assiging to a Heap<T> triggers the appropriate calls into the GC to notify it
|
|
|
|
* of the change.
|
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() {
|
|
|
|
MOZ_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); }
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2013-07-02 01:43:45 -07:00
|
|
|
bool operator==(const Heap<T> &other) { return ptr == other.ptr; }
|
|
|
|
bool operator!=(const Heap<T> &other) { return ptr != other.ptr; }
|
|
|
|
|
2013-06-18 03:00:37 -07:00
|
|
|
bool operator==(const T &other) const { return ptr == other; }
|
2013-07-02 01:43:45 -07:00
|
|
|
bool operator!=(const T &other) const { return ptr != other; }
|
2013-05-27 04:51:25 -07:00
|
|
|
|
|
|
|
operator T() const { return ptr; }
|
|
|
|
T operator->() const { return ptr; }
|
|
|
|
const T *address() const { return &ptr; }
|
|
|
|
const T &get() const { return ptr; }
|
|
|
|
|
|
|
|
T *unsafeGet() { return &ptr; }
|
|
|
|
|
|
|
|
Heap<T> &operator=(T p) {
|
|
|
|
set(p);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set(T newPtr) {
|
2013-06-19 03:32:26 -07:00
|
|
|
JS_ASSERT(!js::GCMethods<T>::poisoned(newPtr));
|
|
|
|
if (js::GCMethods<T>::needsPostBarrier(newPtr)) {
|
2013-05-27 04:51:25 -07:00
|
|
|
ptr = newPtr;
|
|
|
|
post();
|
2013-06-19 03:32:26 -07:00
|
|
|
} else if (js::GCMethods<T>::needsPostBarrier(ptr)) {
|
2013-05-27 04:51:25 -07:00
|
|
|
relocate(); /* Called before overwriting ptr. */
|
|
|
|
ptr = newPtr;
|
|
|
|
} else {
|
|
|
|
ptr = newPtr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-06-18 03:00:37 -07:00
|
|
|
void init(T newPtr) {
|
2013-06-19 03:32:26 -07:00
|
|
|
JS_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();
|
|
|
|
}
|
|
|
|
|
2013-05-27 04:51:25 -07:00
|
|
|
void post() {
|
|
|
|
#ifdef JSGC_GENERATIONAL
|
2013-06-19 03:32:26 -07:00
|
|
|
JS_ASSERT(js::GCMethods<T>::needsPostBarrier(ptr));
|
|
|
|
js::GCMethods<T>::postBarrier(&ptr);
|
2013-05-27 04:51:25 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void relocate() {
|
|
|
|
#ifdef JSGC_GENERATIONAL
|
2013-06-19 03:32:26 -07:00
|
|
|
js::GCMethods<T>::relocate(&ptr);
|
2013-05-27 04:51:25 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
T ptr;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
{
|
2012-11-14 16:06:27 -08:00
|
|
|
friend class MutableHandle<T>;
|
|
|
|
|
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-06-13 15:14:44 -07:00
|
|
|
MOZ_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
|
|
|
|
2012-07-08 03:00:11 -07:00
|
|
|
/* Create a handle for a NULL pointer. */
|
2013-03-20 03:41:20 -07:00
|
|
|
Handle(js::NullPtr) {
|
2013-03-21 18:26:15 -07:00
|
|
|
MOZ_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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a handle for a NULL pointer. */
|
|
|
|
Handle(JS::NullPtr) {
|
2013-03-21 18:26:15 -07:00
|
|
|
MOZ_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
|
|
|
}
|
|
|
|
|
2012-07-18 14:31:24 -07:00
|
|
|
Handle(MutableHandle<T> handle) {
|
|
|
|
ptr = handle.address();
|
|
|
|
}
|
|
|
|
|
2013-05-27 04:51:25 -07:00
|
|
|
Handle(const Heap<T> &heapPtr) {
|
|
|
|
ptr = heapPtr.address();
|
|
|
|
}
|
|
|
|
|
2012-04-13 18:06:40 -07:00
|
|
|
/*
|
2012-04-27 16:40:10 -07:00
|
|
|
* This may be called only if the location of the T is guaranteed
|
2012-05-24 16:05:18 -07:00
|
|
|
* to be marked (for some reason other than being a Rooted),
|
2012-04-27 16:40:10 -07:00
|
|
|
* e.g., if it is guaranteed to be reachable from an implicit root.
|
|
|
|
*
|
|
|
|
* Create a Handle from a raw location of a T.
|
2012-04-13 18:06:40 -07:00
|
|
|
*/
|
|
|
|
static Handle fromMarkedLocation(const T *p) {
|
|
|
|
Handle h;
|
|
|
|
h.ptr = p;
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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);
|
|
|
|
|
2012-05-19 15:03:45 -07:00
|
|
|
const T *address() const { return ptr; }
|
2013-05-24 19:38:09 -07:00
|
|
|
const T& get() const { return *ptr; }
|
2012-04-12 09:23:51 -07:00
|
|
|
|
2013-05-24 19:38:09 -07:00
|
|
|
/*
|
|
|
|
* Return a reference so passing a Handle<T> to something that
|
|
|
|
* takes a |const T&| is not a GC hazard.
|
|
|
|
*/
|
|
|
|
operator const T&() const { return get(); }
|
2012-11-14 16:06:27 -08:00
|
|
|
T operator->() const { return get(); }
|
2012-04-12 09:23:51 -07:00
|
|
|
|
2013-06-18 03:00:37 -07:00
|
|
|
bool operator!=(const T &other) const { return *ptr != other; }
|
|
|
|
bool operator==(const T &other) const { return *ptr == other; }
|
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() {}
|
|
|
|
|
2012-04-12 09:23:51 -07:00
|
|
|
const T *ptr;
|
2012-07-10 18:17:29 -07:00
|
|
|
|
|
|
|
template <typename S>
|
2012-11-14 16:06:27 -08:00
|
|
|
void operator=(S v) MOZ_DELETE;
|
2012-04-12 22:07:43 -07:00
|
|
|
};
|
2012-04-12 09:23:51 -07:00
|
|
|
|
2013-05-22 16:06:54 -07:00
|
|
|
typedef Handle<JSObject*> HandleObject;
|
|
|
|
typedef Handle<js::Module*> HandleModule;
|
|
|
|
typedef Handle<js::ScriptSourceObject *> HandleScriptSource;
|
|
|
|
typedef Handle<JSFunction*> HandleFunction;
|
|
|
|
typedef Handle<JSScript*> HandleScript;
|
|
|
|
typedef Handle<JSString*> HandleString;
|
|
|
|
typedef Handle<jsid> HandleId;
|
|
|
|
typedef Handle<Value> HandleValue;
|
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:
|
2013-03-06 08:41:38 -08:00
|
|
|
inline MutableHandle(Rooted<T> *root);
|
2012-07-04 11:12:16 -07:00
|
|
|
|
2012-11-14 16:06:27 -08:00
|
|
|
void set(T v) {
|
2013-06-19 03:32:26 -07:00
|
|
|
JS_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;
|
|
|
|
}
|
|
|
|
|
2012-07-04 13:34:42 -07:00
|
|
|
T *address() const { return ptr; }
|
|
|
|
T get() const { return *ptr; }
|
2012-07-04 11:12:16 -07:00
|
|
|
|
2012-11-14 16:06:27 -08:00
|
|
|
operator T() const { return get(); }
|
|
|
|
T operator->() const { return get(); }
|
2012-07-04 11:12:16 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
MutableHandle() {}
|
|
|
|
|
|
|
|
T *ptr;
|
2012-07-30 04:19:09 -07:00
|
|
|
|
|
|
|
template <typename S>
|
2012-11-14 16:06:27 -08:00
|
|
|
void operator=(S v) MOZ_DELETE;
|
2012-07-04 11:12:16 -07:00
|
|
|
};
|
|
|
|
|
2012-09-12 16:19:40 -07:00
|
|
|
typedef MutableHandle<JSObject*> MutableHandleObject;
|
|
|
|
typedef MutableHandle<JSFunction*> MutableHandleFunction;
|
|
|
|
typedef MutableHandle<JSScript*> MutableHandleScript;
|
|
|
|
typedef MutableHandle<JSString*> MutableHandleString;
|
|
|
|
typedef MutableHandle<jsid> MutableHandleId;
|
|
|
|
typedef MutableHandle<Value> MutableHandleValue;
|
2012-07-04 11:12:16 -07:00
|
|
|
|
2013-05-27 04:51:25 -07:00
|
|
|
#ifdef JSGC_GENERATIONAL
|
|
|
|
JS_PUBLIC_API(void) HeapCellPostBarrier(js::gc::Cell **cellp);
|
|
|
|
JS_PUBLIC_API(void) HeapCellRelocate(js::gc::Cell **cellp);
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
|
|
|
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().
|
|
|
|
*/
|
|
|
|
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
|
|
|
{}
|
2012-09-26 11:13:20 -07:00
|
|
|
};
|
|
|
|
|
2012-07-18 14:32:39 -07:00
|
|
|
/*
|
|
|
|
* By default, pointers should use the inheritance hierarchy to find their
|
|
|
|
* ThingRootKind. Some pointer types are explicitly set in jspubtd.h so that
|
|
|
|
* Rooted<T> may be used without the class definition being available.
|
|
|
|
*/
|
|
|
|
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
|
|
|
{
|
|
|
|
static T *initial() { return NULL; }
|
2012-07-18 14:32:39 -07:00
|
|
|
static ThingRootKind kind() { return RootKind<T *>::rootKind(); }
|
2013-03-19 03:35:41 -07:00
|
|
|
static bool poisoned(T *v) { return JS::IsPoisonedPtr(v); }
|
2013-05-27 04:51:25 -07:00
|
|
|
static bool needsPostBarrier(T *v) { return v; }
|
|
|
|
#ifdef JSGC_GENERATIONAL
|
|
|
|
static void postBarrier(T **vp) {
|
|
|
|
JS::HeapCellPostBarrier(reinterpret_cast<js::gc::Cell **>(vp));
|
|
|
|
}
|
|
|
|
static void relocate(T **vp) {
|
|
|
|
JS::HeapCellRelocate(reinterpret_cast<js::gc::Cell **>(vp));
|
|
|
|
}
|
|
|
|
#endif
|
2012-01-20 19:49:33 -08:00
|
|
|
};
|
|
|
|
|
2013-06-20 11:05:34 -07:00
|
|
|
#if defined(DEBUG) && defined(JS_THREADSAFE)
|
|
|
|
/* This helper allows us to assert that Rooted<T> is scoped within a request. */
|
|
|
|
extern JS_PUBLIC_API(bool)
|
|
|
|
IsInRequest(JSContext *cx);
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
{
|
2012-11-14 16:06:27 -08:00
|
|
|
void init(JSContext *cxArg) {
|
2013-06-20 11:05:34 -07:00
|
|
|
MOZ_ASSERT(cxArg);
|
|
|
|
#ifdef JS_THREADSAFE
|
|
|
|
MOZ_ASSERT(js::IsInRequest(cxArg));
|
|
|
|
#endif
|
2013-05-03 13:53:15 -07:00
|
|
|
#ifdef JSGC_TRACK_EXACT_ROOTS
|
2013-03-06 08:41:38 -08:00
|
|
|
js::ContextFriendFields *cx = js::ContextFriendFields::get(cxArg);
|
2012-11-06 19:32:01 -08:00
|
|
|
commonInit(cx->thingGCRooters);
|
2012-04-30 17:10:30 -07:00
|
|
|
#endif
|
2012-01-20 19:49:33 -08:00
|
|
|
}
|
|
|
|
|
2013-06-12 14:17:54 -07:00
|
|
|
void init(js::PerThreadDataFriendFields *pt) {
|
2013-06-20 11:05:34 -07:00
|
|
|
MOZ_ASSERT(pt);
|
2013-05-03 13:53:15 -07:00
|
|
|
#ifdef JSGC_TRACK_EXACT_ROOTS
|
2013-02-07 11:34:43 -08:00
|
|
|
commonInit(pt->thingGCRooters);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-05-24 08:52:21 -07:00
|
|
|
public:
|
2012-09-11 14:58:47 -07:00
|
|
|
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;
|
|
|
|
init(cx);
|
|
|
|
}
|
|
|
|
|
|
|
|
Rooted(JSContext *cx, T initial
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
2012-11-27 13:28:51 -08:00
|
|
|
: ptr(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-03-06 08:41:38 -08:00
|
|
|
Rooted(js::PerThreadData *pt
|
2013-02-07 11:34:43 -08:00
|
|
|
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-06-12 14:17:54 -07:00
|
|
|
init(js::PerThreadDataFriendFields::get(pt));
|
2013-02-07 11:34:43 -08:00
|
|
|
}
|
|
|
|
|
2013-03-06 08:41:38 -08:00
|
|
|
Rooted(js::PerThreadData *pt, T initial
|
2013-02-07 11:34:43 -08:00
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
|
|
|
: ptr(initial)
|
|
|
|
{
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
2013-06-12 14:17:54 -07:00
|
|
|
init(js::PerThreadDataFriendFields::get(pt));
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-11-14 16:06:27 -08:00
|
|
|
~Rooted() {
|
2013-05-03 13:53:15 -07:00
|
|
|
#ifdef JSGC_TRACK_EXACT_ROOTS
|
2013-02-15 10:18:30 -08:00
|
|
|
JS_ASSERT(*stack == reinterpret_cast<Rooted<void*>*>(this));
|
2012-01-20 19:49:33 -08:00
|
|
|
*stack = prev;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-05-03 13:53:15 -07:00
|
|
|
#ifdef JSGC_TRACK_EXACT_ROOTS
|
2012-05-24 16:05:18 -07:00
|
|
|
Rooted<T> *previous() { return prev; }
|
2012-01-20 19:49:33 -08:00
|
|
|
#endif
|
|
|
|
|
2013-05-24 19:38:09 -07:00
|
|
|
/*
|
|
|
|
* Important: Return a reference here so passing a Rooted<T> to
|
|
|
|
* something that takes a |const T&| is not a GC hazard.
|
|
|
|
*/
|
|
|
|
operator const T&() const { return ptr; }
|
2012-11-14 16:06:27 -08:00
|
|
|
T operator->() const { return ptr; }
|
|
|
|
T *address() { return &ptr; }
|
|
|
|
const T *address() const { return &ptr; }
|
|
|
|
T &get() { return ptr; }
|
|
|
|
const T &get() const { return ptr; }
|
2012-05-24 08:52:21 -07:00
|
|
|
|
2012-11-14 16:06:27 -08:00
|
|
|
T &operator=(T value) {
|
2013-06-19 03:32:26 -07:00
|
|
|
JS_ASSERT(!js::GCMethods<T>::poisoned(value));
|
2012-05-24 08:52:21 -07:00
|
|
|
ptr = value;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2012-11-14 16:06:27 -08:00
|
|
|
T &operator=(const Rooted &value) {
|
2012-05-24 08:52:21 -07:00
|
|
|
ptr = value;
|
|
|
|
return ptr;
|
|
|
|
}
|
2012-01-20 19:49:33 -08:00
|
|
|
|
2013-06-07 19:45:45 -07:00
|
|
|
void set(T value) {
|
2013-06-19 03:32:26 -07:00
|
|
|
JS_ASSERT(!js::GCMethods<T>::poisoned(value));
|
2013-06-07 19:45:45 -07:00
|
|
|
ptr = value;
|
|
|
|
}
|
|
|
|
|
2013-06-18 03:00:37 -07:00
|
|
|
bool operator!=(const T &other) const { return ptr != other; }
|
|
|
|
bool operator==(const T &other) const { return ptr == other; }
|
2012-12-31 12:40:21 -08:00
|
|
|
|
2012-01-20 19:49:33 -08:00
|
|
|
private:
|
2012-11-06 19:32:01 -08:00
|
|
|
void commonInit(Rooted<void*> **thingGCRooters) {
|
2013-06-14 13:48:39 -07:00
|
|
|
#if defined(DEBUG) && defined(JS_GC_ZEAL) && defined(JSGC_ROOT_ANALYSIS) && !defined(JS_THREADSAFE)
|
|
|
|
scanned = false;
|
|
|
|
#endif
|
2013-05-03 13:53:15 -07:00
|
|
|
#ifdef JSGC_TRACK_EXACT_ROOTS
|
2013-06-19 03:32:26 -07:00
|
|
|
js::ThingRootKind kind = js::GCMethods<T>::kind();
|
2013-02-15 10:18:30 -08:00
|
|
|
this->stack = &thingGCRooters[kind];
|
2012-11-06 19:32:01 -08:00
|
|
|
this->prev = *stack;
|
2013-02-15 10:18:30 -08:00
|
|
|
*stack = reinterpret_cast<Rooted<void*>*>(this);
|
2012-11-06 19:32:01 -08:00
|
|
|
|
2013-06-19 03:32:26 -07:00
|
|
|
JS_ASSERT(!js::GCMethods<T>::poisoned(ptr));
|
2012-11-06 19:32:01 -08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-05-03 13:53:15 -07:00
|
|
|
#ifdef JSGC_TRACK_EXACT_ROOTS
|
2013-02-15 10:18:30 -08:00
|
|
|
Rooted<void*> **stack, *prev;
|
2012-01-20 19:49:33 -08:00
|
|
|
#endif
|
2012-11-19 14:20:12 -08:00
|
|
|
|
2013-06-26 11:21:36 -07:00
|
|
|
#if defined(DEBUG) && defined(JS_GC_ZEAL) && defined(JSGC_ROOT_ANALYSIS) && !defined(JS_THREADSAFE)
|
2012-11-19 14:20:12 -08:00
|
|
|
/* Has the rooting analysis ever scanned this Rooted's stack location? */
|
2013-01-10 17:46:06 -08:00
|
|
|
friend void JS::CheckStackRoots(JSContext*);
|
2013-06-26 11:21:36 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef JSGC_ROOT_ANALYSIS
|
2012-11-19 14:20:12 -08:00
|
|
|
bool scanned;
|
|
|
|
#endif
|
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
|
|
|
|
|
|
|
|
Rooted(const Rooted &) MOZ_DELETE;
|
2012-01-20 19:49:33 -08:00
|
|
|
};
|
|
|
|
|
2012-11-02 08:12:45 -07:00
|
|
|
#if !(defined(JSGC_ROOT_ANALYSIS) || defined(JSGC_USE_EXACT_ROOTING))
|
|
|
|
// Defined in vm/String.h.
|
|
|
|
template <>
|
|
|
|
class Rooted<JSStableString *>;
|
|
|
|
#endif
|
|
|
|
|
2013-05-22 16:06:54 -07:00
|
|
|
typedef Rooted<JSObject*> RootedObject;
|
|
|
|
typedef Rooted<js::Module*> RootedModule;
|
|
|
|
typedef Rooted<js::ScriptSourceObject *> RootedScriptSource;
|
|
|
|
typedef Rooted<JSFunction*> RootedFunction;
|
|
|
|
typedef Rooted<JSScript*> RootedScript;
|
|
|
|
typedef Rooted<JSString*> RootedString;
|
|
|
|
typedef Rooted<jsid> RootedId;
|
|
|
|
typedef Rooted<JS::Value> RootedValue;
|
2012-04-12 09:23:51 -07:00
|
|
|
|
2013-03-06 08:41:38 -08:00
|
|
|
} /* namespace JS */
|
|
|
|
|
|
|
|
namespace js {
|
|
|
|
|
2012-04-12 09:23:51 -07:00
|
|
|
/*
|
|
|
|
* Mark a stack location as a root for the rooting analysis, without actually
|
|
|
|
* rooting it in release builds. This should only be used for stack locations
|
|
|
|
* of GC things that cannot be relocated by a garbage collection, and that
|
|
|
|
* are definitely reachable via another path.
|
|
|
|
*/
|
|
|
|
class SkipRoot
|
2012-01-20 19:49:33 -08:00
|
|
|
{
|
2012-07-02 09:07:57 -07:00
|
|
|
#if defined(DEBUG) && defined(JS_GC_ZEAL) && defined(JSGC_ROOT_ANALYSIS) && !defined(JS_THREADSAFE)
|
2012-01-20 19:49:33 -08:00
|
|
|
|
2012-04-12 09:23:51 -07:00
|
|
|
SkipRoot **stack, *prev;
|
|
|
|
const uint8_t *start;
|
|
|
|
const uint8_t *end;
|
2012-01-20 19:49:33 -08:00
|
|
|
|
|
|
|
template <typename T>
|
2013-01-17 08:42:00 -08:00
|
|
|
void init(SkipRoot **head, const T *ptr, size_t count) {
|
|
|
|
this->stack = head;
|
2012-01-20 19:49:33 -08:00
|
|
|
this->prev = *stack;
|
|
|
|
*stack = this;
|
2012-04-12 09:23:51 -07:00
|
|
|
this->start = (const uint8_t *) ptr;
|
2012-04-30 17:10:30 -07:00
|
|
|
this->end = this->start + (sizeof(T) * count);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
template <typename T>
|
2012-07-16 06:06:49 -07:00
|
|
|
SkipRoot(JSContext *cx, const T *ptr, size_t count = 1
|
2012-12-27 09:20:22 -08:00
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
2012-04-30 17:10:30 -07:00
|
|
|
{
|
2013-01-17 08:42:00 -08:00
|
|
|
init(&ContextFriendFields::get(cx)->skipGCRooters, ptr, count);
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
SkipRoot(js::PerThreadData *ptd, const T *ptr, size_t count = 1
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
|
|
|
{
|
|
|
|
PerThreadDataFriendFields *ptff = PerThreadDataFriendFields::get(ptd);
|
|
|
|
init(&ptff->skipGCRooters, ptr, count);
|
2012-12-27 09:20:22 -08:00
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
2012-01-20 19:49:33 -08:00
|
|
|
}
|
|
|
|
|
2012-11-14 16:06:27 -08:00
|
|
|
~SkipRoot() {
|
2012-01-20 19:49:33 -08:00
|
|
|
JS_ASSERT(*stack == this);
|
|
|
|
*stack = prev;
|
|
|
|
}
|
|
|
|
|
2012-04-12 09:23:51 -07:00
|
|
|
SkipRoot *previous() { return prev; }
|
2012-01-20 19:49:33 -08:00
|
|
|
|
|
|
|
bool contains(const uint8_t *v, size_t len) {
|
2012-04-12 09:23:51 -07:00
|
|
|
return v >= start && v + len <= end;
|
2012-01-20 19:49:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* DEBUG && JSGC_ROOT_ANALYSIS */
|
|
|
|
|
|
|
|
public:
|
|
|
|
template <typename T>
|
2012-07-16 06:06:49 -07:00
|
|
|
SkipRoot(JSContext *cx, const T *ptr, size_t count = 1
|
2012-12-27 09:20:22 -08:00
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
2012-04-30 17:10:30 -07:00
|
|
|
{
|
2012-12-27 09:20:22 -08:00
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
2012-04-30 17:10:30 -07:00
|
|
|
}
|
|
|
|
|
2013-01-17 08:42:00 -08:00
|
|
|
template <typename T>
|
|
|
|
SkipRoot(PerThreadData *ptd, const T *ptr, size_t count = 1
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
|
|
|
{
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
}
|
|
|
|
|
2012-01-20 19:49:33 -08:00
|
|
|
#endif /* DEBUG && JSGC_ROOT_ANALYSIS */
|
|
|
|
|
2012-12-27 09:20:22 -08:00
|
|
|
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
|
2012-01-20 19:49:33 -08:00
|
|
|
};
|
|
|
|
|
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:
|
|
|
|
FakeRooted(JSContext *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;
|
|
|
|
}
|
|
|
|
|
|
|
|
FakeRooted(JSContext *cx, T initial
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
|
|
|
: ptr(initial)
|
|
|
|
{
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator T() const { return ptr; }
|
|
|
|
T operator->() const { return ptr; }
|
|
|
|
T *address() { return &ptr; }
|
|
|
|
const T *address() const { return &ptr; }
|
|
|
|
T &get() { return ptr; }
|
|
|
|
const T &get() const { return ptr; }
|
|
|
|
|
|
|
|
T &operator=(T value) {
|
2013-06-19 03:32:26 -07:00
|
|
|
JS_ASSERT(!GCMethods<T>::poisoned(value));
|
2013-01-23 14:22:10 -08:00
|
|
|
ptr = value;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2013-06-18 03:00:37 -07:00
|
|
|
bool operator!=(const T &other) const { return ptr != other; }
|
|
|
|
bool operator==(const T &other) const { return ptr == other; }
|
2013-01-23 14:22:10 -08:00
|
|
|
|
|
|
|
private:
|
|
|
|
T ptr;
|
|
|
|
|
|
|
|
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
|
|
|
|
|
|
|
|
FakeRooted(const FakeRooted &) MOZ_DELETE;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Interface substitute for MutableHandle<T> which is not required to point to rooted memory. */
|
|
|
|
template <typename T>
|
|
|
|
class FakeMutableHandle : public js::MutableHandleBase<T>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FakeMutableHandle(T *t) {
|
|
|
|
ptr = t;
|
|
|
|
}
|
|
|
|
|
2013-01-24 19:18:34 -08:00
|
|
|
FakeMutableHandle(FakeRooted<T> *root) {
|
|
|
|
ptr = root->address();
|
|
|
|
}
|
|
|
|
|
2013-01-23 14:22:10 -08:00
|
|
|
void set(T v) {
|
2013-06-19 03:32:26 -07:00
|
|
|
JS_ASSERT(!js::GCMethods<T>::poisoned(v));
|
2013-01-23 14:22:10 -08:00
|
|
|
*ptr = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
T *address() const { return ptr; }
|
|
|
|
T get() const { return *ptr; }
|
|
|
|
|
|
|
|
operator T() const { return get(); }
|
|
|
|
T operator->() const { return get(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
FakeMutableHandle() {}
|
|
|
|
|
|
|
|
T *ptr;
|
|
|
|
|
|
|
|
template <typename S>
|
|
|
|
void operator=(S v) MOZ_DELETE;
|
|
|
|
};
|
|
|
|
|
2013-01-18 19:35:08 -08:00
|
|
|
/*
|
|
|
|
* Types for a variable that either should or shouldn't be rooted, depending on
|
|
|
|
* the template parameter Rooted. Used for implementing functions that can
|
|
|
|
* 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;
|
|
|
|
}
|
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;
|
|
|
|
|
2013-03-19 03:35:41 -07:00
|
|
|
static inline JS::Handle<T> toHandle(HandleType v) {
|
2013-06-28 19:20:12 -07:00
|
|
|
MOZ_ASSUME_UNREACHABLE("Bad conversion");
|
2013-01-23 14:22:10 -08:00
|
|
|
}
|
|
|
|
|
2013-03-19 03:35:41 -07:00
|
|
|
static inline JS::MutableHandle<T> toMutableHandle(MutableHandleType v) {
|
2013-06-28 19:20:12 -07:00
|
|
|
MOZ_ASSUME_UNREACHABLE("Bad conversion");
|
2013-01-23 14:22:10 -08:00
|
|
|
}
|
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());
|
|
|
|
}
|
|
|
|
|
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-06-13 15:14:44 -07:00
|
|
|
MOZ_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();
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace JS */
|
|
|
|
|
|
|
|
namespace js {
|
|
|
|
|
2012-04-30 17:10:30 -07:00
|
|
|
/*
|
|
|
|
* Hook for dynamic root analysis. Checks the native stack and poisons
|
|
|
|
* references to GC things which have not been rooted.
|
|
|
|
*/
|
2013-03-08 00:54:59 -08:00
|
|
|
inline void MaybeCheckStackRoots(JSContext *cx)
|
2012-06-21 14:15:12 -07:00
|
|
|
{
|
2012-10-04 14:22:51 -07:00
|
|
|
#if defined(DEBUG) && defined(JS_GC_ZEAL) && defined(JSGC_ROOT_ANALYSIS) && !defined(JS_THREADSAFE)
|
2013-03-19 03:35:41 -07:00
|
|
|
JS::CheckStackRoots(cx);
|
2012-04-30 17:10:30 -07:00
|
|
|
#endif
|
2012-06-21 14:15:12 -07:00
|
|
|
}
|
2012-04-30 17:10:30 -07:00
|
|
|
|
2012-05-14 14:24:21 -07:00
|
|
|
/* Base class for automatic read-only object rooting during compilation. */
|
|
|
|
class CompilerRootNode
|
|
|
|
{
|
|
|
|
protected:
|
2013-02-17 22:56:32 -08:00
|
|
|
CompilerRootNode(js::gc::Cell *ptr) : next(NULL), ptr_(ptr) {}
|
2012-05-14 14:24:21 -07:00
|
|
|
|
|
|
|
public:
|
2013-02-17 22:56:32 -08:00
|
|
|
void **address() { return (void **)&ptr_; }
|
2012-05-14 14:24:21 -07:00
|
|
|
|
|
|
|
public:
|
|
|
|
CompilerRootNode *next;
|
|
|
|
|
|
|
|
protected:
|
2013-02-17 22:56:32 -08:00
|
|
|
js::gc::Cell *ptr_;
|
2012-05-14 14:24:21 -07:00
|
|
|
};
|
|
|
|
|
2012-09-24 18:08:22 -07:00
|
|
|
} /* namespace js */
|
2012-04-12 09:23:51 -07:00
|
|
|
|
2013-06-19 17:59:09 -07:00
|
|
|
#endif /* js_RootingAPI_h */
|