blob: 9c0ad45179dc538bc39aab0c08036dca229677b4 [file] [log] [blame]
ager@chromium.org71daaf62009-04-01 07:22:49 +00001// Copyright 2009 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "api.h"
31#include "global-handles.h"
32
kasperl@chromium.orga5551262010-12-07 12:49:48 +000033#include "vm-state-inl.h"
34
kasperl@chromium.org71affb52009-05-26 05:44:31 +000035namespace v8 {
36namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000038
39ObjectGroup::~ObjectGroup() {
40 if (info_ != NULL) info_->Dispose();
41}
42
43
jkummerow@chromium.orge297f592011-06-08 10:05:15 +000044class GlobalHandles::Node {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000045 public:
jkummerow@chromium.orge297f592011-06-08 10:05:15 +000046 // State transition diagram:
47 // FREE -> NORMAL <-> WEAK -> PENDING -> NEAR_DEATH -> { NORMAL, WEAK, FREE }
48 enum State {
49 FREE,
50 NORMAL, // Normal global handle.
51 WEAK, // Flagged as weak but not yet finalized.
52 PENDING, // Has been recognized as only reachable by weak handles.
53 NEAR_DEATH // Callback has informed the handle is near death.
54 };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000055
jkummerow@chromium.orge297f592011-06-08 10:05:15 +000056 // Maps handle location (slot) to the containing node.
57 static Node* FromLocation(Object** location) {
58 ASSERT(OFFSET_OF(Node, object_) == 0);
59 return reinterpret_cast<Node*>(location);
60 }
61
62 Node() {}
63
64#ifdef DEBUG
65 ~Node() {
66 // TODO(1428): if it's a weak handle we should have invoked its callback.
67 // Zap the values for eager trapping.
68 object_ = NULL;
69 class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId;
70 index_ = 0;
71 independent_ = false;
72 in_new_space_list_ = false;
73 parameter_or_next_free_.next_free = NULL;
74 callback_ = NULL;
75 }
76#endif
77
78 void Initialize(int index, Node** first_free) {
79 index_ = static_cast<uint8_t>(index);
80 ASSERT(static_cast<int>(index_) == index);
81 state_ = FREE;
82 in_new_space_list_ = false;
83 parameter_or_next_free_.next_free = *first_free;
84 *first_free = this;
85 }
86
87 void Acquire(Object* object, GlobalHandles* global_handles) {
88 ASSERT(state_ == FREE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000089 object_ = object;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +000090 class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +000091 independent_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000092 state_ = NORMAL;
93 parameter_or_next_free_.parameter = NULL;
94 callback_ = NULL;
jkummerow@chromium.orge297f592011-06-08 10:05:15 +000095 IncreaseBlockUses(global_handles);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000096 }
97
jkummerow@chromium.orge297f592011-06-08 10:05:15 +000098 void Release(GlobalHandles* global_handles) {
99 ASSERT(state_ != FREE);
100 if (IsWeakRetainer()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000101 global_handles->number_of_weak_handles_--;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000102 if (object_->IsJSGlobalObject()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000103 global_handles->number_of_global_object_weak_handles_--;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000104 }
105 }
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000106 state_ = FREE;
107 parameter_or_next_free_.next_free = global_handles->first_free_;
108 global_handles->first_free_ = this;
109 DecreaseBlockUses(global_handles);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000110 }
111
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000112 // Object slot accessors.
113 Object* object() const { return object_; }
114 Object** location() { return &object_; }
115 Handle<Object> handle() { return Handle<Object>(location()); }
116
117 // Wrapper class ID accessors.
118 bool has_wrapper_class_id() const {
119 return class_id_ != v8::HeapProfiler::kPersistentHandleNoClassId;
120 }
121 uint16_t wrapper_class_id() const { return class_id_; }
122 void set_wrapper_class_id(uint16_t class_id) {
123 class_id_ = class_id;
124 }
125
126 // State accessors.
127
128 State state() const { return state_; }
129
130 bool IsNearDeath() const {
131 // Check for PENDING to ensure correct answer when processing callbacks.
132 return state_ == PENDING || state_ == NEAR_DEATH;
133 }
134
135 bool IsWeak() const { return state_ == WEAK; }
136
137 bool IsRetainer() const { return state_ != FREE; }
138
139 bool IsStrongRetainer() const { return state_ == NORMAL; }
140
141 bool IsWeakRetainer() const {
142 return state_ == WEAK || state_ == PENDING || state_ == NEAR_DEATH;
143 }
144
145 void MarkPending() {
146 ASSERT(state_ == WEAK);
147 state_ = PENDING;
148 }
149
150 // Independent flag accessors.
151 void MarkIndependent() {
152 ASSERT(state_ != FREE);
153 independent_ = true;
154 }
155 bool is_independent() const { return independent_; }
156
157 // In-new-space-list flag accessors.
158 void set_in_new_space_list(bool v) { in_new_space_list_ = v; }
159 bool is_in_new_space_list() const { return in_new_space_list_; }
160
161 // Callback accessor.
162 WeakReferenceCallback callback() { return callback_; }
163
164 // Callback parameter accessors.
165 void set_parameter(void* parameter) {
166 ASSERT(state_ != FREE);
167 parameter_or_next_free_.parameter = parameter;
168 }
169 void* parameter() const {
170 ASSERT(state_ != FREE);
171 return parameter_or_next_free_.parameter;
172 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000173
174 // Accessors for next free node in the free list.
175 Node* next_free() {
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000176 ASSERT(state_ == FREE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000177 return parameter_or_next_free_.next_free;
178 }
179 void set_next_free(Node* value) {
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000180 ASSERT(state_ == FREE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000181 parameter_or_next_free_.next_free = value;
182 }
183
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000184 void MakeWeak(GlobalHandles* global_handles,
185 void* parameter,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000186 WeakReferenceCallback callback) {
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000187 ASSERT(state_ != FREE);
188 if (!IsWeakRetainer()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000189 global_handles->number_of_weak_handles_++;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000190 if (object_->IsJSGlobalObject()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000191 global_handles->number_of_global_object_weak_handles_++;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000192 }
193 }
194 state_ = WEAK;
195 set_parameter(parameter);
196 callback_ = callback;
197 }
198
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000199 void ClearWeakness(GlobalHandles* global_handles) {
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000200 ASSERT(state_ != FREE);
201 if (IsWeakRetainer()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000202 global_handles->number_of_weak_handles_--;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203 if (object_->IsJSGlobalObject()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000204 global_handles->number_of_global_object_weak_handles_--;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000205 }
206 }
207 state_ = NORMAL;
208 set_parameter(NULL);
209 }
210
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000211 bool PostGarbageCollectionProcessing(Isolate* isolate,
212 GlobalHandles* global_handles) {
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000213 if (state_ != Node::PENDING) return false;
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000214 WeakReferenceCallback func = callback();
215 if (func == NULL) {
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000216 Release(global_handles);
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000217 return false;
218 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219 void* par = parameter();
220 state_ = NEAR_DEATH;
221 set_parameter(NULL);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000222
223 v8::Persistent<v8::Object> object = ToApi<v8::Object>(handle());
224 {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000225 // Check that we are not passing a finalized external string to
226 // the callback.
227 ASSERT(!object_->IsExternalAsciiString() ||
228 ExternalAsciiString::cast(object_)->resource() != NULL);
229 ASSERT(!object_->IsExternalTwoByteString() ||
230 ExternalTwoByteString::cast(object_)->resource() != NULL);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000231 // Leaving V8.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000232 VMState state(isolate, EXTERNAL);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000233 func(object, par);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000234 }
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000235 // Absence of explicit cleanup or revival of weak handle
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000236 // in most of the cases would lead to memory leak.
237 ASSERT(state_ != NEAR_DEATH);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000238 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239 }
240
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000241 private:
242 inline NodeBlock* FindBlock();
243 inline void IncreaseBlockUses(GlobalHandles* global_handles);
244 inline void DecreaseBlockUses(GlobalHandles* global_handles);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000245
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000246 // Storage for object pointer.
247 // Placed first to avoid offset computation.
248 Object* object_;
249
250 // Next word stores class_id, index, state, and independent.
251 // Note: the most aligned fields should go first.
252
253 // Wrapper class ID.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000254 uint16_t class_id_;
255
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000256 // Index in the containing handle block.
257 uint8_t index_;
258
259 // Need one more bit for MSVC as it treats enums as signed.
260 State state_ : 4;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000261
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000262 bool independent_ : 1;
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000263 bool in_new_space_list_ : 1;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000264
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000265 // Handle specific callback.
266 WeakReferenceCallback callback_;
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000267
268 // Provided data for callback. In FREE state, this is used for
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269 // the free list link.
270 union {
271 void* parameter;
272 Node* next_free;
273 } parameter_or_next_free_;
274
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000275 DISALLOW_COPY_AND_ASSIGN(Node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276};
277
278
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000279class GlobalHandles::NodeBlock {
280 public:
281 static const int kSize = 256;
ager@chromium.org3811b432009-10-28 14:53:37 +0000282
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000283 explicit NodeBlock(NodeBlock* next)
284 : next_(next), used_nodes_(0), next_used_(NULL), prev_used_(NULL) {}
285
286 void PutNodesOnFreeList(Node** first_free) {
287 for (int i = kSize - 1; i >= 0; --i) {
288 nodes_[i].Initialize(i, first_free);
289 }
290 }
291
292 Node* node_at(int index) {
293 ASSERT(0 <= index && index < kSize);
294 return &nodes_[index];
295 }
296
297 void IncreaseUses(GlobalHandles* global_handles) {
298 ASSERT(used_nodes_ < kSize);
299 if (used_nodes_++ == 0) {
300 NodeBlock* old_first = global_handles->first_used_block_;
301 global_handles->first_used_block_ = this;
302 next_used_ = old_first;
303 prev_used_ = NULL;
304 if (old_first == NULL) return;
305 old_first->prev_used_ = this;
306 }
307 }
308
309 void DecreaseUses(GlobalHandles* global_handles) {
310 ASSERT(used_nodes_ > 0);
311 if (--used_nodes_ == 0) {
312 if (next_used_ != NULL) next_used_->prev_used_ = prev_used_;
313 if (prev_used_ != NULL) prev_used_->next_used_ = next_used_;
314 if (this == global_handles->first_used_block_) {
315 global_handles->first_used_block_ = next_used_;
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000316 }
317 }
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000318 }
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000319
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000320 // Next block in the list of all blocks.
321 NodeBlock* next() const { return next_; }
ager@chromium.org3811b432009-10-28 14:53:37 +0000322
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000323 // Next/previous block in the list of blocks with used nodes.
324 NodeBlock* next_used() const { return next_used_; }
325 NodeBlock* prev_used() const { return prev_used_; }
ager@chromium.org3811b432009-10-28 14:53:37 +0000326
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000327 private:
328 Node nodes_[kSize];
329 NodeBlock* const next_;
330 int used_nodes_;
331 NodeBlock* next_used_;
332 NodeBlock* prev_used_;
333};
ager@chromium.org3811b432009-10-28 14:53:37 +0000334
ager@chromium.org3811b432009-10-28 14:53:37 +0000335
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000336GlobalHandles::NodeBlock* GlobalHandles::Node::FindBlock() {
337 intptr_t ptr = reinterpret_cast<intptr_t>(this);
338 ptr = ptr - index_ * sizeof(Node);
339 NodeBlock* block = reinterpret_cast<NodeBlock*>(ptr);
340 ASSERT(block->node_at(index_) == this);
341 return block;
342}
ager@chromium.org3811b432009-10-28 14:53:37 +0000343
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000344
345void GlobalHandles::Node::IncreaseBlockUses(GlobalHandles* global_handles) {
346 FindBlock()->IncreaseUses(global_handles);
347}
348
349
350void GlobalHandles::Node::DecreaseBlockUses(GlobalHandles* global_handles) {
351 FindBlock()->DecreaseUses(global_handles);
352}
353
354
355class GlobalHandles::NodeIterator {
356 public:
357 explicit NodeIterator(GlobalHandles* global_handles)
358 : block_(global_handles->first_used_block_),
359 index_(0) {}
360
361 bool done() const { return block_ == NULL; }
362
363 Node* node() const {
364 ASSERT(!done());
365 return block_->node_at(index_);
366 }
367
368 void Advance() {
369 ASSERT(!done());
370 if (++index_ < NodeBlock::kSize) return;
371 index_ = 0;
372 block_ = block_->next_used();
373 }
374
375 private:
376 NodeBlock* block_;
377 int index_;
378
379 DISALLOW_COPY_AND_ASSIGN(NodeIterator);
ager@chromium.org3811b432009-10-28 14:53:37 +0000380};
381
382
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000383GlobalHandles::GlobalHandles(Isolate* isolate)
384 : isolate_(isolate),
385 number_of_weak_handles_(0),
386 number_of_global_object_weak_handles_(0),
erik.corry@gmail.combbceb572012-03-09 10:52:05 +0000387 number_of_global_handles_(0),
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000388 first_block_(NULL),
389 first_used_block_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000390 first_free_(NULL),
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000391 post_gc_processing_count_(0) {}
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000392
393
394GlobalHandles::~GlobalHandles() {
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000395 NodeBlock* block = first_block_;
396 while (block != NULL) {
397 NodeBlock* tmp = block->next();
398 delete block;
399 block = tmp;
400 }
401 first_block_ = NULL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000402}
ager@chromium.org3811b432009-10-28 14:53:37 +0000403
404
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000405Handle<Object> GlobalHandles::Create(Object* value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000406 isolate_->counters()->global_handles()->Increment();
erik.corry@gmail.combbceb572012-03-09 10:52:05 +0000407 number_of_global_handles_++;
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000408 if (first_free_ == NULL) {
409 first_block_ = new NodeBlock(first_block_);
410 first_block_->PutNodesOnFreeList(&first_free_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000411 }
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000412 ASSERT(first_free_ != NULL);
413 // Take the first node in the free list.
414 Node* result = first_free_;
415 first_free_ = result->next_free();
416 result->Acquire(value, this);
417 if (isolate_->heap()->InNewSpace(value) &&
418 !result->is_in_new_space_list()) {
419 new_space_nodes_.Add(result);
420 result->set_in_new_space_list(true);
421 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000422 return result->handle();
423}
424
425
426void GlobalHandles::Destroy(Object** location) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000427 isolate_->counters()->global_handles()->Decrement();
erik.corry@gmail.combbceb572012-03-09 10:52:05 +0000428 number_of_global_handles_--;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000429 if (location == NULL) return;
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000430 Node::FromLocation(location)->Release(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000431}
432
433
434void GlobalHandles::MakeWeak(Object** location, void* parameter,
435 WeakReferenceCallback callback) {
436 ASSERT(callback != NULL);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000437 Node::FromLocation(location)->MakeWeak(this, parameter, callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000438}
439
440
441void GlobalHandles::ClearWeakness(Object** location) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000442 Node::FromLocation(location)->ClearWeakness(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000443}
444
445
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000446void GlobalHandles::MarkIndependent(Object** location) {
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000447 Node::FromLocation(location)->MarkIndependent();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000448}
449
450
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000451bool GlobalHandles::IsNearDeath(Object** location) {
452 return Node::FromLocation(location)->IsNearDeath();
453}
454
455
456bool GlobalHandles::IsWeak(Object** location) {
457 return Node::FromLocation(location)->IsWeak();
458}
459
460
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000461void GlobalHandles::SetWrapperClassId(Object** location, uint16_t class_id) {
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000462 Node::FromLocation(location)->set_wrapper_class_id(class_id);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000463}
464
465
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000466void GlobalHandles::IterateWeakRoots(ObjectVisitor* v) {
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000467 for (NodeIterator it(this); !it.done(); it.Advance()) {
468 if (it.node()->IsWeakRetainer()) v->VisitPointer(it.node()->location());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000469 }
470}
471
472
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000473void GlobalHandles::IterateWeakRoots(WeakReferenceGuest f,
474 WeakReferenceCallback callback) {
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000475 for (NodeIterator it(this); !it.done(); it.Advance()) {
476 if (it.node()->IsWeak() && it.node()->callback() == callback) {
477 f(it.node()->object(), it.node()->parameter());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000478 }
479 }
480}
481
482
ager@chromium.org9085a012009-05-11 19:22:57 +0000483void GlobalHandles::IdentifyWeakHandles(WeakSlotCallback f) {
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000484 for (NodeIterator it(this); !it.done(); it.Advance()) {
485 if (it.node()->IsWeak() && f(it.node()->location())) {
486 it.node()->MarkPending();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000487 }
488 }
489}
490
491
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000492void GlobalHandles::IterateNewSpaceStrongAndDependentRoots(ObjectVisitor* v) {
493 for (int i = 0; i < new_space_nodes_.length(); ++i) {
494 Node* node = new_space_nodes_[i];
495 if (node->IsStrongRetainer() ||
496 (node->IsWeakRetainer() && !node->is_independent())) {
497 v->VisitPointer(node->location());
498 }
499 }
500}
501
502
503void GlobalHandles::IdentifyNewSpaceWeakIndependentHandles(
504 WeakSlotCallbackWithHeap f) {
505 for (int i = 0; i < new_space_nodes_.length(); ++i) {
506 Node* node = new_space_nodes_[i];
507 ASSERT(node->is_in_new_space_list());
508 if (node->is_independent() && node->IsWeak() &&
509 f(isolate_->heap(), node->location())) {
510 node->MarkPending();
511 }
512 }
513}
514
515
516void GlobalHandles::IterateNewSpaceWeakIndependentRoots(ObjectVisitor* v) {
517 for (int i = 0; i < new_space_nodes_.length(); ++i) {
518 Node* node = new_space_nodes_[i];
519 ASSERT(node->is_in_new_space_list());
520 if (node->is_independent() && node->IsWeakRetainer()) {
521 v->VisitPointer(node->location());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000522 }
523 }
524}
525
526
527bool GlobalHandles::PostGarbageCollectionProcessing(
528 GarbageCollector collector) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000529 // Process weak global handle callbacks. This must be done after the
530 // GC is completely done, because the callbacks may invoke arbitrary
531 // API functions.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000532 ASSERT(isolate_->heap()->gc_state() == Heap::NOT_IN_GC);
533 const int initial_post_gc_processing_count = ++post_gc_processing_count_;
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000534 bool next_gc_likely_to_collect_more = false;
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000535 if (collector == SCAVENGER) {
536 for (int i = 0; i < new_space_nodes_.length(); ++i) {
537 Node* node = new_space_nodes_[i];
538 ASSERT(node->is_in_new_space_list());
539 // Skip dependent handles. Their weak callbacks might expect to be
540 // called between two global garbage collection callbacks which
541 // are not called for minor collections.
542 if (!node->is_independent()) continue;
543 if (node->PostGarbageCollectionProcessing(isolate_, this)) {
544 if (initial_post_gc_processing_count != post_gc_processing_count_) {
545 // Weak callback triggered another GC and another round of
546 // PostGarbageCollection processing. The current node might
547 // have been deleted in that round, so we need to bail out (or
548 // restart the processing).
549 return next_gc_likely_to_collect_more;
550 }
551 }
552 if (!node->IsRetainer()) {
553 next_gc_likely_to_collect_more = true;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000554 }
555 }
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000556 } else {
557 for (NodeIterator it(this); !it.done(); it.Advance()) {
558 if (it.node()->PostGarbageCollectionProcessing(isolate_, this)) {
559 if (initial_post_gc_processing_count != post_gc_processing_count_) {
560 // See the comment above.
561 return next_gc_likely_to_collect_more;
562 }
ager@chromium.org3811b432009-10-28 14:53:37 +0000563 }
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000564 if (!it.node()->IsRetainer()) {
565 next_gc_likely_to_collect_more = true;
566 }
567 }
568 }
569 // Update the list of new space nodes.
570 int last = 0;
571 for (int i = 0; i < new_space_nodes_.length(); ++i) {
572 Node* node = new_space_nodes_[i];
573 ASSERT(node->is_in_new_space_list());
574 if (node->IsRetainer() && isolate_->heap()->InNewSpace(node->object())) {
575 new_space_nodes_[last++] = node;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576 } else {
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000577 node->set_in_new_space_list(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000578 }
579 }
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000580 new_space_nodes_.Rewind(last);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000581 return next_gc_likely_to_collect_more;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000582}
583
584
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000585void GlobalHandles::IterateStrongRoots(ObjectVisitor* v) {
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000586 for (NodeIterator it(this); !it.done(); it.Advance()) {
587 if (it.node()->IsStrongRetainer()) {
588 v->VisitPointer(it.node()->location());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000589 }
590 }
591}
592
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000593
594void GlobalHandles::IterateAllRoots(ObjectVisitor* v) {
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000595 for (NodeIterator it(this); !it.done(); it.Advance()) {
596 if (it.node()->IsRetainer()) {
597 v->VisitPointer(it.node()->location());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000598 }
599 }
600}
601
602
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000603void GlobalHandles::IterateAllRootsWithClassIds(ObjectVisitor* v) {
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000604 for (NodeIterator it(this); !it.done(); it.Advance()) {
605 if (it.node()->has_wrapper_class_id() && it.node()->IsRetainer()) {
606 v->VisitEmbedderReference(it.node()->location(),
607 it.node()->wrapper_class_id());
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000608 }
609 }
610}
611
612
ager@chromium.org60121232009-12-03 11:25:37 +0000613void GlobalHandles::RecordStats(HeapStats* stats) {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000614 *stats->global_handle_count = 0;
615 *stats->weak_global_handle_count = 0;
616 *stats->pending_global_handle_count = 0;
617 *stats->near_death_global_handle_count = 0;
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000618 *stats->free_global_handle_count = 0;
619 for (NodeIterator it(this); !it.done(); it.Advance()) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000620 *stats->global_handle_count += 1;
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000621 if (it.node()->state() == Node::WEAK) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000622 *stats->weak_global_handle_count += 1;
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000623 } else if (it.node()->state() == Node::PENDING) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000624 *stats->pending_global_handle_count += 1;
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000625 } else if (it.node()->state() == Node::NEAR_DEATH) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000626 *stats->near_death_global_handle_count += 1;
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000627 } else if (it.node()->state() == Node::FREE) {
628 *stats->free_global_handle_count += 1;
ager@chromium.org60121232009-12-03 11:25:37 +0000629 }
630 }
631}
632
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000633#ifdef DEBUG
634
635void GlobalHandles::PrintStats() {
636 int total = 0;
637 int weak = 0;
638 int pending = 0;
639 int near_death = 0;
640 int destroyed = 0;
641
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000642 for (NodeIterator it(this); !it.done(); it.Advance()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000643 total++;
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000644 if (it.node()->state() == Node::WEAK) weak++;
645 if (it.node()->state() == Node::PENDING) pending++;
646 if (it.node()->state() == Node::NEAR_DEATH) near_death++;
647 if (it.node()->state() == Node::FREE) destroyed++;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000648 }
649
650 PrintF("Global Handle Statistics:\n");
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000651 PrintF(" allocated memory = %" V8_PTR_PREFIX "dB\n", sizeof(Node) * total);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652 PrintF(" # weak = %d\n", weak);
653 PrintF(" # pending = %d\n", pending);
654 PrintF(" # near_death = %d\n", near_death);
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000655 PrintF(" # free = %d\n", destroyed);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000656 PrintF(" # total = %d\n", total);
657}
658
659void GlobalHandles::Print() {
660 PrintF("Global handles:\n");
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000661 for (NodeIterator it(this); !it.done(); it.Advance()) {
662 PrintF(" handle %p to %p%s\n",
663 reinterpret_cast<void*>(it.node()->location()),
664 reinterpret_cast<void*>(it.node()->object()),
665 it.node()->IsWeak() ? " (weak)" : "");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000666 }
667}
668
669#endif
670
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000671
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000672
673void GlobalHandles::AddObjectGroup(Object*** handles,
674 size_t length,
675 v8::RetainedObjectInfo* info) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000676#ifdef DEBUG
677 for (size_t i = 0; i < length; ++i) {
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000678 ASSERT(!Node::FromLocation(handles[i])->is_independent());
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000679 }
680#endif
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000681 if (length == 0) {
682 if (info != NULL) info->Dispose();
683 return;
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000684 }
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000685 object_groups_.Add(ObjectGroup::New(handles, length, info));
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000686}
687
688
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000689void GlobalHandles::AddImplicitReferences(HeapObject** parent,
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000690 Object*** children,
691 size_t length) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000692#ifdef DEBUG
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000693 ASSERT(!Node::FromLocation(BitCast<Object**>(parent))->is_independent());
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000694 for (size_t i = 0; i < length; ++i) {
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000695 ASSERT(!Node::FromLocation(children[i])->is_independent());
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000696 }
697#endif
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000698 if (length == 0) return;
699 implicit_ref_groups_.Add(ImplicitRefGroup::New(parent, children, length));
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000700}
701
702
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000703void GlobalHandles::RemoveObjectGroups() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000704 for (int i = 0; i < object_groups_.length(); i++) {
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000705 object_groups_.at(i)->Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000706 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000707 object_groups_.Clear();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000708}
709
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000710
711void GlobalHandles::RemoveImplicitRefGroups() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000712 for (int i = 0; i < implicit_ref_groups_.length(); i++) {
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000713 implicit_ref_groups_.at(i)->Dispose();
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000714 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000715 implicit_ref_groups_.Clear();
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000716}
717
718
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000719void GlobalHandles::TearDown() {
720 // TODO(1428): invoke weak callbacks.
721}
722
723
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000724} } // namespace v8::internal