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
|
|
|
|
|
|
|
ObjectGroup::~ObjectGroup() {
|
|
|
|
if (info_ != NULL) info_->Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
FREE,
|
|
|
|
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.
|
|
|
|
};
|
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
|
|
|
}
|
|
|
|
|
2011-06-06 15:23:04 +00:00
|
|
|
Node() {}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
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.
|
|
|
|
object_ = NULL;
|
2011-06-06 15:23:04 +00:00
|
|
|
class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId;
|
|
|
|
index_ = 0;
|
|
|
|
independent_ = false;
|
|
|
|
in_new_space_list_ = false;
|
2008-07-03 15:10:15 +00:00
|
|
|
parameter_or_next_free_.next_free = NULL;
|
2011-06-06 15:23:04 +00:00
|
|
|
callback_ = NULL;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
#endif
|
2011-06-06 15:23:04 +00:00
|
|
|
|
|
|
|
void Initialize(int index, Node** first_free) {
|
|
|
|
index_ = static_cast<uint8_t>(index);
|
|
|
|
ASSERT(static_cast<int>(index_) == index);
|
|
|
|
state_ = FREE;
|
|
|
|
in_new_space_list_ = false;
|
|
|
|
parameter_or_next_free_.next_free = *first_free;
|
|
|
|
*first_free = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Acquire(Object* object, GlobalHandles* global_handles) {
|
|
|
|
ASSERT(state_ == FREE);
|
|
|
|
object_ = object;
|
|
|
|
class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId;
|
|
|
|
independent_ = false;
|
|
|
|
state_ = NORMAL;
|
|
|
|
parameter_or_next_free_.parameter = NULL;
|
|
|
|
callback_ = NULL;
|
|
|
|
IncreaseBlockUses(global_handles);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2011-06-06 15:23:04 +00:00
|
|
|
void Release(GlobalHandles* global_handles) {
|
|
|
|
ASSERT(state_ != FREE);
|
|
|
|
if (IsWeakRetainer()) {
|
2011-03-18 20:35:07 +00:00
|
|
|
global_handles->number_of_weak_handles_--;
|
2008-07-03 15:10:15 +00:00
|
|
|
if (object_->IsJSGlobalObject()) {
|
2011-03-18 20:35:07 +00:00
|
|
|
global_handles->number_of_global_object_weak_handles_--;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-06 15:23:04 +00:00
|
|
|
state_ = FREE;
|
|
|
|
parameter_or_next_free_.next_free = global_handles->first_free_;
|
|
|
|
global_handles->first_free_ = this;
|
|
|
|
DecreaseBlockUses(global_handles);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
uint16_t wrapper_class_id() const { return class_id_; }
|
|
|
|
void set_wrapper_class_id(uint16_t class_id) {
|
|
|
|
class_id_ = class_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// State accessors.
|
|
|
|
|
|
|
|
State state() const { return state_; }
|
|
|
|
|
|
|
|
bool IsNearDeath() const {
|
|
|
|
// Check for PENDING to ensure correct answer when processing callbacks.
|
|
|
|
return state_ == PENDING || state_ == NEAR_DEATH;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsWeak() const { return state_ == WEAK; }
|
|
|
|
|
|
|
|
bool IsRetainer() const { return state_ != FREE; }
|
|
|
|
|
|
|
|
bool IsStrongRetainer() const { return state_ == NORMAL; }
|
|
|
|
|
|
|
|
bool IsWeakRetainer() const {
|
|
|
|
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() {
|
|
|
|
ASSERT(state_ == WEAK);
|
|
|
|
state_ = PENDING;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Independent flag accessors.
|
|
|
|
void MarkIndependent() {
|
|
|
|
ASSERT(state_ != FREE);
|
|
|
|
independent_ = true;
|
|
|
|
}
|
|
|
|
bool is_independent() const { return independent_; }
|
|
|
|
|
|
|
|
// In-new-space-list flag accessors.
|
|
|
|
void set_in_new_space_list(bool v) { in_new_space_list_ = v; }
|
|
|
|
bool is_in_new_space_list() const { return in_new_space_list_; }
|
|
|
|
|
|
|
|
// Callback accessor.
|
|
|
|
WeakReferenceCallback callback() { return callback_; }
|
|
|
|
|
|
|
|
// Callback parameter accessors.
|
|
|
|
void set_parameter(void* parameter) {
|
|
|
|
ASSERT(state_ != FREE);
|
|
|
|
parameter_or_next_free_.parameter = parameter;
|
|
|
|
}
|
|
|
|
void* parameter() const {
|
|
|
|
ASSERT(state_ != FREE);
|
|
|
|
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() {
|
2011-06-06 15:23:04 +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) {
|
2011-06-06 15:23:04 +00:00
|
|
|
ASSERT(state_ == FREE);
|
2008-07-03 15:10:15 +00:00
|
|
|
parameter_or_next_free_.next_free = value;
|
|
|
|
}
|
|
|
|
|
2011-06-06 15:23:04 +00:00
|
|
|
void MakeWeak(GlobalHandles* global_handles,
|
|
|
|
void* parameter,
|
2011-03-18 20:35:07 +00:00
|
|
|
WeakReferenceCallback callback) {
|
2011-06-06 15:23:04 +00:00
|
|
|
ASSERT(state_ != FREE);
|
|
|
|
if (!IsWeakRetainer()) {
|
2011-03-18 20:35:07 +00:00
|
|
|
global_handles->number_of_weak_handles_++;
|
2008-07-03 15:10:15 +00:00
|
|
|
if (object_->IsJSGlobalObject()) {
|
2011-03-18 20:35:07 +00:00
|
|
|
global_handles->number_of_global_object_weak_handles_++;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
state_ = WEAK;
|
|
|
|
set_parameter(parameter);
|
|
|
|
callback_ = callback;
|
|
|
|
}
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
void ClearWeakness(GlobalHandles* global_handles) {
|
2011-06-06 15:23:04 +00:00
|
|
|
ASSERT(state_ != FREE);
|
|
|
|
if (IsWeakRetainer()) {
|
2011-03-18 20:35:07 +00:00
|
|
|
global_handles->number_of_weak_handles_--;
|
2008-07-03 15:10:15 +00:00
|
|
|
if (object_->IsJSGlobalObject()) {
|
2011-03-18 20:35:07 +00:00
|
|
|
global_handles->number_of_global_object_weak_handles_--;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
state_ = NORMAL;
|
|
|
|
set_parameter(NULL);
|
|
|
|
}
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
bool PostGarbageCollectionProcessing(Isolate* isolate,
|
|
|
|
GlobalHandles* global_handles) {
|
2009-08-21 08:52:24 +00:00
|
|
|
if (state_ != Node::PENDING) return false;
|
2010-07-19 13:26:25 +00:00
|
|
|
WeakReferenceCallback func = callback();
|
|
|
|
if (func == NULL) {
|
2011-06-06 15:23:04 +00:00
|
|
|
Release(global_handles);
|
2010-07-19 13:26:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
void* par = parameter();
|
|
|
|
state_ = NEAR_DEATH;
|
|
|
|
set_parameter(NULL);
|
2009-08-21 08:52:24 +00:00
|
|
|
|
|
|
|
v8::Persistent<v8::Object> object = ToApi<v8::Object>(handle());
|
|
|
|
{
|
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.
|
2011-03-18 20:35:07 +00:00
|
|
|
VMState state(isolate, EXTERNAL);
|
2009-08-21 08:52:24 +00:00
|
|
|
func(object, par);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2010-07-19 13:26:25 +00:00
|
|
|
// Absense of explicit cleanup or revival of weak handle
|
|
|
|
// in most of the cases would lead to memory leak.
|
|
|
|
ASSERT(state_ != NEAR_DEATH);
|
2009-08-21 08:52:24 +00:00
|
|
|
return true;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2011-06-06 15:23:04 +00:00
|
|
|
private:
|
|
|
|
inline NodeBlock* FindBlock();
|
|
|
|
inline void IncreaseBlockUses(GlobalHandles* global_handles);
|
|
|
|
inline void DecreaseBlockUses(GlobalHandles* global_handles);
|
|
|
|
|
|
|
|
// 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_;
|
|
|
|
|
|
|
|
// Need one more bit for MSVC as it treats enums as signed.
|
|
|
|
State state_ : 4;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-05-17 12:18:19 +00:00
|
|
|
bool independent_ : 1;
|
2011-06-06 15:23:04 +00:00
|
|
|
bool in_new_space_list_ : 1;
|
2011-05-17 12:18:19 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Handle specific callback.
|
|
|
|
WeakReferenceCallback 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
|
|
|
|
2011-06-06 15:23:04 +00:00
|
|
|
class GlobalHandles::NodeBlock {
|
2008-07-03 15:10:15 +00:00
|
|
|
public:
|
2011-06-06 15:23:04 +00:00
|
|
|
static const int kSize = 256;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-06-06 15:23:04 +00:00
|
|
|
explicit NodeBlock(NodeBlock* next)
|
|
|
|
: next_(next), used_nodes_(0), next_used_(NULL), prev_used_(NULL) {}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-06-06 15:23:04 +00:00
|
|
|
void PutNodesOnFreeList(Node** first_free) {
|
|
|
|
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
|
|
|
|
2011-06-06 15:23:04 +00:00
|
|
|
Node* node_at(int index) {
|
|
|
|
ASSERT(0 <= index && index < kSize);
|
|
|
|
return &nodes_[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
void IncreaseUses(GlobalHandles* global_handles) {
|
|
|
|
ASSERT(used_nodes_ < kSize);
|
|
|
|
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
|
|
|
|
2011-06-06 15:23:04 +00:00
|
|
|
void DecreaseUses(GlobalHandles* global_handles) {
|
|
|
|
ASSERT(used_nodes_ > 0);
|
|
|
|
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
|
|
|
|
2011-06-06 15:23:04 +00:00
|
|
|
// Next block in the list of all blocks.
|
|
|
|
NodeBlock* next() const { return next_; }
|
2009-10-26 12:54:41 +00:00
|
|
|
|
2011-06-06 15:23:04 +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];
|
|
|
|
NodeBlock* const next_;
|
|
|
|
int used_nodes_;
|
|
|
|
NodeBlock* next_used_;
|
|
|
|
NodeBlock* prev_used_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
GlobalHandles::NodeBlock* GlobalHandles::Node::FindBlock() {
|
|
|
|
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(GlobalHandles* global_handles) {
|
|
|
|
FindBlock()->IncreaseUses(global_handles);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GlobalHandles::Node::DecreaseBlockUses(GlobalHandles* global_handles) {
|
|
|
|
FindBlock()->DecreaseUses(global_handles);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class GlobalHandles::NodeIterator {
|
|
|
|
public:
|
|
|
|
explicit NodeIterator(GlobalHandles* global_handles)
|
|
|
|
: block_(global_handles->first_used_block_),
|
|
|
|
index_(0) {}
|
|
|
|
|
|
|
|
bool done() const { return block_ == NULL; }
|
|
|
|
|
|
|
|
Node* node() const {
|
|
|
|
ASSERT(!done());
|
|
|
|
return block_->node_at(index_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Advance() {
|
|
|
|
ASSERT(!done());
|
|
|
|
if (++index_ < NodeBlock::kSize) return;
|
|
|
|
index_ = 0;
|
|
|
|
block_ = block_->next_used();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
NodeBlock* block_;
|
|
|
|
int index_;
|
|
|
|
|
|
|
|
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),
|
|
|
|
number_of_weak_handles_(0),
|
|
|
|
number_of_global_object_weak_handles_(0),
|
2011-06-06 15:23:04 +00:00
|
|
|
first_block_(NULL),
|
|
|
|
first_used_block_(NULL),
|
2011-03-18 20:35:07 +00:00
|
|
|
first_free_(NULL),
|
2011-06-06 15:23:04 +00:00
|
|
|
post_gc_processing_count_(0) {}
|
2011-03-18 20:35:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
GlobalHandles::~GlobalHandles() {
|
2011-06-06 15:23:04 +00:00
|
|
|
NodeBlock* block = first_block_;
|
|
|
|
while (block != NULL) {
|
|
|
|
NodeBlock* tmp = block->next();
|
|
|
|
delete block;
|
|
|
|
block = tmp;
|
|
|
|
}
|
|
|
|
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) {
|
2011-03-18 20:35:07 +00:00
|
|
|
isolate_->counters()->global_handles()->Increment();
|
2011-06-06 15:23:04 +00:00
|
|
|
if (first_free_ == NULL) {
|
|
|
|
first_block_ = new NodeBlock(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, this);
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GlobalHandles::Destroy(Object** location) {
|
2011-03-18 20:35:07 +00:00
|
|
|
isolate_->counters()->global_handles()->Decrement();
|
2008-07-03 15:10:15 +00:00
|
|
|
if (location == NULL) return;
|
2011-06-06 15:23:04 +00:00
|
|
|
Node::FromLocation(location)->Release(this);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GlobalHandles::MakeWeak(Object** location, void* parameter,
|
|
|
|
WeakReferenceCallback callback) {
|
|
|
|
ASSERT(callback != NULL);
|
2011-03-18 20:35:07 +00:00
|
|
|
Node::FromLocation(location)->MakeWeak(this, parameter, callback);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GlobalHandles::ClearWeakness(Object** location) {
|
2011-03-18 20:35:07 +00:00
|
|
|
Node::FromLocation(location)->ClearWeakness(this);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-10 12:05:31 +00:00
|
|
|
void GlobalHandles::SetWrapperClassId(Object** location, uint16_t class_id) {
|
2011-06-06 15:23:04 +00:00
|
|
|
Node::FromLocation(location)->set_wrapper_class_id(class_id);
|
2011-03-10 12:05:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
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-10-15 07:50:23 +00:00
|
|
|
void GlobalHandles::IterateWeakRoots(WeakReferenceGuest f,
|
|
|
|
WeakReferenceCallback callback) {
|
2011-06-06 15:23:04 +00:00
|
|
|
for (NodeIterator it(this); !it.done(); it.Advance()) {
|
|
|
|
if (it.node()->IsWeak() && it.node()->callback() == callback) {
|
|
|
|
f(it.node()->object(), it.node()->parameter());
|
2009-10-15 07:50:23 +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() ||
|
|
|
|
(node->IsWeakRetainer() && !node->is_independent())) {
|
|
|
|
v->VisitPointer(node->location());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
if (node->is_independent() && node->IsWeak() &&
|
|
|
|
f(isolate_->heap(), node->location())) {
|
|
|
|
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());
|
|
|
|
if (node->is_independent() && node->IsWeakRetainer()) {
|
|
|
|
v->VisitPointer(node->location());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-17 12:18:19 +00:00
|
|
|
bool GlobalHandles::PostGarbageCollectionProcessing(
|
|
|
|
GarbageCollector collector) {
|
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());
|
|
|
|
// 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()) continue;
|
|
|
|
if (node->PostGarbageCollectionProcessing(isolate_, this)) {
|
|
|
|
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 {
|
|
|
|
for (NodeIterator it(this); !it.done(); it.Advance()) {
|
|
|
|
if (it.node()->PostGarbageCollectionProcessing(isolate_, this)) {
|
|
|
|
if (initial_post_gc_processing_count != post_gc_processing_count_) {
|
|
|
|
// See the comment above.
|
|
|
|
return next_gc_likely_to_collect_more;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!it.node()->IsRetainer()) {
|
|
|
|
next_gc_likely_to_collect_more = true;
|
2009-10-26 12:54:41 +00:00
|
|
|
}
|
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());
|
|
|
|
if (node->IsRetainer() && isolate_->heap()->InNewSpace(node->object())) {
|
|
|
|
new_space_nodes_[last++] = node;
|
|
|
|
} else {
|
|
|
|
node->set_in_new_space_list(false);
|
|
|
|
}
|
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()) {
|
|
|
|
if (it.node()->has_wrapper_class_id() && it.node()->IsRetainer()) {
|
|
|
|
v->VisitEmbedderReference(it.node()->location(),
|
|
|
|
it.node()->wrapper_class_id());
|
2011-03-10 12:05:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-03 10:16:37 +00:00
|
|
|
void GlobalHandles::RecordStats(HeapStats* stats) {
|
2009-12-04 10:18:30 +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;
|
2011-06-06 15:23:04 +00:00
|
|
|
*stats->free_global_handle_count = 0;
|
|
|
|
for (NodeIterator it(this); !it.done(); it.Advance()) {
|
2009-12-04 14:35:33 +00:00
|
|
|
*stats->global_handle_count += 1;
|
2011-06-06 15:23:04 +00:00
|
|
|
if (it.node()->state() == Node::WEAK) {
|
2009-12-04 14:35:33 +00:00
|
|
|
*stats->weak_global_handle_count += 1;
|
2011-06-06 15:23:04 +00:00
|
|
|
} else if (it.node()->state() == Node::PENDING) {
|
2009-12-04 14:35:33 +00:00
|
|
|
*stats->pending_global_handle_count += 1;
|
2011-06-06 15:23:04 +00:00
|
|
|
} else if (it.node()->state() == Node::NEAR_DEATH) {
|
2009-12-04 14:35:33 +00:00
|
|
|
*stats->near_death_global_handle_count += 1;
|
2011-06-06 15:23:04 +00:00
|
|
|
} 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() {
|
|
|
|
int total = 0;
|
|
|
|
int weak = 0;
|
|
|
|
int pending = 0;
|
|
|
|
int near_death = 0;
|
|
|
|
int destroyed = 0;
|
|
|
|
|
2011-06-06 15:23:04 +00:00
|
|
|
for (NodeIterator it(this); !it.done(); it.Advance()) {
|
2008-07-03 15:10:15 +00:00
|
|
|
total++;
|
2011-06-06 15:23:04 +00:00
|
|
|
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");
|
2010-09-30 07:22:53 +00:00
|
|
|
PrintF(" allocated memory = %" V8_PTR_PREFIX "dB\n", sizeof(Node) * total);
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintF(" # weak = %d\n", weak);
|
|
|
|
PrintF(" # pending = %d\n", pending);
|
|
|
|
PrintF(" # near_death = %d\n", near_death);
|
2011-06-06 15:23:04 +00:00
|
|
|
PrintF(" # free = %d\n", destroyed);
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintF(" # total = %d\n", total);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2011-04-06 19:17:54 +00:00
|
|
|
object_groups_.Add(ObjectGroup::New(handles, length, info));
|
2011-03-16 12:02:28 +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
|
2011-04-06 19:17:54 +00:00
|
|
|
if (length == 0) return;
|
|
|
|
implicit_ref_groups_.Add(ImplicitRefGroup::New(parent, children, length));
|
2011-03-16 12:02:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
void GlobalHandles::RemoveObjectGroups() {
|
2011-03-18 20:35:07 +00:00
|
|
|
for (int i = 0; i < object_groups_.length(); i++) {
|
2011-04-06 19:17:54 +00:00
|
|
|
object_groups_.at(i)->Dispose();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
object_groups_.Clear();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2011-03-16 12:02:28 +00:00
|
|
|
|
|
|
|
void GlobalHandles::RemoveImplicitRefGroups() {
|
2011-03-18 20:35:07 +00:00
|
|
|
for (int i = 0; i < implicit_ref_groups_.length(); i++) {
|
2011-04-06 19:17:54 +00:00
|
|
|
implicit_ref_groups_.at(i)->Dispose();
|
2011-03-16 12:02:28 +00:00
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
implicit_ref_groups_.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.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
} } // namespace v8::internal
|