2012-10-26 22:17:24 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2013-04-17 00:47:10 +04:00
|
|
|
* vim: set ts=8 sts=4 et sw=4 tw=99:
|
|
|
|
* 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-10-26 22:17:24 +04:00
|
|
|
|
2013-06-20 04:59:09 +04:00
|
|
|
#ifndef js_HeapAPI_h
|
|
|
|
#define js_HeapAPI_h
|
2012-10-26 22:17:24 +04:00
|
|
|
|
2013-10-08 17:54:33 +04:00
|
|
|
#include <limits.h>
|
|
|
|
|
2015-06-11 20:03:33 +03:00
|
|
|
#include "jspubtd.h"
|
|
|
|
|
|
|
|
#include "js/TraceKind.h"
|
2013-07-24 04:34:12 +04:00
|
|
|
#include "js/Utility.h"
|
|
|
|
|
2012-10-26 22:18:50 +04:00
|
|
|
/* These values are private to the JS engine. */
|
2012-10-26 22:17:24 +04:00
|
|
|
namespace js {
|
2013-09-05 06:19:04 +04:00
|
|
|
|
2013-11-05 20:55:28 +04:00
|
|
|
JS_FRIEND_API(bool)
|
2015-03-29 01:22:11 +03:00
|
|
|
CurrentThreadCanAccessZone(JS::Zone* zone);
|
2013-09-05 06:19:04 +04:00
|
|
|
|
2012-10-26 22:18:50 +04:00
|
|
|
namespace gc {
|
|
|
|
|
2014-04-26 12:30:04 +04:00
|
|
|
struct Cell;
|
|
|
|
|
2012-10-26 22:18:50 +04:00
|
|
|
const size_t ArenaShift = 12;
|
|
|
|
const size_t ArenaSize = size_t(1) << ArenaShift;
|
|
|
|
const size_t ArenaMask = ArenaSize - 1;
|
|
|
|
|
2014-07-15 12:42:47 +04:00
|
|
|
#ifdef JS_GC_SMALL_CHUNK_SIZE
|
|
|
|
const size_t ChunkShift = 18;
|
|
|
|
#else
|
2012-10-26 22:18:50 +04:00
|
|
|
const size_t ChunkShift = 20;
|
2014-07-15 12:42:47 +04:00
|
|
|
#endif
|
2012-10-26 22:18:50 +04:00
|
|
|
const size_t ChunkSize = size_t(1) << ChunkShift;
|
|
|
|
const size_t ChunkMask = ChunkSize - 1;
|
|
|
|
|
2017-05-09 13:38:32 +03:00
|
|
|
const size_t CellAlignShift = 3;
|
|
|
|
const size_t CellAlignBytes = size_t(1) << CellAlignShift;
|
|
|
|
const size_t CellAlignMask = CellAlignBytes - 1;
|
|
|
|
|
|
|
|
const size_t CellBytesPerMarkBit = CellAlignBytes;
|
2012-11-29 05:03:54 +04:00
|
|
|
|
2017-10-18 12:09:05 +03:00
|
|
|
/*
|
|
|
|
* We sometimes use an index to refer to a cell in an arena. The index for a
|
|
|
|
* cell is found by dividing by the cell alignment so not all indicies refer to
|
|
|
|
* valid cells.
|
|
|
|
*/
|
|
|
|
const size_t ArenaCellIndexBytes = CellAlignBytes;
|
|
|
|
const size_t MaxArenaCellIndex = ArenaSize / CellAlignBytes;
|
|
|
|
|
2012-11-29 05:03:54 +04:00
|
|
|
/* These are magic constants derived from actual offsets in gc/Heap.h. */
|
2014-07-15 12:42:47 +04:00
|
|
|
#ifdef JS_GC_SMALL_CHUNK_SIZE
|
|
|
|
const size_t ChunkMarkBitmapOffset = 258104;
|
|
|
|
const size_t ChunkMarkBitmapBits = 31744;
|
|
|
|
#else
|
2014-05-01 20:26:12 +04:00
|
|
|
const size_t ChunkMarkBitmapOffset = 1032352;
|
2012-11-29 05:03:54 +04:00
|
|
|
const size_t ChunkMarkBitmapBits = 129024;
|
2014-07-15 12:42:47 +04:00
|
|
|
#endif
|
2013-03-20 04:59:42 +04:00
|
|
|
const size_t ChunkRuntimeOffset = ChunkSize - sizeof(void*);
|
2015-08-20 20:35:22 +03:00
|
|
|
const size_t ChunkTrailerSize = 2 * sizeof(uintptr_t) + sizeof(uint64_t);
|
|
|
|
const size_t ChunkLocationOffset = ChunkSize - ChunkTrailerSize;
|
2017-09-19 14:31:31 +03:00
|
|
|
const size_t ChunkStoreBufferOffset = ChunkLocationOffset + sizeof(uint64_t);
|
2016-02-29 19:21:00 +03:00
|
|
|
const size_t ArenaZoneOffset = sizeof(size_t);
|
2016-06-21 17:30:34 +03:00
|
|
|
const size_t ArenaHeaderSize = sizeof(size_t) + 2 * sizeof(uintptr_t) +
|
|
|
|
sizeof(size_t) + sizeof(uintptr_t);
|
2012-11-29 05:03:54 +04:00
|
|
|
|
|
|
|
/*
|
2017-07-12 20:31:55 +03:00
|
|
|
* Live objects are marked black or gray. Everything reachable from a JS root is
|
|
|
|
* marked black. Objects marked gray are eligible for cycle collection.
|
|
|
|
*
|
|
|
|
* BlackBit: GrayOrBlackBit: Color:
|
2016-12-15 00:59:43 +03:00
|
|
|
* 0 0 white
|
|
|
|
* 0 1 gray
|
|
|
|
* 1 0 black
|
|
|
|
* 1 1 black
|
|
|
|
*/
|
|
|
|
enum class ColorBit : uint32_t
|
|
|
|
{
|
|
|
|
BlackBit = 0,
|
|
|
|
GrayOrBlackBit = 1
|
|
|
|
};
|
|
|
|
|
2014-04-26 12:30:04 +04:00
|
|
|
/*
|
2016-08-11 19:14:56 +03:00
|
|
|
* The "location" field in the Chunk trailer is a enum indicating various roles
|
|
|
|
* of the chunk.
|
2014-04-26 12:30:04 +04:00
|
|
|
*/
|
2016-08-11 19:14:56 +03:00
|
|
|
enum class ChunkLocation : uint32_t
|
|
|
|
{
|
|
|
|
Invalid = 0,
|
|
|
|
Nursery = 1,
|
|
|
|
TenuredHeap = 2
|
|
|
|
};
|
2014-04-26 12:30:04 +04:00
|
|
|
|
2014-05-15 06:48:09 +04:00
|
|
|
#ifdef JS_DEBUG
|
|
|
|
/* When downcasting, ensure we are actually the right type. */
|
|
|
|
extern JS_FRIEND_API(void)
|
2015-05-22 20:40:24 +03:00
|
|
|
AssertGCThingHasType(js::gc::Cell* cell, JS::TraceKind kind);
|
2014-05-15 06:48:09 +04:00
|
|
|
#else
|
|
|
|
inline void
|
2015-05-22 20:40:24 +03:00
|
|
|
AssertGCThingHasType(js::gc::Cell* cell, JS::TraceKind kind) {}
|
2014-05-15 06:48:09 +04:00
|
|
|
#endif
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
MOZ_ALWAYS_INLINE bool IsInsideNursery(const js::gc::Cell* cell);
|
2014-12-05 20:38:32 +03:00
|
|
|
|
2012-10-26 22:18:50 +04:00
|
|
|
} /* namespace gc */
|
|
|
|
} /* namespace js */
|
|
|
|
|
2013-01-28 00:37:18 +04:00
|
|
|
namespace JS {
|
2017-11-16 15:21:07 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This list enumerates the different types of conceptual stacks we have in
|
|
|
|
* SpiderMonkey. In reality, they all share the C stack, but we allow different
|
|
|
|
* stack limits depending on the type of code running.
|
|
|
|
*/
|
|
|
|
enum StackKind
|
|
|
|
{
|
|
|
|
StackForSystemCode, // C++, such as the GC, running on behalf of the VM.
|
|
|
|
StackForTrustedScript, // Script running with trusted principals.
|
|
|
|
StackForUntrustedScript, // Script running with untrusted principals.
|
|
|
|
StackKindCount
|
|
|
|
};
|
2013-01-28 00:37:18 +04:00
|
|
|
|
2017-07-21 07:34:46 +03:00
|
|
|
/*
|
|
|
|
* Default size for the generational nursery in bytes.
|
|
|
|
* This is the initial nursery size, when running in the browser this is
|
|
|
|
* updated by JS_SetGCParameter().
|
|
|
|
*/
|
2014-11-20 02:01:54 +03:00
|
|
|
const uint32_t DefaultNurseryBytes = 16 * js::gc::ChunkSize;
|
2014-07-11 12:59:05 +04:00
|
|
|
|
2017-07-21 08:56:11 +03:00
|
|
|
/* Default maximum heap size in bytes to pass to JS_NewContext(). */
|
2014-07-16 13:01:20 +04:00
|
|
|
const uint32_t DefaultHeapMaxBytes = 32 * 1024 * 1024;
|
|
|
|
|
2012-10-26 22:18:50 +04:00
|
|
|
namespace shadow {
|
|
|
|
|
2013-01-28 00:37:18 +04:00
|
|
|
struct Zone
|
2012-11-29 05:03:54 +04:00
|
|
|
{
|
2017-04-26 13:18:39 +03:00
|
|
|
enum GCState : uint8_t {
|
|
|
|
NoGC,
|
|
|
|
Mark,
|
|
|
|
MarkGray,
|
|
|
|
Sweep,
|
|
|
|
Finished,
|
|
|
|
Compact
|
|
|
|
};
|
|
|
|
|
2013-09-05 06:19:04 +04:00
|
|
|
protected:
|
2015-03-29 01:22:11 +03:00
|
|
|
JSRuntime* const runtime_;
|
|
|
|
JSTracer* const barrierTracer_; // A pointer to the JSRuntime's |gcMarker|.
|
2017-04-30 15:42:34 +03:00
|
|
|
uint32_t needsIncrementalBarrier_;
|
2017-04-26 13:18:39 +03:00
|
|
|
GCState gcState_;
|
2012-11-29 05:03:54 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
Zone(JSRuntime* runtime, JSTracer* barrierTracerArg)
|
2013-09-05 06:19:04 +04:00
|
|
|
: runtime_(runtime),
|
|
|
|
barrierTracer_(barrierTracerArg),
|
2017-04-30 15:42:34 +03:00
|
|
|
needsIncrementalBarrier_(0),
|
2017-04-26 13:18:39 +03:00
|
|
|
gcState_(NoGC)
|
2017-02-02 22:12:43 +03:00
|
|
|
{}
|
2013-09-05 06:19:04 +04:00
|
|
|
|
2017-04-26 13:18:39 +03:00
|
|
|
public:
|
2014-07-28 21:16:56 +04:00
|
|
|
bool needsIncrementalBarrier() const {
|
|
|
|
return needsIncrementalBarrier_;
|
2013-09-05 06:19:04 +04:00
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
JSTracer* barrierTracer() {
|
2014-07-28 21:16:56 +04:00
|
|
|
MOZ_ASSERT(needsIncrementalBarrier_);
|
2014-02-18 10:24:15 +04:00
|
|
|
MOZ_ASSERT(js::CurrentThreadCanAccessRuntime(runtime_));
|
2013-09-05 06:19:04 +04:00
|
|
|
return barrierTracer_;
|
|
|
|
}
|
|
|
|
|
2017-02-11 15:27:32 +03:00
|
|
|
JSRuntime* runtimeFromActiveCooperatingThread() const {
|
2014-02-18 10:24:15 +04:00
|
|
|
MOZ_ASSERT(js::CurrentThreadCanAccessRuntime(runtime_));
|
2013-09-05 06:19:04 +04:00
|
|
|
return runtime_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: Unrestricted access to the zone's runtime from an arbitrary
|
|
|
|
// thread can easily lead to races. Use this method very carefully.
|
2015-03-29 01:22:11 +03:00
|
|
|
JSRuntime* runtimeFromAnyThread() const {
|
2013-09-05 06:19:04 +04:00
|
|
|
return runtime_;
|
|
|
|
}
|
|
|
|
|
2017-04-26 13:18:39 +03:00
|
|
|
GCState gcState() const { return gcState_; }
|
|
|
|
bool wasGCStarted() const { return gcState_ != NoGC; }
|
2017-06-02 12:32:37 +03:00
|
|
|
bool isGCMarkingBlack() const { return gcState_ == Mark; }
|
|
|
|
bool isGCMarkingGray() const { return gcState_ == MarkGray; }
|
|
|
|
bool isGCSweeping() const { return gcState_ == Sweep; }
|
|
|
|
bool isGCFinished() const { return gcState_ == Finished; }
|
|
|
|
bool isGCCompacting() const { return gcState_ == Compact; }
|
2017-06-16 12:25:41 +03:00
|
|
|
bool isGCMarking() const { return gcState_ == Mark || gcState_ == MarkGray; }
|
2017-06-02 12:32:37 +03:00
|
|
|
bool isGCSweepingOrCompacting() const { return gcState_ == Sweep || gcState_ == Compact; }
|
2017-04-26 13:18:39 +03:00
|
|
|
|
2016-04-25 22:32:36 +03:00
|
|
|
static MOZ_ALWAYS_INLINE JS::shadow::Zone* asShadowZone(JS::Zone* zone) {
|
2013-09-05 06:19:04 +04:00
|
|
|
return reinterpret_cast<JS::shadow::Zone*>(zone);
|
|
|
|
}
|
2012-11-29 05:03:54 +04:00
|
|
|
};
|
|
|
|
|
2012-10-26 22:18:50 +04:00
|
|
|
} /* namespace shadow */
|
2014-12-02 01:49:21 +03:00
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/**
|
|
|
|
* A GC pointer, tagged with the trace kind.
|
|
|
|
*
|
|
|
|
* In general, a GC pointer should be stored with an exact type. This class
|
|
|
|
* is for use when that is not possible because a single pointer must point
|
|
|
|
* to several kinds of GC thing.
|
|
|
|
*/
|
2014-12-02 01:49:21 +03:00
|
|
|
class JS_FRIEND_API(GCCellPtr)
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Construction from a void* and trace kind.
|
2015-05-22 20:40:24 +03:00
|
|
|
GCCellPtr(void* gcthing, JS::TraceKind traceKind) : ptr(checkedCast(gcthing, traceKind)) {}
|
2014-12-02 01:49:21 +03:00
|
|
|
|
2015-05-14 00:07:34 +03:00
|
|
|
// Automatically construct a null GCCellPtr from nullptr.
|
2015-05-22 20:40:24 +03:00
|
|
|
MOZ_IMPLICIT GCCellPtr(decltype(nullptr)) : ptr(checkedCast(nullptr, JS::TraceKind::Null)) {}
|
2015-05-14 00:07:34 +03:00
|
|
|
|
2014-12-02 01:49:21 +03:00
|
|
|
// Construction from an explicit type.
|
2015-07-04 01:06:23 +03:00
|
|
|
template <typename T>
|
2015-07-29 19:31:47 +03:00
|
|
|
explicit GCCellPtr(T* p) : ptr(checkedCast(p, JS::MapTypeToTraceKind<T>::kind)) { }
|
|
|
|
explicit GCCellPtr(JSFunction* p) : ptr(checkedCast(p, JS::TraceKind::Object)) { }
|
2015-05-22 20:40:24 +03:00
|
|
|
explicit GCCellPtr(JSFlatString* str) : ptr(checkedCast(str, JS::TraceKind::String)) { }
|
2015-03-29 01:22:11 +03:00
|
|
|
explicit GCCellPtr(const Value& v);
|
2014-12-02 01:49:21 +03:00
|
|
|
|
2015-05-22 20:40:24 +03:00
|
|
|
JS::TraceKind kind() const {
|
|
|
|
JS::TraceKind traceKind = JS::TraceKind(ptr & OutOfLineTraceKindMask);
|
|
|
|
if (uintptr_t(traceKind) != OutOfLineTraceKindMask)
|
2014-12-02 01:49:21 +03:00
|
|
|
return traceKind;
|
|
|
|
return outOfLineKind();
|
|
|
|
}
|
|
|
|
|
2014-12-02 09:34:25 +03:00
|
|
|
// Allow GCCellPtr to be used in a boolean context.
|
2015-02-03 19:52:36 +03:00
|
|
|
explicit operator bool() const {
|
2015-05-22 20:40:24 +03:00
|
|
|
MOZ_ASSERT(bool(asCell()) == (kind() != JS::TraceKind::Null));
|
2015-02-03 19:52:36 +03:00
|
|
|
return asCell();
|
2014-12-02 09:34:25 +03:00
|
|
|
}
|
2014-12-02 01:49:21 +03:00
|
|
|
|
2014-12-02 02:06:37 +03:00
|
|
|
// Simplify checks to the kind.
|
2015-07-04 01:06:23 +03:00
|
|
|
template <typename T>
|
|
|
|
bool is() const { return kind() == JS::MapTypeToTraceKind<T>::kind; }
|
2014-12-02 02:06:37 +03:00
|
|
|
|
2014-12-02 01:49:21 +03:00
|
|
|
// Conversions to more specific types must match the kind. Access to
|
|
|
|
// further refined types is not allowed directly from a GCCellPtr.
|
2015-07-04 01:06:23 +03:00
|
|
|
template <typename T>
|
2015-07-29 19:31:47 +03:00
|
|
|
T& as() const {
|
2015-07-04 01:06:23 +03:00
|
|
|
MOZ_ASSERT(kind() == JS::MapTypeToTraceKind<T>::kind);
|
|
|
|
// We can't use static_cast here, because the fact that JSObject
|
|
|
|
// inherits from js::gc::Cell is not part of the public API.
|
|
|
|
return *reinterpret_cast<T*>(asCell());
|
2014-12-02 02:06:37 +03:00
|
|
|
}
|
2015-07-04 01:06:23 +03:00
|
|
|
|
|
|
|
// Return a pointer to the cell this |GCCellPtr| refers to, or |nullptr|.
|
|
|
|
// (It would be more symmetrical with |to| for this to return a |Cell&|, but
|
|
|
|
// the result can be |nullptr|, and null references are undefined behavior.)
|
2015-03-29 01:22:11 +03:00
|
|
|
js::gc::Cell* asCell() const {
|
2015-05-22 20:40:24 +03:00
|
|
|
return reinterpret_cast<js::gc::Cell*>(ptr & ~OutOfLineTraceKindMask);
|
2014-12-02 09:34:25 +03:00
|
|
|
}
|
2014-12-02 01:49:21 +03:00
|
|
|
|
2014-12-05 20:38:32 +03:00
|
|
|
// The CC's trace logger needs an identity that is XPIDL serializable.
|
|
|
|
uint64_t unsafeAsInteger() const {
|
2015-01-10 02:42:42 +03:00
|
|
|
return static_cast<uint64_t>(unsafeAsUIntPtr());
|
2014-12-05 20:38:32 +03:00
|
|
|
}
|
2014-12-05 20:38:33 +03:00
|
|
|
// Inline mark bitmap access requires direct pointer arithmetic.
|
|
|
|
uintptr_t unsafeAsUIntPtr() const {
|
2015-01-10 02:42:42 +03:00
|
|
|
MOZ_ASSERT(asCell());
|
|
|
|
MOZ_ASSERT(!js::gc::IsInsideNursery(asCell()));
|
|
|
|
return reinterpret_cast<uintptr_t>(asCell());
|
2014-12-05 20:38:33 +03:00
|
|
|
}
|
2014-12-02 01:49:21 +03:00
|
|
|
|
2017-04-25 16:07:55 +03:00
|
|
|
MOZ_ALWAYS_INLINE bool mayBeOwnedByOtherRuntime() const {
|
|
|
|
if (is<JSString>() || is<JS::Symbol>())
|
|
|
|
return mayBeOwnedByOtherRuntimeSlow();
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-07 12:14:40 +03:00
|
|
|
|
2014-12-02 01:49:21 +03:00
|
|
|
private:
|
2015-05-22 20:40:24 +03:00
|
|
|
static uintptr_t checkedCast(void* p, JS::TraceKind traceKind) {
|
2015-03-29 01:22:11 +03:00
|
|
|
js::gc::Cell* cell = static_cast<js::gc::Cell*>(p);
|
2015-05-22 20:40:24 +03:00
|
|
|
MOZ_ASSERT((uintptr_t(p) & OutOfLineTraceKindMask) == 0);
|
2014-12-02 01:49:21 +03:00
|
|
|
AssertGCThingHasType(cell, traceKind);
|
2015-05-22 20:40:24 +03:00
|
|
|
// Note: the OutOfLineTraceKindMask bits are set on all out-of-line kinds
|
2014-12-02 01:49:21 +03:00
|
|
|
// so that we can mask instead of branching.
|
2015-05-22 20:40:24 +03:00
|
|
|
MOZ_ASSERT_IF(uintptr_t(traceKind) >= OutOfLineTraceKindMask,
|
|
|
|
(uintptr_t(traceKind) & OutOfLineTraceKindMask) == OutOfLineTraceKindMask);
|
|
|
|
return uintptr_t(p) | (uintptr_t(traceKind) & OutOfLineTraceKindMask);
|
2014-12-02 01:49:21 +03:00
|
|
|
}
|
|
|
|
|
2017-04-25 16:07:55 +03:00
|
|
|
bool mayBeOwnedByOtherRuntimeSlow() const;
|
|
|
|
|
2015-05-22 20:40:24 +03:00
|
|
|
JS::TraceKind outOfLineKind() const;
|
2014-12-02 01:49:21 +03:00
|
|
|
|
|
|
|
uintptr_t ptr;
|
|
|
|
};
|
|
|
|
|
2015-02-03 19:52:28 +03:00
|
|
|
inline bool
|
2015-03-29 01:22:11 +03:00
|
|
|
operator==(const GCCellPtr& ptr1, const GCCellPtr& ptr2)
|
2015-02-03 19:52:28 +03:00
|
|
|
{
|
|
|
|
return ptr1.asCell() == ptr2.asCell();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2015-03-29 01:22:11 +03:00
|
|
|
operator!=(const GCCellPtr& ptr1, const GCCellPtr& ptr2)
|
2015-02-03 19:52:28 +03:00
|
|
|
{
|
|
|
|
return !(ptr1 == ptr2);
|
|
|
|
}
|
|
|
|
|
2015-09-17 20:57:55 +03:00
|
|
|
// Unwraps the given GCCellPtr and calls the given functor with a template
|
|
|
|
// argument of the actual type of the pointer.
|
|
|
|
template <typename F, typename... Args>
|
|
|
|
auto
|
|
|
|
DispatchTyped(F f, GCCellPtr thing, Args&&... args)
|
|
|
|
-> decltype(f(static_cast<JSObject*>(nullptr), mozilla::Forward<Args>(args)...))
|
|
|
|
{
|
|
|
|
switch (thing.kind()) {
|
|
|
|
#define JS_EXPAND_DEF(name, type, _) \
|
|
|
|
case JS::TraceKind::name: \
|
|
|
|
return f(&thing.as<type>(), mozilla::Forward<Args>(args)...);
|
|
|
|
JS_FOR_EACH_TRACEKIND(JS_EXPAND_DEF);
|
|
|
|
#undef JS_EXPAND_DEF
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Invalid trace kind in DispatchTyped for GCCellPtr.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-29 05:03:54 +04:00
|
|
|
} /* namespace JS */
|
|
|
|
|
|
|
|
namespace js {
|
|
|
|
namespace gc {
|
2014-12-05 20:38:33 +03:00
|
|
|
namespace detail {
|
2012-11-29 05:03:54 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
static MOZ_ALWAYS_INLINE uintptr_t*
|
2014-12-05 20:38:33 +03:00
|
|
|
GetGCThingMarkBitmap(const uintptr_t addr)
|
2012-11-29 05:03:54 +04:00
|
|
|
{
|
2017-11-22 11:40:00 +03:00
|
|
|
// Note: the JIT pre-barrier trampolines inline this code. Update that
|
|
|
|
// code too when making changes here!
|
2014-12-05 20:38:33 +03:00
|
|
|
MOZ_ASSERT(addr);
|
|
|
|
const uintptr_t bmap_addr = (addr & ~ChunkMask) | ChunkMarkBitmapOffset;
|
2015-03-29 01:22:11 +03:00
|
|
|
return reinterpret_cast<uintptr_t*>(bmap_addr);
|
2012-11-29 05:03:54 +04:00
|
|
|
}
|
2012-10-26 22:18:50 +04:00
|
|
|
|
2014-01-25 08:14:56 +04:00
|
|
|
static MOZ_ALWAYS_INLINE void
|
2016-12-15 00:59:43 +03:00
|
|
|
GetGCThingMarkWordAndMask(const uintptr_t addr, ColorBit colorBit,
|
2015-03-29 01:22:11 +03:00
|
|
|
uintptr_t** wordp, uintptr_t* maskp)
|
2012-11-29 05:03:54 +04:00
|
|
|
{
|
2017-11-22 11:40:00 +03:00
|
|
|
// Note: the JIT pre-barrier trampolines inline this code. Update that
|
|
|
|
// code too when making changes here!
|
2014-12-05 20:38:33 +03:00
|
|
|
MOZ_ASSERT(addr);
|
2016-12-15 00:59:43 +03:00
|
|
|
const size_t bit = (addr & js::gc::ChunkMask) / js::gc::CellBytesPerMarkBit +
|
|
|
|
static_cast<uint32_t>(colorBit);
|
2014-02-18 10:24:15 +04:00
|
|
|
MOZ_ASSERT(bit < js::gc::ChunkMarkBitmapBits);
|
2015-03-29 01:22:11 +03:00
|
|
|
uintptr_t* bitmap = GetGCThingMarkBitmap(addr);
|
2013-10-08 17:54:33 +04:00
|
|
|
const uintptr_t nbits = sizeof(*bitmap) * CHAR_BIT;
|
|
|
|
*maskp = uintptr_t(1) << (bit % nbits);
|
|
|
|
*wordp = &bitmap[bit / nbits];
|
2012-11-29 05:03:54 +04:00
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
static MOZ_ALWAYS_INLINE JS::Zone*
|
2014-12-14 19:27:51 +03:00
|
|
|
GetGCThingZone(const uintptr_t addr)
|
2012-10-26 22:18:50 +04:00
|
|
|
{
|
2014-12-05 20:38:33 +03:00
|
|
|
MOZ_ASSERT(addr);
|
2014-12-14 19:27:51 +03:00
|
|
|
const uintptr_t zone_addr = (addr & ~ArenaMask) | ArenaZoneOffset;
|
2015-03-29 01:22:11 +03:00
|
|
|
return *reinterpret_cast<JS::Zone**>(zone_addr);
|
2012-10-26 22:18:50 +04:00
|
|
|
}
|
2012-10-26 22:17:24 +04:00
|
|
|
|
2014-12-05 20:38:34 +03:00
|
|
|
static MOZ_ALWAYS_INLINE bool
|
2017-04-25 16:07:55 +03:00
|
|
|
TenuredCellIsMarkedGray(const Cell* cell)
|
2014-12-05 20:38:34 +03:00
|
|
|
{
|
2016-12-15 00:59:43 +03:00
|
|
|
// Return true if GrayOrBlackBit is set and BlackBit is not set.
|
2014-12-05 20:38:34 +03:00
|
|
|
MOZ_ASSERT(cell);
|
2017-04-25 16:07:55 +03:00
|
|
|
MOZ_ASSERT(!js::gc::IsInsideNursery(cell));
|
2017-01-06 14:23:21 +03:00
|
|
|
|
2016-12-15 00:59:43 +03:00
|
|
|
uintptr_t* grayWord, grayMask;
|
|
|
|
js::gc::detail::GetGCThingMarkWordAndMask(uintptr_t(cell), js::gc::ColorBit::GrayOrBlackBit,
|
|
|
|
&grayWord, &grayMask);
|
|
|
|
if (!(*grayWord & grayMask))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
uintptr_t* blackWord, blackMask;
|
|
|
|
js::gc::detail::GetGCThingMarkWordAndMask(uintptr_t(cell), js::gc::ColorBit::BlackBit,
|
|
|
|
&blackWord, &blackMask);
|
|
|
|
return !(*blackWord & blackMask);
|
2014-12-05 20:38:34 +03:00
|
|
|
}
|
|
|
|
|
2017-04-25 16:07:55 +03:00
|
|
|
static MOZ_ALWAYS_INLINE bool
|
|
|
|
CellIsMarkedGray(const Cell* cell)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(cell);
|
|
|
|
if (js::gc::IsInsideNursery(cell))
|
|
|
|
return false;
|
|
|
|
return TenuredCellIsMarkedGray(cell);
|
|
|
|
}
|
|
|
|
|
2017-01-31 13:15:17 +03:00
|
|
|
extern JS_PUBLIC_API(bool)
|
|
|
|
CellIsMarkedGrayIfKnown(const Cell* cell);
|
2017-01-06 14:23:21 +03:00
|
|
|
|
2017-03-02 13:22:47 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
extern JS_PUBLIC_API(bool)
|
|
|
|
CellIsNotGray(const Cell* cell);
|
|
|
|
#endif
|
|
|
|
|
2017-09-19 14:31:31 +03:00
|
|
|
MOZ_ALWAYS_INLINE ChunkLocation
|
|
|
|
GetCellLocation(const void* cell)
|
|
|
|
{
|
|
|
|
uintptr_t addr = uintptr_t(cell);
|
|
|
|
addr &= ~js::gc::ChunkMask;
|
|
|
|
addr |= js::gc::ChunkLocationOffset;
|
|
|
|
return *reinterpret_cast<ChunkLocation*>(addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_INLINE bool
|
|
|
|
NurseryCellHasStoreBuffer(const void* cell)
|
|
|
|
{
|
|
|
|
uintptr_t addr = uintptr_t(cell);
|
|
|
|
addr &= ~js::gc::ChunkMask;
|
|
|
|
addr |= js::gc::ChunkStoreBufferOffset;
|
|
|
|
return *reinterpret_cast<void**>(addr) != nullptr;
|
|
|
|
}
|
|
|
|
|
2014-12-05 20:38:33 +03:00
|
|
|
} /* namespace detail */
|
|
|
|
|
2014-04-26 12:30:04 +04:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2015-03-29 01:22:11 +03:00
|
|
|
IsInsideNursery(const js::gc::Cell* cell)
|
2014-04-26 12:30:04 +04:00
|
|
|
{
|
|
|
|
if (!cell)
|
|
|
|
return false;
|
2017-09-19 14:31:31 +03:00
|
|
|
auto location = detail::GetCellLocation(cell);
|
2016-08-11 19:14:56 +03:00
|
|
|
MOZ_ASSERT(location == ChunkLocation::Nursery || location == ChunkLocation::TenuredHeap);
|
|
|
|
return location == ChunkLocation::Nursery;
|
2014-04-26 12:30:04 +04:00
|
|
|
}
|
|
|
|
|
2017-09-19 14:31:31 +03:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
|
|
|
IsCellPointerValid(const void* cell)
|
|
|
|
{
|
|
|
|
auto addr = uintptr_t(cell);
|
|
|
|
if (addr < ChunkSize || addr % CellAlignBytes != 0)
|
|
|
|
return false;
|
|
|
|
auto location = detail::GetCellLocation(cell);
|
|
|
|
if (location == ChunkLocation::TenuredHeap)
|
|
|
|
return !!detail::GetGCThingZone(addr);
|
|
|
|
if (location == ChunkLocation::Nursery)
|
|
|
|
return detail::NurseryCellHasStoreBuffer(cell);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-26 12:23:14 +03:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
|
|
|
IsCellPointerValidOrNull(const void* cell)
|
|
|
|
{
|
|
|
|
if (!cell)
|
|
|
|
return true;
|
|
|
|
return IsCellPointerValid(cell);
|
|
|
|
}
|
|
|
|
|
2012-11-29 05:03:54 +04:00
|
|
|
} /* namespace gc */
|
|
|
|
} /* namespace js */
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
static MOZ_ALWAYS_INLINE Zone*
|
2015-06-02 00:11:08 +03:00
|
|
|
GetTenuredGCThingZone(GCCellPtr thing)
|
2013-01-28 01:51:41 +04:00
|
|
|
{
|
2015-06-02 00:11:08 +03:00
|
|
|
MOZ_ASSERT(!js::gc::IsInsideNursery(thing.asCell()));
|
|
|
|
return js::gc::detail::GetGCThingZone(thing.unsafeAsUIntPtr());
|
|
|
|
}
|
|
|
|
|
|
|
|
static MOZ_ALWAYS_INLINE Zone*
|
|
|
|
GetStringZone(JSString* str)
|
|
|
|
{
|
|
|
|
return js::gc::detail::GetGCThingZone(uintptr_t(str));
|
2013-01-28 01:51:41 +04:00
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
extern JS_PUBLIC_API(Zone*)
|
|
|
|
GetObjectZone(JSObject* obj);
|
2013-01-28 01:51:41 +04:00
|
|
|
|
2017-07-11 19:17:17 +03:00
|
|
|
extern JS_PUBLIC_API(Zone*)
|
|
|
|
GetValueZone(const Value& value);
|
|
|
|
|
2014-12-10 03:22:51 +03:00
|
|
|
static MOZ_ALWAYS_INLINE bool
|
2014-12-05 20:38:34 +03:00
|
|
|
GCThingIsMarkedGray(GCCellPtr thing)
|
2014-12-10 03:22:51 +03:00
|
|
|
{
|
2015-05-07 12:14:40 +03:00
|
|
|
if (thing.mayBeOwnedByOtherRuntime())
|
|
|
|
return false;
|
2017-01-06 14:23:21 +03:00
|
|
|
return js::gc::detail::CellIsMarkedGrayIfKnown(thing.asCell());
|
2014-12-10 03:22:51 +03:00
|
|
|
}
|
|
|
|
|
2016-04-30 04:10:07 +03:00
|
|
|
extern JS_PUBLIC_API(JS::TraceKind)
|
|
|
|
GCThingTraceKind(void* thing);
|
|
|
|
|
2017-11-16 15:21:07 +03:00
|
|
|
/*
|
|
|
|
* Returns true when writes to GC thing pointers (and reads from weak pointers)
|
|
|
|
* must call an incremental barrier. This is generally only true when running
|
|
|
|
* mutator code in-between GC slices. At other times, the barrier may be elided
|
|
|
|
* for performance.
|
|
|
|
*/
|
|
|
|
extern JS_PUBLIC_API(bool)
|
|
|
|
IsIncrementalBarrierNeeded(JSContext* cx);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Notify the GC that a reference to a JSObject is about to be overwritten.
|
|
|
|
* This method must be called if IsIncrementalBarrierNeeded.
|
|
|
|
*/
|
|
|
|
extern JS_PUBLIC_API(void)
|
|
|
|
IncrementalPreWriteBarrier(JSObject* obj);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Notify the GC that a weak reference to a GC thing has been read.
|
|
|
|
* This method must be called if IsIncrementalBarrierNeeded.
|
|
|
|
*/
|
|
|
|
extern JS_PUBLIC_API(void)
|
|
|
|
IncrementalReadBarrier(GCCellPtr thing);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsets the gray bit for anything reachable from |thing|. |kind| should not be
|
|
|
|
* JS::TraceKind::Shape. |thing| should be non-null. The return value indicates
|
|
|
|
* if anything was unmarked.
|
|
|
|
*/
|
|
|
|
extern JS_FRIEND_API(bool)
|
|
|
|
UnmarkGrayGCThingRecursively(GCCellPtr thing);
|
|
|
|
|
|
|
|
} // namespace JS
|
2014-05-20 01:58:05 +04:00
|
|
|
|
|
|
|
namespace js {
|
|
|
|
namespace gc {
|
|
|
|
|
2014-01-25 08:14:56 +04:00
|
|
|
static MOZ_ALWAYS_INLINE bool
|
2017-02-02 22:12:43 +03:00
|
|
|
IsIncrementalBarrierNeededOnTenuredGCThing(const JS::GCCellPtr thing)
|
2012-11-29 05:03:54 +04:00
|
|
|
{
|
2014-07-29 21:47:43 +04:00
|
|
|
MOZ_ASSERT(thing);
|
2014-12-02 09:34:25 +03:00
|
|
|
MOZ_ASSERT(!js::gc::IsInsideNursery(thing.asCell()));
|
2016-11-04 20:32:36 +03:00
|
|
|
|
2017-02-02 22:12:43 +03:00
|
|
|
// TODO: I'd like to assert !CurrentThreadIsHeapBusy() here but this gets
|
|
|
|
// called while we are tracing the heap, e.g. during memory reporting
|
|
|
|
// (see bug 1313318).
|
|
|
|
MOZ_ASSERT(!JS::CurrentThreadIsHeapCollecting());
|
2016-11-04 20:32:36 +03:00
|
|
|
|
2015-06-02 00:11:08 +03:00
|
|
|
JS::Zone* zone = JS::GetTenuredGCThingZone(thing);
|
2014-05-20 01:58:05 +04:00
|
|
|
return JS::shadow::Zone::asShadowZone(zone)->needsIncrementalBarrier();
|
2012-11-29 05:03:54 +04:00
|
|
|
}
|
|
|
|
|
2017-11-16 15:21:07 +03:00
|
|
|
static MOZ_ALWAYS_INLINE void
|
|
|
|
ExposeGCThingToActiveJS(JS::GCCellPtr thing)
|
|
|
|
{
|
|
|
|
// GC things residing in the nursery cannot be gray: they have no mark bits.
|
|
|
|
// All live objects in the nursery are moved to tenured at the beginning of
|
|
|
|
// each GC slice, so the gray marker never sees nursery things.
|
|
|
|
if (IsInsideNursery(thing.asCell()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// There's nothing to do for permanent GC things that might be owned by
|
|
|
|
// another runtime.
|
|
|
|
if (thing.mayBeOwnedByOtherRuntime())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (IsIncrementalBarrierNeededOnTenuredGCThing(thing))
|
|
|
|
JS::IncrementalReadBarrier(thing);
|
|
|
|
else if (js::gc::detail::TenuredCellIsMarkedGray(thing.asCell()))
|
|
|
|
JS::UnmarkGrayGCThingRecursively(thing);
|
|
|
|
|
|
|
|
MOZ_ASSERT(!js::gc::detail::TenuredCellIsMarkedGray(thing.asCell()));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
extern JS_PUBLIC_API(bool)
|
|
|
|
EdgeNeedsSweepUnbarrieredSlow(T* thingp);
|
|
|
|
|
|
|
|
static MOZ_ALWAYS_INLINE bool
|
|
|
|
EdgeNeedsSweepUnbarriered(JSObject** objp)
|
|
|
|
{
|
|
|
|
// This function does not handle updating nursery pointers. Raw JSObject
|
|
|
|
// pointers should be updated separately or replaced with
|
|
|
|
// JS::Heap<JSObject*> which handles this automatically.
|
|
|
|
MOZ_ASSERT(!JS::CurrentThreadIsHeapMinorCollecting());
|
|
|
|
if (IsInsideNursery(reinterpret_cast<Cell*>(*objp)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto zone = JS::shadow::Zone::asShadowZone(detail::GetGCThingZone(uintptr_t(*objp)));
|
|
|
|
if (!zone->isGCSweepingOrCompacting())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return EdgeNeedsSweepUnbarrieredSlow(objp);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace gc
|
|
|
|
} // namesapce js
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This should be called when an object that is marked gray is exposed to the JS
|
|
|
|
* engine (by handing it to running JS code or writing it into live JS
|
|
|
|
* data). During incremental GC, since the gray bits haven't been computed yet,
|
|
|
|
* we conservatively mark the object black.
|
2015-01-27 02:32:54 +03:00
|
|
|
*/
|
2017-11-16 15:21:07 +03:00
|
|
|
static MOZ_ALWAYS_INLINE void
|
|
|
|
ExposeObjectToActiveJS(JSObject* obj)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(obj);
|
|
|
|
MOZ_ASSERT(!js::gc::EdgeNeedsSweepUnbarrieredSlow(&obj));
|
|
|
|
js::gc::ExposeGCThingToActiveJS(GCCellPtr(obj));
|
|
|
|
}
|
2015-01-27 02:32:54 +03:00
|
|
|
|
2017-11-16 15:21:07 +03:00
|
|
|
static MOZ_ALWAYS_INLINE void
|
|
|
|
ExposeScriptToActiveJS(JSScript* script)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!js::gc::EdgeNeedsSweepUnbarrieredSlow(&script));
|
|
|
|
js::gc::ExposeGCThingToActiveJS(GCCellPtr(script));
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace JS */
|
2012-10-26 22:17:24 +04:00
|
|
|
|
2013-06-20 04:59:09 +04:00
|
|
|
#endif /* js_HeapAPI_h */
|