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