2011-04-20 05:30:10 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2013-04-16 20:47:10 +00:00
|
|
|
* vim: set ts=8 sts=4 et sw=4 tw=99:
|
2012-05-21 11:12:37 +00: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/. */
|
2010-09-24 17:54:39 +00:00
|
|
|
|
2013-06-20 00:59:46 +00:00
|
|
|
#ifndef jsgcinlines_h
|
|
|
|
#define jsgcinlines_h
|
2010-09-24 17:54:39 +00:00
|
|
|
|
|
|
|
#include "jsgc.h"
|
2013-07-24 00:34:18 +00:00
|
|
|
|
2013-08-08 23:07:22 +00:00
|
|
|
#include "gc/Zone.h"
|
2010-09-24 17:54:39 +00:00
|
|
|
|
2010-10-13 18:49:22 +00:00
|
|
|
namespace js {
|
2011-03-23 18:57:44 +00:00
|
|
|
|
2012-12-22 00:16:50 +00:00
|
|
|
class Shape;
|
2011-03-23 18:57:44 +00:00
|
|
|
|
2012-11-09 19:44:21 +00:00
|
|
|
/*
|
|
|
|
* This auto class should be used around any code that might cause a mark bit to
|
2013-02-07 02:08:16 +00:00
|
|
|
* be set on an object in a dead zone. See AutoMaybeTouchDeadZones
|
2012-11-12 22:57:53 +00:00
|
|
|
* for more details.
|
2012-11-09 19:44:21 +00:00
|
|
|
*/
|
2013-02-07 02:08:16 +00:00
|
|
|
struct AutoMarkInDeadZone
|
2012-11-09 19:44:21 +00:00
|
|
|
{
|
2013-02-07 02:08:16 +00:00
|
|
|
AutoMarkInDeadZone(JS::Zone *zone)
|
|
|
|
: zone(zone),
|
|
|
|
scheduled(zone->scheduledForDestruction)
|
2012-11-09 19:44:21 +00:00
|
|
|
{
|
2013-08-05 14:48:34 +00:00
|
|
|
JSRuntime *rt = zone->runtimeFromMainThread();
|
2014-04-30 13:38:59 +00:00
|
|
|
if (rt->gcManipulatingDeadZones && zone->scheduledForDestruction) {
|
|
|
|
rt->gcObjectsMarkedInDeadZones++;
|
2013-02-07 02:08:16 +00:00
|
|
|
zone->scheduledForDestruction = false;
|
2012-11-09 19:44:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-07 02:08:16 +00:00
|
|
|
~AutoMarkInDeadZone() {
|
|
|
|
zone->scheduledForDestruction = scheduled;
|
2012-11-09 19:44:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-02-07 02:08:16 +00:00
|
|
|
JS::Zone *zone;
|
2012-11-09 19:44:21 +00:00
|
|
|
bool scheduled;
|
|
|
|
};
|
|
|
|
|
2013-06-21 00:39:24 +00:00
|
|
|
inline Allocator *const
|
|
|
|
ThreadSafeContext::allocator()
|
|
|
|
{
|
|
|
|
JS_ASSERT_IF(isJSContext(), &asJSContext()->zone()->allocator == allocator_);
|
|
|
|
return allocator_;
|
|
|
|
}
|
|
|
|
|
2013-10-08 22:14:03 +00:00
|
|
|
template <typename T>
|
|
|
|
inline bool
|
|
|
|
ThreadSafeContext::isThreadLocal(T thing) const
|
|
|
|
{
|
2014-01-31 22:57:57 +00:00
|
|
|
if (!isForkJoinContext())
|
2013-10-08 22:14:03 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!IsInsideNursery(runtime_, thing) &&
|
|
|
|
allocator_->arenas.containsArena(runtime_, thing->arenaHeader()))
|
|
|
|
{
|
|
|
|
// GC should be suppressed in preparation for mutating thread local
|
|
|
|
// objects, as we don't want to trip any barriers.
|
|
|
|
JS_ASSERT(!thing->zoneFromAnyThread()->needsBarrier());
|
|
|
|
JS_ASSERT(!thing->runtimeFromAnyThread()->needsBarrier());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-13 18:49:22 +00:00
|
|
|
namespace gc {
|
|
|
|
|
2011-10-11 00:14:38 +00:00
|
|
|
static inline AllocKind
|
2013-09-11 12:49:05 +00:00
|
|
|
GetGCObjectKind(const Class *clasp)
|
2011-10-11 00:14:38 +00:00
|
|
|
{
|
2013-06-18 06:53:49 +00:00
|
|
|
if (clasp == FunctionClassPtr)
|
2011-10-26 20:02:57 +00:00
|
|
|
return JSFunction::FinalizeKind;
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-09 03:54:10 +00:00
|
|
|
uint32_t nslots = JSCLASS_RESERVED_SLOTS(clasp);
|
2011-10-11 00:14:38 +00:00
|
|
|
if (clasp->flags & JSCLASS_HAS_PRIVATE)
|
|
|
|
nslots++;
|
|
|
|
return GetGCObjectKind(nslots);
|
|
|
|
}
|
|
|
|
|
2013-02-05 22:07:28 +00:00
|
|
|
#ifdef JSGC_GENERATIONAL
|
|
|
|
inline bool
|
2013-06-17 20:16:29 +00:00
|
|
|
ShouldNurseryAllocate(const Nursery &nursery, AllocKind kind, InitialHeap heap)
|
2013-02-05 22:07:28 +00:00
|
|
|
{
|
|
|
|
return nursery.isEnabled() && IsNurseryAllocable(kind) && heap != TenuredHeap;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-03-11 17:47:33 +00:00
|
|
|
inline JSGCTraceKind
|
|
|
|
GetGCThingTraceKind(const void *thing)
|
|
|
|
{
|
|
|
|
JS_ASSERT(thing);
|
|
|
|
const Cell *cell = static_cast<const Cell *>(thing);
|
|
|
|
#ifdef JSGC_GENERATIONAL
|
2014-02-19 16:02:13 +00:00
|
|
|
if (IsInsideNursery(cell->runtimeFromAnyThread(), cell))
|
2013-03-11 17:47:33 +00:00
|
|
|
return JSTRACE_OBJECT;
|
|
|
|
#endif
|
2013-02-14 22:51:07 +00:00
|
|
|
return MapAllocToTraceKind(cell->tenuredGetAllocKind());
|
2013-03-11 17:47:33 +00:00
|
|
|
}
|
|
|
|
|
2011-06-02 00:48:52 +00:00
|
|
|
static inline void
|
2013-01-11 00:53:11 +00:00
|
|
|
GCPoke(JSRuntime *rt)
|
2011-06-02 00:48:52 +00:00
|
|
|
{
|
2014-04-30 13:38:59 +00:00
|
|
|
rt->gcPoke = true;
|
2011-06-02 00:48:52 +00:00
|
|
|
|
|
|
|
#ifdef JS_GC_ZEAL
|
|
|
|
/* Schedule a GC to happen "soon" after a GC poke. */
|
2012-02-17 22:35:20 +00:00
|
|
|
if (rt->gcZeal() == js::gc::ZealPokeValue)
|
2014-04-30 13:38:59 +00:00
|
|
|
rt->gcNextScheduled = 1;
|
2011-06-02 00:48:52 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-08-09 18:16:33 +00:00
|
|
|
class ArenaIter
|
2011-08-26 19:39:31 +00:00
|
|
|
{
|
2012-08-09 18:16:33 +00:00
|
|
|
ArenaHeader *aheader;
|
|
|
|
ArenaHeader *remainingHeader;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ArenaIter() {
|
2013-10-07 16:43:32 +00:00
|
|
|
aheader = nullptr;
|
|
|
|
remainingHeader = nullptr;
|
2012-08-09 18:16:33 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 00:59:03 +00:00
|
|
|
ArenaIter(JS::Zone *zone, AllocKind kind) {
|
|
|
|
init(zone, kind);
|
2012-08-09 18:16:33 +00:00
|
|
|
}
|
|
|
|
|
2013-01-27 21:51:41 +00:00
|
|
|
void init(JS::Zone *zone, AllocKind kind) {
|
|
|
|
aheader = zone->allocator.arenas.getFirstArena(kind);
|
|
|
|
remainingHeader = zone->allocator.arenas.getFirstArenaToSweep(kind);
|
2012-08-09 18:16:33 +00:00
|
|
|
if (!aheader) {
|
|
|
|
aheader = remainingHeader;
|
2013-10-07 16:43:32 +00:00
|
|
|
remainingHeader = nullptr;
|
2011-08-26 19:39:31 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-09 18:16:33 +00:00
|
|
|
|
2014-04-30 01:00:27 +00:00
|
|
|
bool done() const {
|
2012-08-09 18:16:33 +00:00
|
|
|
return !aheader;
|
|
|
|
}
|
|
|
|
|
2014-04-30 01:00:27 +00:00
|
|
|
ArenaHeader *get() const {
|
2012-08-09 18:16:33 +00:00
|
|
|
return aheader;
|
|
|
|
}
|
|
|
|
|
|
|
|
void next() {
|
2014-04-30 01:00:27 +00:00
|
|
|
JS_ASSERT(!done());
|
2012-08-09 18:16:33 +00:00
|
|
|
aheader = aheader->next;
|
|
|
|
if (!aheader) {
|
|
|
|
aheader = remainingHeader;
|
2013-10-07 16:43:32 +00:00
|
|
|
remainingHeader = nullptr;
|
2012-08-09 18:16:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2011-08-26 19:39:31 +00:00
|
|
|
|
2014-04-29 06:39:44 +00:00
|
|
|
class ArenaCellIterImpl
|
2011-08-09 08:51:59 +00:00
|
|
|
{
|
2014-04-30 00:59:03 +00:00
|
|
|
// These three are set in init().
|
2011-08-18 07:16:08 +00:00
|
|
|
size_t firstThingOffset;
|
2011-08-09 08:51:59 +00:00
|
|
|
size_t thingSize;
|
2014-04-30 00:59:03 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
bool isInited;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// These three are set in reset() (which is called by init()).
|
|
|
|
FreeSpan span;
|
2011-08-09 08:51:59 +00:00
|
|
|
uintptr_t thing;
|
2014-04-30 00:59:03 +00:00
|
|
|
uintptr_t limit;
|
2011-08-09 08:51:59 +00:00
|
|
|
|
2014-04-30 00:59:03 +00:00
|
|
|
// Upon entry, |thing| points to any thing (free or used) and finds the
|
|
|
|
// first used thing, which may be |thing|.
|
|
|
|
void moveForwardIfFree() {
|
|
|
|
JS_ASSERT(!done());
|
|
|
|
if (thing == span.first) {
|
|
|
|
if (span.hasNext()) {
|
|
|
|
thing = span.last + thingSize;
|
|
|
|
span = *span.nextSpan();
|
|
|
|
} else {
|
|
|
|
thing = limit;
|
|
|
|
}
|
|
|
|
}
|
2011-08-09 08:51:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 00:59:03 +00:00
|
|
|
public:
|
|
|
|
ArenaCellIterImpl() {}
|
|
|
|
|
|
|
|
void init(ArenaHeader *aheader) {
|
|
|
|
AllocKind kind = aheader->getAllocKind();
|
|
|
|
#ifdef DEBUG
|
|
|
|
JS_ASSERT(aheader->zone->allocator.arenas.isSynchronizedFreeList(kind));
|
|
|
|
isInited = true;
|
|
|
|
#endif
|
2011-08-18 07:16:08 +00:00
|
|
|
firstThingOffset = Arena::firstThingOffset(kind);
|
|
|
|
thingSize = Arena::thingSize(kind);
|
2014-04-30 00:59:03 +00:00
|
|
|
reset(aheader);
|
2012-02-17 22:35:20 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 00:59:03 +00:00
|
|
|
// Use this to move from an Arena of a particular kind to another Arena of
|
|
|
|
// the same kind.
|
|
|
|
void reset(ArenaHeader *aheader) {
|
|
|
|
JS_ASSERT(isInited);
|
|
|
|
span = aheader->getFirstFreeSpan();
|
|
|
|
uintptr_t arenaAddr = aheader->arenaAddress();
|
|
|
|
thing = arenaAddr + firstThingOffset;
|
|
|
|
limit = arenaAddr + ArenaSize;
|
|
|
|
moveForwardIfFree();
|
2012-02-17 22:35:20 +00:00
|
|
|
}
|
|
|
|
|
2014-04-29 06:39:44 +00:00
|
|
|
bool done() const {
|
2014-04-30 00:59:03 +00:00
|
|
|
return thing == limit;
|
2014-04-29 06:39:44 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 00:59:03 +00:00
|
|
|
Cell *getCell() const {
|
2014-04-29 06:39:44 +00:00
|
|
|
JS_ASSERT(!done());
|
2014-04-30 00:59:03 +00:00
|
|
|
return reinterpret_cast<Cell *>(thing);
|
2014-04-29 06:39:44 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 00:59:03 +00:00
|
|
|
template<typename T> T *get() const {
|
2014-04-29 06:39:44 +00:00
|
|
|
JS_ASSERT(!done());
|
2014-04-30 00:59:03 +00:00
|
|
|
return static_cast<T *>(getCell());
|
2014-04-29 06:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void next() {
|
2014-04-30 00:59:03 +00:00
|
|
|
MOZ_ASSERT(!done());
|
2014-04-29 06:39:44 +00:00
|
|
|
thing += thingSize;
|
2014-04-30 00:59:03 +00:00
|
|
|
if (thing < limit)
|
|
|
|
moveForwardIfFree();
|
2014-04-29 06:39:44 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ArenaCellIterUnderGC : public ArenaCellIterImpl
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ArenaCellIterUnderGC(ArenaHeader *aheader) {
|
|
|
|
JS_ASSERT(aheader->zone->runtimeFromAnyThread()->isHeapBusy());
|
|
|
|
init(aheader);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ZoneCellIterImpl
|
|
|
|
{
|
2014-04-30 01:00:27 +00:00
|
|
|
ArenaIter arenaIter;
|
|
|
|
ArenaCellIterImpl cellIter;
|
2014-04-29 06:39:44 +00:00
|
|
|
|
|
|
|
protected:
|
2014-04-30 01:00:27 +00:00
|
|
|
ZoneCellIterImpl() {}
|
2014-04-29 06:39:44 +00:00
|
|
|
|
2013-03-17 03:36:37 +00:00
|
|
|
void init(JS::Zone *zone, AllocKind kind) {
|
2014-04-30 01:00:27 +00:00
|
|
|
JS_ASSERT(zone->allocator.arenas.isSynchronizedFreeList(kind));
|
|
|
|
arenaIter.init(zone, kind);
|
|
|
|
if (!arenaIter.done())
|
|
|
|
cellIter.init(arenaIter.get());
|
2011-08-09 08:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool done() const {
|
2014-04-30 01:00:27 +00:00
|
|
|
return arenaIter.done();
|
2011-08-09 08:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T> T *get() const {
|
|
|
|
JS_ASSERT(!done());
|
2014-04-30 01:00:27 +00:00
|
|
|
return cellIter.get<T>();
|
2011-08-09 08:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Cell *getCell() const {
|
|
|
|
JS_ASSERT(!done());
|
2014-04-30 01:00:27 +00:00
|
|
|
return cellIter.getCell();
|
2011-08-09 08:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void next() {
|
2014-04-30 01:00:27 +00:00
|
|
|
JS_ASSERT(!done());
|
|
|
|
cellIter.next();
|
|
|
|
if (cellIter.done()) {
|
|
|
|
JS_ASSERT(!arenaIter.done());
|
|
|
|
arenaIter.next();
|
|
|
|
if (!arenaIter.done())
|
|
|
|
cellIter.reset(arenaIter.get());
|
2011-08-09 08:51:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-29 06:39:44 +00:00
|
|
|
class ZoneCellIterUnderGC : public ZoneCellIterImpl
|
2012-02-17 22:35:20 +00:00
|
|
|
{
|
2011-08-09 08:51:59 +00:00
|
|
|
public:
|
2014-04-29 06:39:44 +00:00
|
|
|
ZoneCellIterUnderGC(JS::Zone *zone, AllocKind kind) {
|
2013-12-04 23:23:30 +00:00
|
|
|
#ifdef JSGC_GENERATIONAL
|
2014-04-30 13:38:59 +00:00
|
|
|
JS_ASSERT(zone->runtimeFromAnyThread()->gcNursery.isEmpty());
|
2013-12-04 23:23:30 +00:00
|
|
|
#endif
|
2013-08-05 14:48:34 +00:00
|
|
|
JS_ASSERT(zone->runtimeFromAnyThread()->isHeapBusy());
|
2013-03-17 03:36:37 +00:00
|
|
|
init(zone, kind);
|
2011-08-09 08:51:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-29 06:39:44 +00:00
|
|
|
class ZoneCellIter : public ZoneCellIterImpl
|
2011-08-09 08:51:59 +00:00
|
|
|
{
|
2011-08-18 07:16:08 +00:00
|
|
|
ArenaLists *lists;
|
|
|
|
AllocKind kind;
|
2011-08-09 08:51:59 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
size_t *counter;
|
|
|
|
#endif
|
|
|
|
public:
|
2014-04-29 06:39:44 +00:00
|
|
|
ZoneCellIter(JS::Zone *zone, AllocKind kind)
|
2013-03-17 03:36:37 +00:00
|
|
|
: lists(&zone->allocator.arenas),
|
2012-02-17 22:35:20 +00:00
|
|
|
kind(kind)
|
|
|
|
{
|
2012-03-03 01:19:56 +00:00
|
|
|
/*
|
|
|
|
* We have a single-threaded runtime, so there's no need to protect
|
|
|
|
* against other threads iterating or allocating. However, we do have
|
2013-02-08 08:40:39 +00:00
|
|
|
* background finalization; we have to wait for this to finish if it's
|
|
|
|
* currently active.
|
2012-03-03 01:19:56 +00:00
|
|
|
*/
|
2013-02-08 08:40:39 +00:00
|
|
|
if (IsBackgroundFinalized(kind) &&
|
2013-03-17 03:36:37 +00:00
|
|
|
zone->allocator.arenas.needBackgroundFinalizeWait(kind))
|
2013-02-08 08:40:39 +00:00
|
|
|
{
|
2013-08-05 14:48:34 +00:00
|
|
|
gc::FinishBackgroundFinalize(zone->runtimeFromMainThread());
|
2013-02-08 08:40:39 +00:00
|
|
|
}
|
2013-12-04 23:23:30 +00:00
|
|
|
|
|
|
|
#ifdef JSGC_GENERATIONAL
|
|
|
|
/* Evict the nursery before iterating so we can see all things. */
|
|
|
|
JSRuntime *rt = zone->runtimeFromMainThread();
|
2014-04-30 13:38:59 +00:00
|
|
|
if (!rt->gcNursery.isEmpty())
|
2013-12-04 23:23:30 +00:00
|
|
|
MinorGC(rt, JS::gcreason::EVICT_NURSERY);
|
|
|
|
#endif
|
|
|
|
|
2011-08-18 07:16:08 +00:00
|
|
|
if (lists->isSynchronizedFreeList(kind)) {
|
2013-10-07 16:43:32 +00:00
|
|
|
lists = nullptr;
|
2011-08-09 08:51:59 +00:00
|
|
|
} else {
|
2013-08-05 14:48:34 +00:00
|
|
|
JS_ASSERT(!zone->runtimeFromMainThread()->isHeapBusy());
|
2011-08-18 07:16:08 +00:00
|
|
|
lists->copyFreeListToArena(kind);
|
2011-08-09 08:51:59 +00:00
|
|
|
}
|
2013-12-04 23:23:30 +00:00
|
|
|
|
2011-08-09 08:51:59 +00:00
|
|
|
#ifdef DEBUG
|
2014-04-29 06:39:44 +00:00
|
|
|
/* Assert that no GCs can occur while a ZoneCellIter is live. */
|
2014-04-30 13:38:59 +00:00
|
|
|
counter = &zone->runtimeFromAnyThread()->noGCOrAllocationCheck;
|
2011-08-09 08:51:59 +00:00
|
|
|
++*counter;
|
|
|
|
#endif
|
2013-12-04 23:23:30 +00:00
|
|
|
|
2013-03-17 03:36:37 +00:00
|
|
|
init(zone, kind);
|
2011-08-09 08:51:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-29 06:39:44 +00:00
|
|
|
~ZoneCellIter() {
|
2011-08-09 08:51:59 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
JS_ASSERT(*counter > 0);
|
|
|
|
--*counter;
|
|
|
|
#endif
|
|
|
|
if (lists)
|
2011-08-18 07:16:08 +00:00
|
|
|
lists->clearFreeListInArena(kind);
|
2011-08-09 08:51:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-03-17 03:36:37 +00:00
|
|
|
class GCZonesIter
|
2012-08-09 18:16:33 +00:00
|
|
|
{
|
2012-04-03 00:02:25 +00:00
|
|
|
private:
|
2013-03-17 03:36:37 +00:00
|
|
|
ZonesIter zone;
|
2012-04-03 00:02:25 +00:00
|
|
|
|
|
|
|
public:
|
2013-11-11 06:48:58 +00:00
|
|
|
GCZonesIter(JSRuntime *rt) : zone(rt, WithAtoms) {
|
2013-03-17 03:36:37 +00:00
|
|
|
if (!zone->isCollecting())
|
2012-04-03 00:02:25 +00:00
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
2013-03-17 03:36:37 +00:00
|
|
|
bool done() const { return zone.done(); }
|
2012-04-03 00:02:25 +00:00
|
|
|
|
|
|
|
void next() {
|
|
|
|
JS_ASSERT(!done());
|
|
|
|
do {
|
2013-03-17 03:36:37 +00:00
|
|
|
zone.next();
|
|
|
|
} while (!zone.done() && !zone->isCollecting());
|
2012-04-03 00:02:25 +00:00
|
|
|
}
|
|
|
|
|
2013-03-17 03:36:37 +00:00
|
|
|
JS::Zone *get() const {
|
2012-04-03 00:02:25 +00:00
|
|
|
JS_ASSERT(!done());
|
2013-03-17 03:36:37 +00:00
|
|
|
return zone;
|
2012-04-03 00:02:25 +00:00
|
|
|
}
|
|
|
|
|
2013-03-17 03:36:37 +00:00
|
|
|
operator JS::Zone *() const { return get(); }
|
|
|
|
JS::Zone *operator->() const { return get(); }
|
2012-04-03 00:02:25 +00:00
|
|
|
};
|
|
|
|
|
2013-03-17 03:36:37 +00:00
|
|
|
typedef CompartmentsIterT<GCZonesIter> GCCompartmentsIter;
|
2013-01-27 21:51:40 +00:00
|
|
|
|
2013-03-17 03:36:37 +00:00
|
|
|
/* Iterates over all zones in the current zone group. */
|
|
|
|
class GCZoneGroupIter {
|
2012-11-16 15:34:22 +00:00
|
|
|
private:
|
2013-03-17 03:36:37 +00:00
|
|
|
JS::Zone *current;
|
2012-11-16 15:34:22 +00:00
|
|
|
|
|
|
|
public:
|
2013-03-17 03:36:37 +00:00
|
|
|
GCZoneGroupIter(JSRuntime *rt) {
|
2012-11-16 15:34:22 +00:00
|
|
|
JS_ASSERT(rt->isHeapBusy());
|
2014-04-30 13:38:59 +00:00
|
|
|
current = rt->gcCurrentZoneGroup;
|
2012-11-16 15:34:22 +00:00
|
|
|
}
|
|
|
|
|
2012-12-04 19:31:41 +00:00
|
|
|
bool done() const { return !current; }
|
2012-11-16 15:34:22 +00:00
|
|
|
|
|
|
|
void next() {
|
|
|
|
JS_ASSERT(!done());
|
2012-12-04 19:31:41 +00:00
|
|
|
current = current->nextNodeInGroup();
|
2012-11-16 15:34:22 +00:00
|
|
|
}
|
|
|
|
|
2013-03-17 03:36:37 +00:00
|
|
|
JS::Zone *get() const {
|
2012-11-16 15:34:22 +00:00
|
|
|
JS_ASSERT(!done());
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
2013-03-17 03:36:37 +00:00
|
|
|
operator JS::Zone *() const { return get(); }
|
|
|
|
JS::Zone *operator->() const { return get(); }
|
2012-11-16 15:34:22 +00:00
|
|
|
};
|
|
|
|
|
2013-03-17 03:36:37 +00:00
|
|
|
typedef CompartmentsIterT<GCZoneGroupIter> GCCompartmentGroupIter;
|
2013-01-27 21:51:40 +00:00
|
|
|
|
2013-03-14 17:26:06 +00:00
|
|
|
#ifdef JSGC_GENERATIONAL
|
|
|
|
/*
|
|
|
|
* Attempt to allocate a new GC thing out of the nursery. If there is not enough
|
2013-10-07 16:43:32 +00:00
|
|
|
* room in the nursery or there is an OOM, this method will return nullptr.
|
2013-03-14 17:26:06 +00:00
|
|
|
*/
|
2014-01-31 19:43:44 +00:00
|
|
|
template <AllowGC allowGC>
|
|
|
|
inline JSObject *
|
|
|
|
TryNewNurseryObject(ThreadSafeContext *cxArg, size_t thingSize, size_t nDynamicSlots)
|
2013-03-14 17:26:06 +00:00
|
|
|
{
|
2013-07-11 01:25:42 +00:00
|
|
|
JSContext *cx = cxArg->asJSContext();
|
2013-06-20 23:40:53 +00:00
|
|
|
|
2013-06-10 22:08:55 +00:00
|
|
|
JS_ASSERT(!IsAtomsCompartment(cx->compartment()));
|
|
|
|
JSRuntime *rt = cx->runtime();
|
2014-04-30 13:38:59 +00:00
|
|
|
Nursery &nursery = rt->gcNursery;
|
2014-01-31 19:43:44 +00:00
|
|
|
JSObject *obj = nursery.allocateObject(cx, thingSize, nDynamicSlots);
|
|
|
|
if (obj)
|
|
|
|
return obj;
|
2013-03-14 17:26:06 +00:00
|
|
|
if (allowGC && !rt->mainThread.suppressGC) {
|
2013-11-22 03:20:25 +00:00
|
|
|
MinorGC(cx, JS::gcreason::OUT_OF_NURSERY);
|
2013-03-14 17:26:06 +00:00
|
|
|
|
|
|
|
/* Exceeding gcMaxBytes while tenuring can disable the Nursery. */
|
|
|
|
if (nursery.isEnabled()) {
|
2014-01-31 19:43:44 +00:00
|
|
|
JSObject *obj = nursery.allocateObject(cx, thingSize, nDynamicSlots);
|
|
|
|
JS_ASSERT(obj);
|
|
|
|
return obj;
|
2013-03-14 17:26:06 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-07 16:43:32 +00:00
|
|
|
return nullptr;
|
2013-03-14 17:26:06 +00:00
|
|
|
}
|
|
|
|
#endif /* JSGC_GENERATIONAL */
|
|
|
|
|
2014-04-04 22:03:12 +00:00
|
|
|
static inline bool
|
|
|
|
PossiblyFail()
|
|
|
|
{
|
|
|
|
JS_OOM_POSSIBLY_FAIL();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-31 19:43:43 +00:00
|
|
|
template <AllowGC allowGC>
|
|
|
|
static inline bool
|
|
|
|
CheckAllocatorState(ThreadSafeContext *cx, AllocKind kind)
|
2010-09-24 17:54:39 +00:00
|
|
|
{
|
2014-01-31 19:43:43 +00:00
|
|
|
if (!cx->isJSContext())
|
|
|
|
return true;
|
2010-09-24 17:54:39 +00:00
|
|
|
|
2014-01-31 19:43:43 +00:00
|
|
|
JSContext *ncx = cx->asJSContext();
|
|
|
|
JSRuntime *rt = ncx->runtime();
|
2014-02-07 11:20:38 +00:00
|
|
|
#if defined(JS_GC_ZEAL) || defined(DEBUG)
|
2014-01-31 19:43:43 +00:00
|
|
|
JS_ASSERT_IF(rt->isAtomsCompartment(ncx->compartment()),
|
|
|
|
kind == FINALIZE_STRING ||
|
2014-04-04 06:30:26 +00:00
|
|
|
kind == FINALIZE_FAT_INLINE_STRING ||
|
2014-01-31 19:43:43 +00:00
|
|
|
kind == FINALIZE_JITCODE);
|
|
|
|
JS_ASSERT(!rt->isHeapBusy());
|
2014-04-30 13:38:59 +00:00
|
|
|
JS_ASSERT(!rt->noGCOrAllocationCheck);
|
2014-01-08 18:21:30 +00:00
|
|
|
#endif
|
2013-06-20 23:40:53 +00:00
|
|
|
|
2014-03-10 21:28:43 +00:00
|
|
|
// For testing out of memory conditions
|
2014-04-04 22:03:12 +00:00
|
|
|
if (!PossiblyFail()) {
|
|
|
|
js_ReportOutOfMemory(cx);
|
|
|
|
return false;
|
|
|
|
}
|
2012-03-10 20:53:03 +00:00
|
|
|
|
2014-02-07 11:20:38 +00:00
|
|
|
if (allowGC) {
|
2011-06-02 00:48:52 +00:00
|
|
|
#ifdef JS_GC_ZEAL
|
2014-02-07 11:20:38 +00:00
|
|
|
if (rt->needZealousGC())
|
|
|
|
js::gc::RunDebugGC(ncx);
|
2011-06-02 00:48:52 +00:00
|
|
|
#endif
|
|
|
|
|
2014-02-07 11:20:38 +00:00
|
|
|
if (rt->interrupt) {
|
2014-03-10 21:28:43 +00:00
|
|
|
// Invoking the interrupt callback can fail and we can't usefully
|
|
|
|
// handle that here. Just check in case we need to collect instead.
|
2014-02-07 11:20:38 +00:00
|
|
|
js::gc::GCIfNeeded(ncx);
|
|
|
|
}
|
|
|
|
}
|
2014-01-31 19:43:43 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static inline void
|
|
|
|
CheckIncrementalZoneState(ThreadSafeContext *cx, T *t)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (!cx->isJSContext())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Zone *zone = cx->asJSContext()->zone();
|
|
|
|
JS_ASSERT_IF(t && zone->wasGCStarted() && (zone->isGCMarking() || zone->isGCSweeping()),
|
|
|
|
t->arenaHeader()->allocatedDuringIncremental);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate 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.
|
|
|
|
*/
|
|
|
|
|
2014-01-31 19:43:44 +00:00
|
|
|
template <AllowGC allowGC>
|
|
|
|
inline JSObject *
|
|
|
|
AllocateObject(ThreadSafeContext *cx, AllocKind kind, size_t nDynamicSlots, InitialHeap heap)
|
2014-01-31 19:43:43 +00:00
|
|
|
{
|
|
|
|
size_t thingSize = Arena::thingSize(kind);
|
|
|
|
|
|
|
|
JS_ASSERT(thingSize == Arena::thingSize(kind));
|
|
|
|
if (!CheckAllocatorState<allowGC>(cx, kind))
|
|
|
|
return nullptr;
|
2011-12-31 14:32:04 +00:00
|
|
|
|
2013-07-10 15:29:52 +00:00
|
|
|
#ifdef JSGC_GENERATIONAL
|
2013-07-11 01:25:42 +00:00
|
|
|
if (cx->hasNursery() && ShouldNurseryAllocate(cx->nursery(), kind, heap)) {
|
2014-01-31 19:43:44 +00:00
|
|
|
JSObject *obj = TryNewNurseryObject<allowGC>(cx, thingSize, nDynamicSlots);
|
|
|
|
if (obj)
|
|
|
|
return obj;
|
2013-03-14 17:26:06 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-01-31 19:43:44 +00:00
|
|
|
HeapSlot *slots = nullptr;
|
|
|
|
if (nDynamicSlots) {
|
|
|
|
slots = cx->pod_malloc<HeapSlot>(nDynamicSlots);
|
|
|
|
if (MOZ_UNLIKELY(!slots))
|
|
|
|
return nullptr;
|
|
|
|
js::Debug_SetSlotRangeToCrashOnTouch(slots, nDynamicSlots);
|
|
|
|
}
|
2013-06-20 23:40:53 +00:00
|
|
|
|
2014-01-31 19:43:44 +00:00
|
|
|
JSObject *obj = static_cast<JSObject *>(cx->allocator()->arenas.allocateFromFreeList(kind, thingSize));
|
|
|
|
if (!obj)
|
|
|
|
obj = static_cast<JSObject *>(js::gc::ArenaLists::refillFreeList<allowGC>(cx, kind));
|
|
|
|
|
|
|
|
if (obj)
|
|
|
|
obj->setInitialSlots(slots);
|
|
|
|
else
|
|
|
|
js_free(slots);
|
|
|
|
|
|
|
|
CheckIncrementalZoneState(cx, obj);
|
|
|
|
return obj;
|
2014-01-31 19:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, AllowGC allowGC>
|
|
|
|
inline T *
|
|
|
|
AllocateNonObject(ThreadSafeContext *cx)
|
|
|
|
{
|
|
|
|
AllocKind kind = MapTypeToFinalizeKind<T>::kind;
|
|
|
|
size_t thingSize = sizeof(T);
|
2013-06-20 23:40:53 +00:00
|
|
|
|
2014-01-31 19:43:43 +00:00
|
|
|
JS_ASSERT(thingSize == Arena::thingSize(kind));
|
|
|
|
if (!CheckAllocatorState<allowGC>(cx, kind))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
T *t = static_cast<T *>(cx->allocator()->arenas.allocateFromFreeList(kind, thingSize));
|
|
|
|
if (!t)
|
|
|
|
t = static_cast<T *>(js::gc::ArenaLists::refillFreeList<allowGC>(cx, kind));
|
|
|
|
|
|
|
|
CheckIncrementalZoneState(cx, t);
|
2012-12-18 13:27:28 +00:00
|
|
|
return t;
|
2011-11-03 16:57:12 +00:00
|
|
|
}
|
|
|
|
|
2014-02-01 20:04:03 +00:00
|
|
|
/*
|
|
|
|
* When allocating for initialization from a cached object copy, we will
|
|
|
|
* potentially destroy the cache entry we want to copy if we allow GC. On the
|
|
|
|
* other hand, since these allocations are extremely common, we don't want to
|
|
|
|
* delay GC from these allocation sites. Instead we allow the GC, but still
|
|
|
|
* fail the allocation, forcing the non-cached path.
|
|
|
|
*/
|
|
|
|
template <AllowGC allowGC>
|
|
|
|
inline JSObject *
|
|
|
|
AllocateObjectForCacheHit(JSContext *cx, AllocKind kind, InitialHeap heap)
|
|
|
|
{
|
|
|
|
#ifdef JSGC_GENERATIONAL
|
|
|
|
if (ShouldNurseryAllocate(cx->nursery(), kind, heap)) {
|
|
|
|
size_t thingSize = Arena::thingSize(kind);
|
|
|
|
|
|
|
|
JS_ASSERT(thingSize == Arena::thingSize(kind));
|
|
|
|
if (!CheckAllocatorState<NoGC>(cx, kind))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
JSObject *obj = TryNewNurseryObject<NoGC>(cx, thingSize, 0);
|
|
|
|
if (!obj && allowGC) {
|
|
|
|
MinorGC(cx, JS::gcreason::OUT_OF_NURSERY);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
JSObject *obj = AllocateObject<NoGC>(cx, kind, 0, heap);
|
|
|
|
if (!obj && allowGC) {
|
|
|
|
MaybeGC(cx);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2011-11-18 22:40:14 +00:00
|
|
|
} /* namespace gc */
|
2014-01-31 19:43:43 +00:00
|
|
|
|
2014-01-31 19:43:44 +00:00
|
|
|
template <js::AllowGC allowGC>
|
|
|
|
inline JSObject *
|
|
|
|
NewGCObject(js::ThreadSafeContext *cx, js::gc::AllocKind kind, size_t nDynamicSlots, js::gc::InitialHeap heap)
|
|
|
|
{
|
|
|
|
JS_ASSERT(kind >= js::gc::FINALIZE_OBJECT0 && kind <= js::gc::FINALIZE_OBJECT_LAST);
|
|
|
|
return js::gc::AllocateObject<allowGC>(cx, kind, nDynamicSlots, heap);
|
|
|
|
}
|
|
|
|
|
2014-01-31 19:43:43 +00:00
|
|
|
template <js::AllowGC allowGC>
|
|
|
|
inline jit::JitCode *
|
|
|
|
NewJitCode(js::ThreadSafeContext *cx)
|
|
|
|
{
|
|
|
|
return gc::AllocateNonObject<jit::JitCode, allowGC>(cx);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
types::TypeObject *
|
|
|
|
NewTypeObject(js::ThreadSafeContext *cx)
|
|
|
|
{
|
|
|
|
return gc::AllocateNonObject<types::TypeObject, js::CanGC>(cx);
|
|
|
|
}
|
|
|
|
|
2011-11-18 22:40:14 +00:00
|
|
|
} /* namespace js */
|
|
|
|
|
2013-01-23 22:22:10 +00:00
|
|
|
template <js::AllowGC allowGC>
|
2010-09-24 17:54:39 +00:00
|
|
|
inline JSString *
|
2013-07-11 01:25:42 +00:00
|
|
|
js_NewGCString(js::ThreadSafeContext *cx)
|
2010-09-24 17:54:39 +00:00
|
|
|
{
|
2014-01-31 19:43:43 +00:00
|
|
|
return js::gc::AllocateNonObject<JSString, allowGC>(cx);
|
2013-01-15 21:51:56 +00:00
|
|
|
}
|
|
|
|
|
2013-01-23 22:22:10 +00:00
|
|
|
template <js::AllowGC allowGC>
|
2014-04-04 06:30:26 +00:00
|
|
|
inline JSFatInlineString *
|
|
|
|
js_NewGCFatInlineString(js::ThreadSafeContext *cx)
|
2010-09-24 17:54:39 +00:00
|
|
|
{
|
2014-04-04 06:30:26 +00:00
|
|
|
return js::gc::AllocateNonObject<JSFatInlineString, allowGC>(cx);
|
2013-01-15 21:51:56 +00:00
|
|
|
}
|
|
|
|
|
2010-11-15 20:39:00 +00:00
|
|
|
inline JSExternalString *
|
2013-07-11 01:25:42 +00:00
|
|
|
js_NewGCExternalString(js::ThreadSafeContext *cx)
|
2010-09-24 17:54:39 +00:00
|
|
|
{
|
2014-01-31 19:43:43 +00:00
|
|
|
return js::gc::AllocateNonObject<JSExternalString, js::CanGC>(cx);
|
2010-09-24 17:54:39 +00:00
|
|
|
}
|
|
|
|
|
2011-08-09 08:51:59 +00:00
|
|
|
inline JSScript *
|
2013-07-11 01:25:42 +00:00
|
|
|
js_NewGCScript(js::ThreadSafeContext *cx)
|
2011-08-09 08:51:59 +00:00
|
|
|
{
|
2014-01-31 19:43:43 +00:00
|
|
|
return js::gc::AllocateNonObject<JSScript, js::CanGC>(cx);
|
2011-08-09 08:51:59 +00:00
|
|
|
}
|
|
|
|
|
2013-05-30 12:29:56 +00:00
|
|
|
inline js::LazyScript *
|
2013-07-11 01:25:42 +00:00
|
|
|
js_NewGCLazyScript(js::ThreadSafeContext *cx)
|
2013-05-30 12:29:56 +00:00
|
|
|
{
|
2014-01-31 19:43:43 +00:00
|
|
|
return js::gc::AllocateNonObject<js::LazyScript, js::CanGC>(cx);
|
2013-05-30 12:29:56 +00:00
|
|
|
}
|
|
|
|
|
2013-04-30 22:40:09 +00:00
|
|
|
inline js::Shape *
|
2013-07-11 01:25:42 +00:00
|
|
|
js_NewGCShape(js::ThreadSafeContext *cx)
|
2011-03-23 18:57:44 +00:00
|
|
|
{
|
2014-01-31 19:43:43 +00:00
|
|
|
return js::gc::AllocateNonObject<js::Shape, js::CanGC>(cx);
|
2011-03-23 18:57:44 +00:00
|
|
|
}
|
|
|
|
|
2013-01-23 22:22:10 +00:00
|
|
|
template <js::AllowGC allowGC>
|
2013-04-30 22:39:45 +00:00
|
|
|
inline js::BaseShape *
|
2013-07-11 01:25:42 +00:00
|
|
|
js_NewGCBaseShape(js::ThreadSafeContext *cx)
|
2011-09-28 22:04:55 +00:00
|
|
|
{
|
2014-01-31 19:43:43 +00:00
|
|
|
return js::gc::AllocateNonObject<js::BaseShape, allowGC>(cx);
|
2011-09-28 22:04:55 +00:00
|
|
|
}
|
|
|
|
|
2013-06-20 00:59:46 +00:00
|
|
|
#endif /* jsgcinlines_h */
|