2009-03-30 13:32:28 +00:00
|
|
|
// Copyright 2009 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/api.h"
|
|
|
|
#include "src/global-handles.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/vm-state-inl.h"
|
2010-12-07 11:31:57 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
|
2013-04-16 16:37:04 +00:00
|
|
|
ObjectGroup::~ObjectGroup() {
|
2013-04-24 15:59:23 +00:00
|
|
|
if (info != NULL) info->Dispose();
|
|
|
|
delete[] objects;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ImplicitRefGroup::~ImplicitRefGroup() {
|
|
|
|
delete[] children;
|
2013-04-16 16:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-06 15:23:04 +00:00
|
|
|
class GlobalHandles::Node {
|
2008-07-03 15:10:15 +00:00
|
|
|
public:
|
2011-06-06 15:23:04 +00:00
|
|
|
// State transition diagram:
|
|
|
|
// FREE -> NORMAL <-> WEAK -> PENDING -> NEAR_DEATH -> { NORMAL, WEAK, FREE }
|
|
|
|
enum State {
|
2013-01-15 10:26:48 +00:00
|
|
|
FREE = 0,
|
2014-12-18 16:09:03 +00:00
|
|
|
NORMAL, // Normal global handle.
|
|
|
|
WEAK, // Flagged as weak but not yet finalized.
|
|
|
|
PENDING, // Has been recognized as only reachable by weak handles.
|
|
|
|
NEAR_DEATH, // Callback has informed the handle is near death.
|
|
|
|
NUMBER_OF_NODE_STATES
|
2011-06-06 15:23:04 +00:00
|
|
|
};
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-06-06 15:23:04 +00:00
|
|
|
// Maps handle location (slot) to the containing node.
|
|
|
|
static Node* FromLocation(Object** location) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(OFFSET_OF(Node, object_) == 0);
|
2011-06-06 15:23:04 +00:00
|
|
|
return reinterpret_cast<Node*>(location);
|
2009-10-26 12:54:41 +00:00
|
|
|
}
|
|
|
|
|
2013-01-16 10:10:53 +00:00
|
|
|
Node() {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(OFFSET_OF(Node, class_id_) == Internals::kNodeClassIdOffset);
|
|
|
|
DCHECK(OFFSET_OF(Node, flags_) == Internals::kNodeFlagsOffset);
|
2013-01-25 08:31:46 +00:00
|
|
|
STATIC_ASSERT(static_cast<int>(NodeState::kMask) ==
|
|
|
|
Internals::kNodeStateMask);
|
|
|
|
STATIC_ASSERT(WEAK == Internals::kNodeStateIsWeakValue);
|
2013-08-05 06:58:48 +00:00
|
|
|
STATIC_ASSERT(PENDING == Internals::kNodeStateIsPendingValue);
|
2013-01-25 08:31:46 +00:00
|
|
|
STATIC_ASSERT(NEAR_DEATH == Internals::kNodeStateIsNearDeathValue);
|
|
|
|
STATIC_ASSERT(static_cast<int>(IsIndependent::kShift) ==
|
|
|
|
Internals::kNodeIsIndependentShift);
|
|
|
|
STATIC_ASSERT(static_cast<int>(IsPartiallyDependent::kShift) ==
|
|
|
|
Internals::kNodeIsPartiallyDependentShift);
|
2013-01-16 10:10:53 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-09-30 11:56:52 +00:00
|
|
|
#ifdef ENABLE_HANDLE_ZAPPING
|
2011-06-06 15:23:04 +00:00
|
|
|
~Node() {
|
|
|
|
// TODO(1428): if it's a weak handle we should have invoked its callback.
|
2008-07-03 15:10:15 +00:00
|
|
|
// Zap the values for eager trapping.
|
2013-02-18 10:20:58 +00:00
|
|
|
object_ = reinterpret_cast<Object*>(kGlobalHandleZapValue);
|
2011-06-06 15:23:04 +00:00
|
|
|
class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId;
|
|
|
|
index_ = 0;
|
2013-01-15 10:26:48 +00:00
|
|
|
set_independent(false);
|
|
|
|
set_partially_dependent(false);
|
|
|
|
set_in_new_space_list(false);
|
2008-07-03 15:10:15 +00:00
|
|
|
parameter_or_next_free_.next_free = NULL;
|
2013-09-09 09:25:23 +00:00
|
|
|
weak_callback_ = NULL;
|
2011-06-06 15:23:04 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
#endif
|
2011-06-06 15:23:04 +00:00
|
|
|
|
2013-08-14 12:40:44 +00:00
|
|
|
void Initialize(int index, Node** first_free) {
|
2011-06-06 15:23:04 +00:00
|
|
|
index_ = static_cast<uint8_t>(index);
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(static_cast<int>(index_) == index);
|
2013-01-15 10:26:48 +00:00
|
|
|
set_state(FREE);
|
|
|
|
set_in_new_space_list(false);
|
2013-08-14 12:40:44 +00:00
|
|
|
parameter_or_next_free_.next_free = *first_free;
|
|
|
|
*first_free = this;
|
2011-06-06 15:23:04 +00:00
|
|
|
}
|
|
|
|
|
2013-05-23 08:19:27 +00:00
|
|
|
void Acquire(Object* object) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(state() == FREE);
|
2011-06-06 15:23:04 +00:00
|
|
|
object_ = object;
|
|
|
|
class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId;
|
2013-01-15 10:26:48 +00:00
|
|
|
set_independent(false);
|
|
|
|
set_partially_dependent(false);
|
|
|
|
set_state(NORMAL);
|
2011-06-06 15:23:04 +00:00
|
|
|
parameter_or_next_free_.parameter = NULL;
|
2013-09-09 09:25:23 +00:00
|
|
|
weak_callback_ = NULL;
|
2013-08-14 12:40:44 +00:00
|
|
|
IncreaseBlockUses();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2014-12-18 16:09:03 +00:00
|
|
|
void Zap() {
|
|
|
|
DCHECK(IsInUse());
|
|
|
|
// Zap the values for eager trapping.
|
|
|
|
object_ = reinterpret_cast<Object*>(kGlobalHandleZapValue);
|
|
|
|
}
|
|
|
|
|
2013-05-23 08:19:27 +00:00
|
|
|
void Release() {
|
2014-12-18 16:09:03 +00:00
|
|
|
DCHECK(IsInUse());
|
2013-01-15 10:26:48 +00:00
|
|
|
set_state(FREE);
|
2013-03-04 18:24:37 +00:00
|
|
|
// Zap the values for eager trapping.
|
2013-03-21 14:18:16 +00:00
|
|
|
object_ = reinterpret_cast<Object*>(kGlobalHandleZapValue);
|
2013-02-15 12:18:24 +00:00
|
|
|
class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId;
|
|
|
|
set_independent(false);
|
|
|
|
set_partially_dependent(false);
|
2013-09-09 09:25:23 +00:00
|
|
|
weak_callback_ = NULL;
|
2013-08-14 12:40:44 +00:00
|
|
|
DecreaseBlockUses();
|
2011-06-06 15:23:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Object slot accessors.
|
|
|
|
Object* object() const { return object_; }
|
|
|
|
Object** location() { return &object_; }
|
|
|
|
Handle<Object> handle() { return Handle<Object>(location()); }
|
|
|
|
|
|
|
|
// Wrapper class ID accessors.
|
|
|
|
bool has_wrapper_class_id() const {
|
|
|
|
return class_id_ != v8::HeapProfiler::kPersistentHandleNoClassId;
|
|
|
|
}
|
2013-01-15 10:26:48 +00:00
|
|
|
|
2011-06-06 15:23:04 +00:00
|
|
|
uint16_t wrapper_class_id() const { return class_id_; }
|
2013-01-15 10:26:48 +00:00
|
|
|
|
|
|
|
// State and flag accessors.
|
|
|
|
|
|
|
|
State state() const {
|
|
|
|
return NodeState::decode(flags_);
|
|
|
|
}
|
|
|
|
void set_state(State state) {
|
|
|
|
flags_ = NodeState::update(flags_, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_independent() {
|
|
|
|
return IsIndependent::decode(flags_);
|
|
|
|
}
|
|
|
|
void set_independent(bool v) {
|
|
|
|
flags_ = IsIndependent::update(flags_, v);
|
2011-06-06 15:23:04 +00:00
|
|
|
}
|
|
|
|
|
2013-01-15 10:26:48 +00:00
|
|
|
bool is_partially_dependent() {
|
|
|
|
return IsPartiallyDependent::decode(flags_);
|
|
|
|
}
|
|
|
|
void set_partially_dependent(bool v) {
|
|
|
|
flags_ = IsPartiallyDependent::update(flags_, v);
|
|
|
|
}
|
2011-06-06 15:23:04 +00:00
|
|
|
|
2013-01-15 10:26:48 +00:00
|
|
|
bool is_in_new_space_list() {
|
|
|
|
return IsInNewSpaceList::decode(flags_);
|
|
|
|
}
|
|
|
|
void set_in_new_space_list(bool v) {
|
|
|
|
flags_ = IsInNewSpaceList::update(flags_, v);
|
|
|
|
}
|
2011-06-06 15:23:04 +00:00
|
|
|
|
2014-12-18 16:09:03 +00:00
|
|
|
WeaknessType weakness_type() const {
|
|
|
|
return NodeWeaknessType::decode(flags_);
|
2014-11-03 17:23:55 +00:00
|
|
|
}
|
2014-12-18 16:09:03 +00:00
|
|
|
void set_weakness_type(WeaknessType weakness_type) {
|
|
|
|
flags_ = NodeWeaknessType::update(flags_, weakness_type);
|
2014-11-03 17:23:55 +00:00
|
|
|
}
|
|
|
|
|
2011-06-06 15:23:04 +00:00
|
|
|
bool IsNearDeath() const {
|
|
|
|
// Check for PENDING to ensure correct answer when processing callbacks.
|
2013-01-15 10:26:48 +00:00
|
|
|
return state() == PENDING || state() == NEAR_DEATH;
|
2011-06-06 15:23:04 +00:00
|
|
|
}
|
|
|
|
|
2013-01-15 10:26:48 +00:00
|
|
|
bool IsWeak() const { return state() == WEAK; }
|
2011-06-06 15:23:04 +00:00
|
|
|
|
2014-12-18 16:09:03 +00:00
|
|
|
bool IsInUse() const { return state() != FREE; }
|
|
|
|
|
2013-01-15 10:26:48 +00:00
|
|
|
bool IsRetainer() const { return state() != FREE; }
|
2011-06-06 15:23:04 +00:00
|
|
|
|
2013-01-15 10:26:48 +00:00
|
|
|
bool IsStrongRetainer() const { return state() == NORMAL; }
|
2011-06-06 15:23:04 +00:00
|
|
|
|
|
|
|
bool IsWeakRetainer() const {
|
2013-01-15 10:26:48 +00:00
|
|
|
return state() == WEAK || state() == PENDING || state() == NEAR_DEATH;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2011-06-06 15:23:04 +00:00
|
|
|
void MarkPending() {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(state() == WEAK);
|
2013-01-15 10:26:48 +00:00
|
|
|
set_state(PENDING);
|
2011-06-06 15:23:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Independent flag accessors.
|
|
|
|
void MarkIndependent() {
|
2014-12-18 16:09:03 +00:00
|
|
|
DCHECK(IsInUse());
|
2013-01-15 10:26:48 +00:00
|
|
|
set_independent(true);
|
2011-06-06 15:23:04 +00:00
|
|
|
}
|
|
|
|
|
2013-05-29 08:32:45 +00:00
|
|
|
void MarkPartiallyDependent() {
|
2014-12-18 16:09:03 +00:00
|
|
|
DCHECK(IsInUse());
|
2013-05-29 08:32:45 +00:00
|
|
|
if (GetGlobalHandles()->isolate()->heap()->InNewSpace(object_)) {
|
2013-01-15 10:26:48 +00:00
|
|
|
set_partially_dependent(true);
|
2012-11-06 17:32:15 +00:00
|
|
|
}
|
|
|
|
}
|
2013-01-15 10:26:48 +00:00
|
|
|
void clear_partially_dependent() { set_partially_dependent(false); }
|
2011-06-06 15:23:04 +00:00
|
|
|
|
2013-08-14 12:40:44 +00:00
|
|
|
// Callback accessor.
|
|
|
|
// TODO(svenpanne) Re-enable or nuke later.
|
|
|
|
// WeakReferenceCallback callback() { return callback_; }
|
|
|
|
|
2011-06-06 15:23:04 +00:00
|
|
|
// Callback parameter accessors.
|
|
|
|
void set_parameter(void* parameter) {
|
2014-12-18 16:09:03 +00:00
|
|
|
DCHECK(IsInUse());
|
2011-06-06 15:23:04 +00:00
|
|
|
parameter_or_next_free_.parameter = parameter;
|
|
|
|
}
|
|
|
|
void* parameter() const {
|
2014-12-18 16:09:03 +00:00
|
|
|
DCHECK(IsInUse());
|
2011-06-06 15:23:04 +00:00
|
|
|
return parameter_or_next_free_.parameter;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Accessors for next free node in the free list.
|
|
|
|
Node* next_free() {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(state() == FREE);
|
2008-07-03 15:10:15 +00:00
|
|
|
return parameter_or_next_free_.next_free;
|
|
|
|
}
|
|
|
|
void set_next_free(Node* value) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(state() == FREE);
|
2008-07-03 15:10:15 +00:00
|
|
|
parameter_or_next_free_.next_free = value;
|
|
|
|
}
|
|
|
|
|
2014-12-18 16:09:03 +00:00
|
|
|
void MakeWeak(void* parameter, WeakCallback weak_callback) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(weak_callback != NULL);
|
2014-12-18 16:09:03 +00:00
|
|
|
DCHECK(IsInUse());
|
2014-05-27 12:26:44 +00:00
|
|
|
CHECK(object_ != NULL);
|
2013-01-15 10:26:48 +00:00
|
|
|
set_state(WEAK);
|
2014-12-18 16:09:03 +00:00
|
|
|
set_weakness_type(NORMAL_WEAK);
|
2008-07-03 15:10:15 +00:00
|
|
|
set_parameter(parameter);
|
2013-12-18 08:09:37 +00:00
|
|
|
weak_callback_ = weak_callback;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2015-03-09 21:47:54 +00:00
|
|
|
void MakeWeak(void* parameter,
|
|
|
|
WeakCallbackInfo<void>::Callback phantom_callback,
|
|
|
|
v8::WeakCallbackType type) {
|
|
|
|
DCHECK(phantom_callback != nullptr);
|
2014-12-18 16:09:03 +00:00
|
|
|
DCHECK(IsInUse());
|
2015-03-09 21:47:54 +00:00
|
|
|
CHECK(object_ != nullptr);
|
2014-12-18 16:09:03 +00:00
|
|
|
set_state(WEAK);
|
2015-03-09 21:47:54 +00:00
|
|
|
switch (type) {
|
|
|
|
case v8::WeakCallbackType::kParameter:
|
|
|
|
set_weakness_type(PHANTOM_WEAK);
|
|
|
|
break;
|
|
|
|
case v8::WeakCallbackType::kInternalFields:
|
2015-01-12 12:11:49 +00:00
|
|
|
set_weakness_type(PHANTOM_WEAK_2_INTERNAL_FIELDS);
|
2015-03-09 21:47:54 +00:00
|
|
|
break;
|
2014-12-18 16:09:03 +00:00
|
|
|
}
|
2015-01-12 12:11:49 +00:00
|
|
|
set_parameter(parameter);
|
2014-12-18 16:09:03 +00:00
|
|
|
weak_callback_ = reinterpret_cast<WeakCallback>(phantom_callback);
|
|
|
|
}
|
|
|
|
|
2014-03-13 07:10:59 +00:00
|
|
|
void* ClearWeakness() {
|
2014-12-18 16:09:03 +00:00
|
|
|
DCHECK(IsInUse());
|
2014-03-13 07:10:59 +00:00
|
|
|
void* p = parameter();
|
2013-01-15 10:26:48 +00:00
|
|
|
set_state(NORMAL);
|
2008-07-03 15:10:15 +00:00
|
|
|
set_parameter(NULL);
|
2014-03-13 07:10:59 +00:00
|
|
|
return p;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2014-12-18 16:09:03 +00:00
|
|
|
void CollectPhantomCallbackData(
|
2015-01-12 12:11:49 +00:00
|
|
|
Isolate* isolate,
|
|
|
|
List<PendingPhantomCallback>* pending_phantom_callbacks) {
|
|
|
|
if (state() != PENDING) return;
|
2014-12-18 16:09:03 +00:00
|
|
|
if (weak_callback_ != NULL) {
|
|
|
|
if (weakness_type() == NORMAL_WEAK) return;
|
|
|
|
|
|
|
|
v8::Isolate* api_isolate = reinterpret_cast<v8::Isolate*>(isolate);
|
|
|
|
|
2015-03-09 21:47:54 +00:00
|
|
|
DCHECK(weakness_type() == PHANTOM_WEAK ||
|
2015-01-12 12:11:49 +00:00
|
|
|
weakness_type() == PHANTOM_WEAK_2_INTERNAL_FIELDS);
|
2014-12-18 16:09:03 +00:00
|
|
|
|
2015-01-12 12:11:49 +00:00
|
|
|
Object* internal_field0 = nullptr;
|
|
|
|
Object* internal_field1 = nullptr;
|
2015-03-09 21:47:54 +00:00
|
|
|
if (weakness_type() != PHANTOM_WEAK) {
|
|
|
|
if (object()->IsJSObject()) {
|
|
|
|
JSObject* jsobject = JSObject::cast(object());
|
|
|
|
int field_count = jsobject->GetInternalFieldCount();
|
|
|
|
if (field_count > 0) {
|
|
|
|
internal_field0 = jsobject->GetInternalField(0);
|
|
|
|
if (!internal_field0->IsSmi()) internal_field0 = nullptr;
|
|
|
|
}
|
|
|
|
if (field_count > 1) {
|
|
|
|
internal_field1 = jsobject->GetInternalField(1);
|
|
|
|
if (!internal_field1->IsSmi()) internal_field1 = nullptr;
|
|
|
|
}
|
2015-01-12 12:11:49 +00:00
|
|
|
}
|
2014-12-18 16:09:03 +00:00
|
|
|
}
|
2015-01-12 12:11:49 +00:00
|
|
|
|
|
|
|
// Zap with harmless value.
|
|
|
|
*location() = Smi::FromInt(0);
|
2015-03-09 21:47:54 +00:00
|
|
|
typedef v8::WeakCallbackInfo<void> Data;
|
2015-01-12 12:11:49 +00:00
|
|
|
|
|
|
|
Data data(api_isolate, parameter(), internal_field0, internal_field1);
|
|
|
|
Data::Callback callback =
|
|
|
|
reinterpret_cast<Data::Callback>(weak_callback_);
|
|
|
|
|
|
|
|
pending_phantom_callbacks->Add(
|
|
|
|
PendingPhantomCallback(this, data, callback));
|
|
|
|
DCHECK(IsInUse());
|
|
|
|
set_state(NEAR_DEATH);
|
2014-12-18 16:09:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-23 08:19:27 +00:00
|
|
|
bool PostGarbageCollectionProcessing(Isolate* isolate) {
|
2015-01-12 12:11:49 +00:00
|
|
|
// Handles only weak handles (not phantom) that are dying.
|
2013-01-15 10:26:48 +00:00
|
|
|
if (state() != Node::PENDING) return false;
|
2013-09-09 09:25:23 +00:00
|
|
|
if (weak_callback_ == NULL) {
|
2013-05-23 08:19:27 +00:00
|
|
|
Release();
|
2010-07-19 13:26:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-01-15 10:26:48 +00:00
|
|
|
set_state(NEAR_DEATH);
|
2009-08-21 08:52:24 +00:00
|
|
|
|
2014-12-18 16:09:03 +00:00
|
|
|
// Check that we are not passing a finalized external string to
|
|
|
|
// the callback.
|
|
|
|
DCHECK(!object_->IsExternalOneByteString() ||
|
|
|
|
ExternalOneByteString::cast(object_)->resource() != NULL);
|
|
|
|
DCHECK(!object_->IsExternalTwoByteString() ||
|
|
|
|
ExternalTwoByteString::cast(object_)->resource() != NULL);
|
2015-01-12 12:11:49 +00:00
|
|
|
if (weakness_type() != NORMAL_WEAK) return false;
|
|
|
|
|
2014-12-18 16:09:03 +00:00
|
|
|
// Leaving V8.
|
|
|
|
VMState<EXTERNAL> vmstate(isolate);
|
|
|
|
HandleScope handle_scope(isolate);
|
2013-06-03 08:17:04 +00:00
|
|
|
Object** object = location();
|
2014-12-18 16:09:03 +00:00
|
|
|
Handle<Object> handle(*object, isolate);
|
|
|
|
v8::WeakCallbackData<v8::Value, void> data(
|
|
|
|
reinterpret_cast<v8::Isolate*>(isolate), parameter(),
|
|
|
|
v8::Utils::ToLocal(handle));
|
|
|
|
set_parameter(NULL);
|
|
|
|
weak_callback_(data);
|
|
|
|
|
2012-01-16 12:38:59 +00:00
|
|
|
// Absence of explicit cleanup or revival of weak handle
|
2010-07-19 13:26:25 +00:00
|
|
|
// in most of the cases would lead to memory leak.
|
2014-02-25 09:27:05 +00:00
|
|
|
CHECK(state() != NEAR_DEATH);
|
2009-08-21 08:52:24 +00:00
|
|
|
return true;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2013-09-09 09:25:23 +00:00
|
|
|
inline GlobalHandles* GetGlobalHandles();
|
|
|
|
|
2011-06-06 15:23:04 +00:00
|
|
|
private:
|
|
|
|
inline NodeBlock* FindBlock();
|
2013-08-14 12:40:44 +00:00
|
|
|
inline void IncreaseBlockUses();
|
|
|
|
inline void DecreaseBlockUses();
|
2011-06-06 15:23:04 +00:00
|
|
|
|
|
|
|
// Storage for object pointer.
|
|
|
|
// Placed first to avoid offset computation.
|
|
|
|
Object* object_;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-06-06 15:23:04 +00:00
|
|
|
// Next word stores class_id, index, state, and independent.
|
|
|
|
// Note: the most aligned fields should go first.
|
|
|
|
|
|
|
|
// Wrapper class ID.
|
2011-03-10 12:05:31 +00:00
|
|
|
uint16_t class_id_;
|
|
|
|
|
2011-06-06 15:23:04 +00:00
|
|
|
// Index in the containing handle block.
|
|
|
|
uint8_t index_;
|
|
|
|
|
2013-01-15 10:26:48 +00:00
|
|
|
// This stores three flags (independent, partially_dependent and
|
|
|
|
// in_new_space_list) and a State.
|
2014-12-18 16:09:03 +00:00
|
|
|
class NodeState : public BitField<State, 0, 3> {};
|
|
|
|
class IsIndependent : public BitField<bool, 3, 1> {};
|
|
|
|
class IsPartiallyDependent : public BitField<bool, 4, 1> {};
|
|
|
|
class IsInNewSpaceList : public BitField<bool, 5, 1> {};
|
|
|
|
class NodeWeaknessType : public BitField<WeaknessType, 6, 2> {};
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-01-15 10:26:48 +00:00
|
|
|
uint8_t flags_;
|
2011-05-17 12:18:19 +00:00
|
|
|
|
2013-03-19 10:06:20 +00:00
|
|
|
// Handle specific callback - might be a weak reference in disguise.
|
2013-09-09 09:25:23 +00:00
|
|
|
WeakCallback weak_callback_;
|
2011-06-06 15:23:04 +00:00
|
|
|
|
|
|
|
// Provided data for callback. In FREE state, this is used for
|
2008-07-03 15:10:15 +00:00
|
|
|
// the free list link.
|
|
|
|
union {
|
|
|
|
void* parameter;
|
|
|
|
Node* next_free;
|
|
|
|
} parameter_or_next_free_;
|
|
|
|
|
2011-06-06 15:23:04 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(Node);
|
|
|
|
};
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-08-14 12:40:44 +00:00
|
|
|
class GlobalHandles::NodeBlock {
|
2013-08-05 07:34:29 +00:00
|
|
|
public:
|
|
|
|
static const int kSize = 256;
|
|
|
|
|
2013-08-14 12:40:44 +00:00
|
|
|
explicit NodeBlock(GlobalHandles* global_handles, NodeBlock* next)
|
|
|
|
: next_(next),
|
|
|
|
used_nodes_(0),
|
|
|
|
next_used_(NULL),
|
|
|
|
prev_used_(NULL),
|
|
|
|
global_handles_(global_handles) {}
|
|
|
|
|
|
|
|
void PutNodesOnFreeList(Node** first_free) {
|
2011-06-06 15:23:04 +00:00
|
|
|
for (int i = kSize - 1; i >= 0; --i) {
|
|
|
|
nodes_[i].Initialize(i, first_free);
|
2009-10-26 12:54:41 +00:00
|
|
|
}
|
2011-06-06 15:23:04 +00:00
|
|
|
}
|
2009-10-26 12:54:41 +00:00
|
|
|
|
2013-08-14 12:40:44 +00:00
|
|
|
Node* node_at(int index) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(0 <= index && index < kSize);
|
2013-08-14 12:40:44 +00:00
|
|
|
return &nodes_[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
void IncreaseUses() {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(used_nodes_ < kSize);
|
2013-08-14 12:40:44 +00:00
|
|
|
if (used_nodes_++ == 0) {
|
|
|
|
NodeBlock* old_first = global_handles_->first_used_block_;
|
|
|
|
global_handles_->first_used_block_ = this;
|
|
|
|
next_used_ = old_first;
|
|
|
|
prev_used_ = NULL;
|
|
|
|
if (old_first == NULL) return;
|
|
|
|
old_first->prev_used_ = this;
|
2010-07-28 12:34:41 +00:00
|
|
|
}
|
2011-06-06 15:23:04 +00:00
|
|
|
}
|
2010-07-28 12:34:41 +00:00
|
|
|
|
2013-08-14 12:40:44 +00:00
|
|
|
void DecreaseUses() {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(used_nodes_ > 0);
|
2013-08-14 12:40:44 +00:00
|
|
|
if (--used_nodes_ == 0) {
|
|
|
|
if (next_used_ != NULL) next_used_->prev_used_ = prev_used_;
|
|
|
|
if (prev_used_ != NULL) prev_used_->next_used_ = next_used_;
|
|
|
|
if (this == global_handles_->first_used_block_) {
|
|
|
|
global_handles_->first_used_block_ = next_used_;
|
|
|
|
}
|
2009-10-26 12:54:41 +00:00
|
|
|
}
|
2011-06-06 15:23:04 +00:00
|
|
|
}
|
2009-10-26 12:54:41 +00:00
|
|
|
|
2013-05-23 08:19:27 +00:00
|
|
|
GlobalHandles* global_handles() { return global_handles_; }
|
|
|
|
|
2013-08-14 12:40:44 +00:00
|
|
|
// Next block in the list of all blocks.
|
|
|
|
NodeBlock* next() const { return next_; }
|
2009-10-26 12:54:41 +00:00
|
|
|
|
2013-08-14 12:40:44 +00:00
|
|
|
// Next/previous block in the list of blocks with used nodes.
|
|
|
|
NodeBlock* next_used() const { return next_used_; }
|
|
|
|
NodeBlock* prev_used() const { return prev_used_; }
|
2009-10-26 12:54:41 +00:00
|
|
|
|
2011-06-06 15:23:04 +00:00
|
|
|
private:
|
|
|
|
Node nodes_[kSize];
|
2013-08-14 12:40:44 +00:00
|
|
|
NodeBlock* const next_;
|
|
|
|
int used_nodes_;
|
|
|
|
NodeBlock* next_used_;
|
|
|
|
NodeBlock* prev_used_;
|
2013-05-23 08:19:27 +00:00
|
|
|
GlobalHandles* global_handles_;
|
2011-06-06 15:23:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-08-05 07:34:29 +00:00
|
|
|
GlobalHandles* GlobalHandles::Node::GetGlobalHandles() {
|
|
|
|
return FindBlock()->global_handles();
|
2011-06-06 15:23:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-05 07:34:29 +00:00
|
|
|
GlobalHandles::NodeBlock* GlobalHandles::Node::FindBlock() {
|
2013-08-14 12:40:44 +00:00
|
|
|
intptr_t ptr = reinterpret_cast<intptr_t>(this);
|
|
|
|
ptr = ptr - index_ * sizeof(Node);
|
|
|
|
NodeBlock* block = reinterpret_cast<NodeBlock*>(ptr);
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(block->node_at(index_) == this);
|
2013-08-14 12:40:44 +00:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GlobalHandles::Node::IncreaseBlockUses() {
|
|
|
|
NodeBlock* node_block = FindBlock();
|
|
|
|
node_block->IncreaseUses();
|
|
|
|
GlobalHandles* global_handles = node_block->global_handles();
|
|
|
|
global_handles->isolate()->counters()->global_handles()->Increment();
|
|
|
|
global_handles->number_of_global_handles_++;
|
2011-06-06 15:23:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-14 12:40:44 +00:00
|
|
|
void GlobalHandles::Node::DecreaseBlockUses() {
|
|
|
|
NodeBlock* node_block = FindBlock();
|
|
|
|
GlobalHandles* global_handles = node_block->global_handles();
|
|
|
|
parameter_or_next_free_.next_free = global_handles->first_free_;
|
|
|
|
global_handles->first_free_ = this;
|
|
|
|
node_block->DecreaseUses();
|
|
|
|
global_handles->isolate()->counters()->global_handles()->Decrement();
|
|
|
|
global_handles->number_of_global_handles_--;
|
2011-06-06 15:23:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class GlobalHandles::NodeIterator {
|
|
|
|
public:
|
|
|
|
explicit NodeIterator(GlobalHandles* global_handles)
|
2013-08-14 12:40:44 +00:00
|
|
|
: block_(global_handles->first_used_block_),
|
|
|
|
index_(0) {}
|
2011-06-06 15:23:04 +00:00
|
|
|
|
2013-08-14 12:40:44 +00:00
|
|
|
bool done() const { return block_ == NULL; }
|
2011-06-06 15:23:04 +00:00
|
|
|
|
|
|
|
Node* node() const {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!done());
|
2013-08-14 12:40:44 +00:00
|
|
|
return block_->node_at(index_);
|
2011-06-06 15:23:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Advance() {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!done());
|
2013-08-14 12:40:44 +00:00
|
|
|
if (++index_ < NodeBlock::kSize) return;
|
|
|
|
index_ = 0;
|
|
|
|
block_ = block_->next_used();
|
2011-06-06 15:23:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-08-14 12:40:44 +00:00
|
|
|
NodeBlock* block_;
|
|
|
|
int index_;
|
2011-06-06 15:23:04 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(NodeIterator);
|
2009-10-26 12:54:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
GlobalHandles::GlobalHandles(Isolate* isolate)
|
|
|
|
: isolate_(isolate),
|
2012-03-07 17:38:50 +00:00
|
|
|
number_of_global_handles_(0),
|
2013-08-14 12:40:44 +00:00
|
|
|
first_block_(NULL),
|
|
|
|
first_used_block_(NULL),
|
|
|
|
first_free_(NULL),
|
2013-04-24 15:59:23 +00:00
|
|
|
post_gc_processing_count_(0),
|
2013-08-14 12:40:44 +00:00
|
|
|
object_group_connections_(kObjectGroupConnectionsCapacity) {}
|
2011-03-18 20:35:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
GlobalHandles::~GlobalHandles() {
|
2013-08-14 12:40:44 +00:00
|
|
|
NodeBlock* block = first_block_;
|
|
|
|
while (block != NULL) {
|
|
|
|
NodeBlock* tmp = block->next();
|
|
|
|
delete block;
|
|
|
|
block = tmp;
|
2011-06-06 15:23:04 +00:00
|
|
|
}
|
2013-08-14 12:40:44 +00:00
|
|
|
first_block_ = NULL;
|
2011-03-18 20:35:07 +00:00
|
|
|
}
|
2009-10-26 12:54:41 +00:00
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<Object> GlobalHandles::Create(Object* value) {
|
2013-08-14 12:40:44 +00:00
|
|
|
if (first_free_ == NULL) {
|
|
|
|
first_block_ = new NodeBlock(this, first_block_);
|
|
|
|
first_block_->PutNodesOnFreeList(&first_free_);
|
|
|
|
}
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(first_free_ != NULL);
|
2013-08-14 12:40:44 +00:00
|
|
|
// Take the first node in the free list.
|
|
|
|
Node* result = first_free_;
|
|
|
|
first_free_ = result->next_free();
|
|
|
|
result->Acquire(value);
|
2011-06-06 15:23:04 +00:00
|
|
|
if (isolate_->heap()->InNewSpace(value) &&
|
|
|
|
!result->is_in_new_space_list()) {
|
|
|
|
new_space_nodes_.Add(result);
|
|
|
|
result->set_in_new_space_list(true);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
return result->handle();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-09 09:25:23 +00:00
|
|
|
Handle<Object> GlobalHandles::CopyGlobal(Object** location) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(location != NULL);
|
2013-09-09 09:25:23 +00:00
|
|
|
return Node::FromLocation(location)->GetGlobalHandles()->Create(*location);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
void GlobalHandles::Destroy(Object** location) {
|
2013-05-23 08:19:27 +00:00
|
|
|
if (location != NULL) Node::FromLocation(location)->Release();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-03 17:23:55 +00:00
|
|
|
void GlobalHandles::MakeWeak(Object** location, void* parameter,
|
2014-12-18 16:09:03 +00:00
|
|
|
WeakCallback weak_callback) {
|
|
|
|
Node::FromLocation(location)->MakeWeak(parameter, weak_callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-09 21:47:54 +00:00
|
|
|
typedef v8::WeakCallbackInfo<void>::Callback GenericCallback;
|
2014-12-18 16:09:03 +00:00
|
|
|
|
|
|
|
|
2015-03-09 21:47:54 +00:00
|
|
|
void GlobalHandles::MakeWeak(Object** location, void* parameter,
|
|
|
|
GenericCallback phantom_callback,
|
|
|
|
v8::WeakCallbackType type) {
|
|
|
|
Node::FromLocation(location)->MakeWeak(parameter, phantom_callback, type);
|
2014-12-18 16:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-30 08:22:52 +00:00
|
|
|
void GlobalHandles::CollectAllPhantomCallbackData() {
|
2014-12-18 16:09:03 +00:00
|
|
|
for (NodeIterator it(this); !it.done(); it.Advance()) {
|
|
|
|
Node* node = it.node();
|
2015-01-12 12:11:49 +00:00
|
|
|
node->CollectPhantomCallbackData(isolate(), &pending_phantom_callbacks_);
|
2014-12-18 16:09:03 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-30 08:22:52 +00:00
|
|
|
void GlobalHandles::CollectYoungPhantomCallbackData() {
|
|
|
|
for (int i = 0; i < new_space_nodes_.length(); ++i) {
|
|
|
|
Node* node = new_space_nodes_[i];
|
|
|
|
DCHECK(node->is_in_new_space_list());
|
|
|
|
node->CollectPhantomCallbackData(isolate(), &pending_phantom_callbacks_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-13 07:10:59 +00:00
|
|
|
void* GlobalHandles::ClearWeakness(Object** location) {
|
|
|
|
return Node::FromLocation(location)->ClearWeakness();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-17 12:18:19 +00:00
|
|
|
void GlobalHandles::MarkIndependent(Object** location) {
|
2011-06-06 15:23:04 +00:00
|
|
|
Node::FromLocation(location)->MarkIndependent();
|
2011-05-17 12:18:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-06 17:32:15 +00:00
|
|
|
void GlobalHandles::MarkPartiallyDependent(Object** location) {
|
2013-05-29 08:32:45 +00:00
|
|
|
Node::FromLocation(location)->MarkPartiallyDependent();
|
2012-11-06 17:32:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-18 06:52:37 +00:00
|
|
|
bool GlobalHandles::IsIndependent(Object** location) {
|
|
|
|
return Node::FromLocation(location)->is_independent();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
bool GlobalHandles::IsNearDeath(Object** location) {
|
|
|
|
return Node::FromLocation(location)->IsNearDeath();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool GlobalHandles::IsWeak(Object** location) {
|
|
|
|
return Node::FromLocation(location)->IsWeak();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GlobalHandles::IterateWeakRoots(ObjectVisitor* v) {
|
2011-06-06 15:23:04 +00:00
|
|
|
for (NodeIterator it(this); !it.done(); it.Advance()) {
|
2014-11-03 17:23:55 +00:00
|
|
|
Node* node = it.node();
|
|
|
|
if (node->IsWeakRetainer()) {
|
2015-01-12 12:11:49 +00:00
|
|
|
// Weakness type can be normal or phantom, with or without internal
|
|
|
|
// fields). For normal weakness we mark through the handle so that the
|
|
|
|
// object and things reachable from it are available to the callback.
|
|
|
|
//
|
|
|
|
// In the case of phantom with no internal fields, we can zap the object
|
|
|
|
// handle now and we won't need it, so we don't need to mark through it.
|
2014-12-18 16:09:03 +00:00
|
|
|
// In the internal fields case we will need the internal
|
2015-01-12 12:11:49 +00:00
|
|
|
// fields, so we can't zap the handle.
|
2014-12-18 16:09:03 +00:00
|
|
|
if (node->state() == Node::PENDING) {
|
2015-03-09 21:47:54 +00:00
|
|
|
if (node->weakness_type() == PHANTOM_WEAK) {
|
2014-12-18 16:09:03 +00:00
|
|
|
*(node->location()) = Smi::FromInt(0);
|
|
|
|
} else if (node->weakness_type() == NORMAL_WEAK) {
|
|
|
|
v->VisitPointer(node->location());
|
|
|
|
} else {
|
2015-03-09 21:47:54 +00:00
|
|
|
DCHECK(node->weakness_type() == PHANTOM_WEAK_2_INTERNAL_FIELDS);
|
2014-12-18 16:09:03 +00:00
|
|
|
}
|
2014-11-03 17:23:55 +00:00
|
|
|
} else {
|
2014-12-18 16:09:03 +00:00
|
|
|
// Node is not pending, so that means the object survived. We still
|
|
|
|
// need to visit the pointer in case the object moved, eg. because of
|
|
|
|
// compaction.
|
2014-11-03 17:23:55 +00:00
|
|
|
v->VisitPointer(node->location());
|
|
|
|
}
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-05 09:26:06 +00:00
|
|
|
void GlobalHandles::IdentifyWeakHandles(WeakSlotCallback f) {
|
2011-06-06 15:23:04 +00:00
|
|
|
for (NodeIterator it(this); !it.done(); it.Advance()) {
|
|
|
|
if (it.node()->IsWeak() && f(it.node()->location())) {
|
|
|
|
it.node()->MarkPending();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-06 16:18:59 +00:00
|
|
|
void GlobalHandles::IterateNewSpaceStrongAndDependentRoots(ObjectVisitor* v) {
|
|
|
|
for (int i = 0; i < new_space_nodes_.length(); ++i) {
|
|
|
|
Node* node = new_space_nodes_[i];
|
|
|
|
if (node->IsStrongRetainer() ||
|
2012-11-06 17:32:15 +00:00
|
|
|
(node->IsWeakRetainer() && !node->is_independent() &&
|
|
|
|
!node->is_partially_dependent())) {
|
|
|
|
v->VisitPointer(node->location());
|
2011-06-06 16:18:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-06 15:23:04 +00:00
|
|
|
void GlobalHandles::IdentifyNewSpaceWeakIndependentHandles(
|
|
|
|
WeakSlotCallbackWithHeap f) {
|
|
|
|
for (int i = 0; i < new_space_nodes_.length(); ++i) {
|
|
|
|
Node* node = new_space_nodes_[i];
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(node->is_in_new_space_list());
|
2012-11-06 17:32:15 +00:00
|
|
|
if ((node->is_independent() || node->is_partially_dependent()) &&
|
|
|
|
node->IsWeak() && f(isolate_->heap(), node->location())) {
|
2011-06-06 15:23:04 +00:00
|
|
|
node->MarkPending();
|
2011-05-17 12:18:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-06 16:18:59 +00:00
|
|
|
void GlobalHandles::IterateNewSpaceWeakIndependentRoots(ObjectVisitor* v) {
|
|
|
|
for (int i = 0; i < new_space_nodes_.length(); ++i) {
|
|
|
|
Node* node = new_space_nodes_[i];
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(node->is_in_new_space_list());
|
2012-11-06 17:32:15 +00:00
|
|
|
if ((node->is_independent() || node->is_partially_dependent()) &&
|
|
|
|
node->IsWeakRetainer()) {
|
2015-03-09 21:47:54 +00:00
|
|
|
if (node->weakness_type() == PHANTOM_WEAK) {
|
2014-12-18 16:09:03 +00:00
|
|
|
*(node->location()) = Smi::FromInt(0);
|
|
|
|
} else if (node->weakness_type() == NORMAL_WEAK) {
|
2014-11-03 17:23:55 +00:00
|
|
|
v->VisitPointer(node->location());
|
2014-12-18 16:09:03 +00:00
|
|
|
} else {
|
2015-03-09 21:47:54 +00:00
|
|
|
DCHECK(node->weakness_type() == PHANTOM_WEAK_2_INTERNAL_FIELDS);
|
2014-12-18 16:09:03 +00:00
|
|
|
// For this case we only need to trace if it's alive: The tracing of
|
|
|
|
// something that is already alive is just to get the pointer updated
|
|
|
|
// to the new location of the object).
|
|
|
|
DCHECK(node->state() != Node::NEAR_DEATH);
|
|
|
|
if (node->state() != Node::PENDING) {
|
|
|
|
v->VisitPointer(node->location());
|
|
|
|
}
|
2014-11-03 17:23:55 +00:00
|
|
|
}
|
2011-06-06 16:18:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-04 10:23:43 +00:00
|
|
|
bool GlobalHandles::IterateObjectGroups(ObjectVisitor* v,
|
|
|
|
WeakSlotCallbackWithHeap can_skip) {
|
2013-04-24 15:59:23 +00:00
|
|
|
ComputeObjectGroupsAndImplicitReferences();
|
2013-04-16 16:37:04 +00:00
|
|
|
int last = 0;
|
2012-12-04 10:23:43 +00:00
|
|
|
bool any_group_was_visited = false;
|
2013-04-16 16:37:04 +00:00
|
|
|
for (int i = 0; i < object_groups_.length(); i++) {
|
|
|
|
ObjectGroup* entry = object_groups_.at(i);
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(entry != NULL);
|
2013-04-16 16:37:04 +00:00
|
|
|
|
2013-04-24 15:59:23 +00:00
|
|
|
Object*** objects = entry->objects;
|
2013-04-16 16:37:04 +00:00
|
|
|
bool group_should_be_visited = false;
|
2013-04-24 15:59:23 +00:00
|
|
|
for (size_t j = 0; j < entry->length; j++) {
|
2013-04-16 16:37:04 +00:00
|
|
|
Object* object = *objects[j];
|
|
|
|
if (object->IsHeapObject()) {
|
|
|
|
if (!can_skip(isolate_->heap(), &object)) {
|
|
|
|
group_should_be_visited = true;
|
|
|
|
break;
|
2012-12-04 10:23:43 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-16 16:37:04 +00:00
|
|
|
}
|
2012-12-04 10:23:43 +00:00
|
|
|
|
2013-04-16 16:37:04 +00:00
|
|
|
if (!group_should_be_visited) {
|
|
|
|
object_groups_[last++] = entry;
|
|
|
|
continue;
|
|
|
|
}
|
2012-12-04 10:23:43 +00:00
|
|
|
|
2013-04-16 16:37:04 +00:00
|
|
|
// An object in the group requires visiting, so iterate over all
|
|
|
|
// objects in the group.
|
2013-04-24 15:59:23 +00:00
|
|
|
for (size_t j = 0; j < entry->length; ++j) {
|
2013-04-16 16:37:04 +00:00
|
|
|
Object* object = *objects[j];
|
|
|
|
if (object->IsHeapObject()) {
|
|
|
|
v->VisitPointer(&object);
|
|
|
|
any_group_was_visited = true;
|
2012-12-04 10:23:43 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-16 16:37:04 +00:00
|
|
|
|
|
|
|
// Once the entire group has been iterated over, set the object
|
|
|
|
// group to NULL so it won't be processed again.
|
2013-04-24 15:59:23 +00:00
|
|
|
delete entry;
|
2013-04-16 16:37:04 +00:00
|
|
|
object_groups_.at(i) = NULL;
|
2012-12-04 10:23:43 +00:00
|
|
|
}
|
2013-04-16 16:37:04 +00:00
|
|
|
object_groups_.Rewind(last);
|
2012-12-04 10:23:43 +00:00
|
|
|
return any_group_was_visited;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-18 16:09:03 +00:00
|
|
|
int GlobalHandles::PostScavengeProcessing(
|
|
|
|
const int initial_post_gc_processing_count) {
|
2014-06-25 09:36:45 +00:00
|
|
|
int freed_nodes = 0;
|
2014-12-18 16:09:03 +00:00
|
|
|
for (int i = 0; i < new_space_nodes_.length(); ++i) {
|
|
|
|
Node* node = new_space_nodes_[i];
|
|
|
|
DCHECK(node->is_in_new_space_list());
|
|
|
|
if (!node->IsRetainer()) {
|
|
|
|
// Free nodes do not have weak callbacks. Do not use them to compute
|
|
|
|
// the freed_nodes.
|
|
|
|
continue;
|
2009-08-21 08:52:24 +00:00
|
|
|
}
|
2014-12-18 16:09:03 +00:00
|
|
|
// Skip dependent handles. Their weak callbacks might expect to be
|
|
|
|
// called between two global garbage collection callbacks which
|
|
|
|
// are not called for minor collections.
|
|
|
|
if (!node->is_independent() && !node->is_partially_dependent()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
node->clear_partially_dependent();
|
|
|
|
if (node->PostGarbageCollectionProcessing(isolate_)) {
|
|
|
|
if (initial_post_gc_processing_count != post_gc_processing_count_) {
|
|
|
|
// Weak callback triggered another GC and another round of
|
|
|
|
// PostGarbageCollection processing. The current node might
|
|
|
|
// have been deleted in that round, so we need to bail out (or
|
|
|
|
// restart the processing).
|
|
|
|
return freed_nodes;
|
2009-10-26 12:54:41 +00:00
|
|
|
}
|
2014-12-18 16:09:03 +00:00
|
|
|
}
|
|
|
|
if (!node->IsRetainer()) {
|
|
|
|
freed_nodes++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return freed_nodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int GlobalHandles::PostMarkSweepProcessing(
|
|
|
|
const int initial_post_gc_processing_count) {
|
|
|
|
int freed_nodes = 0;
|
|
|
|
for (NodeIterator it(this); !it.done(); it.Advance()) {
|
|
|
|
if (!it.node()->IsRetainer()) {
|
|
|
|
// Free nodes do not have weak callbacks. Do not use them to compute
|
|
|
|
// the freed_nodes.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
it.node()->clear_partially_dependent();
|
|
|
|
if (it.node()->PostGarbageCollectionProcessing(isolate_)) {
|
|
|
|
if (initial_post_gc_processing_count != post_gc_processing_count_) {
|
|
|
|
// See the comment above.
|
|
|
|
return freed_nodes;
|
2013-08-14 12:40:44 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2014-12-18 16:09:03 +00:00
|
|
|
if (!it.node()->IsRetainer()) {
|
|
|
|
freed_nodes++;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2014-12-18 16:09:03 +00:00
|
|
|
return freed_nodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GlobalHandles::UpdateListOfNewSpaceNodes() {
|
2011-06-06 15:23:04 +00:00
|
|
|
int last = 0;
|
|
|
|
for (int i = 0; i < new_space_nodes_.length(); ++i) {
|
|
|
|
Node* node = new_space_nodes_[i];
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(node->is_in_new_space_list());
|
2012-12-07 09:44:10 +00:00
|
|
|
if (node->IsRetainer()) {
|
|
|
|
if (isolate_->heap()->InNewSpace(node->object())) {
|
|
|
|
new_space_nodes_[last++] = node;
|
2014-07-17 13:00:46 +00:00
|
|
|
isolate_->heap()->IncrementNodesCopiedInNewSpace();
|
2012-12-07 09:44:10 +00:00
|
|
|
} else {
|
|
|
|
node->set_in_new_space_list(false);
|
2014-07-17 13:00:46 +00:00
|
|
|
isolate_->heap()->IncrementNodesPromoted();
|
2012-12-07 09:44:10 +00:00
|
|
|
}
|
2011-06-06 15:23:04 +00:00
|
|
|
} else {
|
|
|
|
node->set_in_new_space_list(false);
|
2014-07-17 13:00:46 +00:00
|
|
|
isolate_->heap()->IncrementNodesDiedInNewSpace();
|
2011-06-06 15:23:04 +00:00
|
|
|
}
|
2009-10-26 12:54:41 +00:00
|
|
|
}
|
2011-06-06 15:23:04 +00:00
|
|
|
new_space_nodes_.Rewind(last);
|
2014-12-18 16:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int GlobalHandles::DispatchPendingPhantomCallbacks() {
|
|
|
|
int freed_nodes = 0;
|
|
|
|
while (pending_phantom_callbacks_.length() != 0) {
|
|
|
|
PendingPhantomCallback callback = pending_phantom_callbacks_.RemoveLast();
|
2015-01-12 12:11:49 +00:00
|
|
|
DCHECK(callback.node()->IsInUse());
|
2014-12-18 16:09:03 +00:00
|
|
|
callback.invoke();
|
2015-01-12 12:11:49 +00:00
|
|
|
DCHECK(!callback.node()->IsInUse());
|
2014-12-18 16:09:03 +00:00
|
|
|
freed_nodes++;
|
|
|
|
}
|
|
|
|
return freed_nodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int GlobalHandles::PostGarbageCollectionProcessing(GarbageCollector collector) {
|
|
|
|
// Process weak global handle callbacks. This must be done after the
|
|
|
|
// GC is completely done, because the callbacks may invoke arbitrary
|
|
|
|
// API functions.
|
|
|
|
DCHECK(isolate_->heap()->gc_state() == Heap::NOT_IN_GC);
|
|
|
|
const int initial_post_gc_processing_count = ++post_gc_processing_count_;
|
|
|
|
int freed_nodes = 0;
|
|
|
|
if (collector == SCAVENGER) {
|
|
|
|
freed_nodes = PostScavengeProcessing(initial_post_gc_processing_count);
|
|
|
|
} else {
|
|
|
|
freed_nodes = PostMarkSweepProcessing(initial_post_gc_processing_count);
|
|
|
|
}
|
|
|
|
if (initial_post_gc_processing_count != post_gc_processing_count_) {
|
|
|
|
// If the callbacks caused a nested GC, then return. See comment in
|
|
|
|
// PostScavengeProcessing.
|
|
|
|
return freed_nodes;
|
|
|
|
}
|
|
|
|
freed_nodes += DispatchPendingPhantomCallbacks();
|
|
|
|
if (initial_post_gc_processing_count == post_gc_processing_count_) {
|
|
|
|
UpdateListOfNewSpaceNodes();
|
|
|
|
}
|
2014-06-25 09:36:45 +00:00
|
|
|
return freed_nodes;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-18 16:09:03 +00:00
|
|
|
void GlobalHandles::PendingPhantomCallback::invoke() {
|
|
|
|
if (node_->state() == Node::FREE) return;
|
|
|
|
DCHECK(node_->state() == Node::NEAR_DEATH);
|
|
|
|
callback_(data_);
|
|
|
|
if (node_->state() != Node::FREE) node_->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-05 15:12:36 +00:00
|
|
|
void GlobalHandles::IterateStrongRoots(ObjectVisitor* v) {
|
2011-06-06 15:23:04 +00:00
|
|
|
for (NodeIterator it(this); !it.done(); it.Advance()) {
|
|
|
|
if (it.node()->IsStrongRetainer()) {
|
|
|
|
v->VisitPointer(it.node()->location());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-30 10:23:12 +00:00
|
|
|
|
2009-11-05 15:12:36 +00:00
|
|
|
void GlobalHandles::IterateAllRoots(ObjectVisitor* v) {
|
2011-06-06 15:23:04 +00:00
|
|
|
for (NodeIterator it(this); !it.done(); it.Advance()) {
|
|
|
|
if (it.node()->IsRetainer()) {
|
|
|
|
v->VisitPointer(it.node()->location());
|
2009-11-05 15:12:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-10 12:05:31 +00:00
|
|
|
void GlobalHandles::IterateAllRootsWithClassIds(ObjectVisitor* v) {
|
2011-06-06 15:23:04 +00:00
|
|
|
for (NodeIterator it(this); !it.done(); it.Advance()) {
|
2012-11-08 14:40:55 +00:00
|
|
|
if (it.node()->IsRetainer() && it.node()->has_wrapper_class_id()) {
|
2011-06-06 15:23:04 +00:00
|
|
|
v->VisitEmbedderReference(it.node()->location(),
|
|
|
|
it.node()->wrapper_class_id());
|
2011-03-10 12:05:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-24 15:53:16 +00:00
|
|
|
void GlobalHandles::IterateAllRootsInNewSpaceWithClassIds(ObjectVisitor* v) {
|
|
|
|
for (int i = 0; i < new_space_nodes_.length(); ++i) {
|
|
|
|
Node* node = new_space_nodes_[i];
|
|
|
|
if (node->IsRetainer() && node->has_wrapper_class_id()) {
|
|
|
|
v->VisitEmbedderReference(node->location(),
|
|
|
|
node->wrapper_class_id());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-21 12:15:31 +00:00
|
|
|
int GlobalHandles::NumberOfWeakHandles() {
|
|
|
|
int count = 0;
|
|
|
|
for (NodeIterator it(this); !it.done(); it.Advance()) {
|
|
|
|
if (it.node()->IsWeakRetainer()) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int GlobalHandles::NumberOfGlobalObjectWeakHandles() {
|
|
|
|
int count = 0;
|
|
|
|
for (NodeIterator it(this); !it.done(); it.Advance()) {
|
|
|
|
if (it.node()->IsWeakRetainer() &&
|
|
|
|
it.node()->object()->IsJSGlobalObject()) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-03 10:16:37 +00:00
|
|
|
void GlobalHandles::RecordStats(HeapStats* stats) {
|
2013-08-14 12:40:44 +00:00
|
|
|
*stats->global_handle_count = 0;
|
|
|
|
*stats->weak_global_handle_count = 0;
|
|
|
|
*stats->pending_global_handle_count = 0;
|
|
|
|
*stats->near_death_global_handle_count = 0;
|
|
|
|
*stats->free_global_handle_count = 0;
|
|
|
|
for (NodeIterator it(this); !it.done(); it.Advance()) {
|
|
|
|
*stats->global_handle_count += 1;
|
|
|
|
if (it.node()->state() == Node::WEAK) {
|
|
|
|
*stats->weak_global_handle_count += 1;
|
|
|
|
} else if (it.node()->state() == Node::PENDING) {
|
|
|
|
*stats->pending_global_handle_count += 1;
|
|
|
|
} else if (it.node()->state() == Node::NEAR_DEATH) {
|
|
|
|
*stats->near_death_global_handle_count += 1;
|
|
|
|
} else if (it.node()->state() == Node::FREE) {
|
|
|
|
*stats->free_global_handle_count += 1;
|
|
|
|
}
|
|
|
|
}
|
2009-12-03 10:16:37 +00:00
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
|
|
|
void GlobalHandles::PrintStats() {
|
2013-08-14 12:40:44 +00:00
|
|
|
int total = 0;
|
|
|
|
int weak = 0;
|
|
|
|
int pending = 0;
|
|
|
|
int near_death = 0;
|
|
|
|
int destroyed = 0;
|
|
|
|
|
|
|
|
for (NodeIterator it(this); !it.done(); it.Advance()) {
|
|
|
|
total++;
|
|
|
|
if (it.node()->state() == Node::WEAK) weak++;
|
|
|
|
if (it.node()->state() == Node::PENDING) pending++;
|
|
|
|
if (it.node()->state() == Node::NEAR_DEATH) near_death++;
|
|
|
|
if (it.node()->state() == Node::FREE) destroyed++;
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintF("Global Handle Statistics:\n");
|
2013-08-14 12:40:44 +00:00
|
|
|
PrintF(" allocated memory = %" V8_PTR_PREFIX "dB\n", sizeof(Node) * total);
|
|
|
|
PrintF(" # weak = %d\n", weak);
|
|
|
|
PrintF(" # pending = %d\n", pending);
|
|
|
|
PrintF(" # near_death = %d\n", near_death);
|
|
|
|
PrintF(" # free = %d\n", destroyed);
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintF(" # total = %d\n", total);
|
|
|
|
}
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
void GlobalHandles::Print() {
|
|
|
|
PrintF("Global handles:\n");
|
2011-06-06 15:23:04 +00:00
|
|
|
for (NodeIterator it(this); !it.done(); it.Advance()) {
|
|
|
|
PrintF(" handle %p to %p%s\n",
|
|
|
|
reinterpret_cast<void*>(it.node()->location()),
|
|
|
|
reinterpret_cast<void*>(it.node()->object()),
|
|
|
|
it.node()->IsWeak() ? " (weak)" : "");
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2011-03-16 12:02:28 +00:00
|
|
|
|
|
|
|
void GlobalHandles::AddObjectGroup(Object*** handles,
|
|
|
|
size_t length,
|
|
|
|
v8::RetainedObjectInfo* info) {
|
2011-05-18 15:28:43 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
for (size_t i = 0; i < length; ++i) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!Node::FromLocation(handles[i])->is_independent());
|
2011-05-18 15:28:43 +00:00
|
|
|
}
|
|
|
|
#endif
|
2011-04-06 19:17:54 +00:00
|
|
|
if (length == 0) {
|
|
|
|
if (info != NULL) info->Dispose();
|
|
|
|
return;
|
2011-03-16 12:02:28 +00:00
|
|
|
}
|
2013-04-24 15:59:23 +00:00
|
|
|
ObjectGroup* group = new ObjectGroup(length);
|
|
|
|
for (size_t i = 0; i < length; ++i)
|
|
|
|
group->objects[i] = handles[i];
|
|
|
|
group->info = info;
|
|
|
|
object_groups_.Add(group);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GlobalHandles::SetObjectGroupId(Object** handle,
|
|
|
|
UniqueId id) {
|
|
|
|
object_group_connections_.Add(ObjectGroupConnection(id, handle));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GlobalHandles::SetRetainedObjectInfo(UniqueId id,
|
|
|
|
RetainedObjectInfo* info) {
|
|
|
|
retainer_infos_.Add(ObjectGroupRetainerInfo(id, info));
|
2013-04-10 14:53:03 +00:00
|
|
|
}
|
|
|
|
|
2013-04-16 12:57:01 +00:00
|
|
|
|
2013-04-24 15:59:23 +00:00
|
|
|
void GlobalHandles::SetReferenceFromGroup(UniqueId id, Object** child) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!Node::FromLocation(child)->is_independent());
|
2013-04-24 15:59:23 +00:00
|
|
|
implicit_ref_connections_.Add(ObjectGroupConnection(id, child));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GlobalHandles::SetReference(HeapObject** parent, Object** child) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!Node::FromLocation(child)->is_independent());
|
2013-04-24 15:59:23 +00:00
|
|
|
ImplicitRefGroup* group = new ImplicitRefGroup(parent, 1);
|
|
|
|
group->children[0] = child;
|
|
|
|
implicit_ref_groups_.Add(group);
|
2011-03-16 12:02:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
void GlobalHandles::RemoveObjectGroups() {
|
2013-04-24 15:59:23 +00:00
|
|
|
for (int i = 0; i < object_groups_.length(); i++)
|
|
|
|
delete object_groups_.at(i);
|
2013-04-16 16:37:04 +00:00
|
|
|
object_groups_.Clear();
|
2013-04-24 15:59:23 +00:00
|
|
|
for (int i = 0; i < retainer_infos_.length(); ++i)
|
|
|
|
retainer_infos_[i].info->Dispose();
|
|
|
|
retainer_infos_.Clear();
|
|
|
|
object_group_connections_.Clear();
|
|
|
|
object_group_connections_.Initialize(kObjectGroupConnectionsCapacity);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2011-03-16 12:02:28 +00:00
|
|
|
|
|
|
|
void GlobalHandles::RemoveImplicitRefGroups() {
|
2013-04-16 16:37:04 +00:00
|
|
|
for (int i = 0; i < implicit_ref_groups_.length(); i++) {
|
2013-04-24 15:59:23 +00:00
|
|
|
delete implicit_ref_groups_.at(i);
|
2013-04-16 16:37:04 +00:00
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
implicit_ref_groups_.Clear();
|
2013-04-24 15:59:23 +00:00
|
|
|
implicit_ref_connections_.Clear();
|
2011-03-16 12:02:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-06 15:23:04 +00:00
|
|
|
void GlobalHandles::TearDown() {
|
|
|
|
// TODO(1428): invoke weak callbacks.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-24 15:59:23 +00:00
|
|
|
void GlobalHandles::ComputeObjectGroupsAndImplicitReferences() {
|
|
|
|
if (object_group_connections_.length() == 0) {
|
|
|
|
for (int i = 0; i < retainer_infos_.length(); ++i)
|
|
|
|
retainer_infos_[i].info->Dispose();
|
|
|
|
retainer_infos_.Clear();
|
|
|
|
implicit_ref_connections_.Clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
object_group_connections_.Sort();
|
|
|
|
retainer_infos_.Sort();
|
|
|
|
implicit_ref_connections_.Sort();
|
|
|
|
|
|
|
|
int info_index = 0; // For iterating retainer_infos_.
|
|
|
|
UniqueId current_group_id(0);
|
|
|
|
int current_group_start = 0;
|
|
|
|
|
|
|
|
int current_implicit_refs_start = 0;
|
|
|
|
int current_implicit_refs_end = 0;
|
|
|
|
for (int i = 0; i <= object_group_connections_.length(); ++i) {
|
|
|
|
if (i == 0)
|
|
|
|
current_group_id = object_group_connections_[i].id;
|
|
|
|
if (i == object_group_connections_.length() ||
|
|
|
|
current_group_id != object_group_connections_[i].id) {
|
|
|
|
// Group detected: objects in indices [current_group_start, i[.
|
|
|
|
|
|
|
|
// Find out which implicit references are related to this group. (We want
|
|
|
|
// to ignore object groups which only have 1 object, but that object is
|
|
|
|
// needed as a representative object for the implicit refrerence group.)
|
|
|
|
while (current_implicit_refs_start < implicit_ref_connections_.length() &&
|
|
|
|
implicit_ref_connections_[current_implicit_refs_start].id <
|
|
|
|
current_group_id)
|
|
|
|
++current_implicit_refs_start;
|
|
|
|
current_implicit_refs_end = current_implicit_refs_start;
|
|
|
|
while (current_implicit_refs_end < implicit_ref_connections_.length() &&
|
|
|
|
implicit_ref_connections_[current_implicit_refs_end].id ==
|
|
|
|
current_group_id)
|
|
|
|
++current_implicit_refs_end;
|
|
|
|
|
|
|
|
if (current_implicit_refs_end > current_implicit_refs_start) {
|
|
|
|
// Find a representative object for the implicit references.
|
|
|
|
HeapObject** representative = NULL;
|
|
|
|
for (int j = current_group_start; j < i; ++j) {
|
|
|
|
Object** object = object_group_connections_[j].object;
|
|
|
|
if ((*object)->IsHeapObject()) {
|
|
|
|
representative = reinterpret_cast<HeapObject**>(object);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (representative) {
|
|
|
|
ImplicitRefGroup* group = new ImplicitRefGroup(
|
|
|
|
representative,
|
|
|
|
current_implicit_refs_end - current_implicit_refs_start);
|
|
|
|
for (int j = current_implicit_refs_start;
|
|
|
|
j < current_implicit_refs_end;
|
|
|
|
++j) {
|
|
|
|
group->children[j - current_implicit_refs_start] =
|
|
|
|
implicit_ref_connections_[j].object;
|
|
|
|
}
|
|
|
|
implicit_ref_groups_.Add(group);
|
|
|
|
}
|
|
|
|
current_implicit_refs_start = current_implicit_refs_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find a RetainedObjectInfo for the group.
|
|
|
|
RetainedObjectInfo* info = NULL;
|
|
|
|
while (info_index < retainer_infos_.length() &&
|
|
|
|
retainer_infos_[info_index].id < current_group_id) {
|
|
|
|
retainer_infos_[info_index].info->Dispose();
|
|
|
|
++info_index;
|
|
|
|
}
|
|
|
|
if (info_index < retainer_infos_.length() &&
|
|
|
|
retainer_infos_[info_index].id == current_group_id) {
|
|
|
|
// This object group has an associated ObjectGroupRetainerInfo.
|
|
|
|
info = retainer_infos_[info_index].info;
|
|
|
|
++info_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore groups which only contain one object.
|
|
|
|
if (i > current_group_start + 1) {
|
|
|
|
ObjectGroup* group = new ObjectGroup(i - current_group_start);
|
|
|
|
for (int j = current_group_start; j < i; ++j) {
|
|
|
|
group->objects[j - current_group_start] =
|
|
|
|
object_group_connections_[j].object;
|
|
|
|
}
|
|
|
|
group->info = info;
|
|
|
|
object_groups_.Add(group);
|
|
|
|
} else if (info) {
|
|
|
|
info->Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i < object_group_connections_.length()) {
|
|
|
|
current_group_id = object_group_connections_[i].id;
|
|
|
|
current_group_start = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
object_group_connections_.Clear();
|
|
|
|
object_group_connections_.Initialize(kObjectGroupConnectionsCapacity);
|
|
|
|
retainer_infos_.Clear();
|
|
|
|
implicit_ref_connections_.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-05 09:46:23 +00:00
|
|
|
EternalHandles::EternalHandles() : size_(0) {
|
2014-08-26 09:19:24 +00:00
|
|
|
for (unsigned i = 0; i < arraysize(singleton_handles_); i++) {
|
2013-08-05 09:46:23 +00:00
|
|
|
singleton_handles_[i] = kInvalidIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EternalHandles::~EternalHandles() {
|
2013-08-06 06:34:54 +00:00
|
|
|
for (int i = 0; i < blocks_.length(); i++) delete[] blocks_[i];
|
2013-08-05 09:46:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void EternalHandles::IterateAllRoots(ObjectVisitor* visitor) {
|
|
|
|
int limit = size_;
|
|
|
|
for (int i = 0; i < blocks_.length(); i++) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(limit > 0);
|
2013-08-05 09:46:23 +00:00
|
|
|
Object** block = blocks_[i];
|
|
|
|
visitor->VisitPointers(block, block + Min(limit, kSize));
|
|
|
|
limit -= kSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void EternalHandles::IterateNewSpaceRoots(ObjectVisitor* visitor) {
|
|
|
|
for (int i = 0; i < new_space_indices_.length(); i++) {
|
|
|
|
visitor->VisitPointer(GetLocation(new_space_indices_[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void EternalHandles::PostGarbageCollectionProcessing(Heap* heap) {
|
|
|
|
int last = 0;
|
|
|
|
for (int i = 0; i < new_space_indices_.length(); i++) {
|
|
|
|
int index = new_space_indices_[i];
|
|
|
|
if (heap->InNewSpace(*GetLocation(index))) {
|
|
|
|
new_space_indices_[last++] = index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new_space_indices_.Rewind(last);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-26 09:41:22 +00:00
|
|
|
void EternalHandles::Create(Isolate* isolate, Object* object, int* index) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK_EQ(kInvalidIndex, *index);
|
2013-08-26 09:41:22 +00:00
|
|
|
if (object == NULL) return;
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK_NE(isolate->heap()->the_hole_value(), object);
|
2013-08-05 09:46:23 +00:00
|
|
|
int block = size_ >> kShift;
|
|
|
|
int offset = size_ & kMask;
|
|
|
|
// need to resize
|
|
|
|
if (offset == 0) {
|
|
|
|
Object** next_block = new Object*[kSize];
|
|
|
|
Object* the_hole = isolate->heap()->the_hole_value();
|
|
|
|
MemsetPointer(next_block, the_hole, kSize);
|
|
|
|
blocks_.Add(next_block);
|
|
|
|
}
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK_EQ(isolate->heap()->the_hole_value(), blocks_[block][offset]);
|
2013-08-05 09:46:23 +00:00
|
|
|
blocks_[block][offset] = object;
|
|
|
|
if (isolate->heap()->InNewSpace(object)) {
|
|
|
|
new_space_indices_.Add(size_);
|
|
|
|
}
|
2013-08-26 09:41:22 +00:00
|
|
|
*index = size_++;
|
2013-08-05 09:46:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
} } // namespace v8::internal
|