OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef VM_HANDLES_H_ | 5 #ifndef VM_HANDLES_H_ |
6 #define VM_HANDLES_H_ | 6 #define VM_HANDLES_H_ |
7 | 7 |
8 #include "vm/allocation.h" | 8 #include "vm/allocation.h" |
9 | 9 |
10 namespace dart { | 10 namespace dart { |
11 | 11 |
12 // Handles are used in the Dart Virtual Machine to ensure that access | 12 // Handles are used in the Dart Virtual Machine to ensure that access |
13 // to dart objects in the virtual machine code is done in a | 13 // to dart objects in the virtual machine code is done in a |
14 // Garbage Collection safe manner. | 14 // Garbage Collection safe manner. |
15 // | 15 // |
16 // The class Handles is the basic type that implements creation of handles and | 16 // The class Handles is the basic type that implements creation of handles and |
17 // manages their life cycle (allocated either in the current zone or | 17 // manages their life cycle (allocated either in the current zone or |
18 // current handle scope). | 18 // current handle scope). |
19 // The two forms of handle allocation are: | 19 // The two forms of handle allocation are: |
20 // - allocation of handles in the current zone (Handle::AllocateZoneHandle). | 20 // - allocation of handles in the current zone (Handle::AllocateZoneHandle). |
21 // Handles allocated in this manner are destroyed when the zone is destroyed. | 21 // Handles allocated in this manner are destroyed when the zone is destroyed. |
22 // - allocation of handles in a scoped manner (Handle::AllocateHandle). | 22 // - allocation of handles in a scoped manner (Handle::AllocateHandle). |
23 // A new scope can be started using HANDLESCOPE(). | 23 // A new scope can be started using HANDLESCOPE(). |
24 // Handles allocated in this manner are destroyed when the HandleScope | 24 // Handles allocated in this manner are destroyed when the HandleScope |
25 // object is destroyed. | 25 // object is destroyed. |
26 // Code that uses scoped handles typically looks as follows: | 26 // Code that uses scoped handles typically looks as follows: |
27 // { | 27 // { |
28 // HANDLESCOPE(); | 28 // HANDLESCOPE(isolate); |
29 // const String& str = String::Handle(String::New("abc")); | 29 // const String& str = String::Handle(String::New("abc")); |
30 // ..... | 30 // ..... |
31 // ..... | 31 // ..... |
32 // } | 32 // } |
33 // Code that uses zone handles typically looks as follows: | 33 // Code that uses zone handles typically looks as follows: |
34 // const String& str = String::ZoneHandle(String::New("abc")); | 34 // const String& str = String::ZoneHandle(String::New("abc")); |
35 // ..... | 35 // ..... |
36 // ..... | 36 // ..... |
37 // | 37 // |
38 // The Handle function for each object type internally uses the | 38 // The Handle function for each object type internally uses the |
39 // Handles::AllocateHandle() function for creating handles. The Handle | 39 // Handles::AllocateHandle() function for creating handles. The Handle |
40 // function of the object type is the only way to create scoped handles | 40 // function of the object type is the only way to create scoped handles |
41 // in the dart VM. | 41 // in the dart VM. |
42 // The ZoneHandle function for each object type internally uses the | 42 // The ZoneHandle function for each object type internally uses the |
43 // Handles::AllocateZoneHandle() function for creating zone handles. | 43 // Handles::AllocateZoneHandle() function for creating zone handles. |
44 // The ZoneHandle function of the object type is the only way to create | 44 // The ZoneHandle function of the object type is the only way to create |
45 // zone handles in the dart VM. | 45 // zone handles in the dart VM. |
46 // | 46 // |
47 // There are some critical regions of the Dart VM were we may need to manipulate | 47 // There are some critical regions of the Dart VM were we may need to manipulate |
48 // raw dart objects directly. We use NOHANDLESCOPE to assert that we do not | 48 // raw dart objects directly. We use NOHANDLESCOPE to assert that we do not |
49 // add code that will allocate new handles during this critical area. | 49 // add code that will allocate new handles during this critical area. |
50 // { | 50 // { |
51 // NOHANDLESCOPE(); | 51 // NOHANDLESCOPE(isolate); |
52 // .... | 52 // .... |
53 // .... | 53 // .... |
54 // } | 54 // } |
55 | 55 |
56 | 56 |
57 // Forward declarations. | 57 // Forward declarations. |
58 class ObjectPointerVisitor; | 58 class ObjectPointerVisitor; |
59 | 59 |
60 | 60 |
61 template <int kHandleSizeInWords, int kHandlesPerChunk, int kOffsetOfRawPtr> | 61 template <int kHandleSizeInWords, int kHandlesPerChunk, int kOffsetOfRawPtr> |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 | 236 |
237 // Returns number of handles, these functions are used for testing purposes. | 237 // Returns number of handles, these functions are used for testing purposes. |
238 static int ScopedHandleCount(); | 238 static int ScopedHandleCount(); |
239 static int ZoneHandleCount(); | 239 static int ZoneHandleCount(); |
240 }; | 240 }; |
241 | 241 |
242 | 242 |
243 // The class HandleScope is used to start a new handles scope in the code. | 243 // The class HandleScope is used to start a new handles scope in the code. |
244 // It is used as follows: | 244 // It is used as follows: |
245 // { | 245 // { |
246 // HANDLESCOPE(); | 246 // HANDLESCOPE(isolate); |
247 // .... | 247 // .... |
248 // ..... | 248 // ..... |
249 // code that creates some scoped handles. | 249 // code that creates some scoped handles. |
250 // .... | 250 // .... |
251 // } | 251 // } |
252 class HandleScope : public StackResource { | 252 class HandleScope : public StackResource { |
253 public: | 253 public: |
254 HandleScope(); | 254 explicit HandleScope(Isolate* isolate); |
255 ~HandleScope(); | 255 ~HandleScope(); |
256 | 256 |
257 private: | 257 private: |
258 VMHandles::HandlesBlock* saved_handle_block_; // Handle block at prev scope. | 258 VMHandles::HandlesBlock* saved_handle_block_; // Handle block at prev scope. |
259 uword saved_handle_slot_; // Next available handle slot at previous scope. | 259 uword saved_handle_slot_; // Next available handle slot at previous scope. |
260 #if defined(DEBUG) | 260 #if defined(DEBUG) |
261 HandleScope* link_; // Link to previous scope. | 261 HandleScope* link_; // Link to previous scope. |
262 #endif | 262 #endif |
263 DISALLOW_COPY_AND_ASSIGN(HandleScope); | 263 DISALLOW_COPY_AND_ASSIGN(HandleScope); |
264 }; | 264 }; |
265 | 265 |
266 // Macro to start a new Handle scope. | 266 // Macro to start a new Handle scope. |
267 #define HANDLESCOPE() dart::HandleScope vm_internal_handles_scope_; | 267 #define HANDLESCOPE(isolate) \ |
| 268 dart::HandleScope vm_internal_handles_scope_(isolate); |
268 | 269 |
269 | 270 |
270 // The class NoHandleScope is used in critical regions of the virtual machine | 271 // The class NoHandleScope is used in critical regions of the virtual machine |
271 // code where raw dart object pointers are directly manipulated. | 272 // code where raw dart object pointers are directly manipulated. |
272 // This class asserts that we do not add code that will allocate new handles | 273 // This class asserts that we do not add code that will allocate new handles |
273 // during this critical area. | 274 // during this critical area. |
274 // It is used as follows: | 275 // It is used as follows: |
275 // { | 276 // { |
276 // NOHANDLESCOPE(); | 277 // NOHANDLESCOPE(isolate); |
277 // .... | 278 // .... |
278 // ..... | 279 // ..... |
279 // critical code that manipulates dart objects directly. | 280 // critical code that manipulates dart objects directly. |
280 // .... | 281 // .... |
281 // } | 282 // } |
282 #if defined(DEBUG) | 283 #if defined(DEBUG) |
283 class NoHandleScope : public StackResource { | 284 class NoHandleScope : public StackResource { |
284 public: | 285 public: |
285 NoHandleScope(); | 286 explicit NoHandleScope(Isolate* isolate); |
286 ~NoHandleScope(); | 287 ~NoHandleScope(); |
287 | 288 |
288 private: | 289 private: |
289 DISALLOW_COPY_AND_ASSIGN(NoHandleScope); | 290 DISALLOW_COPY_AND_ASSIGN(NoHandleScope); |
290 }; | 291 }; |
291 #else // defined(DEBUG) | 292 #else // defined(DEBUG) |
292 class NoHandleScope : public ValueObject { | 293 class NoHandleScope : public ValueObject { |
293 public: | 294 public: |
294 NoHandleScope() { } | 295 explicit NoHandleScope(Isolate* isolate) { } |
295 ~NoHandleScope() { } | 296 ~NoHandleScope() { } |
296 | 297 |
297 private: | 298 private: |
298 DISALLOW_COPY_AND_ASSIGN(NoHandleScope); | 299 DISALLOW_COPY_AND_ASSIGN(NoHandleScope); |
299 }; | 300 }; |
300 #endif // defined(DEBUG) | 301 #endif // defined(DEBUG) |
301 | 302 |
302 // Macro to start a no handles scope in the code. | 303 // Macro to start a no handles scope in the code. |
303 #define NOHANDLESCOPE() dart::NoHandleScope no_vm_internal_handles_scope_; | 304 #define NOHANDLESCOPE(isolate) \ |
| 305 dart::NoHandleScope no_vm_internal_handles_scope_(isolate); |
304 | 306 |
305 } // namespace dart | 307 } // namespace dart |
306 | 308 |
307 #endif // VM_HANDLES_H_ | 309 #endif // VM_HANDLES_H_ |
OLD | NEW |