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 | |
| 33 | // HandleScopes can be allocated within the bridge frame between managed and native code backed by |
| 34 | // stack storage or manually allocated in native. |
Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 35 | class PACKED(4) HandleScope { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 36 | public: |
| 37 | ~HandleScope() {} |
| 38 | |
| 39 | // Number of references contained within this handle scope. |
| 40 | uint32_t NumberOfReferences() const { |
| 41 | return number_of_references_; |
| 42 | } |
| 43 | |
| 44 | // We have versions with and without explicit pointer size of the following. The first two are |
| 45 | // used at runtime, so OFFSETOF_MEMBER computes the right offsets automatically. The last one |
| 46 | // takes the pointer size explicitly so that at compile time we can cross-compile correctly. |
| 47 | |
| 48 | // Returns the size of a HandleScope containing num_references handles. |
| 49 | static size_t SizeOf(uint32_t num_references) { |
Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 50 | size_t header_size = sizeof(HandleScope); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 51 | size_t data_size = sizeof(StackReference<mirror::Object>) * num_references; |
| 52 | return header_size + data_size; |
| 53 | } |
| 54 | |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 55 | // Returns the size of a HandleScope containing num_references handles. |
| 56 | static size_t SizeOf(size_t pointer_size, uint32_t num_references) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 57 | // Assume that the layout is packed. |
| 58 | size_t header_size = pointer_size + sizeof(number_of_references_); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 59 | size_t data_size = sizeof(StackReference<mirror::Object>) * num_references; |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 60 | return header_size + data_size; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // Link to previous HandleScope or null. |
| 64 | HandleScope* GetLink() const { |
| 65 | return link_; |
| 66 | } |
| 67 | |
| 68 | void SetLink(HandleScope* link) { |
| 69 | DCHECK_NE(this, link); |
| 70 | link_ = link; |
| 71 | } |
| 72 | |
| 73 | // Sets the number_of_references_ field for constructing tables out of raw memory. Warning: will |
| 74 | // not resize anything. |
| 75 | void SetNumberOfReferences(uint32_t num_references) { |
| 76 | number_of_references_ = num_references; |
| 77 | } |
| 78 | |
| 79 | mirror::Object* GetReference(size_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 80 | ALWAYS_INLINE { |
| 81 | DCHECK_LT(i, number_of_references_); |
| 82 | return references_[i].AsMirrorPtr(); |
| 83 | } |
| 84 | |
| 85 | Handle<mirror::Object> GetHandle(size_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 86 | ALWAYS_INLINE { |
| 87 | DCHECK_LT(i, number_of_references_); |
| 88 | return Handle<mirror::Object>(&references_[i]); |
| 89 | } |
| 90 | |
| 91 | void SetReference(size_t i, mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 92 | ALWAYS_INLINE { |
| 93 | DCHECK_LT(i, number_of_references_); |
| 94 | references_[i].Assign(object); |
| 95 | } |
| 96 | |
| 97 | bool Contains(StackReference<mirror::Object>* handle_scope_entry) const { |
| 98 | // A HandleScope should always contain something. One created by the |
| 99 | // jni_compiler should have a jobject/jclass as a native method is |
| 100 | // passed in a this pointer or a class |
| 101 | DCHECK_GT(number_of_references_, 0U); |
Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 102 | return &references_[0] <= handle_scope_entry && |
| 103 | handle_scope_entry <= &references_[number_of_references_ - 1]; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // Offset of link within HandleScope, used by generated code |
| 107 | static size_t LinkOffset(size_t pointer_size) { |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | // Offset of length within handle scope, used by generated code |
| 112 | static size_t NumberOfReferencesOffset(size_t pointer_size) { |
| 113 | return pointer_size; |
| 114 | } |
| 115 | |
| 116 | // Offset of link within handle scope, used by generated code |
| 117 | static size_t ReferencesOffset(size_t pointer_size) { |
| 118 | return pointer_size + sizeof(number_of_references_); |
| 119 | } |
| 120 | |
| 121 | protected: |
| 122 | explicit HandleScope(size_t number_of_references) : |
| 123 | link_(nullptr), number_of_references_(number_of_references) { |
| 124 | } |
| 125 | |
| 126 | HandleScope* link_; |
| 127 | uint32_t number_of_references_; |
| 128 | |
| 129 | // number_of_references_ are available if this is allocated and filled in by jni_compiler. |
| 130 | StackReference<mirror::Object> references_[0]; |
| 131 | |
| 132 | private: |
| 133 | template<size_t kNumReferences> friend class StackHandleScope; |
| 134 | DISALLOW_COPY_AND_ASSIGN(HandleScope); |
| 135 | }; |
| 136 | |
| 137 | // A wrapper which wraps around Object** and restores the pointer in the destructor. |
| 138 | // TODO: Add more functionality. |
| 139 | template<class T> |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 140 | class HandleWrapper : public Handle<T> { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 141 | public: |
| 142 | HandleWrapper(T** obj, const Handle<T>& handle) |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 143 | : Handle<T>(handle), obj_(obj) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | ~HandleWrapper() { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 147 | *obj_ = Handle<T>::Get(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | private: |
| 151 | T** obj_; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | // Scoped handle storage of a fixed size that is usually stack allocated. |
| 155 | template<size_t kNumReferences> |
Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 156 | class PACKED(4) StackHandleScope : public HandleScope { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 157 | public: |
| 158 | explicit StackHandleScope(Thread* self); |
| 159 | ~StackHandleScope(); |
| 160 | |
Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 161 | // Currently unused, using this GetReference instead of the one in HandleScope is preferred to |
| 162 | // avoid compiler optimizations incorrectly optimizing out of bound array accesses. |
| 163 | // TODO: Remove this when it is un-necessary. |
| 164 | mirror::Object* GetReference(size_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 165 | ALWAYS_INLINE { |
| 166 | DCHECK_LT(i, number_of_references_); |
| 167 | return references_storage_[i].AsMirrorPtr(); |
| 168 | } |
| 169 | |
| 170 | Handle<mirror::Object> GetHandle(size_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 171 | ALWAYS_INLINE { |
| 172 | DCHECK_LT(i, number_of_references_); |
| 173 | return Handle<mirror::Object>(&references_storage_[i]); |
| 174 | } |
| 175 | |
| 176 | void SetReference(size_t i, mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 177 | ALWAYS_INLINE { |
| 178 | DCHECK_LT(i, number_of_references_); |
| 179 | references_storage_[i].Assign(object); |
| 180 | } |
| 181 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 182 | template<class T> |
| 183 | Handle<T> NewHandle(T* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 184 | SetReference(pos_, object); |
| 185 | return Handle<T>(GetHandle(pos_++)); |
| 186 | } |
| 187 | |
| 188 | template<class T> |
| 189 | HandleWrapper<T> NewHandleWrapper(T** object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 190 | SetReference(pos_, *object); |
| 191 | Handle<T> h(GetHandle(pos_++)); |
| 192 | return HandleWrapper<T>(object, h); |
| 193 | } |
| 194 | |
| 195 | private: |
| 196 | // references_storage_ needs to be first so that it matches the address of references_. |
| 197 | StackReference<mirror::Object> references_storage_[kNumReferences]; |
| 198 | Thread* const self_; |
| 199 | size_t pos_; |
| 200 | |
| 201 | template<size_t kNumRefs> friend class StackHandleScope; |
| 202 | }; |
| 203 | |
| 204 | } // namespace art |
| 205 | |
| 206 | #endif // ART_RUNTIME_HANDLE_SCOPE_H_ |