2012-01-20 19:49:33 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=8 sw=4 et tw=78:
|
|
|
|
*
|
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
|
|
|
|
|
|
|
#ifndef jsgc_root_h__
|
|
|
|
#define jsgc_root_h__
|
|
|
|
|
2012-06-19 15:01:58 -07:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
|
|
|
#include "mozilla/TypeTraits.h"
|
|
|
|
|
2012-07-10 15:48:07 -07:00
|
|
|
#include "jsapi.h"
|
2012-01-20 19:49:33 -08:00
|
|
|
|
2012-07-08 03:00:11 -07:00
|
|
|
#include "js/TemplateLib.h"
|
2012-04-12 18:15:26 -07:00
|
|
|
#include "js/Utility.h"
|
|
|
|
|
2012-04-12 09:23:51 -07:00
|
|
|
namespace JS {
|
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.
|
|
|
|
*
|
|
|
|
* The classes below are used to root stack locations whose value may be held
|
|
|
|
* live across a call that can trigger GC (i.e. a call which might allocate any
|
|
|
|
* GC things). For a code fragment such as:
|
|
|
|
*
|
|
|
|
* Foo();
|
|
|
|
* ... = obj->lastProperty();
|
|
|
|
*
|
|
|
|
* If Foo() 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.
|
|
|
|
*
|
|
|
|
* If Foo() cannot trigger a GC, and the same holds for all other calls made
|
|
|
|
* between obj's definitions and its last uses, then no rooting is required.
|
|
|
|
*
|
|
|
|
* Several classes are available for rooting stack locations. All are templated
|
|
|
|
* on the type T of the value being rooted, for which RootMethods<T> must
|
|
|
|
* have an instantiation.
|
|
|
|
*
|
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
|
|
|
|
* call which can allocate GC things or otherwise 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-01-20 19:49:33 -08:00
|
|
|
*/
|
|
|
|
|
2012-05-24 16:05:18 -07:00
|
|
|
template <typename T> class Rooted;
|
2012-04-12 09:23:51 -07:00
|
|
|
|
2012-04-12 09:23:51 -07:00
|
|
|
template <typename T>
|
|
|
|
struct RootMethods { };
|
2012-04-12 09:23:51 -07:00
|
|
|
|
2012-07-08 03:00:11 -07:00
|
|
|
/*
|
|
|
|
* Handle provides an implicit constructor for NullPtr so that, given:
|
|
|
|
* foo(Handle<JSObject*> h);
|
|
|
|
* callers can simply write:
|
|
|
|
* foo(NullPtr());
|
|
|
|
* which avoids creating a Rooted<JSObject*> just to pass NULL.
|
|
|
|
*/
|
|
|
|
struct NullPtr
|
|
|
|
{
|
|
|
|
static void * const constNullValue;
|
|
|
|
};
|
|
|
|
|
2012-07-18 14:31:24 -07:00
|
|
|
template <typename T>
|
|
|
|
class MutableHandle;
|
|
|
|
|
2012-07-12 03:01:59 -07:00
|
|
|
template <typename T>
|
|
|
|
class HandleBase {};
|
|
|
|
|
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>
|
2012-07-12 03:01:59 -07:00
|
|
|
class Handle : public HandleBase<T>
|
2012-04-12 22:07:43 -07: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)
|
|
|
|
{
|
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. */
|
2012-07-10 15:48:07 -07:00
|
|
|
Handle(NullPtr) {
|
2012-07-08 03:00:11 -07:00
|
|
|
typedef typename js::tl::StaticAssert<js::tl::IsPointerType<T>::result>::result _;
|
|
|
|
ptr = reinterpret_cast<const T *>(&NullPtr::constNullValue);
|
|
|
|
}
|
|
|
|
|
2012-07-18 14:31:24 -07:00
|
|
|
friend class MutableHandle<T>;
|
|
|
|
Handle(MutableHandle<T> handle) {
|
|
|
|
ptr = handle.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
|
|
|
|
Handle(Rooted<S> &root,
|
|
|
|
typename mozilla::EnableIf<mozilla::IsConvertible<S, T>::value, int>::Type dummy = 0);
|
2012-04-12 09:23:51 -07:00
|
|
|
|
2012-05-19 15:03:45 -07:00
|
|
|
const T *address() const { return ptr; }
|
2012-07-04 13:34:42 -07:00
|
|
|
T get() const { return *ptr; }
|
2012-04-12 09:23:51 -07:00
|
|
|
|
2012-07-04 13:34:42 -07:00
|
|
|
operator T () const { return get(); }
|
|
|
|
T operator ->() const { return get(); }
|
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>
|
|
|
|
void operator =(S v) MOZ_DELETE;
|
2012-04-12 22:07:43 -07:00
|
|
|
};
|
2012-04-12 09:23:51 -07:00
|
|
|
|
2012-04-12 09:23:51 -07:00
|
|
|
typedef Handle<JSObject*> HandleObject;
|
|
|
|
typedef Handle<JSFunction*> HandleFunction;
|
2012-06-05 16:52:41 -07:00
|
|
|
typedef Handle<JSScript*> HandleScript;
|
2012-04-12 09:23:51 -07:00
|
|
|
typedef Handle<JSString*> HandleString;
|
|
|
|
typedef Handle<jsid> HandleId;
|
|
|
|
typedef Handle<Value> HandleValue;
|
|
|
|
|
2012-07-12 03:01:59 -07:00
|
|
|
template <typename T>
|
|
|
|
class MutableHandleBase {};
|
|
|
|
|
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>
|
2012-07-12 03:01:59 -07:00
|
|
|
class MutableHandle : public MutableHandleBase<T>
|
2012-07-04 11:12:16 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
template <typename S>
|
|
|
|
MutableHandle(MutableHandle<S> handle,
|
|
|
|
typename mozilla::EnableIf<mozilla::IsConvertible<S, T>::value, int>::Type dummy = 0)
|
|
|
|
{
|
|
|
|
this->ptr = reinterpret_cast<const T *>(handle.address());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename S>
|
|
|
|
inline
|
|
|
|
MutableHandle(Rooted<S> *root,
|
|
|
|
typename mozilla::EnableIf<mozilla::IsConvertible<S, T>::value, int>::Type dummy = 0);
|
|
|
|
|
2012-07-10 18:17:29 -07:00
|
|
|
void set(T v)
|
|
|
|
{
|
|
|
|
JS_ASSERT(!RootMethods<T>::poisoned(v));
|
|
|
|
*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-07-04 13:34:42 -07:00
|
|
|
operator T () const { return get(); }
|
|
|
|
T operator ->() const { return get(); }
|
2012-07-04 11:12:16 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
MutableHandle() {}
|
|
|
|
|
|
|
|
T *ptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef MutableHandle<JSObject*> MutableHandleObject;
|
|
|
|
typedef MutableHandle<Value> MutableHandleValue;
|
|
|
|
|
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>
|
|
|
|
struct RootKind<T *> { static ThingRootKind rootKind() { return T::rootKind(); }; };
|
|
|
|
|
2012-01-20 19:49:33 -08:00
|
|
|
template <typename T>
|
|
|
|
struct RootMethods<T *>
|
|
|
|
{
|
|
|
|
static T *initial() { return NULL; }
|
2012-07-18 14:32:39 -07:00
|
|
|
static ThingRootKind kind() { return RootKind<T *>::rootKind(); }
|
2012-01-20 19:49:33 -08:00
|
|
|
static bool poisoned(T *v) { return IsPoisonedPtr(v); }
|
|
|
|
};
|
|
|
|
|
2012-07-12 03:01:59 -07:00
|
|
|
template <typename T>
|
|
|
|
class RootedBase {};
|
|
|
|
|
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>
|
2012-07-12 03:01:59 -07:00
|
|
|
class Rooted : public RootedBase<T>
|
2012-01-20 19:49:33 -08:00
|
|
|
{
|
2012-06-28 17:35:56 -07:00
|
|
|
void init(JSContext *cx_)
|
2012-01-20 19:49:33 -08:00
|
|
|
{
|
2012-06-13 11:15:23 -07:00
|
|
|
#if defined(JSGC_ROOT_ANALYSIS) || defined(JSGC_USE_EXACT_ROOTING)
|
2012-04-12 09:23:51 -07:00
|
|
|
ContextFriendFields *cx = ContextFriendFields::get(cx_);
|
|
|
|
|
2012-01-20 19:49:33 -08:00
|
|
|
ThingRootKind kind = RootMethods<T>::kind();
|
2012-05-24 16:05:18 -07:00
|
|
|
this->stack = reinterpret_cast<Rooted<T>**>(&cx->thingGCRooters[kind]);
|
2012-01-20 19:49:33 -08:00
|
|
|
this->prev = *stack;
|
|
|
|
*stack = this;
|
|
|
|
|
2012-06-28 17:35:56 -07:00
|
|
|
JS_ASSERT(!RootMethods<T>::poisoned(ptr));
|
2012-04-30 17:10:30 -07:00
|
|
|
#endif
|
2012-01-20 19:49:33 -08:00
|
|
|
}
|
|
|
|
|
2012-05-24 08:52:21 -07:00
|
|
|
public:
|
2012-06-28 17:35:56 -07:00
|
|
|
Rooted(JSContext *cx) : ptr(RootMethods<T>::initial()) { init(cx); }
|
|
|
|
Rooted(JSContext *cx, T initial) : ptr(initial) { init(cx); }
|
2012-05-24 08:52:21 -07:00
|
|
|
|
2012-05-24 16:05:18 -07:00
|
|
|
~Rooted()
|
2012-01-20 19:49:33 -08:00
|
|
|
{
|
2012-06-13 11:15:23 -07:00
|
|
|
#if defined(JSGC_ROOT_ANALYSIS) || defined(JSGC_USE_EXACT_ROOTING)
|
2012-01-20 19:49:33 -08:00
|
|
|
JS_ASSERT(*stack == this);
|
|
|
|
*stack = prev;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-06-13 11:15:23 -07:00
|
|
|
#if defined(JSGC_ROOT_ANALYSIS) || defined(JSGC_USE_EXACT_ROOTING)
|
2012-05-24 16:05:18 -07:00
|
|
|
Rooted<T> *previous() { return prev; }
|
2012-01-20 19:49:33 -08:00
|
|
|
#endif
|
|
|
|
|
2012-05-24 08:52:21 -07:00
|
|
|
operator T () const { return ptr; }
|
|
|
|
T operator ->() const { return ptr; }
|
|
|
|
T * address() { return &ptr; }
|
|
|
|
const T * address() const { return &ptr; }
|
2012-07-04 13:34:42 -07:00
|
|
|
T & get() { return ptr; }
|
|
|
|
const T & get() const { return ptr; }
|
2012-05-24 08:52:21 -07:00
|
|
|
|
|
|
|
T & operator =(T value)
|
|
|
|
{
|
|
|
|
JS_ASSERT(!RootMethods<T>::poisoned(value));
|
|
|
|
ptr = value;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2012-05-24 16:05:18 -07: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
|
|
|
|
|
|
|
private:
|
|
|
|
|
2012-06-13 11:15:23 -07:00
|
|
|
#if defined(JSGC_ROOT_ANALYSIS) || defined(JSGC_USE_EXACT_ROOTING)
|
2012-05-24 16:05:18 -07:00
|
|
|
Rooted<T> **stack, *prev;
|
2012-01-20 19:49:33 -08:00
|
|
|
#endif
|
2012-05-24 08:52:21 -07:00
|
|
|
T ptr;
|
2012-01-20 19:49:33 -08:00
|
|
|
|
2012-05-24 16:05:18 -07:00
|
|
|
Rooted() MOZ_DELETE;
|
|
|
|
Rooted(const Rooted &) MOZ_DELETE;
|
2012-01-20 19:49:33 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T> template <typename S>
|
|
|
|
inline
|
2012-06-19 15:01:58 -07:00
|
|
|
Handle<T>::Handle(Rooted<S> &root,
|
|
|
|
typename mozilla::EnableIf<mozilla::IsConvertible<S, T>::value, int>::Type dummy)
|
2012-01-20 19:49:33 -08:00
|
|
|
{
|
|
|
|
ptr = reinterpret_cast<const T *>(root.address());
|
|
|
|
}
|
|
|
|
|
2012-07-04 11:12:16 -07:00
|
|
|
template<typename T> template <typename S>
|
|
|
|
inline
|
|
|
|
MutableHandle<T>::MutableHandle(Rooted<S> *root,
|
|
|
|
typename mozilla::EnableIf<mozilla::IsConvertible<S, T>::value, int>::Type dummy)
|
|
|
|
{
|
|
|
|
ptr = root->address();
|
|
|
|
}
|
|
|
|
|
2012-05-24 16:05:18 -07:00
|
|
|
typedef Rooted<JSObject*> RootedObject;
|
|
|
|
typedef Rooted<JSFunction*> RootedFunction;
|
2012-06-05 16:52:41 -07:00
|
|
|
typedef Rooted<JSScript*> RootedScript;
|
2012-05-24 16:05:18 -07:00
|
|
|
typedef Rooted<JSString*> RootedString;
|
|
|
|
typedef Rooted<jsid> RootedId;
|
|
|
|
typedef Rooted<Value> RootedValue;
|
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
|
|
|
{
|
|
|
|
#if defined(DEBUG) && defined(JSGC_ROOT_ANALYSIS)
|
|
|
|
|
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>
|
2012-04-30 17:10:30 -07:00
|
|
|
void init(ContextFriendFields *cx, const T *ptr, size_t count)
|
2012-01-20 19:49:33 -08:00
|
|
|
{
|
2012-04-12 09:23:51 -07:00
|
|
|
this->stack = &cx->skipGCRooters;
|
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-05-24 08:52:21 -07:00
|
|
|
JS_GUARD_OBJECT_NOTIFIER_PARAM)
|
2012-04-30 17:10:30 -07:00
|
|
|
{
|
|
|
|
init(ContextFriendFields::get(cx), ptr, count);
|
2012-01-20 19:49:33 -08:00
|
|
|
JS_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
}
|
|
|
|
|
2012-04-12 09:23:51 -07: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-04-30 17:10:30 -07:00
|
|
|
JS_GUARD_OBJECT_NOTIFIER_PARAM)
|
|
|
|
{
|
|
|
|
JS_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
}
|
|
|
|
|
2012-01-20 19:49:33 -08:00
|
|
|
#endif /* DEBUG && JSGC_ROOT_ANALYSIS */
|
|
|
|
|
|
|
|
JS_DECL_USE_GUARD_OBJECT_NOTIFIER
|
|
|
|
};
|
|
|
|
|
2012-06-21 14:15:12 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
JS_FRIEND_API(bool) IsRootingUnnecessaryForContext(JSContext *cx);
|
|
|
|
JS_FRIEND_API(void) SetRootingUnnecessaryForContext(JSContext *cx, bool value);
|
2012-07-13 09:13:50 -07:00
|
|
|
JS_FRIEND_API(bool) RelaxRootChecksForContext(JSContext *cx);
|
2012-06-21 14:15:12 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
class AssertRootingUnnecessary {
|
|
|
|
JS_DECL_USE_GUARD_OBJECT_NOTIFIER
|
2012-07-16 13:39:31 -07:00
|
|
|
#ifdef DEBUG
|
2012-06-21 14:15:12 -07:00
|
|
|
JSContext *cx;
|
|
|
|
bool prev;
|
2012-07-16 13:39:31 -07:00
|
|
|
#endif
|
2012-06-21 14:15:12 -07:00
|
|
|
public:
|
|
|
|
AssertRootingUnnecessary(JSContext *cx JS_GUARD_OBJECT_NOTIFIER_PARAM)
|
|
|
|
{
|
|
|
|
JS_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
#ifdef DEBUG
|
2012-07-16 13:39:31 -07:00
|
|
|
this->cx = cx;
|
2012-06-21 14:15:12 -07:00
|
|
|
prev = IsRootingUnnecessaryForContext(cx);
|
|
|
|
SetRootingUnnecessaryForContext(cx, true);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
~AssertRootingUnnecessary() {
|
|
|
|
#ifdef DEBUG
|
|
|
|
SetRootingUnnecessaryForContext(cx, prev);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-07-09 08:58:22 -07:00
|
|
|
#if defined(DEBUG) && defined(JS_GC_ZEAL) && defined(JSGC_ROOT_ANALYSIS) && !defined(JS_THREADSAFE)
|
|
|
|
extern void
|
|
|
|
CheckStackRoots(JSContext *cx);
|
|
|
|
#endif
|
|
|
|
|
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.
|
|
|
|
*/
|
2012-07-13 09:13:50 -07:00
|
|
|
inline void MaybeCheckStackRoots(JSContext *cx, bool relax = true)
|
2012-06-21 14:15:12 -07:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
JS_ASSERT(!IsRootingUnnecessaryForContext(cx));
|
|
|
|
# if defined(JS_GC_ZEAL) && defined(JSGC_ROOT_ANALYSIS) && !defined(JS_THREADSAFE)
|
2012-07-13 09:13:50 -07:00
|
|
|
if (relax && RelaxRootChecksForContext(cx))
|
|
|
|
return;
|
2012-06-21 14:15:12 -07:00
|
|
|
CheckStackRoots(cx);
|
|
|
|
# endif
|
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-04-12 09:23:51 -07:00
|
|
|
} /* namespace JS */
|
|
|
|
|
|
|
|
#endif /* __cplusplus */
|
|
|
|
|
2012-01-20 19:49:33 -08:00
|
|
|
#endif /* jsgc_root_h___ */
|