| 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() { |
| 111 current_.extensions = 0; | 111 ImplementationUtilities::HandleScopeData& current = |
| 112 v8_context()->handle_scope_data_; |
| 113 previous_ = current; |
| 114 current.extensions = 0; |
| 112 } | 115 } |
| 113 | 116 |
| 114 ~HandleScope() { | 117 ~HandleScope() { |
| 115 Leave(&previous_); | 118 Leave(&previous_); |
| 116 } | 119 } |
| 117 | 120 |
| 118 // Counts the number of allocated handles. | 121 // Counts the number of allocated handles. |
| 119 static int NumberOfHandles(); | 122 static int NumberOfHandles(); |
| 120 | 123 |
| 121 // Creates a new handle with the given value. | 124 // Creates a new handle with the given value. |
| 122 template <typename T> | 125 template <typename T> |
| 123 static inline T** CreateHandle(T* value) { | 126 static inline T** CreateHandle(T* value) { |
| 124 internal::Object** cur = current_.next; | 127 ImplementationUtilities::HandleScopeData& current = |
| 125 if (cur == current_.limit) cur = Extend(); | 128 v8_context()->handle_scope_data_; |
| 129 internal::Object** cur = current.next; |
| 130 if (cur == current.limit) cur = Extend(); |
| 126 // Update the current next field, set the value in the created | 131 // Update the current next field, set the value in the created |
| 127 // handle, and return the result. | 132 // handle, and return the result. |
| 128 ASSERT(cur < current_.limit); | 133 ASSERT(cur < current.limit); |
| 129 current_.next = cur + 1; | 134 current.next = cur + 1; |
| 130 | 135 |
| 131 T** result = reinterpret_cast<T**>(cur); | 136 T** result = reinterpret_cast<T**>(cur); |
| 132 *result = value; | 137 *result = value; |
| 133 return result; | 138 return result; |
| 134 } | 139 } |
| 135 | 140 |
| 136 // Deallocates any extensions used by the current scope. | 141 // Deallocates any extensions used by the current scope. |
| 137 static void DeleteExtensions(); | 142 static void DeleteExtensions(); |
| 138 | 143 |
| 139 static Address current_extensions_address(); | 144 static Address current_extensions_address(); |
| 140 static Address current_next_address(); | 145 static Address current_next_address(); |
| 141 static Address current_limit_address(); | 146 static Address current_limit_address(); |
| 142 | 147 |
| 143 private: | 148 private: |
| 144 // Prevent heap allocation or illegal handle scopes. | 149 // Prevent heap allocation or illegal handle scopes. |
| 145 HandleScope(const HandleScope&); | 150 HandleScope(const HandleScope&); |
| 146 void operator=(const HandleScope&); | 151 void operator=(const HandleScope&); |
| 147 void* operator new(size_t size); | 152 void* operator new(size_t size); |
| 148 void operator delete(void* size_t); | 153 void operator delete(void* size_t); |
| 149 | 154 |
| 150 static v8::ImplementationUtilities::HandleScopeData current_; | 155 ImplementationUtilities::HandleScopeData previous_; |
| 151 const v8::ImplementationUtilities::HandleScopeData previous_; | |
| 152 | 156 |
| 153 // Pushes a fresh handle scope to be used when allocating new handles. | 157 // Pushes a fresh handle scope to be used when allocating new handles. |
| 154 static void Enter( | 158 static void Enter( |
| 155 v8::ImplementationUtilities::HandleScopeData* previous) { | 159 v8::ImplementationUtilities::HandleScopeData* previous) { |
| 156 *previous = current_; | 160 ImplementationUtilities::HandleScopeData& current = |
| 157 current_.extensions = 0; | 161 v8_context()->handle_scope_data_; |
| 162 *previous = current; |
| 163 current.extensions = 0; |
| 158 } | 164 } |
| 159 | 165 |
| 160 // Re-establishes the previous scope state. Should be called only | 166 // Re-establishes the previous scope state. Should be called only |
| 161 // once, and only for the current scope. | 167 // once, and only for the current scope. |
| 162 static void Leave( | 168 static void Leave( |
| 163 const v8::ImplementationUtilities::HandleScopeData* previous) { | 169 const v8::ImplementationUtilities::HandleScopeData* previous) { |
| 164 if (current_.extensions > 0) { | 170 ImplementationUtilities::HandleScopeData& current = |
| 171 v8_context()->handle_scope_data_; |
| 172 if (current.extensions > 0) { |
| 165 DeleteExtensions(); | 173 DeleteExtensions(); |
| 166 } | 174 } |
| 167 current_ = *previous; | 175 current = *previous; |
| 168 #ifdef DEBUG | 176 #ifdef DEBUG |
| 169 ZapRange(current_.next, current_.limit); | 177 ZapRange(current.next, current.limit); |
| 170 #endif | 178 #endif |
| 171 } | 179 } |
| 172 | 180 |
| 173 // Extend the handle scope making room for more handles. | 181 // Extend the handle scope making room for more handles. |
| 174 static internal::Object** Extend(); | 182 static internal::Object** Extend(); |
| 175 | 183 |
| 176 // Zaps the handles in the half-open interval [start, end). | 184 // Zaps the handles in the half-open interval [start, end). |
| 177 static void ZapRange(internal::Object** start, internal::Object** end); | 185 static void ZapRange(internal::Object** start, internal::Object** end); |
| 178 | 186 |
| 179 friend class v8::HandleScope; | 187 friend class v8::HandleScope; |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 private: | 366 private: |
| 359 bool has_been_transformed_; // Tells whether the object has been transformed. | 367 bool has_been_transformed_; // Tells whether the object has been transformed. |
| 360 int unused_property_fields_; // Captures the unused number of field. | 368 int unused_property_fields_; // Captures the unused number of field. |
| 361 Handle<JSObject> object_; // The object being optimized. | 369 Handle<JSObject> object_; // The object being optimized. |
| 362 }; | 370 }; |
| 363 | 371 |
| 364 | 372 |
| 365 } } // namespace v8::internal | 373 } } // namespace v8::internal |
| 366 | 374 |
| 367 #endif // V8_HANDLES_H_ | 375 #endif // V8_HANDLES_H_ |
| OLD | NEW |