OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, 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 #ifndef VM_REUSABLE_HANDLES_H_ |
| 6 #define VM_REUSABLE_HANDLES_H_ |
| 7 |
| 8 #include "vm/allocation.h" |
| 9 #include "vm/handles.h" |
| 10 #include "vm/object.h" |
| 11 |
| 12 namespace dart { |
| 13 |
| 14 // The class ReusableHandleScope is used in regions of the |
| 15 // virtual machine where isolate specific reusable handles are used. |
| 16 // This class asserts that we do not add code that will result in recursive |
| 17 // uses of reusable handles. |
| 18 // It is used as follows: |
| 19 // { |
| 20 // ReusableHandleScope reused_handles(isolate); |
| 21 // .... |
| 22 // ..... |
| 23 // code that uses isolate specific reusable handles. |
| 24 // Array& funcs = reused_handles.ArrayHandle(); |
| 25 // .... |
| 26 // } |
| 27 #if defined(DEBUG) |
| 28 class ReusableObjectHandleScope : public StackResource { |
| 29 public: |
| 30 explicit ReusableObjectHandleScope(Isolate* isolate) |
| 31 : StackResource(isolate), isolate_(isolate) { |
| 32 ASSERT(!isolate->reusable_handle_scope_active()); |
| 33 isolate->set_reusable_handle_scope_active(true); |
| 34 } |
| 35 ReusableObjectHandleScope() |
| 36 : StackResource(Isolate::Current()), isolate_(Isolate::Current()) { |
| 37 ASSERT(!isolate()->reusable_handle_scope_active()); |
| 38 isolate()->set_reusable_handle_scope_active(true); |
| 39 } |
| 40 ~ReusableObjectHandleScope() { |
| 41 ASSERT(isolate()->reusable_handle_scope_active()); |
| 42 isolate()->set_reusable_handle_scope_active(false); |
| 43 Handle().raw_ = Object::null(); |
| 44 } |
| 45 Object& Handle() { |
| 46 ASSERT(isolate_->Object_handle_ != NULL); |
| 47 return *isolate_->Object_handle_; |
| 48 } |
| 49 |
| 50 private: |
| 51 Isolate* isolate_; |
| 52 DISALLOW_COPY_AND_ASSIGN(ReusableObjectHandleScope); |
| 53 }; |
| 54 |
| 55 |
| 56 class ReusableHandleScope : public StackResource { |
| 57 public: |
| 58 explicit ReusableHandleScope(Isolate* isolate) |
| 59 : StackResource(isolate), isolate_(isolate) { |
| 60 ASSERT(!isolate->reusable_handle_scope_active()); |
| 61 isolate->set_reusable_handle_scope_active(true); |
| 62 } |
| 63 ReusableHandleScope() |
| 64 : StackResource(Isolate::Current()), isolate_(Isolate::Current()) { |
| 65 ASSERT(!isolate()->reusable_handle_scope_active()); |
| 66 isolate()->set_reusable_handle_scope_active(true); |
| 67 } |
| 68 ~ReusableHandleScope() { |
| 69 ASSERT(isolate()->reusable_handle_scope_active()); |
| 70 isolate()->set_reusable_handle_scope_active(false); |
| 71 #define CLEAR_REUSABLE_HANDLE(object) \ |
| 72 object##Handle().raw_ = Object::null(); \ |
| 73 |
| 74 REUSABLE_HANDLE_LIST(CLEAR_REUSABLE_HANDLE); |
| 75 } |
| 76 |
| 77 #define REUSABLE_HANDLE_ACCESSORS(object) \ |
| 78 object& object##Handle() { \ |
| 79 ASSERT(isolate_->object##_handle_ != NULL); \ |
| 80 return *isolate_->object##_handle_; \ |
| 81 } \ |
| 82 |
| 83 REUSABLE_HANDLE_LIST(REUSABLE_HANDLE_ACCESSORS) |
| 84 #undef REUSABLE_HANDLE_ACCESSORS |
| 85 |
| 86 private: |
| 87 void ResetHandles(); |
| 88 Isolate* isolate_; |
| 89 DISALLOW_COPY_AND_ASSIGN(ReusableHandleScope); |
| 90 }; |
| 91 #else |
| 92 class ReusableObjectHandleScope : public ValueObject { |
| 93 public: |
| 94 explicit ReusableObjectHandleScope(Isolate* isolate) |
| 95 : handle_(isolate->Object_handle_) { |
| 96 } |
| 97 ReusableObjectHandleScope() : handle_(Isolate::Current()->Object_handle_) { |
| 98 } |
| 99 ~ReusableObjectHandleScope() { |
| 100 handle_->raw_ = Object::null(); |
| 101 } |
| 102 Object& Handle() { |
| 103 ASSERT(handle_ != NULL); |
| 104 return *handle_; |
| 105 } |
| 106 |
| 107 private: |
| 108 Object* handle_; |
| 109 DISALLOW_COPY_AND_ASSIGN(ReusableObjectHandleScope); |
| 110 }; |
| 111 |
| 112 |
| 113 class ReusableHandleScope : public ValueObject { |
| 114 public: |
| 115 explicit ReusableHandleScope(Isolate* isolate) : isolate_(isolate) { |
| 116 } |
| 117 ReusableHandleScope() : isolate_(Isolate::Current()) { |
| 118 } |
| 119 ~ReusableHandleScope() { |
| 120 #define CLEAR_REUSABLE_HANDLE(object) \ |
| 121 object##Handle().raw_ = Object::null(); \ |
| 122 |
| 123 REUSABLE_HANDLE_LIST(CLEAR_REUSABLE_HANDLE); |
| 124 } |
| 125 |
| 126 #define REUSABLE_HANDLE_ACCESSORS(object) \ |
| 127 object& object##Handle() { \ |
| 128 ASSERT(isolate_->object##_handle_ != NULL); \ |
| 129 return *isolate_->object##_handle_; \ |
| 130 } \ |
| 131 |
| 132 REUSABLE_HANDLE_LIST(REUSABLE_HANDLE_ACCESSORS) |
| 133 #undef REUSABLE_HANDLE_ACCESSORS |
| 134 |
| 135 private: |
| 136 void ResetHandles(); |
| 137 Isolate* isolate_; |
| 138 DISALLOW_COPY_AND_ASSIGN(ReusableHandleScope); |
| 139 }; |
| 140 #endif // defined(DEBUG) |
| 141 |
| 142 } // namespace dart |
| 143 |
| 144 #endif // VM_REUSABLE_HANDLES_H_ |
OLD | NEW |