| 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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 | 86 |
| 87 // Closes the given scope, but lets this handle escape. See | 87 // Closes the given scope, but lets this handle escape. See |
| 88 // implementation in api.h. | 88 // implementation in api.h. |
| 89 inline Handle<T> EscapeFrom(v8::HandleScope* scope); | 89 inline Handle<T> EscapeFrom(v8::HandleScope* scope); |
| 90 | 90 |
| 91 private: | 91 private: |
| 92 T** location_; | 92 T** location_; |
| 93 }; | 93 }; |
| 94 | 94 |
| 95 | 95 |
| 96 // A handle-scope based variable. The value stored in the variable can change | |
| 97 // over time. The value stored in the variable at any time is a root | |
| 98 // for garbage collection. | |
| 99 // The variable is backed by the current HandleScope. | |
| 100 template <typename T> | |
| 101 class HandleCell { | |
| 102 public: | |
| 103 // Create a new HandleCell holding the given value. | |
| 104 explicit HandleCell(Handle<T> value); | |
| 105 explicit HandleCell(T* value); | |
| 106 | |
| 107 // Create an alias of an existing HandleCell. | |
| 108 explicit HandleCell(const HandleCell<T>& value) | |
| 109 : location_(value.location_) { } | |
| 110 | |
| 111 INLINE(T* operator->() const) { return operator*(); } | |
| 112 INLINE(T* operator*() const) { | |
| 113 return *location_; | |
| 114 } | |
| 115 INLINE(void operator=(T* value)) { | |
| 116 *location_ = value; | |
| 117 } | |
| 118 INLINE(void operator=(Handle<T> value)) { | |
| 119 *location_ = *value; | |
| 120 } | |
| 121 INLINE(void operator=(const HandleCell<T>& value)) { | |
| 122 *location_ = *value.location_; | |
| 123 } | |
| 124 | |
| 125 // Extract the value of the variable and cast it to a give type. | |
| 126 // This is typically used for calling methods on a more specialized type. | |
| 127 template <typename S> | |
| 128 inline S* cast() { | |
| 129 S::cast(*location_); | |
| 130 return *reinterpret_cast<S**>(location_); | |
| 131 } | |
| 132 | |
| 133 Handle<T> ToHandle() const { | |
| 134 return Handle<T>(*location_); | |
| 135 } | |
| 136 | |
| 137 private: | |
| 138 // Prevent implicit constructor from being created. | |
| 139 HandleCell(); | |
| 140 | |
| 141 T** location_; | |
| 142 }; | |
| 143 | |
| 144 | |
| 145 // A stack-allocated class that governs a number of local handles. | 96 // A stack-allocated class that governs a number of local handles. |
| 146 // After a handle scope has been created, all local handles will be | 97 // After a handle scope has been created, all local handles will be |
| 147 // allocated within that handle scope until either the handle scope is | 98 // allocated within that handle scope until either the handle scope is |
| 148 // deleted or another handle scope is created. If there is already a | 99 // deleted or another handle scope is created. If there is already a |
| 149 // 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 |
| 150 // 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, |
| 151 // new handles will again be allocated in the original handle scope. | 102 // new handles will again be allocated in the original handle scope. |
| 152 // | 103 // |
| 153 // 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 |
| 154 // garbage collector will no longer track the object stored in the | 105 // garbage collector will no longer track the object stored in the |
| 155 // handle and may deallocate it. The behavior of accessing a handle | 106 // handle and may deallocate it. The behavior of accessing a handle |
| 156 // for which the handle scope has been deleted is undefined. | 107 // for which the handle scope has been deleted is undefined. |
| 157 class HandleScope { | 108 class HandleScope { |
| 158 public: | 109 public: |
| 159 HandleScope() : prev_next_(current_.next), prev_limit_(current_.limit) { | 110 HandleScope() : prev_next_(current_.next), prev_limit_(current_.limit) { |
| 160 current_.level++; | 111 current_.level++; |
| 161 } | 112 } |
| 162 | 113 |
| 163 ~HandleScope() { | 114 ~HandleScope() { |
| 164 current_.next = prev_next_; | 115 CloseScope(); |
| 165 current_.level--; | |
| 166 if (current_.limit != prev_limit_) { | |
| 167 current_.limit = prev_limit_; | |
| 168 DeleteExtensions(); | |
| 169 } | |
| 170 #ifdef DEBUG | |
| 171 ZapRange(prev_next_, prev_limit_); | |
| 172 #endif | |
| 173 } | 116 } |
| 174 | 117 |
| 175 // Counts the number of allocated handles. | 118 // Counts the number of allocated handles. |
| 176 static int NumberOfHandles(); | 119 static int NumberOfHandles(); |
| 177 | 120 |
| 178 // Creates a new handle with the given value. | 121 // Creates a new handle with the given value. |
| 179 template <typename T> | 122 template <typename T> |
| 180 static inline T** CreateHandle(T* value) { | 123 static inline T** CreateHandle(T* value) { |
| 181 internal::Object** cur = current_.next; | 124 internal::Object** cur = current_.next; |
| 182 if (cur == current_.limit) cur = Extend(); | 125 if (cur == current_.limit) cur = Extend(); |
| 183 // Update the current next field, set the value in the created | 126 // Update the current next field, set the value in the created |
| 184 // handle, and return the result. | 127 // handle, and return the result. |
| 185 ASSERT(cur < current_.limit); | 128 ASSERT(cur < current_.limit); |
| 186 current_.next = cur + 1; | 129 current_.next = cur + 1; |
| 187 | 130 |
| 188 T** result = reinterpret_cast<T**>(cur); | 131 T** result = reinterpret_cast<T**>(cur); |
| 189 *result = value; | 132 *result = value; |
| 190 return result; | 133 return result; |
| 191 } | 134 } |
| 192 | 135 |
| 193 // Deallocates any extensions used by the current scope. | 136 // Deallocates any extensions used by the current scope. |
| 194 static void DeleteExtensions(); | 137 static void DeleteExtensions(); |
| 195 | 138 |
| 196 static Address current_next_address(); | 139 static Address current_next_address(); |
| 197 static Address current_limit_address(); | 140 static Address current_limit_address(); |
| 198 static Address current_level_address(); | 141 static Address current_level_address(); |
| 199 | 142 |
| 143 // Closes the HandleScope (invalidating all handles |
| 144 // created in the scope of the HandleScope) and returns |
| 145 // a Handle backed by the parent scope holding the |
| 146 // value of the argument handle. |
| 147 template <typename T> |
| 148 Handle<T> CloseAndEscape(Handle<T> handle_value) { |
| 149 T* value = *handle_value; |
| 150 // Throw away all handles in the current scope. |
| 151 CloseScope(); |
| 152 // Allocate one handle in the parent scope. |
| 153 ASSERT(current_.level > 0); |
| 154 Handle<T> result(CreateHandle<T>(value)); |
| 155 // Reinitialize the current scope (so that it's ready |
| 156 // to be used or closed again). |
| 157 prev_next_ = current_.next; |
| 158 prev_limit_ = current_.limit; |
| 159 current_.level++; |
| 160 return result; |
| 161 } |
| 162 |
| 200 private: | 163 private: |
| 201 // Prevent heap allocation or illegal handle scopes. | 164 // Prevent heap allocation or illegal handle scopes. |
| 202 HandleScope(const HandleScope&); | 165 HandleScope(const HandleScope&); |
| 203 void operator=(const HandleScope&); | 166 void operator=(const HandleScope&); |
| 204 void* operator new(size_t size); | 167 void* operator new(size_t size); |
| 205 void operator delete(void* size_t); | 168 void operator delete(void* size_t); |
| 206 | 169 |
| 170 inline void CloseScope() { |
| 171 current_.next = prev_next_; |
| 172 current_.level--; |
| 173 if (current_.limit != prev_limit_) { |
| 174 current_.limit = prev_limit_; |
| 175 DeleteExtensions(); |
| 176 } |
| 177 #ifdef DEBUG |
| 178 ZapRange(prev_next_, prev_limit_); |
| 179 #endif |
| 180 } |
| 181 |
| 207 static v8::ImplementationUtilities::HandleScopeData current_; | 182 static v8::ImplementationUtilities::HandleScopeData current_; |
| 208 Object** const prev_next_; | 183 // Holds values on entry. The prev_next_ value is never NULL |
| 209 Object** const prev_limit_; | 184 // on_entry, but is set to NULL when this scope is closed. |
| 185 Object** prev_next_; |
| 186 Object** prev_limit_; |
| 210 | 187 |
| 211 // Extend the handle scope making room for more handles. | 188 // Extend the handle scope making room for more handles. |
| 212 static internal::Object** Extend(); | 189 static internal::Object** Extend(); |
| 213 | 190 |
| 214 // Zaps the handles in the half-open interval [start, end). | 191 // Zaps the handles in the half-open interval [start, end). |
| 215 static void ZapRange(internal::Object** start, internal::Object** end); | 192 static void ZapRange(internal::Object** start, internal::Object** end); |
| 216 | 193 |
| 217 friend class v8::HandleScope; | 194 friend class v8::HandleScope; |
| 218 friend class v8::ImplementationUtilities; | 195 friend class v8::ImplementationUtilities; |
| 219 }; | 196 }; |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 private: | 412 private: |
| 436 bool has_been_transformed_; // Tells whether the object has been transformed. | 413 bool has_been_transformed_; // Tells whether the object has been transformed. |
| 437 int unused_property_fields_; // Captures the unused number of field. | 414 int unused_property_fields_; // Captures the unused number of field. |
| 438 Handle<JSObject> object_; // The object being optimized. | 415 Handle<JSObject> object_; // The object being optimized. |
| 439 }; | 416 }; |
| 440 | 417 |
| 441 | 418 |
| 442 } } // namespace v8::internal | 419 } } // namespace v8::internal |
| 443 | 420 |
| 444 #endif // V8_HANDLES_H_ | 421 #endif // V8_HANDLES_H_ |
| OLD | NEW |