| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 // handle scope and a new one is created, all allocations will take | 100 // handle scope and a new one is created, all allocations will take |
| 101 // place in the new handle scope until it is deleted. After that, | 101 // place in the new handle scope until it is deleted. After that, |
| 102 // new handles will again be allocated in the original handle scope. | 102 // new handles will again be allocated in the original handle scope. |
| 103 // | 103 // |
| 104 // After the handle scope of a local handle has been deleted the | 104 // After the handle scope of a local handle has been deleted the |
| 105 // garbage collector will no longer track the object stored in the | 105 // garbage collector will no longer track the object stored in the |
| 106 // handle and may deallocate it. The behavior of accessing a handle | 106 // handle and may deallocate it. The behavior of accessing a handle |
| 107 // for which the handle scope has been deleted is undefined. | 107 // for which the handle scope has been deleted is undefined. |
| 108 class HandleScope { | 108 class HandleScope { |
| 109 public: | 109 public: |
| 110 HandleScope() : previous_(current_) { | 110 HandleScope() : previous_(*Isolate::Current()->handle_scope_data()) { |
| 111 current_.extensions = 0; | 111 Isolate::Current()->handle_scope_data()->extensions = 0; |
| 112 } | 112 } |
| 113 | 113 |
| 114 ~HandleScope() { | 114 ~HandleScope() { |
| 115 Leave(&previous_); | 115 Leave(&previous_); |
| 116 } | 116 } |
| 117 | 117 |
| 118 // Counts the number of allocated handles. | 118 // Counts the number of allocated handles. |
| 119 static int NumberOfHandles(); | 119 static int NumberOfHandles(); |
| 120 | 120 |
| 121 // Creates a new handle with the given value. | 121 // Creates a new handle with the given value. |
| 122 template <typename T> | 122 template <typename T> |
| 123 static inline T** CreateHandle(T* value) { | 123 static inline T** CreateHandle(T* value) { |
| 124 internal::Object** cur = current_.next; | 124 v8::ImplementationUtilities::HandleScopeData* current = |
| 125 if (cur == current_.limit) cur = Extend(); | 125 Isolate::Current()->handle_scope_data(); |
| 126 |
| 127 internal::Object** cur = current->next; |
| 128 if (cur == current->limit) cur = Extend(); |
| 126 // Update the current next field, set the value in the created | 129 // Update the current next field, set the value in the created |
| 127 // handle, and return the result. | 130 // handle, and return the result. |
| 128 ASSERT(cur < current_.limit); | 131 ASSERT(cur < current->limit); |
| 129 current_.next = cur + 1; | 132 current->next = cur + 1; |
| 130 | 133 |
| 131 T** result = reinterpret_cast<T**>(cur); | 134 T** result = reinterpret_cast<T**>(cur); |
| 132 *result = value; | 135 *result = value; |
| 133 return result; | 136 return result; |
| 134 } | 137 } |
| 135 | 138 |
| 136 // Deallocates any extensions used by the current scope. | 139 // Deallocates any extensions used by the current scope. |
| 137 static void DeleteExtensions(); | 140 static void DeleteExtensions(); |
| 138 | 141 |
| 139 static Address current_extensions_address(); | 142 static Address current_extensions_address(); |
| 140 static Address current_next_address(); | 143 static Address current_next_address(); |
| 141 static Address current_limit_address(); | 144 static Address current_limit_address(); |
| 142 | 145 |
| 143 private: | 146 private: |
| 144 // Prevent heap allocation or illegal handle scopes. | 147 // Prevent heap allocation or illegal handle scopes. |
| 145 HandleScope(const HandleScope&); | 148 HandleScope(const HandleScope&); |
| 146 void operator=(const HandleScope&); | 149 void operator=(const HandleScope&); |
| 147 void* operator new(size_t size); | 150 void* operator new(size_t size); |
| 148 void operator delete(void* size_t); | 151 void operator delete(void* size_t); |
| 149 | 152 |
| 150 static v8::ImplementationUtilities::HandleScopeData current_; | |
| 151 const v8::ImplementationUtilities::HandleScopeData previous_; | 153 const v8::ImplementationUtilities::HandleScopeData previous_; |
| 152 | 154 |
| 153 // Pushes a fresh handle scope to be used when allocating new handles. | 155 // Pushes a fresh handle scope to be used when allocating new handles. |
| 154 static void Enter( | 156 static void Enter( |
| 155 v8::ImplementationUtilities::HandleScopeData* previous) { | 157 v8::ImplementationUtilities::HandleScopeData* previous) { |
| 156 *previous = current_; | 158 v8::ImplementationUtilities::HandleScopeData* current = |
| 157 current_.extensions = 0; | 159 Isolate::Current()->handle_scope_data(); |
| 160 *previous = *current; |
| 161 current->extensions = 0; |
| 158 } | 162 } |
| 159 | 163 |
| 160 // Re-establishes the previous scope state. Should be called only | 164 // Re-establishes the previous scope state. Should be called only |
| 161 // once, and only for the current scope. | 165 // once, and only for the current scope. |
| 162 static void Leave( | 166 static void Leave( |
| 163 const v8::ImplementationUtilities::HandleScopeData* previous) { | 167 const v8::ImplementationUtilities::HandleScopeData* previous) { |
| 164 if (current_.extensions > 0) { | 168 v8::ImplementationUtilities::HandleScopeData* current = |
| 169 Isolate::Current()->handle_scope_data(); |
| 170 if (current->extensions > 0) { |
| 165 DeleteExtensions(); | 171 DeleteExtensions(); |
| 166 } | 172 } |
| 167 current_ = *previous; | 173 *current = *previous; |
| 168 #ifdef DEBUG | 174 #ifdef DEBUG |
| 169 ZapRange(current_.next, current_.limit); | 175 ZapRange(current->next, current->limit); |
| 170 #endif | 176 #endif |
| 171 } | 177 } |
| 172 | 178 |
| 173 // Extend the handle scope making room for more handles. | 179 // Extend the handle scope making room for more handles. |
| 174 static internal::Object** Extend(); | 180 static internal::Object** Extend(); |
| 175 | 181 |
| 176 // Zaps the handles in the half-open interval [start, end). | 182 // Zaps the handles in the half-open interval [start, end). |
| 177 static void ZapRange(internal::Object** start, internal::Object** end); | 183 static void ZapRange(internal::Object** start, internal::Object** end); |
| 178 | 184 |
| 179 friend class v8::HandleScope; | 185 friend class v8::HandleScope; |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 private: | 384 private: |
| 379 bool has_been_transformed_; // Tells whether the object has been transformed. | 385 bool has_been_transformed_; // Tells whether the object has been transformed. |
| 380 int unused_property_fields_; // Captures the unused number of field. | 386 int unused_property_fields_; // Captures the unused number of field. |
| 381 Handle<JSObject> object_; // The object being optimized. | 387 Handle<JSObject> object_; // The object being optimized. |
| 382 }; | 388 }; |
| 383 | 389 |
| 384 | 390 |
| 385 } } // namespace v8::internal | 391 } } // namespace v8::internal |
| 386 | 392 |
| 387 #endif // V8_HANDLES_H_ | 393 #endif // V8_HANDLES_H_ |
| OLD | NEW |