2011-04-20 05:30:10 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2010-09-24 17:54:39 +00:00
|
|
|
*
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is SpiderMonkey code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Mozilla Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#ifndef jsgcinlines_h___
|
|
|
|
#define jsgcinlines_h___
|
|
|
|
|
|
|
|
#include "jsgc.h"
|
|
|
|
#include "jscntxt.h"
|
|
|
|
#include "jscompartment.h"
|
2011-03-14 20:59:53 +00:00
|
|
|
#include "jsscope.h"
|
2011-05-11 12:46:33 +00:00
|
|
|
#include "jsxml.h"
|
2010-09-24 17:54:39 +00:00
|
|
|
|
|
|
|
#include "jslock.h"
|
|
|
|
#include "jstl.h"
|
|
|
|
|
2011-03-14 20:59:53 +00:00
|
|
|
inline bool
|
2011-03-23 18:57:15 +00:00
|
|
|
JSAtom::isUnitString(const void *ptr)
|
2011-03-14 20:59:53 +00:00
|
|
|
{
|
|
|
|
jsuword delta = reinterpret_cast<jsuword>(ptr) -
|
|
|
|
reinterpret_cast<jsuword>(unitStaticTable);
|
|
|
|
if (delta >= UNIT_STATIC_LIMIT * sizeof(JSString))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* If ptr points inside the static array, it must be well-aligned. */
|
|
|
|
JS_ASSERT(delta % sizeof(JSString) == 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2011-03-23 18:57:15 +00:00
|
|
|
JSAtom::isLength2String(const void *ptr)
|
2011-03-14 20:59:53 +00:00
|
|
|
{
|
|
|
|
jsuword delta = reinterpret_cast<jsuword>(ptr) -
|
|
|
|
reinterpret_cast<jsuword>(length2StaticTable);
|
|
|
|
if (delta >= NUM_SMALL_CHARS * NUM_SMALL_CHARS * sizeof(JSString))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* If ptr points inside the static array, it must be well-aligned. */
|
|
|
|
JS_ASSERT(delta % sizeof(JSString) == 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2011-03-23 18:57:15 +00:00
|
|
|
JSAtom::isHundredString(const void *ptr)
|
2011-03-14 20:59:53 +00:00
|
|
|
{
|
|
|
|
jsuword delta = reinterpret_cast<jsuword>(ptr) -
|
|
|
|
reinterpret_cast<jsuword>(hundredStaticTable);
|
|
|
|
if (delta >= NUM_HUNDRED_STATICS * sizeof(JSString))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* If ptr points inside the static array, it must be well-aligned. */
|
|
|
|
JS_ASSERT(delta % sizeof(JSString) == 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2011-03-23 18:57:15 +00:00
|
|
|
JSAtom::isStatic(const void *ptr)
|
2011-03-14 20:59:53 +00:00
|
|
|
{
|
|
|
|
return isUnitString(ptr) || isLength2String(ptr) || isHundredString(ptr);
|
|
|
|
}
|
|
|
|
|
2010-10-13 18:49:22 +00:00
|
|
|
namespace js {
|
2011-03-23 18:57:44 +00:00
|
|
|
|
|
|
|
struct Shape;
|
|
|
|
|
2010-10-13 18:49:22 +00:00
|
|
|
namespace gc {
|
|
|
|
|
2011-03-14 20:59:53 +00:00
|
|
|
inline uint32
|
2011-03-23 18:57:15 +00:00
|
|
|
GetGCThingTraceKind(const void *thing)
|
2011-03-14 20:59:53 +00:00
|
|
|
{
|
|
|
|
JS_ASSERT(thing);
|
|
|
|
if (JSAtom::isStatic(thing))
|
|
|
|
return JSTRACE_STRING;
|
2011-03-23 18:57:15 +00:00
|
|
|
const Cell *cell = reinterpret_cast<const Cell *>(thing);
|
2011-04-20 05:30:10 +00:00
|
|
|
return GetFinalizableTraceKind(cell->arenaHeader()->getThingKind());
|
2011-03-14 20:59:53 +00:00
|
|
|
}
|
|
|
|
|
2010-10-13 18:49:22 +00:00
|
|
|
/* Capacity for slotsToThingKind */
|
|
|
|
const size_t SLOTS_TO_THING_KIND_LIMIT = 17;
|
|
|
|
|
|
|
|
/* Get the best kind to use when making an object with the given slot count. */
|
|
|
|
static inline FinalizeKind
|
|
|
|
GetGCObjectKind(size_t numSlots)
|
|
|
|
{
|
|
|
|
extern FinalizeKind slotsToThingKind[];
|
|
|
|
|
|
|
|
if (numSlots >= SLOTS_TO_THING_KIND_LIMIT)
|
|
|
|
return FINALIZE_OBJECT0;
|
|
|
|
return slotsToThingKind[numSlots];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the number of fixed slots and initial capacity associated with a kind. */
|
|
|
|
static inline size_t
|
|
|
|
GetGCKindSlots(FinalizeKind thingKind)
|
|
|
|
{
|
|
|
|
/* Using a switch in hopes that thingKind will usually be a compile-time constant. */
|
|
|
|
switch (thingKind) {
|
|
|
|
case FINALIZE_OBJECT0:
|
2011-04-13 20:43:33 +00:00
|
|
|
case FINALIZE_OBJECT0_BACKGROUND:
|
2010-10-13 18:49:22 +00:00
|
|
|
return 0;
|
|
|
|
case FINALIZE_OBJECT2:
|
2011-04-13 20:43:33 +00:00
|
|
|
case FINALIZE_OBJECT2_BACKGROUND:
|
2010-10-13 18:49:22 +00:00
|
|
|
return 2;
|
|
|
|
case FINALIZE_OBJECT4:
|
2011-04-13 20:43:33 +00:00
|
|
|
case FINALIZE_OBJECT4_BACKGROUND:
|
2010-10-13 18:49:22 +00:00
|
|
|
return 4;
|
|
|
|
case FINALIZE_OBJECT8:
|
2011-04-13 20:43:33 +00:00
|
|
|
case FINALIZE_OBJECT8_BACKGROUND:
|
2010-10-13 18:49:22 +00:00
|
|
|
return 8;
|
|
|
|
case FINALIZE_OBJECT12:
|
2011-04-13 20:43:33 +00:00
|
|
|
case FINALIZE_OBJECT12_BACKGROUND:
|
2010-10-13 18:49:22 +00:00
|
|
|
return 12;
|
|
|
|
case FINALIZE_OBJECT16:
|
2011-04-13 20:43:33 +00:00
|
|
|
case FINALIZE_OBJECT16_BACKGROUND:
|
2010-10-13 18:49:22 +00:00
|
|
|
return 16;
|
|
|
|
default:
|
|
|
|
JS_NOT_REACHED("Bad object finalize kind");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-02 00:48:52 +00:00
|
|
|
static inline void
|
|
|
|
GCPoke(JSContext *cx, Value oldval)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Since we're forcing a GC from JS_GC anyway, don't bother wasting cycles
|
|
|
|
* loading oldval. XXX remove implied force, fix jsinterp.c's "second arg
|
|
|
|
* ignored", etc.
|
|
|
|
*/
|
|
|
|
#if 1
|
|
|
|
cx->runtime->gcPoke = JS_TRUE;
|
|
|
|
#else
|
|
|
|
cx->runtime->gcPoke = oldval.isGCThing();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef JS_GC_ZEAL
|
|
|
|
/* Schedule a GC to happen "soon" after a GC poke. */
|
|
|
|
if (cx->runtime->gcZeal())
|
|
|
|
cx->runtime->gcNextScheduled = 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-10-13 18:49:22 +00:00
|
|
|
} /* namespace gc */
|
|
|
|
} /* namespace js */
|
|
|
|
|
2010-09-24 17:54:39 +00:00
|
|
|
/*
|
|
|
|
* Allocates a new GC thing. After a successful allocation the caller must
|
|
|
|
* fully initialize the thing before calling any function that can potentially
|
|
|
|
* trigger GC. This will ensure that GC tracing never sees junk values stored
|
|
|
|
* in the partially initialized thing.
|
|
|
|
*/
|
|
|
|
|
|
|
|
template <typename T>
|
2011-04-15 23:56:08 +00:00
|
|
|
inline T *
|
2011-05-11 12:46:33 +00:00
|
|
|
NewGCThing(JSContext *cx, unsigned thingKind, size_t thingSize)
|
2010-09-24 17:54:39 +00:00
|
|
|
{
|
|
|
|
JS_ASSERT(thingKind < js::gc::FINALIZE_LIMIT);
|
2011-05-11 12:46:33 +00:00
|
|
|
JS_ASSERT(thingSize == js::gc::GCThingSizeMap[thingKind]);
|
2010-10-01 18:10:30 +00:00
|
|
|
#ifdef JS_THREADSAFE
|
2011-01-13 22:42:36 +00:00
|
|
|
JS_ASSERT_IF((cx->compartment == cx->runtime->atomsCompartment),
|
2010-10-01 18:10:30 +00:00
|
|
|
(thingKind == js::gc::FINALIZE_STRING) ||
|
2011-03-30 21:02:21 +00:00
|
|
|
(thingKind == js::gc::FINALIZE_SHORT_STRING));
|
2010-10-01 18:10:30 +00:00
|
|
|
#endif
|
2011-04-21 22:20:12 +00:00
|
|
|
JS_ASSERT(!cx->runtime->gcRunning);
|
2010-09-24 17:54:39 +00:00
|
|
|
|
2011-06-02 00:48:52 +00:00
|
|
|
#ifdef JS_GC_ZEAL
|
|
|
|
if (cx->runtime->needZealousGC())
|
|
|
|
js::gc::RunDebugGC(cx);
|
|
|
|
#endif
|
|
|
|
|
2011-05-11 12:46:33 +00:00
|
|
|
js::gc::Cell *cell = cx->compartment->freeLists.getNext(thingKind, thingSize);
|
2011-04-21 22:20:12 +00:00
|
|
|
return static_cast<T *>(cell ? cell : js::gc::RefillFinalizableFreeList(cx, thingKind));
|
2010-09-24 17:54:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline JSObject *
|
2010-10-13 18:49:22 +00:00
|
|
|
js_NewGCObject(JSContext *cx, js::gc::FinalizeKind kind)
|
2010-09-24 17:54:39 +00:00
|
|
|
{
|
2010-10-13 18:49:22 +00:00
|
|
|
JS_ASSERT(kind >= js::gc::FINALIZE_OBJECT0 && kind <= js::gc::FINALIZE_OBJECT_LAST);
|
2011-05-11 12:46:33 +00:00
|
|
|
JSObject *obj = NewGCThing<JSObject>(cx, kind, js::gc::GCThingSizeMap[kind]);
|
2011-03-23 18:57:19 +00:00
|
|
|
if (obj) {
|
2010-10-13 18:49:22 +00:00
|
|
|
obj->capacity = js::gc::GetGCKindSlots(kind);
|
2011-04-08 21:21:08 +00:00
|
|
|
obj->lastProp = NULL; /* Stops obj from being scanned until initializated. */
|
2011-03-23 18:57:19 +00:00
|
|
|
}
|
2010-10-13 18:49:22 +00:00
|
|
|
return obj;
|
2010-09-24 17:54:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline JSString *
|
|
|
|
js_NewGCString(JSContext *cx)
|
|
|
|
{
|
2011-05-11 12:46:33 +00:00
|
|
|
return NewGCThing<JSString>(cx, js::gc::FINALIZE_STRING, sizeof(JSString));
|
2010-09-24 17:54:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline JSShortString *
|
|
|
|
js_NewGCShortString(JSContext *cx)
|
|
|
|
{
|
2011-05-11 12:46:33 +00:00
|
|
|
return NewGCThing<JSShortString>(cx, js::gc::FINALIZE_SHORT_STRING, sizeof(JSShortString));
|
2010-09-24 17:54:39 +00:00
|
|
|
}
|
|
|
|
|
2010-11-15 20:39:00 +00:00
|
|
|
inline JSExternalString *
|
2011-05-11 12:46:33 +00:00
|
|
|
js_NewGCExternalString(JSContext *cx)
|
2010-09-24 17:54:39 +00:00
|
|
|
{
|
2011-05-11 12:46:33 +00:00
|
|
|
return NewGCThing<JSExternalString>(cx, js::gc::FINALIZE_EXTERNAL_STRING,
|
|
|
|
sizeof(JSExternalString));
|
2010-09-24 17:54:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline JSFunction*
|
|
|
|
js_NewGCFunction(JSContext *cx)
|
|
|
|
{
|
2011-05-11 12:46:33 +00:00
|
|
|
JSFunction *fun = NewGCThing<JSFunction>(cx, js::gc::FINALIZE_FUNCTION, sizeof(JSFunction));
|
2011-03-23 18:57:19 +00:00
|
|
|
if (fun) {
|
2010-10-13 18:49:22 +00:00
|
|
|
fun->capacity = JSObject::FUN_CLASS_RESERVED_SLOTS;
|
2011-04-08 21:21:08 +00:00
|
|
|
fun->lastProp = NULL; /* Stops fun from being scanned until initializated. */
|
2011-03-23 18:57:19 +00:00
|
|
|
}
|
2010-10-13 18:49:22 +00:00
|
|
|
return fun;
|
2010-09-24 17:54:39 +00:00
|
|
|
}
|
|
|
|
|
2011-03-23 18:57:44 +00:00
|
|
|
inline js::Shape *
|
|
|
|
js_NewGCShape(JSContext *cx)
|
|
|
|
{
|
2011-05-11 12:46:33 +00:00
|
|
|
return NewGCThing<js::Shape>(cx, js::gc::FINALIZE_SHAPE, sizeof(js::Shape));
|
2011-03-23 18:57:44 +00:00
|
|
|
}
|
|
|
|
|
2010-09-24 17:54:39 +00:00
|
|
|
#if JS_HAS_XML_SUPPORT
|
2011-08-04 22:54:14 +00:00
|
|
|
inline JSXML *
|
|
|
|
js_NewGCXML(JSContext *cx)
|
|
|
|
{
|
|
|
|
return NewGCThing<JSXML>(cx, js::gc::FINALIZE_XML, sizeof(JSXML));
|
|
|
|
}
|
2010-09-24 17:54:39 +00:00
|
|
|
#endif
|
|
|
|
|
2011-08-04 22:54:14 +00:00
|
|
|
|
2010-09-24 17:54:39 +00:00
|
|
|
#endif /* jsgcinlines_h___ */
|