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 | } |
| 30 | class Thread; |
| 31 | |
| 32 | // HandleScopes can be allocated within the bridge frame between managed and native code backed by |
| 33 | // stack storage or manually allocated in native. |
| 34 | class HandleScope { |
| 35 | public: |
| 36 | ~HandleScope() {} |
| 37 | |
| 38 | // Number of references contained within this handle scope. |
| 39 | uint32_t NumberOfReferences() const { |
| 40 | return number_of_references_; |
| 41 | } |
| 42 | |
| 43 | // We have versions with and without explicit pointer size of the following. The first two are |
| 44 | // used at runtime, so OFFSETOF_MEMBER computes the right offsets automatically. The last one |
| 45 | // takes the pointer size explicitly so that at compile time we can cross-compile correctly. |
| 46 | |
| 47 | // Returns the size of a HandleScope containing num_references handles. |
| 48 | static size_t SizeOf(uint32_t num_references) { |
| 49 | size_t header_size = OFFSETOF_MEMBER(HandleScope, references_); |
| 50 | size_t data_size = sizeof(StackReference<mirror::Object>) * num_references; |
| 51 | return header_size + data_size; |
| 52 | } |
| 53 | |
| 54 | // Get the size of the handle scope for the number of entries, with padding added for potential alignment. |
| 55 | static size_t GetAlignedHandleScopeSize(uint32_t num_references) { |
| 56 | size_t handle_scope_size = SizeOf(num_references); |
| 57 | return RoundUp(handle_scope_size, 8); |
| 58 | } |
| 59 | |
| 60 | // Get the size of the handle scope for the number of entries, with padding added for potential alignment. |
| 61 | static size_t GetAlignedHandleScopeSizeTarget(size_t pointer_size, uint32_t num_references) { |
| 62 | // Assume that the layout is packed. |
| 63 | size_t header_size = pointer_size + sizeof(number_of_references_); |
| 64 | // This assumes there is no layout change between 32 and 64b. |
| 65 | size_t data_size = sizeof(StackReference<mirror::Object>) * num_references; |
| 66 | size_t handle_scope_size = header_size + data_size; |
| 67 | return RoundUp(handle_scope_size, 8); |
| 68 | } |
| 69 | |
| 70 | // Link to previous HandleScope or null. |
| 71 | HandleScope* GetLink() const { |
| 72 | return link_; |
| 73 | } |
| 74 | |
| 75 | void SetLink(HandleScope* link) { |
| 76 | DCHECK_NE(this, link); |
| 77 | link_ = link; |
| 78 | } |
| 79 | |
| 80 | // Sets the number_of_references_ field for constructing tables out of raw memory. Warning: will |
| 81 | // not resize anything. |
| 82 | void SetNumberOfReferences(uint32_t num_references) { |
| 83 | number_of_references_ = num_references; |
| 84 | } |
| 85 | |
| 86 | mirror::Object* GetReference(size_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 87 | ALWAYS_INLINE { |
| 88 | DCHECK_LT(i, number_of_references_); |
| 89 | return references_[i].AsMirrorPtr(); |
| 90 | } |
| 91 | |
| 92 | Handle<mirror::Object> GetHandle(size_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 93 | ALWAYS_INLINE { |
| 94 | DCHECK_LT(i, number_of_references_); |
| 95 | return Handle<mirror::Object>(&references_[i]); |
| 96 | } |
| 97 | |
| 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); |
| 109 | return ((&references_[0] <= handle_scope_entry) |
| 110 | && (handle_scope_entry <= (&references_[number_of_references_ - 1]))); |
| 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; |
| 141 | DISALLOW_COPY_AND_ASSIGN(HandleScope); |
| 142 | }; |
| 143 | |
| 144 | // A wrapper which wraps around Object** and restores the pointer in the destructor. |
| 145 | // TODO: Add more functionality. |
| 146 | template<class T> |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 147 | class HandleWrapper : public Handle<T> { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 148 | public: |
| 149 | HandleWrapper(T** obj, const Handle<T>& handle) |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 150 | : Handle<T>(handle), obj_(obj) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | ~HandleWrapper() { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 154 | *obj_ = Handle<T>::Get(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | private: |
| 158 | T** obj_; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | // Scoped handle storage of a fixed size that is usually stack allocated. |
| 162 | template<size_t kNumReferences> |
| 163 | class StackHandleScope : public HandleScope { |
| 164 | public: |
| 165 | explicit StackHandleScope(Thread* self); |
| 166 | ~StackHandleScope(); |
| 167 | |
| 168 | template<class T> |
| 169 | Handle<T> NewHandle(T* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 170 | SetReference(pos_, object); |
| 171 | return Handle<T>(GetHandle(pos_++)); |
| 172 | } |
| 173 | |
| 174 | template<class T> |
| 175 | HandleWrapper<T> NewHandleWrapper(T** object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 176 | SetReference(pos_, *object); |
| 177 | Handle<T> h(GetHandle(pos_++)); |
| 178 | return HandleWrapper<T>(object, h); |
| 179 | } |
| 180 | |
| 181 | private: |
| 182 | // references_storage_ needs to be first so that it matches the address of references_. |
| 183 | StackReference<mirror::Object> references_storage_[kNumReferences]; |
| 184 | Thread* const self_; |
| 185 | size_t pos_; |
| 186 | |
| 187 | template<size_t kNumRefs> friend class StackHandleScope; |
| 188 | }; |
| 189 | |
| 190 | } // namespace art |
| 191 | |
| 192 | #endif // ART_RUNTIME_HANDLE_SCOPE_H_ |