Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/global-handles.h" | 5 #include "src/global-handles.h" |
| 6 | 6 |
| 7 #include "src/api.h" | 7 #include "src/api.h" |
| 8 #include "src/v8.h" | 8 #include "src/v8.h" |
| 9 #include "src/vm-state-inl.h" | 9 #include "src/vm-state-inl.h" |
| 10 | 10 |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 190 // Check for PENDING to ensure correct answer when processing callbacks. | 190 // Check for PENDING to ensure correct answer when processing callbacks. |
| 191 return state() == PENDING || state() == NEAR_DEATH; | 191 return state() == PENDING || state() == NEAR_DEATH; |
| 192 } | 192 } |
| 193 | 193 |
| 194 bool IsWeak() const { return state() == WEAK; } | 194 bool IsWeak() const { return state() == WEAK; } |
| 195 | 195 |
| 196 bool IsInUse() const { return state() != FREE; } | 196 bool IsInUse() const { return state() != FREE; } |
| 197 | 197 |
| 198 bool IsRetainer() const { | 198 bool IsRetainer() const { |
| 199 return state() != FREE && | 199 return state() != FREE && |
| 200 !(state() == NEAR_DEATH && weakness_type() != NORMAL_WEAK); | 200 !(state() == NEAR_DEATH && weakness_type() != NORMAL_WEAK && |
| 201 weakness_type() != FINALIZER_WEAK); | |
| 201 } | 202 } |
| 202 | 203 |
| 203 bool IsStrongRetainer() const { return state() == NORMAL; } | 204 bool IsStrongRetainer() const { return state() == NORMAL; } |
| 204 | 205 |
| 205 bool IsWeakRetainer() const { | 206 bool IsWeakRetainer() const { |
| 206 return state() == WEAK || state() == PENDING || | 207 return state() == WEAK || state() == PENDING || |
| 207 (state() == NEAR_DEATH && weakness_type() == NORMAL_WEAK); | 208 (state() == NEAR_DEATH && weakness_type() == NORMAL_WEAK && |
| 209 weakness_type() != FINALIZER_WEAK); | |
| 208 } | 210 } |
| 209 | 211 |
| 210 void MarkPending() { | 212 void MarkPending() { |
| 211 DCHECK(state() == WEAK); | 213 DCHECK(state() == WEAK); |
| 212 set_state(PENDING); | 214 set_state(PENDING); |
| 213 } | 215 } |
| 214 | 216 |
| 215 // Independent flag accessors. | 217 // Independent flag accessors. |
| 216 void MarkIndependent() { | 218 void MarkIndependent() { |
| 217 DCHECK(IsInUse()); | 219 DCHECK(IsInUse()); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 265 v8::WeakCallbackType type) { | 267 v8::WeakCallbackType type) { |
| 266 DCHECK(phantom_callback != nullptr); | 268 DCHECK(phantom_callback != nullptr); |
| 267 DCHECK(IsInUse()); | 269 DCHECK(IsInUse()); |
| 268 CHECK_NE(object_, reinterpret_cast<Object*>(kGlobalHandleZapValue)); | 270 CHECK_NE(object_, reinterpret_cast<Object*>(kGlobalHandleZapValue)); |
| 269 set_state(WEAK); | 271 set_state(WEAK); |
| 270 switch (type) { | 272 switch (type) { |
| 271 case v8::WeakCallbackType::kParameter: | 273 case v8::WeakCallbackType::kParameter: |
| 272 set_weakness_type(PHANTOM_WEAK); | 274 set_weakness_type(PHANTOM_WEAK); |
| 273 break; | 275 break; |
| 274 case v8::WeakCallbackType::kInternalFields: | 276 case v8::WeakCallbackType::kInternalFields: |
| 275 set_weakness_type(PHANTOM_WEAK_2_INTERNAL_FIELDS); | 277 set_weakness_type(PHANTOM_WEAK_2_INTERNAL_FIELDS); |
| 276 break; | 278 break; |
| 279 case v8::WeakCallbackType::kFinalizer: | |
| 280 set_weakness_type(NORMAL_WEAK); | |
| 281 break; | |
| 277 } | 282 } |
| 278 set_parameter(parameter); | 283 set_parameter(parameter); |
| 279 weak_callback_ = reinterpret_cast<WeakCallback>(phantom_callback); | 284 weak_callback_ = reinterpret_cast<WeakCallback>(phantom_callback); |
| 280 } | 285 } |
| 281 | 286 |
| 282 void* ClearWeakness() { | 287 void* ClearWeakness() { |
| 283 DCHECK(IsInUse()); | 288 DCHECK(IsInUse()); |
| 284 void* p = parameter(); | 289 void* p = parameter(); |
| 285 set_state(NORMAL); | 290 set_state(NORMAL); |
| 286 set_parameter(NULL); | 291 set_parameter(NULL); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 325 return false; | 330 return false; |
| 326 } | 331 } |
| 327 set_state(NEAR_DEATH); | 332 set_state(NEAR_DEATH); |
| 328 | 333 |
| 329 // Check that we are not passing a finalized external string to | 334 // Check that we are not passing a finalized external string to |
| 330 // the callback. | 335 // the callback. |
| 331 DCHECK(!object_->IsExternalOneByteString() || | 336 DCHECK(!object_->IsExternalOneByteString() || |
| 332 ExternalOneByteString::cast(object_)->resource() != NULL); | 337 ExternalOneByteString::cast(object_)->resource() != NULL); |
| 333 DCHECK(!object_->IsExternalTwoByteString() || | 338 DCHECK(!object_->IsExternalTwoByteString() || |
| 334 ExternalTwoByteString::cast(object_)->resource() != NULL); | 339 ExternalTwoByteString::cast(object_)->resource() != NULL); |
| 335 if (weakness_type() != NORMAL_WEAK) return false; | 340 if (weakness_type() != NORMAL_WEAK && weakness_type() != FINALIZER_WEAK) { |
| 341 return false; | |
| 342 } | |
| 336 | 343 |
| 337 // Leaving V8. | 344 // Leaving V8. |
| 338 VMState<EXTERNAL> vmstate(isolate); | 345 VMState<EXTERNAL> vmstate(isolate); |
| 339 HandleScope handle_scope(isolate); | 346 HandleScope handle_scope(isolate); |
| 340 Object** object = location(); | 347 if (weakness_type() == NORMAL_WEAK) { |
| 341 Handle<Object> handle(*object, isolate); | 348 Object** object = location(); |
| 342 v8::WeakCallbackData<v8::Value, void> data( | 349 Handle<Object> handle(*object, isolate); |
| 343 reinterpret_cast<v8::Isolate*>(isolate), parameter(), | 350 v8::WeakCallbackData<v8::Value, void> data( |
| 344 v8::Utils::ToLocal(handle)); | 351 reinterpret_cast<v8::Isolate*>(isolate), parameter(), |
| 345 set_parameter(NULL); | 352 v8::Utils::ToLocal(handle)); |
| 346 weak_callback_(data); | 353 set_parameter(NULL); |
| 354 weak_callback_(data); | |
| 355 } else { | |
| 356 void* internal_fields[v8::kInternalFieldsInWeakCallback] = {nullptr, | |
| 357 nullptr}; | |
|
noordhuis
2016/04/14 14:52:36
Femto-nit, but other places that depend on v8::kIn
jochen (gone - plz use gerrit)
2016/04/14 15:16:04
i copied this from https://code.google.com/p/chrom
| |
| 358 v8::WeakCallbackInfo<void> data(reinterpret_cast<v8::Isolate*>(isolate), | |
| 359 parameter(), internal_fields, nullptr); | |
| 360 auto callback = reinterpret_cast<v8::WeakCallbackInfo<void>::Callback>( | |
| 361 weak_callback_); | |
| 362 callback(data); | |
| 363 } | |
| 347 | 364 |
| 348 // Absence of explicit cleanup or revival of weak handle | 365 // Absence of explicit cleanup or revival of weak handle |
| 349 // in most of the cases would lead to memory leak. | 366 // in most of the cases would lead to memory leak. |
| 350 CHECK(state() != NEAR_DEATH); | 367 CHECK(state() != NEAR_DEATH); |
| 351 return true; | 368 return true; |
| 352 } | 369 } |
| 353 | 370 |
| 354 inline GlobalHandles* GetGlobalHandles(); | 371 inline GlobalHandles* GetGlobalHandles(); |
| 355 | 372 |
| 356 private: | 373 private: |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 643 bool GlobalHandles::IsWeak(Object** location) { | 660 bool GlobalHandles::IsWeak(Object** location) { |
| 644 return Node::FromLocation(location)->IsWeak(); | 661 return Node::FromLocation(location)->IsWeak(); |
| 645 } | 662 } |
| 646 | 663 |
| 647 void GlobalHandles::IterateWeakRoots(ObjectVisitor* v) { | 664 void GlobalHandles::IterateWeakRoots(ObjectVisitor* v) { |
| 648 for (NodeIterator it(this); !it.done(); it.Advance()) { | 665 for (NodeIterator it(this); !it.done(); it.Advance()) { |
| 649 Node* node = it.node(); | 666 Node* node = it.node(); |
| 650 if (node->IsWeakRetainer()) { | 667 if (node->IsWeakRetainer()) { |
| 651 // Pending weak phantom handles die immediately. Everything else survives. | 668 // Pending weak phantom handles die immediately. Everything else survives. |
| 652 if (node->state() == Node::PENDING && | 669 if (node->state() == Node::PENDING && |
| 653 node->weakness_type() != NORMAL_WEAK) { | 670 node->weakness_type() != NORMAL_WEAK && |
| 654 node->CollectPhantomCallbackData(isolate(), | 671 node->weakness_type() != FINALIZER_WEAK) { |
| 655 &pending_phantom_callbacks_); | 672 node->CollectPhantomCallbackData(isolate(), |
| 673 &pending_phantom_callbacks_); | |
| 656 } else { | 674 } else { |
| 657 v->VisitPointer(node->location()); | 675 v->VisitPointer(node->location()); |
| 658 } | 676 } |
| 659 } | 677 } |
| 660 } | 678 } |
| 661 } | 679 } |
| 662 | 680 |
| 663 | 681 |
| 664 void GlobalHandles::IdentifyWeakHandles(WeakSlotCallback f) { | 682 void GlobalHandles::IdentifyWeakHandles(WeakSlotCallback f) { |
| 665 for (NodeIterator it(this); !it.done(); it.Advance()) { | 683 for (NodeIterator it(this); !it.done(); it.Advance()) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 704 | 722 |
| 705 | 723 |
| 706 void GlobalHandles::IterateNewSpaceWeakIndependentRoots(ObjectVisitor* v) { | 724 void GlobalHandles::IterateNewSpaceWeakIndependentRoots(ObjectVisitor* v) { |
| 707 for (int i = 0; i < new_space_nodes_.length(); ++i) { | 725 for (int i = 0; i < new_space_nodes_.length(); ++i) { |
| 708 Node* node = new_space_nodes_[i]; | 726 Node* node = new_space_nodes_[i]; |
| 709 DCHECK(node->is_in_new_space_list()); | 727 DCHECK(node->is_in_new_space_list()); |
| 710 if ((node->is_independent() || node->is_partially_dependent()) && | 728 if ((node->is_independent() || node->is_partially_dependent()) && |
| 711 node->IsWeakRetainer()) { | 729 node->IsWeakRetainer()) { |
| 712 // Pending weak phantom handles die immediately. Everything else survives. | 730 // Pending weak phantom handles die immediately. Everything else survives. |
| 713 if (node->state() == Node::PENDING && | 731 if (node->state() == Node::PENDING && |
| 714 node->weakness_type() != NORMAL_WEAK) { | 732 node->weakness_type() != NORMAL_WEAK && |
| 733 node->weakness_type() != FINALIZER_WEAK) { | |
| 715 node->CollectPhantomCallbackData(isolate(), | 734 node->CollectPhantomCallbackData(isolate(), |
| 716 &pending_phantom_callbacks_); | 735 &pending_phantom_callbacks_); |
| 717 } else { | 736 } else { |
| 718 v->VisitPointer(node->location()); | 737 v->VisitPointer(node->location()); |
| 719 } | 738 } |
| 720 } | 739 } |
| 721 } | 740 } |
| 722 } | 741 } |
| 723 | 742 |
| 724 | 743 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 747 | 766 |
| 748 | 767 |
| 749 void GlobalHandles::IterateNewSpaceWeakUnmodifiedRoots(ObjectVisitor* v) { | 768 void GlobalHandles::IterateNewSpaceWeakUnmodifiedRoots(ObjectVisitor* v) { |
| 750 for (int i = 0; i < new_space_nodes_.length(); ++i) { | 769 for (int i = 0; i < new_space_nodes_.length(); ++i) { |
| 751 Node* node = new_space_nodes_[i]; | 770 Node* node = new_space_nodes_[i]; |
| 752 DCHECK(node->is_in_new_space_list()); | 771 DCHECK(node->is_in_new_space_list()); |
| 753 if ((node->is_independent() || !node->is_active()) && | 772 if ((node->is_independent() || !node->is_active()) && |
| 754 node->IsWeakRetainer()) { | 773 node->IsWeakRetainer()) { |
| 755 // Pending weak phantom handles die immediately. Everything else survives. | 774 // Pending weak phantom handles die immediately. Everything else survives. |
| 756 if (node->state() == Node::PENDING && | 775 if (node->state() == Node::PENDING && |
| 757 node->weakness_type() != NORMAL_WEAK) { | 776 node->weakness_type() != NORMAL_WEAK && |
| 777 node->weakness_type() != FINALIZER_WEAK) { | |
| 758 node->CollectPhantomCallbackData(isolate(), | 778 node->CollectPhantomCallbackData(isolate(), |
| 759 &pending_phantom_callbacks_); | 779 &pending_phantom_callbacks_); |
| 760 } else { | 780 } else { |
| 761 v->VisitPointer(node->location()); | 781 v->VisitPointer(node->location()); |
| 762 } | 782 } |
| 763 } | 783 } |
| 764 } | 784 } |
| 765 } | 785 } |
| 766 | 786 |
| 767 | 787 |
| (...skipping 713 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1481 blocks_[block][offset] = object; | 1501 blocks_[block][offset] = object; |
| 1482 if (isolate->heap()->InNewSpace(object)) { | 1502 if (isolate->heap()->InNewSpace(object)) { |
| 1483 new_space_indices_.Add(size_); | 1503 new_space_indices_.Add(size_); |
| 1484 } | 1504 } |
| 1485 *index = size_++; | 1505 *index = size_++; |
| 1486 } | 1506 } |
| 1487 | 1507 |
| 1488 | 1508 |
| 1489 } // namespace internal | 1509 } // namespace internal |
| 1490 } // namespace v8 | 1510 } // namespace v8 |
| OLD | NEW |