| 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 |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | #include "vm/handles.h" |
| 6 | |
| 7 | #include "platform/assert.h" |
| 8 | #include "platform/utils.h" |
| 9 | #include "vm/dart_api_state.h" |
| 10 | #include "vm/flags.h" |
| 11 | #include "vm/os.h" |
| 12 | #include "vm/raw_object.h" |
| 13 | #include "vm/visitor.h" |
| 14 | #include "vm/zone.h" |
| 15 | |
| 16 | #include "vm/handles_impl.h" |
| 17 | |
| 18 | namespace dart { |
| 19 | |
| 20 | void VMHandles::VisitObjectPointers(ObjectPointerVisitor* visitor) { |
| 21 | return Handles<kVMHandleSizeInWords, kVMHandlesPerChunk, |
| 22 | kOffsetOfRawPtr>::VisitObjectPointers(visitor); |
| 23 | } |
| 24 | |
| 25 | #if defined(DEBUG) |
| 26 | static bool IsCurrentApiNativeScope(Zone* zone) { |
| 27 | ApiNativeScope* scope = ApiNativeScope::Current(); |
| 28 | return (scope != nullptr) && (scope->zone() == zone); |
| 29 | } |
| 30 | #endif // DEBUG |
| 31 | |
| 32 | uword VMHandles::AllocateHandle(Zone* zone) { |
| 33 | DEBUG_ASSERT(!IsCurrentApiNativeScope(zone)); |
| 34 | uword handle = Handles<kVMHandleSizeInWords, kVMHandlesPerChunk, |
| 35 | kOffsetOfRawPtr>::AllocateHandle(zone); |
| 36 | #if defined(DEBUG) |
| 37 | *reinterpret_cast<uword*>(handle + kOffsetOfIsZoneHandle * kWordSize) = 0; |
| 38 | #endif |
| 39 | return handle; |
| 40 | } |
| 41 | |
| 42 | uword VMHandles::AllocateZoneHandle(Zone* zone) { |
| 43 | DEBUG_ASSERT(!IsCurrentApiNativeScope(zone)); |
| 44 | uword handle = Handles<kVMHandleSizeInWords, kVMHandlesPerChunk, |
| 45 | kOffsetOfRawPtr>::AllocateZoneHandle(zone); |
| 46 | #if defined(DEBUG) |
| 47 | *reinterpret_cast<uword*>(handle + kOffsetOfIsZoneHandle * kWordSize) = 1; |
| 48 | #endif |
| 49 | return handle; |
| 50 | } |
| 51 | |
| 52 | #if defined(DEBUG) |
| 53 | bool VMHandles::IsZoneHandle(uword handle) { |
| 54 | return *reinterpret_cast<uword*>(handle + |
| 55 | kOffsetOfIsZoneHandle * kWordSize) != 0; |
| 56 | } |
| 57 | #endif |
| 58 | |
| 59 | int VMHandles::ScopedHandleCount() { |
| 60 | Thread* thread = Thread::Current(); |
| 61 | ASSERT(thread->zone() != nullptr); |
| 62 | VMHandles* handles = thread->zone()->handles(); |
| 63 | return handles->CountScopedHandles(); |
| 64 | } |
| 65 | |
| 66 | int VMHandles::ZoneHandleCount() { |
| 67 | Thread* thread = Thread::Current(); |
| 68 | ASSERT(thread->zone() != nullptr); |
| 69 | VMHandles* handles = thread->zone()->handles(); |
| 70 | return handles->CountZoneHandles(); |
| 71 | } |
| 72 | |
| 73 | void HandleScope::Initialize() { |
| 74 | ASSERT(thread()->MayAllocateHandles()); |
| 75 | VMHandles* handles = thread()->zone()->handles(); |
| 76 | ASSERT(handles != nullptr); |
| 77 | saved_handle_block_ = handles->scoped_blocks_; |
| 78 | saved_handle_slot_ = handles->scoped_blocks_->next_handle_slot(); |
| 79 | #if defined(DEBUG) |
| 80 | link_ = thread()->top_handle_scope(); |
| 81 | thread()->set_top_handle_scope(this); |
| 82 | #endif |
| 83 | } |
| 84 | |
| 85 | HandleScope::HandleScope(ThreadState* thread) : StackResource(thread) { |
| 86 | Initialize(); |
| 87 | } |
| 88 | |
| 89 | HandleScope::~HandleScope() { |
| 90 | ASSERT(thread()->zone() != nullptr); |
| 91 | VMHandles* handles = thread()->zone()->handles(); |
| 92 | ASSERT(handles != nullptr); |
| 93 | #if defined(DEBUG) |
| 94 | VMHandles::HandlesBlock* last = handles->scoped_blocks_; |
| 95 | #endif |
| 96 | handles->scoped_blocks_ = saved_handle_block_; |
| 97 | handles->scoped_blocks_->set_next_handle_slot(saved_handle_slot_); |
| 98 | #if defined(DEBUG) |
| 99 | VMHandles::HandlesBlock* block = handles->scoped_blocks_; |
| 100 | for (;;) { |
| 101 | block->ZapFreeHandles(); |
| 102 | if (block == last) break; |
| 103 | block = block->next_block(); |
| 104 | } |
| 105 | ASSERT(thread()->top_handle_scope() == this); |
| 106 | thread()->set_top_handle_scope(link_); |
| 107 | #endif |
| 108 | } |
| 109 | |
| 110 | } // namespace dart |
| 111 |
