| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef VM_DART_API_STATE_H_ | 5 #ifndef VM_DART_API_STATE_H_ |
| 6 #define VM_DART_API_STATE_H_ | 6 #define VM_DART_API_STATE_H_ |
| 7 | 7 |
| 8 #include "include/dart_api.h" | 8 #include "include/dart_api.h" |
| 9 | 9 |
| 10 #include "platform/utils.h" | 10 #include "platform/utils.h" |
| (...skipping 650 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 661 }; | 661 }; |
| 662 | 662 |
| 663 | 663 |
| 664 // Implementation of the API State used in dart api for maintaining | 664 // Implementation of the API State used in dart api for maintaining |
| 665 // local scopes, persistent handles etc. These are setup on a per isolate | 665 // local scopes, persistent handles etc. These are setup on a per isolate |
| 666 // basis and destroyed when the isolate is shutdown. | 666 // basis and destroyed when the isolate is shutdown. |
| 667 class ApiState { | 667 class ApiState { |
| 668 public: | 668 public: |
| 669 ApiState() : persistent_handles_(), | 669 ApiState() : persistent_handles_(), |
| 670 weak_persistent_handles_(), | 670 weak_persistent_handles_(), |
| 671 reusable_scope_(NULL), | |
| 672 top_scope_(NULL), | |
| 673 null_(NULL), | 671 null_(NULL), |
| 674 true_(NULL), | 672 true_(NULL), |
| 675 false_(NULL), | 673 false_(NULL), |
| 676 acquired_error_(NULL) {} | 674 acquired_error_(NULL) {} |
| 677 ~ApiState() { | 675 ~ApiState() { |
| 678 while (top_scope_ != NULL) { | |
| 679 ApiLocalScope* scope = top_scope_; | |
| 680 top_scope_ = top_scope_->previous(); | |
| 681 delete scope; | |
| 682 } | |
| 683 if (null_ != NULL) { | 676 if (null_ != NULL) { |
| 684 persistent_handles().FreeHandle(null_); | 677 persistent_handles().FreeHandle(null_); |
| 685 null_ = NULL; | 678 null_ = NULL; |
| 686 } | 679 } |
| 687 if (true_ != NULL) { | 680 if (true_ != NULL) { |
| 688 persistent_handles().FreeHandle(true_); | 681 persistent_handles().FreeHandle(true_); |
| 689 true_ = NULL; | 682 true_ = NULL; |
| 690 } | 683 } |
| 691 if (false_ != NULL) { | 684 if (false_ != NULL) { |
| 692 persistent_handles().FreeHandle(false_); | 685 persistent_handles().FreeHandle(false_); |
| 693 false_ = NULL; | 686 false_ = NULL; |
| 694 } | 687 } |
| 695 if (acquired_error_ != NULL) { | 688 if (acquired_error_ != NULL) { |
| 696 persistent_handles().FreeHandle(acquired_error_); | 689 persistent_handles().FreeHandle(acquired_error_); |
| 697 acquired_error_ = NULL; | 690 acquired_error_ = NULL; |
| 698 } | 691 } |
| 699 } | 692 } |
| 700 | 693 |
| 701 // Accessors. | 694 // Accessors. |
| 702 ApiLocalScope* reusable_scope() const { return reusable_scope_; } | |
| 703 void set_reusable_scope(ApiLocalScope* value) { | |
| 704 ASSERT(value == NULL || reusable_scope_ == NULL); | |
| 705 reusable_scope_ = value; | |
| 706 } | |
| 707 ApiLocalScope* top_scope() const { return top_scope_; } | |
| 708 void set_top_scope(ApiLocalScope* value) { top_scope_ = value; } | |
| 709 | |
| 710 PersistentHandles& persistent_handles() { return persistent_handles_; } | 695 PersistentHandles& persistent_handles() { return persistent_handles_; } |
| 711 | 696 |
| 712 FinalizablePersistentHandles& weak_persistent_handles() { | 697 FinalizablePersistentHandles& weak_persistent_handles() { |
| 713 return weak_persistent_handles_; | 698 return weak_persistent_handles_; |
| 714 } | 699 } |
| 715 | 700 |
| 716 void UnwindScopes(uword stack_marker) { | |
| 717 // Unwind all scopes using the same stack_marker, i.e. all scopes allocated | |
| 718 // under the same top_exit_frame_info. | |
| 719 while (top_scope_ != NULL && | |
| 720 top_scope_->stack_marker() != 0 && | |
| 721 top_scope_->stack_marker() == stack_marker) { | |
| 722 ApiLocalScope* scope = top_scope_; | |
| 723 top_scope_ = top_scope_->previous(); | |
| 724 delete scope; | |
| 725 } | |
| 726 } | |
| 727 | |
| 728 void VisitObjectPointers(ObjectPointerVisitor* visitor) { | 701 void VisitObjectPointers(ObjectPointerVisitor* visitor) { |
| 729 ApiLocalScope* scope = top_scope_; | |
| 730 while (scope != NULL) { | |
| 731 scope->local_handles()->VisitObjectPointers(visitor); | |
| 732 scope = scope->previous(); | |
| 733 } | |
| 734 persistent_handles().VisitObjectPointers(visitor); | 702 persistent_handles().VisitObjectPointers(visitor); |
| 735 } | 703 } |
| 736 | 704 |
| 737 void VisitWeakHandles(HandleVisitor* visitor) { | 705 void VisitWeakHandles(HandleVisitor* visitor) { |
| 738 weak_persistent_handles().VisitHandles(visitor); | 706 weak_persistent_handles().VisitHandles(visitor); |
| 739 } | 707 } |
| 740 | 708 |
| 741 bool IsValidLocalHandle(Dart_Handle object) const { | |
| 742 ApiLocalScope* scope = top_scope_; | |
| 743 while (scope != NULL) { | |
| 744 if (scope->local_handles()->IsValidHandle(object)) { | |
| 745 return true; | |
| 746 } | |
| 747 scope = scope->previous(); | |
| 748 } | |
| 749 return false; | |
| 750 } | |
| 751 | |
| 752 bool IsValidPersistentHandle(Dart_PersistentHandle object) const { | 709 bool IsValidPersistentHandle(Dart_PersistentHandle object) const { |
| 753 return persistent_handles_.IsValidHandle(object); | 710 return persistent_handles_.IsValidHandle(object); |
| 754 } | 711 } |
| 755 | 712 |
| 756 bool IsValidWeakPersistentHandle(Dart_WeakPersistentHandle object) const { | 713 bool IsValidWeakPersistentHandle(Dart_WeakPersistentHandle object) const { |
| 757 return weak_persistent_handles_.IsValidHandle(object); | 714 return weak_persistent_handles_.IsValidHandle(object); |
| 758 } | 715 } |
| 759 | 716 |
| 760 bool IsProtectedHandle(PersistentHandle* object) const { | 717 bool IsProtectedHandle(PersistentHandle* object) const { |
| 761 if (object == NULL) return false; | 718 if (object == NULL) return false; |
| 762 return object == null_ || object == true_ || object == false_; | 719 return object == null_ || object == true_ || object == false_; |
| 763 } | 720 } |
| 764 | 721 |
| 765 int CountLocalHandles() const { | |
| 766 int total = 0; | |
| 767 ApiLocalScope* scope = top_scope_; | |
| 768 while (scope != NULL) { | |
| 769 total += scope->local_handles()->CountHandles(); | |
| 770 scope = scope->previous(); | |
| 771 } | |
| 772 return total; | |
| 773 } | |
| 774 int CountPersistentHandles() const { | 722 int CountPersistentHandles() const { |
| 775 return persistent_handles_.CountHandles(); | 723 return persistent_handles_.CountHandles(); |
| 776 } | 724 } |
| 777 int ZoneSizeInBytes() const { | |
| 778 int total = 0; | |
| 779 ApiLocalScope* scope = top_scope_; | |
| 780 while (scope != NULL) { | |
| 781 total += scope->zone()->SizeInBytes(); | |
| 782 scope = scope->previous(); | |
| 783 } | |
| 784 return total; | |
| 785 } | |
| 786 | 725 |
| 787 void SetupAcquiredError() { | 726 void SetupAcquiredError() { |
| 788 ASSERT(acquired_error_ == NULL); | 727 ASSERT(acquired_error_ == NULL); |
| 789 const String& msg = String::Handle( | 728 const String& msg = String::Handle( |
| 790 String::New("Internal Dart data pointers have been acquired, " | 729 String::New("Internal Dart data pointers have been acquired, " |
| 791 "please release them using Dart_TypedDataReleaseData.")); | 730 "please release them using Dart_TypedDataReleaseData.")); |
| 792 acquired_error_ = persistent_handles().AllocateHandle(); | 731 acquired_error_ = persistent_handles().AllocateHandle(); |
| 793 acquired_error_->set_raw(ApiError::New(msg)); | 732 acquired_error_->set_raw(ApiError::New(msg)); |
| 794 } | 733 } |
| 795 | 734 |
| 796 PersistentHandle* AcquiredError() const { | 735 PersistentHandle* AcquiredError() const { |
| 797 ASSERT(acquired_error_ != NULL); | 736 ASSERT(acquired_error_ != NULL); |
| 798 return acquired_error_; | 737 return acquired_error_; |
| 799 } | 738 } |
| 800 | 739 |
| 801 WeakTable* acquired_table() { return &acquired_table_; } | 740 WeakTable* acquired_table() { return &acquired_table_; } |
| 802 | 741 |
| 803 private: | 742 private: |
| 804 PersistentHandles persistent_handles_; | 743 PersistentHandles persistent_handles_; |
| 805 FinalizablePersistentHandles weak_persistent_handles_; | 744 FinalizablePersistentHandles weak_persistent_handles_; |
| 806 ApiLocalScope* reusable_scope_; | |
| 807 ApiLocalScope* top_scope_; | |
| 808 WeakTable acquired_table_; | 745 WeakTable acquired_table_; |
| 809 | 746 |
| 810 // Persistent handles to important objects. | 747 // Persistent handles to important objects. |
| 811 PersistentHandle* null_; | 748 PersistentHandle* null_; |
| 812 PersistentHandle* true_; | 749 PersistentHandle* true_; |
| 813 PersistentHandle* false_; | 750 PersistentHandle* false_; |
| 814 PersistentHandle* acquired_error_; | 751 PersistentHandle* acquired_error_; |
| 815 | 752 |
| 816 DISALLOW_COPY_AND_ASSIGN(ApiState); | 753 DISALLOW_COPY_AND_ASSIGN(ApiState); |
| 817 }; | 754 }; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 831 ref->set_peer(peer); | 768 ref->set_peer(peer); |
| 832 ref->set_callback(callback); | 769 ref->set_callback(callback); |
| 833 // This may trigger GC, so it must be called last. | 770 // This may trigger GC, so it must be called last. |
| 834 ref->SetExternalSize(external_size, isolate); | 771 ref->SetExternalSize(external_size, isolate); |
| 835 return ref; | 772 return ref; |
| 836 } | 773 } |
| 837 | 774 |
| 838 } // namespace dart | 775 } // namespace dart |
| 839 | 776 |
| 840 #endif // VM_DART_API_STATE_H_ | 777 #endif // VM_DART_API_STATE_H_ |
| OLD | NEW |