Flutter Engine
The Flutter Engine
reusable_handles.h
Go to the documentation of this file.
1// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
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.
4
5#ifndef RUNTIME_VM_REUSABLE_HANDLES_H_
6#define RUNTIME_VM_REUSABLE_HANDLES_H_
7
8#include "vm/allocation.h"
9#include "vm/handles.h"
10#include "vm/object.h"
11
12namespace dart {
13
14// Classes registered in REUSABLE_HANDLE_LIST have an thread specific reusable
15// handle. A guard class (Reusable*ClassName*HandleScope) should be used in
16// regions of the virtual machine where the thread specific reusable handle
17// of that type is used. The class asserts that we do not add code that will
18// result in recursive uses of the class's reusable handle.
19//
20// Below is an example of a reusable array handle via the
21// REUSABLE_*CLASSNAME*_HANDLESCOPE macro:
22//
23// {
24// REUSABLE_ARRAY_HANDLESCOPE(thread);
25// ....
26// ....
27// Array& funcs = reused_array_handle.Handle();
28// code that uses funcs
29// ....
30// }
31//
32
33#if defined(DEBUG)
34#define REUSABLE_SCOPE(name) \
35 class Reusable##name##HandleScope : public StackResource { \
36 public: \
37 explicit Reusable##name##HandleScope(Thread* thread = Thread::Current()) \
38 : StackResource(thread), thread_(thread) { \
39 ASSERT(!thread->reusable_##name##_handle_scope_active()); \
40 thread->set_reusable_##name##_handle_scope_active(true); \
41 } \
42 ~Reusable##name##HandleScope() { \
43 ASSERT(thread_->reusable_##name##_handle_scope_active()); \
44 thread_->set_reusable_##name##_handle_scope_active(false); \
45 Handle().ptr_ = name::null(); \
46 } \
47 name& Handle() const { \
48 ASSERT(thread_->name##_handle_ != nullptr); \
49 return *thread_->name##_handle_; \
50 } \
51 \
52 private: \
53 Thread* thread_; \
54 DISALLOW_COPY_AND_ASSIGN(Reusable##name##HandleScope); \
55 };
56#else
57#define REUSABLE_SCOPE(name) \
58 class Reusable##name##HandleScope : public ValueObject { \
59 public: \
60 explicit Reusable##name##HandleScope(Thread* thread = Thread::Current()) \
61 : handle_(thread->name##_handle_) {} \
62 ~Reusable##name##HandleScope() { \
63 handle_->ptr_ = name::null(); \
64 } \
65 name& Handle() const { \
66 ASSERT(handle_ != nullptr); \
67 return *handle_; \
68 } \
69 \
70 private: \
71 name* handle_; \
72 DISALLOW_COPY_AND_ASSIGN(Reusable##name##HandleScope); \
73 };
74#endif // defined(DEBUG)
76#undef REUSABLE_SCOPE
77
78#define REUSABLE_ABSTRACT_TYPE_HANDLESCOPE(thread) \
79 ReusableAbstractTypeHandleScope reused_abstract_type(thread);
80#define REUSABLE_ARRAY_HANDLESCOPE(thread) \
81 ReusableArrayHandleScope reused_array_handle(thread);
82#define REUSABLE_CLASS_HANDLESCOPE(thread) \
83 ReusableClassHandleScope reused_class_handle(thread);
84#define REUSABLE_CODE_HANDLESCOPE(thread) \
85 ReusableCodeHandleScope reused_code_handle(thread);
86#define REUSABLE_ERROR_HANDLESCOPE(thread) \
87 ReusableErrorHandleScope reused_error_handle(thread);
88#define REUSABLE_EXCEPTION_HANDLERS_HANDLESCOPE(thread) \
89 ReusableExceptionHandlersHandleScope reused_exception_handlers_handle(thread);
90#define REUSABLE_FIELD_HANDLESCOPE(thread) \
91 ReusableFieldHandleScope reused_field_handle(thread);
92#define REUSABLE_FUNCTION_HANDLESCOPE(thread) \
93 ReusableFunctionHandleScope reused_function_handle(thread);
94#define REUSABLE_GROWABLE_OBJECT_ARRAY_HANDLESCOPE(thread) \
95 ReusableGrowableObjectArrayHandleScope reused_growable_object_array_handle( \
96 thread)
97#define REUSABLE_INSTANCE_HANDLESCOPE(thread) \
98 ReusableInstanceHandleScope reused_instance_handle(thread);
99#define REUSABLE_LIBRARY_HANDLESCOPE(thread) \
100 ReusableLibraryHandleScope reused_library_handle(thread);
101#define REUSABLE_LOADING_UNIT_HANDLESCOPE(thread) \
102 ReusableLoadingUnitHandleScope reused_loading_unit_handle(thread);
103#define REUSABLE_OBJECT_HANDLESCOPE(thread) \
104 ReusableObjectHandleScope reused_object_handle(thread);
105#define REUSABLE_PC_DESCRIPTORS_HANDLESCOPE(thread) \
106 ReusablePcDescriptorsHandleScope reused_pc_descriptors_handle(thread);
107#define REUSABLE_SMI_HANDLESCOPE(thread) \
108 ReusableSmiHandleScope reused_smi_handle(thread);
109#define REUSABLE_STRING_HANDLESCOPE(thread) \
110 ReusableStringHandleScope reused_string_handle(thread);
111#define REUSABLE_TYPE_PARAMETERS_HANDLESCOPE(thread) \
112 ReusableTypeParametersHandleScope reused_type_parameters_handle(thread);
113#define REUSABLE_TYPE_ARGUMENTS_HANDLESCOPE(thread) \
114 ReusableTypeArgumentsHandleScope reused_type_arguments_handle(thread);
115#define REUSABLE_TYPE_PARAMETER_HANDLESCOPE(thread) \
116 ReusableTypeParameterHandleScope reused_type_parameter(thread);
117#define REUSABLE_WEAK_ARRAY_HANDLESCOPE(thread) \
118 ReusableWeakArrayHandleScope reused_weak_array(thread);
119
120} // namespace dart
121
122#endif // RUNTIME_VM_REUSABLE_HANDLES_H_
Definition: dart_vm.cc:33
#define REUSABLE_SCOPE(name)
#define REUSABLE_HANDLE_LIST(V)
Definition: thread.h:78