diff --git a/runtime/vm/allocation.cc b/runtime/vm/allocation.cc index 82ec2ef25d4..84cb6e9b8ed 100644 --- a/runtime/vm/allocation.cc +++ b/runtime/vm/allocation.cc @@ -10,27 +10,6 @@ namespace dart { -StackResource::StackResource(Isolate* isolate) - : isolate_(isolate), previous_(NULL) { - // We can only have longjumps and exceptions when there is a current - // isolate. If there is no current isolate, we don't need to - // protect this case. - if (isolate) { - previous_ = isolate->top_resource(); - isolate->set_top_resource(this); - } -} - - -StackResource::~StackResource() { - if (isolate()) { - StackResource* top = isolate()->top_resource(); - ASSERT(top == this); - isolate()->set_top_resource(previous_); - } - ASSERT(Isolate::Current() == isolate()); -} - ZoneAllocated::~ZoneAllocated() { UNREACHABLE(); } diff --git a/runtime/vm/allocation.h b/runtime/vm/allocation.h index e77fd3dc629..9c9c56199b1 100644 --- a/runtime/vm/allocation.h +++ b/runtime/vm/allocation.h @@ -6,6 +6,7 @@ #define VM_ALLOCATION_H_ #include "platform/assert.h" +#include "vm/base_isolate.h" #include "vm/globals.h" namespace dart { @@ -36,17 +37,36 @@ class ValueObject { // to a stack frame above the frame where these objects were allocated. class StackResource { public: - explicit StackResource(Isolate* isolate); - virtual ~StackResource(); + explicit StackResource(BaseIsolate* isolate) + : isolate_(isolate), previous_(NULL) { + // We can only have longjumps and exceptions when there is a current + // isolate. If there is no current isolate, we don't need to + // protect this case. + if (isolate) { + previous_ = isolate->top_resource(); + isolate->set_top_resource(this); + } + } - Isolate* isolate() const { return isolate_; } + virtual ~StackResource() { + if (isolate()) { + StackResource* top = isolate()->top_resource(); + ASSERT(top == this); + isolate()->set_top_resource(previous_); + } +#if defined(DEBUG) + BaseIsolate::AssertCurrent(isolate()); +#endif + } + + BaseIsolate* isolate() const { return isolate_; } // The delete operator should be private instead of public, but unfortunately // the compiler complains when compiling the destructors for subclasses. void operator delete(void* pointer) { UNREACHABLE(); } private: - Isolate* isolate_; // Current isolate for this stack resource. + BaseIsolate* isolate_; // Current isolate for this stack resource. StackResource* previous_; void* operator new(uword size); diff --git a/runtime/vm/base_isolate.h b/runtime/vm/base_isolate.h new file mode 100644 index 00000000000..f52c1b0a2a5 --- /dev/null +++ b/runtime/vm/base_isolate.h @@ -0,0 +1,117 @@ +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#ifndef VM_BASE_ISOLATE_H_ +#define VM_BASE_ISOLATE_H_ + +namespace dart { + +class HandleScope; +class StackResource; +class Zone; + +// A BaseIsolate contains just enough functionality to allocate +// StackResources. This allows us to inline the StackResource +// constructor/destructor for performance. +class BaseIsolate { + public: + StackResource* top_resource() const { return top_resource_; } + void set_top_resource(StackResource* value) { top_resource_ = value; } + + Zone* current_zone() const { return current_zone_; } + void set_current_zone(Zone* zone) { current_zone_ = zone; } + + HandleScope* top_handle_scope() const { +#if defined(DEBUG) + return top_handle_scope_; +#else + return 0; +#endif + } + + void set_top_handle_scope(HandleScope* handle_scope) { +#if defined(DEBUG) + top_handle_scope_ = handle_scope; +#endif + } + + int32_t no_handle_scope_depth() const { +#if defined(DEBUG) + return no_handle_scope_depth_; +#else + return 0; +#endif + } + + void IncrementNoHandleScopeDepth() { +#if defined(DEBUG) + ASSERT(no_handle_scope_depth_ < INT_MAX); + no_handle_scope_depth_ += 1; +#endif + } + + void DecrementNoHandleScopeDepth() { +#if defined(DEBUG) + ASSERT(no_handle_scope_depth_ > 0); + no_handle_scope_depth_ -= 1; +#endif + } + + int32_t no_gc_scope_depth() const { +#if defined(DEBUG) + return no_gc_scope_depth_; +#else + return 0; +#endif + } + + void IncrementNoGCScopeDepth() { +#if defined(DEBUG) + ASSERT(no_gc_scope_depth_ < INT_MAX); + no_gc_scope_depth_ += 1; +#endif + } + + void DecrementNoGCScopeDepth() { +#if defined(DEBUG) + ASSERT(no_gc_scope_depth_ > 0); + no_gc_scope_depth_ -= 1; +#endif + } + +#if defined(DEBUG) + static void AssertCurrent(BaseIsolate* isolate); +#endif + + protected: + BaseIsolate() + : top_resource_(NULL), +#if defined(DEBUG) + current_zone_(NULL), + top_handle_scope_(NULL), + no_handle_scope_depth_(0), + no_gc_scope_depth_(0) +#else + current_zone_(NULL) +#endif + {} + + ~BaseIsolate() { + // Do not delete stack resources: top_resource_ and current_zone_. + } + + StackResource* top_resource_; + Zone* current_zone_; +#if defined(DEBUG) + HandleScope* top_handle_scope_; + int32_t no_handle_scope_depth_; + int32_t no_gc_scope_depth_; +#endif + + DISALLOW_COPY_AND_ASSIGN(BaseIsolate); +}; + +} // namespace dart + +#endif // VM_BASE_ISOLATE_H_ diff --git a/runtime/vm/handles.cc b/runtime/vm/handles.cc index 40956576936..2244c14d7f7 100644 --- a/runtime/vm/handles.cc +++ b/runtime/vm/handles.cc @@ -69,7 +69,7 @@ int VMHandles::ZoneHandleCount() { } -HandleScope::HandleScope(Isolate* isolate) : StackResource(isolate) { +HandleScope::HandleScope(BaseIsolate* isolate) : StackResource(isolate) { ASSERT(isolate->no_handle_scope_depth() == 0); VMHandles* handles = isolate->current_zone()->handles(); ASSERT(handles != NULL); @@ -98,7 +98,7 @@ HandleScope::~HandleScope() { #if defined(DEBUG) -NoHandleScope::NoHandleScope(Isolate* isolate) : StackResource(isolate) { +NoHandleScope::NoHandleScope(BaseIsolate* isolate) : StackResource(isolate) { isolate->IncrementNoHandleScopeDepth(); } diff --git a/runtime/vm/handles.h b/runtime/vm/handles.h index c2987c375c1..cdb24d59d67 100644 --- a/runtime/vm/handles.h +++ b/runtime/vm/handles.h @@ -266,7 +266,7 @@ class VMHandles : public HandlesVerify()); // Clean up debugger resources. Shutting down the debugger diff --git a/runtime/vm/isolate.h b/runtime/vm/isolate.h index 6c94c8b983d..ab8f7001a34 100644 --- a/runtime/vm/isolate.h +++ b/runtime/vm/isolate.h @@ -8,6 +8,7 @@ #include "include/dart_api.h" #include "platform/assert.h" #include "platform/thread.h" +#include "vm/base_isolate.h" #include "vm/gc_callbacks.h" #include "vm/store_buffer.h" #include "vm/timer.h" @@ -33,7 +34,7 @@ class StubCode; class Zone; -class Isolate { +class Isolate : public BaseIsolate { public: ~Isolate(); @@ -83,9 +84,6 @@ class Isolate { return OFFSET_OF(Isolate, object_store_); } - StackResource* top_resource() const { return top_resource_; } - void set_top_resource(StackResource* value) { top_resource_ = value; } - RawContext* top_context() const { return top_context_; } void set_top_context(RawContext* value) { top_context_ = value; } static intptr_t top_context_offset() { @@ -117,70 +115,10 @@ class Isolate { TimerList& timer_list() { return timer_list_; } - Zone* current_zone() const { return current_zone_; } - void set_current_zone(Zone* zone) { current_zone_ = zone; } static intptr_t current_zone_offset() { return OFFSET_OF(Isolate, current_zone_); } - int32_t no_gc_scope_depth() const { -#if defined(DEBUG) - return no_gc_scope_depth_; -#else - return 0; -#endif - } - - void IncrementNoGCScopeDepth() { -#if defined(DEBUG) - ASSERT(no_gc_scope_depth_ < INT_MAX); - no_gc_scope_depth_ += 1; -#endif - } - - void DecrementNoGCScopeDepth() { -#if defined(DEBUG) - ASSERT(no_gc_scope_depth_ > 0); - no_gc_scope_depth_ -= 1; -#endif - } - - int32_t no_handle_scope_depth() const { -#if defined(DEBUG) - return no_handle_scope_depth_; -#else - return 0; -#endif - } - - void IncrementNoHandleScopeDepth() { -#if defined(DEBUG) - ASSERT(no_handle_scope_depth_ < INT_MAX); - no_handle_scope_depth_ += 1; -#endif - } - - void DecrementNoHandleScopeDepth() { -#if defined(DEBUG) - ASSERT(no_handle_scope_depth_ > 0); - no_handle_scope_depth_ -= 1; -#endif - } - - HandleScope* top_handle_scope() const { -#if defined(DEBUG) - return top_handle_scope_; -#else - return 0; -#endif - } - - void set_top_handle_scope(HandleScope* handle_scope) { -#if defined(DEBUG) - top_handle_scope_ = handle_scope; -#endif - } - void set_init_callback_data(void* value) { init_callback_data_ = value; } @@ -263,14 +201,7 @@ class Isolate { Dart_Port main_port_; Heap* heap_; ObjectStore* object_store_; - StackResource* top_resource_; RawContext* top_context_; - Zone* current_zone_; -#if defined(DEBUG) - int32_t no_gc_scope_depth_; - int32_t no_handle_scope_depth_; - HandleScope* top_handle_scope_; -#endif int32_t random_seed_; uword top_exit_frame_info_; void* init_callback_data_; diff --git a/runtime/vm/vm_sources.gypi b/runtime/vm/vm_sources.gypi index 66912d254f2..f47efd2cd4c 100644 --- a/runtime/vm/vm_sources.gypi +++ b/runtime/vm/vm_sources.gypi @@ -30,6 +30,7 @@ 'ast_printer.h', 'ast_printer.cc', 'ast_printer_test.cc', + 'base_isolate.h', 'bigint_operations.cc', 'bigint_operations.h', 'bigint_operations_test.cc', diff --git a/runtime/vm/zone.cc b/runtime/vm/zone.cc index 18c0cff9ed8..bd6d8409f05 100644 --- a/runtime/vm/zone.cc +++ b/runtime/vm/zone.cc @@ -216,7 +216,7 @@ void BaseZone::DumpZoneSizes() { #endif -Zone::Zone(Isolate* isolate) +Zone::Zone(BaseIsolate* isolate) : StackResource(isolate), zone_(), handles_(), diff --git a/runtime/vm/zone.h b/runtime/vm/zone.h index a250afcca28..8c5677a3ccd 100644 --- a/runtime/vm/zone.h +++ b/runtime/vm/zone.h @@ -99,7 +99,7 @@ class BaseZone { class Zone : public StackResource { public: // Create an empty zone and set is at the current zone for the Isolate. - explicit Zone(Isolate* isolate); + explicit Zone(BaseIsolate* isolate); // Delete all memory associated with the zone. ~Zone();