2009-03-30 13:32:28 +00:00
|
|
|
// Copyright 2009 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
|
|
|
#include "api.h"
|
|
|
|
#include "global-handles.h"
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
#include "vm-state-inl.h"
|
|
|
|
|
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,
|
2011-06-06 15:23:04 +00:00
|
|
|
NORMAL, // Normal global handle.
|
|
|
|
WEAK, // Flagged as weak but not yet finalized.
|
|
|
|
PENDING, // Has been recognized as only reachable by weak handles.
|
2013-08-14 12:40:44 +00:00
|
|
|
NEAR_DEATH // Callback has informed the handle is near death.
|
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) {
|
|
|
|
ASSERT(OFFSET_OF(Node, object_) == 0);
|
|
|
|
return reinterpret_cast<Node*>(location);
|
2009-10-26 12:54:41 +00:00
|
|
|
}
|
|
|
|
|
2013-01-16 10:10:53 +00:00
|
|
|
Node() {
|
2013-01-25 08:54:11 +00:00
|
|
|
ASSERT(OFFSET_OF(Node, class_id_) == Internals::kNodeClassIdOffset);
|
|
|
|
ASSERT(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);
|
|
|
|
ASSERT(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) {
|
2013-01-15 10:26:48 +00:00
|
|
|
ASSERT(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
|
|
|
}
|
|
|
|
|
2013-05-23 08:19:27 +00:00
|
|
|
void Release() {
|
2013-01-15 10:26:48 +00:00
|
|
|
ASSERT(state() != FREE);
|
|
|
|
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
|
|
|
|
2013-09-09 09:25:23 +00:00
|
|
|
bool is_revivable_callback() {
|
|
|
|
return IsRevivableCallback::decode(flags_);
|
|
|
|
}
|
|
|
|
void set_revivable_callback(bool v) {
|
|
|
|
flags_ = IsRevivableCallback::update(flags_, v);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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() {
|
2013-01-15 10:26:48 +00:00
|
|
|
ASSERT(state() == WEAK);
|
|
|
|
set_state(PENDING);
|
2011-06-06 15:23:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Independent flag accessors.
|
|
|
|
void MarkIndependent() {
|
2013-01-15 10:26:48 +00:00
|
|
|
ASSERT(state() != FREE);
|
|
|
|
set_independent(true);
|
2011-06-06 15:23:04 +00:00
|
|
|
}
|
|
|
|
|
2013-05-29 08:32:45 +00:00
|
|
|
void MarkPartiallyDependent() {
|
2013-01-15 10:26:48 +00:00
|
|
|
ASSERT(state() != FREE);
|
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) {
|
2013-01-15 10:26:48 +00:00
|
|
|
ASSERT(state() != FREE);
|
2011-06-06 15:23:04 +00:00
|
|
|
parameter_or_next_free_.parameter = parameter;
|
|
|
|
}
|
|
|
|
void* parameter() const {
|
2013-01-15 10:26:48 +00:00
|
|
|
ASSERT(state() != FREE);
|
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() {
|
2013-01-15 10:26:48 +00:00
|
|
|
ASSERT(state() == FREE);
|
2008-07-03 15:10:15 +00:00
|
|
|
return parameter_or_next_free_.next_free;
|
|
|
|
}
|
|
|
|
void set_next_free(Node* value) {
|
2013-01-15 10:26:48 +00:00
|
|
|
ASSERT(state() == FREE);
|
2008-07-03 15:10:15 +00:00
|
|
|
parameter_or_next_free_.next_free = value;
|
|
|
|
}
|
|
|
|
|
2013-05-29 08:32:45 +00:00
|
|
|
void MakeWeak(void* parameter,
|
2013-09-09 09:25:23 +00:00
|
|
|
WeakCallback weak_callback,
|
|
|
|
RevivableCallback revivable_callback) {
|
|
|
|
ASSERT((weak_callback == NULL) != (revivable_callback == NULL));
|
2013-01-15 10:26:48 +00:00
|
|
|
ASSERT(state() != FREE);
|
|
|
|
set_state(WEAK);
|
2008-07-03 15:10:15 +00:00
|
|
|
set_parameter(parameter);
|
2013-09-09 09:25:23 +00:00
|
|
|
if (weak_callback != NULL) {
|
|
|
|
weak_callback_ = weak_callback;
|
|
|
|
set_revivable_callback(false);
|
|
|
|
} else {
|
|
|
|
weak_callback_ =
|
|
|
|
reinterpret_cast<WeakCallback>(revivable_callback);
|
|
|
|
set_revivable_callback(true);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2013-05-29 08:32:45 +00:00
|
|
|
void ClearWeakness() {
|
2013-01-15 10:26:48 +00:00
|
|
|
ASSERT(state() != FREE);
|
|
|
|
set_state(NORMAL);
|
2008-07-03 15:10:15 +00:00
|
|
|
set_parameter(NULL);
|
|
|
|
}
|
|
|
|
|
2013-05-23 08:19:27 +00:00
|
|
|
bool PostGarbageCollectionProcessing(Isolate* isolate) {
|
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;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
void* par = parameter();
|
2013-01-15 10:26:48 +00:00
|
|
|
set_state(NEAR_DEATH);
|
2008-07-03 15:10:15 +00:00
|
|
|
set_parameter(NULL);
|
2009-08-21 08:52:24 +00:00
|
|
|
|
2013-06-03 08:17:04 +00:00
|
|
|
Object** object = location();
|
2009-08-21 08:52:24 +00:00
|
|
|
{
|
2009-12-09 14:32:45 +00:00
|
|
|
// Check that we are not passing a finalized external string to
|
|
|
|
// the callback.
|
|
|
|
ASSERT(!object_->IsExternalAsciiString() ||
|
|
|
|
ExternalAsciiString::cast(object_)->resource() != NULL);
|
|
|
|
ASSERT(!object_->IsExternalTwoByteString() ||
|
|
|
|
ExternalTwoByteString::cast(object_)->resource() != NULL);
|
2009-08-21 08:52:24 +00:00
|
|
|
// Leaving V8.
|
2013-04-24 14:44:08 +00:00
|
|
|
VMState<EXTERNAL> state(isolate);
|
2013-07-24 12:50:53 +00:00
|
|
|
HandleScope handle_scope(isolate);
|
2013-09-09 09:25:23 +00:00
|
|
|
if (is_revivable_callback()) {
|
|
|
|
RevivableCallback revivable =
|
|
|
|
reinterpret_cast<RevivableCallback>(weak_callback_);
|
|
|
|
revivable(reinterpret_cast<v8::Isolate*>(isolate),
|
|
|
|
reinterpret_cast<Persistent<Value>*>(&object),
|
|
|
|
par);
|
|
|
|
} else {
|
|
|
|
Handle<Object> handle(*object, isolate);
|
|
|
|
v8::WeakCallbackData<v8::Value, void> data(
|
|
|
|
reinterpret_cast<v8::Isolate*>(isolate),
|
|
|
|
v8::Utils::ToLocal(handle),
|
|
|
|
par);
|
|
|
|
weak_callback_(data);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
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.
|
2013-01-15 10:26:48 +00:00
|
|
|
ASSERT(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.
|
|
|
|
class NodeState: public BitField<State, 0, 4> {};
|
|
|
|
class IsIndependent: public BitField<bool, 4, 1> {};
|
|
|
|
class IsPartiallyDependent: public BitField<bool, 5, 1> {};
|
|
|
|
class IsInNewSpaceList: public BitField<bool, 6, 1> {};
|
2013-09-09 09:25:23 +00:00
|
|
|
class IsRevivableCallback: public BitField<bool, 7, 1> {};
|
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) {
|
|
|
|
ASSERT(0 <= index && index < kSize);
|
|
|
|
return &nodes_[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
void IncreaseUses() {
|
2011-06-06 15:23:04 +00:00
|
|
|
ASSERT(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() {
|
2011-06-06 15:23:04 +00:00
|
|
|
ASSERT(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);
|
|
|
|
ASSERT(block->node_at(index_) == this);
|
|
|
|
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 {
|
|
|
|
ASSERT(!done());
|
2013-08-14 12:40:44 +00:00
|
|
|
return block_->node_at(index_);
|
2011-06-06 15:23:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Advance() {
|
|
|
|
ASSERT(!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_);
|
|
|
|
}
|
|
|
|
ASSERT(first_free_ != NULL);
|
|
|
|
// 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) {
|
|
|
|
ASSERT(location != NULL);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-25 08:31:46 +00:00
|
|
|
void GlobalHandles::MakeWeak(Object** location,
|
|
|
|
void* parameter,
|
2013-09-09 09:25:23 +00:00
|
|
|
WeakCallback weak_callback,
|
|
|
|
RevivableCallback revivable_callback) {
|
|
|
|
Node::FromLocation(location)->MakeWeak(
|
|
|
|
parameter, weak_callback, revivable_callback);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GlobalHandles::ClearWeakness(Object** location) {
|
2013-05-29 08:32:45 +00:00
|
|
|
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()) {
|
|
|
|
if (it.node()->IsWeakRetainer()) v->VisitPointer(it.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];
|
|
|
|
ASSERT(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];
|
|
|
|
ASSERT(node->is_in_new_space_list());
|
2012-11-06 17:32:15 +00:00
|
|
|
if ((node->is_independent() || node->is_partially_dependent()) &&
|
|
|
|
node->IsWeakRetainer()) {
|
2011-06-06 16:18:59 +00:00
|
|
|
v->VisitPointer(node->location());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
ASSERT(entry != NULL);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-17 12:18:19 +00:00
|
|
|
bool GlobalHandles::PostGarbageCollectionProcessing(
|
2012-12-07 09:44:10 +00:00
|
|
|
GarbageCollector collector, GCTracer* tracer) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// Process weak global handle callbacks. This must be done after the
|
|
|
|
// GC is completely done, because the callbacks may invoke arbitrary
|
|
|
|
// API functions.
|
2011-03-18 20:35:07 +00:00
|
|
|
ASSERT(isolate_->heap()->gc_state() == Heap::NOT_IN_GC);
|
|
|
|
const int initial_post_gc_processing_count = ++post_gc_processing_count_;
|
2010-11-03 13:29:01 +00:00
|
|
|
bool next_gc_likely_to_collect_more = false;
|
2011-06-06 15:23:04 +00:00
|
|
|
if (collector == SCAVENGER) {
|
|
|
|
for (int i = 0; i < new_space_nodes_.length(); ++i) {
|
|
|
|
Node* node = new_space_nodes_[i];
|
|
|
|
ASSERT(node->is_in_new_space_list());
|
2013-07-08 11:51:34 +00:00
|
|
|
if (!node->IsRetainer()) {
|
|
|
|
// Free nodes do not have weak callbacks. Do not use them to compute
|
|
|
|
// the next_gc_likely_to_collect_more.
|
|
|
|
continue;
|
|
|
|
}
|
2011-06-06 15:23:04 +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.
|
2012-11-06 17:32:15 +00:00
|
|
|
if (!node->is_independent() && !node->is_partially_dependent()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
node->clear_partially_dependent();
|
2013-05-23 08:19:27 +00:00
|
|
|
if (node->PostGarbageCollectionProcessing(isolate_)) {
|
2011-06-06 15:23:04 +00:00
|
|
|
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 next_gc_likely_to_collect_more;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!node->IsRetainer()) {
|
|
|
|
next_gc_likely_to_collect_more = true;
|
2009-08-21 08:52:24 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-06 15:23:04 +00:00
|
|
|
} else {
|
2013-08-14 12:40:44 +00:00
|
|
|
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 next_gc_likely_to_collect_more.
|
|
|
|
continue;
|
2013-07-08 11:51:34 +00:00
|
|
|
}
|
2013-08-14 12:40:44 +00:00
|
|
|
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 next_gc_likely_to_collect_more;
|
2011-06-06 15:23:04 +00:00
|
|
|
}
|
2009-10-26 12:54:41 +00:00
|
|
|
}
|
2013-08-14 12:40:44 +00:00
|
|
|
if (!it.node()->IsRetainer()) {
|
|
|
|
next_gc_likely_to_collect_more = true;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-06 15:23:04 +00:00
|
|
|
// Update the list of new space nodes.
|
|
|
|
int last = 0;
|
|
|
|
for (int i = 0; i < new_space_nodes_.length(); ++i) {
|
|
|
|
Node* node = new_space_nodes_[i];
|
|
|
|
ASSERT(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;
|
|
|
|
tracer->increment_nodes_copied_in_new_space();
|
|
|
|
} else {
|
|
|
|
node->set_in_new_space_list(false);
|
|
|
|
tracer->increment_nodes_promoted();
|
|
|
|
}
|
2011-06-06 15:23:04 +00:00
|
|
|
} else {
|
|
|
|
node->set_in_new_space_list(false);
|
2012-12-07 09:44:10 +00:00
|
|
|
tracer->increment_nodes_died_in_new_space();
|
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);
|
2010-11-03 13:29:01 +00:00
|
|
|
return next_gc_likely_to_collect_more;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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) {
|
2011-06-06 15:23:04 +00:00
|
|
|
ASSERT(!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
|
|
|
|
2011-04-06 19:17:54 +00:00
|
|
|
void GlobalHandles::AddImplicitReferences(HeapObject** parent,
|
2011-03-16 12:02:28 +00:00
|
|
|
Object*** children,
|
|
|
|
size_t length) {
|
2011-05-18 15:28:43 +00:00
|
|
|
#ifdef DEBUG
|
2011-06-06 15:23:04 +00:00
|
|
|
ASSERT(!Node::FromLocation(BitCast<Object**>(parent))->is_independent());
|
2011-05-18 15:28:43 +00:00
|
|
|
for (size_t i = 0; i < length; ++i) {
|
2011-06-06 15:23:04 +00:00
|
|
|
ASSERT(!Node::FromLocation(children[i])->is_independent());
|
2011-05-18 15:28:43 +00:00
|
|
|
}
|
|
|
|
#endif
|
2013-04-16 16:37:04 +00:00
|
|
|
if (length == 0) return;
|
2013-04-24 15:59:23 +00:00
|
|
|
ImplicitRefGroup* group = new ImplicitRefGroup(parent, length);
|
|
|
|
for (size_t i = 0; i < length; ++i)
|
|
|
|
group->children[i] = children[i];
|
|
|
|
implicit_ref_groups_.Add(group);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GlobalHandles::SetReferenceFromGroup(UniqueId id, Object** child) {
|
|
|
|
ASSERT(!Node::FromLocation(child)->is_independent());
|
|
|
|
implicit_ref_connections_.Add(ObjectGroupConnection(id, child));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GlobalHandles::SetReference(HeapObject** parent, Object** child) {
|
|
|
|
ASSERT(!Node::FromLocation(child)->is_independent());
|
|
|
|
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) {
|
|
|
|
for (unsigned i = 0; i < ARRAY_SIZE(singleton_handles_); i++) {
|
|
|
|
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++) {
|
|
|
|
ASSERT(limit > 0);
|
|
|
|
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) {
|
|
|
|
ASSERT_EQ(kInvalidIndex, *index);
|
|
|
|
if (object == NULL) return;
|
2013-08-05 09:46:23 +00:00
|
|
|
ASSERT_NE(isolate->heap()->the_hole_value(), object);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
ASSERT_EQ(isolate->heap()->the_hole_value(), blocks_[block][offset]);
|
|
|
|
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
|