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 #include "vm/handles.h" | 5 #include "vm/handles.h" |
6 | 6 |
7 #include "platform/assert.h" | 7 #include "platform/assert.h" |
8 #include "platform/utils.h" | 8 #include "platform/utils.h" |
9 #include "vm/dart_api_state.h" | 9 #include "vm/dart_api_state.h" |
10 #include "vm/flags.h" | 10 #include "vm/flags.h" |
11 #include "vm/os.h" | 11 #include "vm/os.h" |
12 #include "vm/raw_object.h" | 12 #include "vm/raw_object.h" |
13 #include "vm/visitor.h" | 13 #include "vm/visitor.h" |
14 #include "vm/zone.h" | 14 #include "vm/zone.h" |
15 | 15 |
16 #include "vm/handles_impl.h" | 16 #include "vm/handles_impl.h" |
17 | 17 |
18 namespace dart { | 18 namespace dart { |
19 | 19 |
20 DEFINE_FLAG(bool, verify_handles, false, "Verify handles."); | 20 DEFINE_FLAG(bool, verify_handles, false, "Verify handles."); |
21 | 21 |
22 | |
23 VMHandles::~VMHandles() { | 22 VMHandles::~VMHandles() { |
24 if (FLAG_trace_handles) { | 23 if (FLAG_trace_handles) { |
25 OS::PrintErr("*** Handle Counts for 0x(%" Px "):Zone = %d,Scoped = %d\n", | 24 OS::PrintErr("*** Handle Counts for 0x(%" Px "):Zone = %d,Scoped = %d\n", |
26 reinterpret_cast<intptr_t>(this), CountZoneHandles(), | 25 reinterpret_cast<intptr_t>(this), CountZoneHandles(), |
27 CountScopedHandles()); | 26 CountScopedHandles()); |
28 OS::PrintErr("*** Deleting VM handle block 0x%" Px "\n", | 27 OS::PrintErr("*** Deleting VM handle block 0x%" Px "\n", |
29 reinterpret_cast<intptr_t>(this)); | 28 reinterpret_cast<intptr_t>(this)); |
30 } | 29 } |
31 } | 30 } |
32 | 31 |
33 | |
34 void VMHandles::VisitObjectPointers(ObjectPointerVisitor* visitor) { | 32 void VMHandles::VisitObjectPointers(ObjectPointerVisitor* visitor) { |
35 return Handles<kVMHandleSizeInWords, kVMHandlesPerChunk, | 33 return Handles<kVMHandleSizeInWords, kVMHandlesPerChunk, |
36 kOffsetOfRawPtr>::VisitObjectPointers(visitor); | 34 kOffsetOfRawPtr>::VisitObjectPointers(visitor); |
37 } | 35 } |
38 | 36 |
39 | |
40 #if defined(DEBUG) | 37 #if defined(DEBUG) |
41 static bool IsCurrentApiNativeScope(Zone* zone) { | 38 static bool IsCurrentApiNativeScope(Zone* zone) { |
42 ApiNativeScope* scope = ApiNativeScope::Current(); | 39 ApiNativeScope* scope = ApiNativeScope::Current(); |
43 return (scope != NULL) && (scope->zone() == zone); | 40 return (scope != NULL) && (scope->zone() == zone); |
44 } | 41 } |
45 #endif // DEBUG | 42 #endif // DEBUG |
46 | 43 |
47 | |
48 uword VMHandles::AllocateHandle(Zone* zone) { | 44 uword VMHandles::AllocateHandle(Zone* zone) { |
49 DEBUG_ASSERT(!IsCurrentApiNativeScope(zone)); | 45 DEBUG_ASSERT(!IsCurrentApiNativeScope(zone)); |
50 return Handles<kVMHandleSizeInWords, kVMHandlesPerChunk, | 46 return Handles<kVMHandleSizeInWords, kVMHandlesPerChunk, |
51 kOffsetOfRawPtr>::AllocateHandle(zone); | 47 kOffsetOfRawPtr>::AllocateHandle(zone); |
52 } | 48 } |
53 | 49 |
54 | |
55 uword VMHandles::AllocateZoneHandle(Zone* zone) { | 50 uword VMHandles::AllocateZoneHandle(Zone* zone) { |
56 DEBUG_ASSERT(!IsCurrentApiNativeScope(zone)); | 51 DEBUG_ASSERT(!IsCurrentApiNativeScope(zone)); |
57 return Handles<kVMHandleSizeInWords, kVMHandlesPerChunk, | 52 return Handles<kVMHandleSizeInWords, kVMHandlesPerChunk, |
58 kOffsetOfRawPtr>::AllocateZoneHandle(zone); | 53 kOffsetOfRawPtr>::AllocateZoneHandle(zone); |
59 } | 54 } |
60 | 55 |
61 | |
62 bool VMHandles::IsZoneHandle(uword handle) { | 56 bool VMHandles::IsZoneHandle(uword handle) { |
63 return Handles<kVMHandleSizeInWords, kVMHandlesPerChunk, | 57 return Handles<kVMHandleSizeInWords, kVMHandlesPerChunk, |
64 kOffsetOfRawPtr>::IsZoneHandle(handle); | 58 kOffsetOfRawPtr>::IsZoneHandle(handle); |
65 } | 59 } |
66 | 60 |
67 | |
68 int VMHandles::ScopedHandleCount() { | 61 int VMHandles::ScopedHandleCount() { |
69 Thread* thread = Thread::Current(); | 62 Thread* thread = Thread::Current(); |
70 ASSERT(thread->zone() != NULL); | 63 ASSERT(thread->zone() != NULL); |
71 VMHandles* handles = thread->zone()->handles(); | 64 VMHandles* handles = thread->zone()->handles(); |
72 return handles->CountScopedHandles(); | 65 return handles->CountScopedHandles(); |
73 } | 66 } |
74 | 67 |
75 | |
76 int VMHandles::ZoneHandleCount() { | 68 int VMHandles::ZoneHandleCount() { |
77 Thread* thread = Thread::Current(); | 69 Thread* thread = Thread::Current(); |
78 ASSERT(thread->zone() != NULL); | 70 ASSERT(thread->zone() != NULL); |
79 VMHandles* handles = thread->zone()->handles(); | 71 VMHandles* handles = thread->zone()->handles(); |
80 return handles->CountZoneHandles(); | 72 return handles->CountZoneHandles(); |
81 } | 73 } |
82 | 74 |
83 | |
84 void HandleScope::Initialize() { | 75 void HandleScope::Initialize() { |
85 ASSERT(thread()->no_handle_scope_depth() == 0); | 76 ASSERT(thread()->no_handle_scope_depth() == 0); |
86 VMHandles* handles = thread()->zone()->handles(); | 77 VMHandles* handles = thread()->zone()->handles(); |
87 ASSERT(handles != NULL); | 78 ASSERT(handles != NULL); |
88 saved_handle_block_ = handles->scoped_blocks_; | 79 saved_handle_block_ = handles->scoped_blocks_; |
89 saved_handle_slot_ = handles->scoped_blocks_->next_handle_slot(); | 80 saved_handle_slot_ = handles->scoped_blocks_->next_handle_slot(); |
90 #if defined(DEBUG) | 81 #if defined(DEBUG) |
91 link_ = thread()->top_handle_scope(); | 82 link_ = thread()->top_handle_scope(); |
92 thread()->set_top_handle_scope(this); | 83 thread()->set_top_handle_scope(this); |
93 #endif | 84 #endif |
94 } | 85 } |
95 | 86 |
96 | |
97 HandleScope::HandleScope(Thread* thread) : StackResource(thread) { | 87 HandleScope::HandleScope(Thread* thread) : StackResource(thread) { |
98 Initialize(); | 88 Initialize(); |
99 } | 89 } |
100 | 90 |
101 | |
102 HandleScope::~HandleScope() { | 91 HandleScope::~HandleScope() { |
103 ASSERT(thread()->zone() != NULL); | 92 ASSERT(thread()->zone() != NULL); |
104 VMHandles* handles = thread()->zone()->handles(); | 93 VMHandles* handles = thread()->zone()->handles(); |
105 ASSERT(handles != NULL); | 94 ASSERT(handles != NULL); |
106 handles->scoped_blocks_ = saved_handle_block_; | 95 handles->scoped_blocks_ = saved_handle_block_; |
107 handles->scoped_blocks_->set_next_handle_slot(saved_handle_slot_); | 96 handles->scoped_blocks_->set_next_handle_slot(saved_handle_slot_); |
108 #if defined(DEBUG) | 97 #if defined(DEBUG) |
109 handles->VerifyScopedHandleState(); | 98 handles->VerifyScopedHandleState(); |
110 handles->ZapFreeScopedHandles(); | 99 handles->ZapFreeScopedHandles(); |
111 ASSERT(thread()->top_handle_scope() == this); | 100 ASSERT(thread()->top_handle_scope() == this); |
112 thread()->set_top_handle_scope(link_); | 101 thread()->set_top_handle_scope(link_); |
113 #endif | 102 #endif |
114 } | 103 } |
115 | 104 |
116 | |
117 #if defined(DEBUG) | 105 #if defined(DEBUG) |
118 NoHandleScope::NoHandleScope(Thread* thread) : StackResource(thread) { | 106 NoHandleScope::NoHandleScope(Thread* thread) : StackResource(thread) { |
119 thread->IncrementNoHandleScopeDepth(); | 107 thread->IncrementNoHandleScopeDepth(); |
120 } | 108 } |
121 | 109 |
122 | |
123 NoHandleScope::~NoHandleScope() { | 110 NoHandleScope::~NoHandleScope() { |
124 thread()->DecrementNoHandleScopeDepth(); | 111 thread()->DecrementNoHandleScopeDepth(); |
125 } | 112 } |
126 #endif // defined(DEBUG) | 113 #endif // defined(DEBUG) |
127 | 114 |
128 } // namespace dart | 115 } // namespace dart |
OLD | NEW |