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 template <typename T> | |
144 Handle<T> Escape(Handle<T> handle_value) { | |
Mads Ager (chromium)
2011/03/01 13:43:22
I find the name Escape a little bit misleading sin
Lasse Reichstein
2011/03/01 14:02:32
Done.
| |
145 T* value = *handle_value; | |
146 // Throw away all handles in the current scope. | |
147 CloseScope(); | |
148 // Allocate one handle in the parent scope. | |
149 ASSERT(current_.level > 0); | |
150 Handle<T> result(CreateHandle<T>(value)); | |
151 // Reinitialize the current scope (so that it's ready | |
152 // to be used or closed again). | |
153 prev_next_ = current_.next; | |
154 prev_limit_ = current_.limit; | |
155 current_.level++; | |
156 return result; | |
157 } | |
158 | |
200 private: | 159 private: |
201 // Prevent heap allocation or illegal handle scopes. | 160 // Prevent heap allocation or illegal handle scopes. |
202 HandleScope(const HandleScope&); | 161 HandleScope(const HandleScope&); |
203 void operator=(const HandleScope&); | 162 void operator=(const HandleScope&); |
204 void* operator new(size_t size); | 163 void* operator new(size_t size); |
205 void operator delete(void* size_t); | 164 void operator delete(void* size_t); |
206 | 165 |
166 inline void CloseScope() { | |
167 current_.next = prev_next_; | |
168 current_.level--; | |
169 if (current_.limit != prev_limit_) { | |
170 current_.limit = prev_limit_; | |
171 DeleteExtensions(); | |
172 } | |
173 #ifdef DEBUG | |
174 ZapRange(prev_next_, prev_limit_); | |
175 #endif | |
176 } | |
177 | |
207 static v8::ImplementationUtilities::HandleScopeData current_; | 178 static v8::ImplementationUtilities::HandleScopeData current_; |
208 Object** const prev_next_; | 179 // Holds values on entry. The prev_next_ value is never NULL |
209 Object** const prev_limit_; | 180 // on_entry, but is set to NULL when this scope is closed. |
181 Object** prev_next_; | |
182 Object** prev_limit_; | |
210 | 183 |
211 // Extend the handle scope making room for more handles. | 184 // Extend the handle scope making room for more handles. |
212 static internal::Object** Extend(); | 185 static internal::Object** Extend(); |
213 | 186 |
214 // Zaps the handles in the half-open interval [start, end). | 187 // Zaps the handles in the half-open interval [start, end). |
215 static void ZapRange(internal::Object** start, internal::Object** end); | 188 static void ZapRange(internal::Object** start, internal::Object** end); |
216 | 189 |
217 friend class v8::HandleScope; | 190 friend class v8::HandleScope; |
218 friend class v8::ImplementationUtilities; | 191 friend class v8::ImplementationUtilities; |
219 }; | 192 }; |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
435 private: | 408 private: |
436 bool has_been_transformed_; // Tells whether the object has been transformed. | 409 bool has_been_transformed_; // Tells whether the object has been transformed. |
437 int unused_property_fields_; // Captures the unused number of field. | 410 int unused_property_fields_; // Captures the unused number of field. |
438 Handle<JSObject> object_; // The object being optimized. | 411 Handle<JSObject> object_; // The object being optimized. |
439 }; | 412 }; |
440 | 413 |
441 | 414 |
442 } } // namespace v8::internal | 415 } } // namespace v8::internal |
443 | 416 |
444 #endif // V8_HANDLES_H_ | 417 #endif // V8_HANDLES_H_ |
OLD | NEW |