| 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 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 namespace v8 { | 33 namespace v8 { |
| 34 namespace internal { | 34 namespace internal { |
| 35 | 35 |
| 36 // ---------------------------------------------------------------------------- | 36 // ---------------------------------------------------------------------------- |
| 37 // A Handle provides a reference to an object that survives relocation by | 37 // A Handle provides a reference to an object that survives relocation by |
| 38 // the garbage collector. | 38 // the garbage collector. |
| 39 // Handles are only valid within a HandleScope. | 39 // Handles are only valid within a HandleScope. |
| 40 // When a handle is created for an object a cell is allocated in the heap. | 40 // When a handle is created for an object a cell is allocated in the heap. |
| 41 | 41 |
| 42 template<class T> | 42 template<typename T> |
| 43 class Handle { | 43 class Handle { |
| 44 public: | 44 public: |
| 45 INLINE(explicit Handle(T** location)) { location_ = location; } | 45 INLINE(explicit Handle(T** location)) { location_ = location; } |
| 46 INLINE(explicit Handle(T* obj)); | 46 INLINE(explicit Handle(T* obj)); |
| 47 | 47 |
| 48 INLINE(Handle()) : location_(NULL) {} | 48 INLINE(Handle()) : location_(NULL) {} |
| 49 | 49 |
| 50 // Constructor for handling automatic up casting. | 50 // Constructor for handling automatic up casting. |
| 51 // Ex. Handle<JSFunction> can be passed when Handle<Object> is expected. | 51 // Ex. Handle<JSFunction> can be passed when Handle<Object> is expected. |
| 52 template <class S> Handle(Handle<S> handle) { | 52 template <class S> Handle(Handle<S> handle) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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() : prev_next_(current_.next), prev_limit_(current_.limit) { | 110 HandleScope() : prev_next_(current_.next), prev_limit_(current_.limit) { |
| 111 current_.level++; | 111 current_.level++; |
| 112 } | 112 } |
| 113 | 113 |
| 114 ~HandleScope() { | 114 ~HandleScope() { |
| 115 current_.next = prev_next_; | 115 CloseScope(); |
| 116 current_.level--; | |
| 117 if (current_.limit != prev_limit_) { | |
| 118 current_.limit = prev_limit_; | |
| 119 DeleteExtensions(); | |
| 120 } | |
| 121 #ifdef DEBUG | |
| 122 ZapRange(prev_next_, prev_limit_); | |
| 123 #endif | |
| 124 } | 116 } |
| 125 | 117 |
| 126 // Counts the number of allocated handles. | 118 // Counts the number of allocated handles. |
| 127 static int NumberOfHandles(); | 119 static int NumberOfHandles(); |
| 128 | 120 |
| 129 // Creates a new handle with the given value. | 121 // Creates a new handle with the given value. |
| 130 template <typename T> | 122 template <typename T> |
| 131 static inline T** CreateHandle(T* value) { | 123 static inline T** CreateHandle(T* value) { |
| 132 internal::Object** cur = current_.next; | 124 internal::Object** cur = current_.next; |
| 133 if (cur == current_.limit) cur = Extend(); | 125 if (cur == current_.limit) cur = Extend(); |
| 134 // Update the current next field, set the value in the created | 126 // Update the current next field, set the value in the created |
| 135 // handle, and return the result. | 127 // handle, and return the result. |
| 136 ASSERT(cur < current_.limit); | 128 ASSERT(cur < current_.limit); |
| 137 current_.next = cur + 1; | 129 current_.next = cur + 1; |
| 138 | 130 |
| 139 T** result = reinterpret_cast<T**>(cur); | 131 T** result = reinterpret_cast<T**>(cur); |
| 140 *result = value; | 132 *result = value; |
| 141 return result; | 133 return result; |
| 142 } | 134 } |
| 143 | 135 |
| 144 // Deallocates any extensions used by the current scope. | 136 // Deallocates any extensions used by the current scope. |
| 145 static void DeleteExtensions(); | 137 static void DeleteExtensions(); |
| 146 | 138 |
| 147 static Address current_next_address(); | 139 static Address current_next_address(); |
| 148 static Address current_limit_address(); | 140 static Address current_limit_address(); |
| 149 static Address current_level_address(); | 141 static Address current_level_address(); |
| 150 | 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 |
| 151 private: | 163 private: |
| 152 // Prevent heap allocation or illegal handle scopes. | 164 // Prevent heap allocation or illegal handle scopes. |
| 153 HandleScope(const HandleScope&); | 165 HandleScope(const HandleScope&); |
| 154 void operator=(const HandleScope&); | 166 void operator=(const HandleScope&); |
| 155 void* operator new(size_t size); | 167 void* operator new(size_t size); |
| 156 void operator delete(void* size_t); | 168 void operator delete(void* size_t); |
| 157 | 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 |
| 158 static v8::ImplementationUtilities::HandleScopeData current_; | 182 static v8::ImplementationUtilities::HandleScopeData current_; |
| 159 Object** const prev_next_; | 183 // Holds values on entry. The prev_next_ value is never NULL |
| 160 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_; |
| 161 | 187 |
| 162 // Extend the handle scope making room for more handles. | 188 // Extend the handle scope making room for more handles. |
| 163 static internal::Object** Extend(); | 189 static internal::Object** Extend(); |
| 164 | 190 |
| 165 // Zaps the handles in the half-open interval [start, end). | 191 // Zaps the handles in the half-open interval [start, end). |
| 166 static void ZapRange(internal::Object** start, internal::Object** end); | 192 static void ZapRange(internal::Object** start, internal::Object** end); |
| 167 | 193 |
| 168 friend class v8::HandleScope; | 194 friend class v8::HandleScope; |
| 169 friend class v8::ImplementationUtilities; | 195 friend class v8::ImplementationUtilities; |
| 170 }; | 196 }; |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 private: | 403 private: |
| 378 bool has_been_transformed_; // Tells whether the object has been transformed. | 404 bool has_been_transformed_; // Tells whether the object has been transformed. |
| 379 int unused_property_fields_; // Captures the unused number of field. | 405 int unused_property_fields_; // Captures the unused number of field. |
| 380 Handle<JSObject> object_; // The object being optimized. | 406 Handle<JSObject> object_; // The object being optimized. |
| 381 }; | 407 }; |
| 382 | 408 |
| 383 | 409 |
| 384 } } // namespace v8::internal | 410 } } // namespace v8::internal |
| 385 | 411 |
| 386 #endif // V8_HANDLES_H_ | 412 #endif // V8_HANDLES_H_ |
| OLD | NEW |