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 RUNTIME_VM_DART_API_STATE_H_ | 5 #ifndef RUNTIME_VM_DART_API_STATE_H_ |
6 #define RUNTIME_VM_DART_API_STATE_H_ | 6 #define RUNTIME_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 656 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
667 }; | 667 }; |
668 | 668 |
669 | 669 |
670 class ApiNativeScope { | 670 class ApiNativeScope { |
671 public: | 671 public: |
672 ApiNativeScope() { | 672 ApiNativeScope() { |
673 // Currently no support for nesting native scopes. | 673 // Currently no support for nesting native scopes. |
674 ASSERT(Current() == NULL); | 674 ASSERT(Current() == NULL); |
675 OSThread::SetThreadLocal(Api::api_native_key_, | 675 OSThread::SetThreadLocal(Api::api_native_key_, |
676 reinterpret_cast<uword>(this)); | 676 reinterpret_cast<uword>(this)); |
| 677 // We manually increment the memory usage counter since there is memory |
| 678 // initially allocated within the zone on creation. |
| 679 IncrementNativeScopeMemoryUsage(zone_.GetZone()->CapacityInBytes()); |
677 } | 680 } |
678 | 681 |
679 ~ApiNativeScope() { | 682 ~ApiNativeScope() { |
680 ASSERT(Current() == this); | 683 ASSERT(Current() == this); |
681 OSThread::SetThreadLocal(Api::api_native_key_, 0); | 684 OSThread::SetThreadLocal(Api::api_native_key_, 0); |
| 685 // We must also manually decrement the memory usage counter since the native |
| 686 // is still holding it's initial memory and ~Zone() won't be able to |
| 687 // determine which memory usage counter to decrement. |
| 688 DecrementNativeScopeMemoryUsage(zone_.GetZone()->CapacityInBytes()); |
682 } | 689 } |
683 | 690 |
684 static inline ApiNativeScope* Current() { | 691 static inline ApiNativeScope* Current() { |
685 return reinterpret_cast<ApiNativeScope*>( | 692 return reinterpret_cast<ApiNativeScope*>( |
686 OSThread::GetThreadLocal(Api::api_native_key_)); | 693 OSThread::GetThreadLocal(Api::api_native_key_)); |
687 } | 694 } |
688 | 695 |
| 696 static intptr_t current_memory_usage() { return current_memory_usage_; } |
| 697 |
| 698 static void IncrementNativeScopeMemoryUsage(intptr_t size) { |
| 699 AtomicOperations::IncrementBy(¤t_memory_usage_, size); |
| 700 } |
| 701 |
| 702 static void DecrementNativeScopeMemoryUsage(intptr_t size) { |
| 703 AtomicOperations::DecrementBy(¤t_memory_usage_, size); |
| 704 } |
| 705 |
689 Zone* zone() { | 706 Zone* zone() { |
690 Zone* result = zone_.GetZone(); | 707 Zone* result = zone_.GetZone(); |
691 ASSERT(result->handles()->CountScopedHandles() == 0); | 708 ASSERT(result->handles()->CountScopedHandles() == 0); |
692 ASSERT(result->handles()->CountZoneHandles() == 0); | 709 ASSERT(result->handles()->CountZoneHandles() == 0); |
693 return result; | 710 return result; |
694 } | 711 } |
695 | 712 |
696 private: | 713 private: |
| 714 // The current total memory usage within ApiNativeScopes. |
| 715 static intptr_t current_memory_usage_; |
| 716 |
697 ApiZone zone_; | 717 ApiZone zone_; |
698 }; | 718 }; |
699 | 719 |
700 | 720 |
701 // Api growable arrays use a zone for allocation. The constructor | 721 // Api growable arrays use a zone for allocation. The constructor |
702 // picks the zone from the current isolate if in an isolate | 722 // picks the zone from the current isolate if in an isolate |
703 // environment. When outside an isolate environment it picks the zone | 723 // environment. When outside an isolate environment it picks the zone |
704 // from the current native scope. | 724 // from the current native scope. |
705 template <typename T> | 725 template <typename T> |
706 class ApiGrowableArray : public BaseGrowableArray<T, ValueObject> { | 726 class ApiGrowableArray : public BaseGrowableArray<T, ValueObject> { |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
841 ref->set_callback(callback); | 861 ref->set_callback(callback); |
842 ref->set_is_queued_for_finalization(false); | 862 ref->set_is_queued_for_finalization(false); |
843 // This may trigger GC, so it must be called last. | 863 // This may trigger GC, so it must be called last. |
844 ref->SetExternalSize(external_size, isolate); | 864 ref->SetExternalSize(external_size, isolate); |
845 return ref; | 865 return ref; |
846 } | 866 } |
847 | 867 |
848 } // namespace dart | 868 } // namespace dart |
849 | 869 |
850 #endif // RUNTIME_VM_DART_API_STATE_H_ | 870 #endif // RUNTIME_VM_DART_API_STATE_H_ |
OLD | NEW |