| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2014 The Android Open Source Project | 
 | 3 |  * | 
 | 4 |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 5 |  * you may not use this file except in compliance with the License. | 
 | 6 |  * You may obtain a copy of the License at | 
 | 7 |  * | 
 | 8 |  *      http://www.apache.org/licenses/LICENSE-2.0 | 
 | 9 |  * | 
 | 10 |  * Unless required by applicable law or agreed to in writing, software | 
 | 11 |  * distributed under the License is distributed on an "AS IS" BASIS, | 
 | 12 |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 13 |  * See the License for the specific language governing permissions and | 
 | 14 |  * limitations under the License. | 
 | 15 |  */ | 
 | 16 |  | 
 | 17 | #ifndef ART_RUNTIME_HANDLE_SCOPE_H_ | 
 | 18 | #define ART_RUNTIME_HANDLE_SCOPE_H_ | 
 | 19 |  | 
 | 20 | #include "base/logging.h" | 
 | 21 | #include "base/macros.h" | 
 | 22 | #include "handle.h" | 
 | 23 | #include "stack.h" | 
 | 24 | #include "utils.h" | 
 | 25 |  | 
 | 26 | namespace art { | 
 | 27 | namespace mirror { | 
 | 28 | class Object; | 
 | 29 | } | 
| Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 30 |  | 
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 31 | class Thread; | 
 | 32 |  | 
| Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 33 | // HandleScopes are scoped objects containing a number of Handles. They are used to allocate | 
 | 34 | // handles, for these handles (and the objects contained within them) to be visible/roots for the | 
 | 35 | // GC. It is most common to stack allocate HandleScopes using StackHandleScope. | 
| Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 36 | class PACKED(4) HandleScope { | 
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 37 |  public: | 
 | 38 |   ~HandleScope() {} | 
 | 39 |  | 
 | 40 |   // Number of references contained within this handle scope. | 
 | 41 |   uint32_t NumberOfReferences() const { | 
 | 42 |     return number_of_references_; | 
 | 43 |   } | 
 | 44 |  | 
 | 45 |   // We have versions with and without explicit pointer size of the following. The first two are | 
 | 46 |   // used at runtime, so OFFSETOF_MEMBER computes the right offsets automatically. The last one | 
 | 47 |   // takes the pointer size explicitly so that at compile time we can cross-compile correctly. | 
 | 48 |  | 
 | 49 |   // Returns the size of a HandleScope containing num_references handles. | 
 | 50 |   static size_t SizeOf(uint32_t num_references) { | 
| Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 51 |     size_t header_size = sizeof(HandleScope); | 
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 52 |     size_t data_size = sizeof(StackReference<mirror::Object>) * num_references; | 
 | 53 |     return header_size + data_size; | 
 | 54 |   } | 
 | 55 |  | 
| Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 56 |   // Returns the size of a HandleScope containing num_references handles. | 
 | 57 |   static size_t SizeOf(size_t pointer_size, uint32_t num_references) { | 
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 58 |     // Assume that the layout is packed. | 
 | 59 |     size_t header_size = pointer_size + sizeof(number_of_references_); | 
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 60 |     size_t data_size = sizeof(StackReference<mirror::Object>) * num_references; | 
| Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 61 |     return header_size + data_size; | 
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 62 |   } | 
 | 63 |  | 
 | 64 |   // Link to previous HandleScope or null. | 
 | 65 |   HandleScope* GetLink() const { | 
 | 66 |     return link_; | 
 | 67 |   } | 
 | 68 |  | 
 | 69 |   void SetLink(HandleScope* link) { | 
 | 70 |     DCHECK_NE(this, link); | 
 | 71 |     link_ = link; | 
 | 72 |   } | 
 | 73 |  | 
 | 74 |   // Sets the number_of_references_ field for constructing tables out of raw memory. Warning: will | 
 | 75 |   // not resize anything. | 
 | 76 |   void SetNumberOfReferences(uint32_t num_references) { | 
 | 77 |     number_of_references_ = num_references; | 
 | 78 |   } | 
 | 79 |  | 
 | 80 |   mirror::Object* GetReference(size_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) | 
 | 81 |       ALWAYS_INLINE { | 
 | 82 |     DCHECK_LT(i, number_of_references_); | 
 | 83 |     return references_[i].AsMirrorPtr(); | 
 | 84 |   } | 
 | 85 |  | 
 | 86 |   Handle<mirror::Object> GetHandle(size_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) | 
 | 87 |       ALWAYS_INLINE { | 
 | 88 |     DCHECK_LT(i, number_of_references_); | 
 | 89 |     return Handle<mirror::Object>(&references_[i]); | 
 | 90 |   } | 
 | 91 |  | 
| Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 92 |   MutableHandle<mirror::Object> GetMutableHandle(size_t i) | 
 | 93 |       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE { | 
 | 94 |     DCHECK_LT(i, number_of_references_); | 
 | 95 |     return MutableHandle<mirror::Object>(&references_[i]); | 
 | 96 |   } | 
 | 97 |  | 
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 98 |   void SetReference(size_t i, mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) | 
 | 99 |       ALWAYS_INLINE { | 
 | 100 |     DCHECK_LT(i, number_of_references_); | 
 | 101 |     references_[i].Assign(object); | 
 | 102 |   } | 
 | 103 |  | 
 | 104 |   bool Contains(StackReference<mirror::Object>* handle_scope_entry) const { | 
 | 105 |     // A HandleScope should always contain something. One created by the | 
 | 106 |     // jni_compiler should have a jobject/jclass as a native method is | 
 | 107 |     // passed in a this pointer or a class | 
 | 108 |     DCHECK_GT(number_of_references_, 0U); | 
| Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 109 |     return &references_[0] <= handle_scope_entry && | 
 | 110 |         handle_scope_entry <= &references_[number_of_references_ - 1]; | 
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 111 |   } | 
 | 112 |  | 
 | 113 |   // Offset of link within HandleScope, used by generated code | 
 | 114 |   static size_t LinkOffset(size_t pointer_size) { | 
 | 115 |     return 0; | 
 | 116 |   } | 
 | 117 |  | 
 | 118 |   // Offset of length within handle scope, used by generated code | 
 | 119 |   static size_t NumberOfReferencesOffset(size_t pointer_size) { | 
 | 120 |     return pointer_size; | 
 | 121 |   } | 
 | 122 |  | 
 | 123 |   // Offset of link within handle scope, used by generated code | 
 | 124 |   static size_t ReferencesOffset(size_t pointer_size) { | 
 | 125 |     return pointer_size + sizeof(number_of_references_); | 
 | 126 |   } | 
 | 127 |  | 
 | 128 |  protected: | 
 | 129 |   explicit HandleScope(size_t number_of_references) : | 
 | 130 |       link_(nullptr), number_of_references_(number_of_references) { | 
 | 131 |   } | 
 | 132 |  | 
 | 133 |   HandleScope* link_; | 
 | 134 |   uint32_t number_of_references_; | 
 | 135 |  | 
 | 136 |   // number_of_references_ are available if this is allocated and filled in by jni_compiler. | 
 | 137 |   StackReference<mirror::Object> references_[0]; | 
 | 138 |  | 
 | 139 |  private: | 
 | 140 |   template<size_t kNumReferences> friend class StackHandleScope; | 
| Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 141 |  | 
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 142 |   DISALLOW_COPY_AND_ASSIGN(HandleScope); | 
 | 143 | }; | 
 | 144 |  | 
 | 145 | // A wrapper which wraps around Object** and restores the pointer in the destructor. | 
 | 146 | // TODO: Add more functionality. | 
 | 147 | template<class T> | 
| Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 148 | class HandleWrapper : public MutableHandle<T> { | 
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 149 |  public: | 
| Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 150 |   HandleWrapper(T** obj, const MutableHandle<T>& handle) | 
 | 151 |      : MutableHandle<T>(handle), obj_(obj) { | 
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 152 |   } | 
 | 153 |  | 
 | 154 |   ~HandleWrapper() { | 
| Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 155 |     *obj_ = MutableHandle<T>::Get(); | 
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 156 |   } | 
 | 157 |  | 
 | 158 |  private: | 
 | 159 |   T** obj_; | 
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 160 | }; | 
 | 161 |  | 
 | 162 | // Scoped handle storage of a fixed size that is usually stack allocated. | 
 | 163 | template<size_t kNumReferences> | 
| Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 164 | class PACKED(4) StackHandleScope FINAL : public HandleScope { | 
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 165 |  public: | 
 | 166 |   explicit StackHandleScope(Thread* self); | 
 | 167 |   ~StackHandleScope(); | 
 | 168 |  | 
| Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 169 |   // Currently unused, using this GetReference instead of the one in HandleScope is preferred to | 
 | 170 |   // avoid compiler optimizations incorrectly optimizing out of bound array accesses. | 
 | 171 |   // TODO: Remove this when it is un-necessary. | 
 | 172 |   mirror::Object* GetReference(size_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) | 
 | 173 |       ALWAYS_INLINE { | 
 | 174 |     DCHECK_LT(i, number_of_references_); | 
 | 175 |     return references_storage_[i].AsMirrorPtr(); | 
 | 176 |   } | 
 | 177 |  | 
| Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 178 |   MutableHandle<mirror::Object> GetHandle(size_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) | 
| Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 179 |       ALWAYS_INLINE { | 
 | 180 |     DCHECK_LT(i, number_of_references_); | 
| Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 181 |     return MutableHandle<mirror::Object>(&references_storage_[i]); | 
| Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 182 |   } | 
 | 183 |  | 
 | 184 |   void SetReference(size_t i, mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) | 
 | 185 |       ALWAYS_INLINE { | 
 | 186 |     DCHECK_LT(i, number_of_references_); | 
 | 187 |     references_storage_[i].Assign(object); | 
 | 188 |   } | 
 | 189 |  | 
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 190 |   template<class T> | 
| Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 191 |   MutableHandle<T> NewHandle(T* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { | 
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 192 |     SetReference(pos_, object); | 
| Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 193 |     MutableHandle<T> h(GetHandle(pos_)); | 
| Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 194 |     pos_++; | 
 | 195 |     return h; | 
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 196 |   } | 
 | 197 |  | 
 | 198 |   template<class T> | 
 | 199 |   HandleWrapper<T> NewHandleWrapper(T** object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { | 
 | 200 |     SetReference(pos_, *object); | 
| Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 201 |     MutableHandle<T> h(GetHandle(pos_)); | 
| Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 202 |     pos_++; | 
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 203 |     return HandleWrapper<T>(object, h); | 
 | 204 |   } | 
 | 205 |  | 
 | 206 |  private: | 
| Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 207 |   // References_storage_ needs to be first so that it appears in the same location as | 
 | 208 |   // HandleScope::references_. | 
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 209 |   StackReference<mirror::Object> references_storage_[kNumReferences]; | 
| Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 210 |  | 
 | 211 |   // The thread that the stack handle scope is a linked list upon. The stack handle scope will | 
 | 212 |   // push and pop itself from this thread. | 
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 213 |   Thread* const self_; | 
| Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 214 |  | 
 | 215 |   // Position new handles will be created. | 
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 216 |   size_t pos_; | 
 | 217 |  | 
 | 218 |   template<size_t kNumRefs> friend class StackHandleScope; | 
 | 219 | }; | 
 | 220 |  | 
 | 221 | }  // namespace art | 
 | 222 |  | 
 | 223 | #endif  // ART_RUNTIME_HANDLE_SCOPE_H_ |