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 | |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame^] | 54 | // Returns the size of a HandleScope containing num_references handles. |
| 55 | static size_t SizeOf(size_t pointer_size, uint32_t num_references) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 56 | // Assume that the layout is packed. |
| 57 | size_t header_size = pointer_size + sizeof(number_of_references_); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 58 | size_t data_size = sizeof(StackReference<mirror::Object>) * num_references; |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame^] | 59 | return header_size + data_size; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // Link to previous HandleScope or null. |
| 63 | HandleScope* GetLink() const { |
| 64 | return link_; |
| 65 | } |
| 66 | |
| 67 | void SetLink(HandleScope* link) { |
| 68 | DCHECK_NE(this, link); |
| 69 | link_ = link; |
| 70 | } |
| 71 | |
| 72 | // Sets the number_of_references_ field for constructing tables out of raw memory. Warning: will |
| 73 | // not resize anything. |
| 74 | void SetNumberOfReferences(uint32_t num_references) { |
| 75 | number_of_references_ = num_references; |
| 76 | } |
| 77 | |
| 78 | mirror::Object* GetReference(size_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 79 | ALWAYS_INLINE { |
| 80 | DCHECK_LT(i, number_of_references_); |
| 81 | return references_[i].AsMirrorPtr(); |
| 82 | } |
| 83 | |
| 84 | Handle<mirror::Object> GetHandle(size_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 85 | ALWAYS_INLINE { |
| 86 | DCHECK_LT(i, number_of_references_); |
| 87 | return Handle<mirror::Object>(&references_[i]); |
| 88 | } |
| 89 | |
| 90 | void SetReference(size_t i, mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 91 | ALWAYS_INLINE { |
| 92 | DCHECK_LT(i, number_of_references_); |
| 93 | references_[i].Assign(object); |
| 94 | } |
| 95 | |
| 96 | bool Contains(StackReference<mirror::Object>* handle_scope_entry) const { |
| 97 | // A HandleScope should always contain something. One created by the |
| 98 | // jni_compiler should have a jobject/jclass as a native method is |
| 99 | // passed in a this pointer or a class |
| 100 | DCHECK_GT(number_of_references_, 0U); |
| 101 | return ((&references_[0] <= handle_scope_entry) |
| 102 | && (handle_scope_entry <= (&references_[number_of_references_ - 1]))); |
| 103 | } |
| 104 | |
| 105 | // Offset of link within HandleScope, used by generated code |
| 106 | static size_t LinkOffset(size_t pointer_size) { |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | // Offset of length within handle scope, used by generated code |
| 111 | static size_t NumberOfReferencesOffset(size_t pointer_size) { |
| 112 | return pointer_size; |
| 113 | } |
| 114 | |
| 115 | // Offset of link within handle scope, used by generated code |
| 116 | static size_t ReferencesOffset(size_t pointer_size) { |
| 117 | return pointer_size + sizeof(number_of_references_); |
| 118 | } |
| 119 | |
| 120 | protected: |
| 121 | explicit HandleScope(size_t number_of_references) : |
| 122 | link_(nullptr), number_of_references_(number_of_references) { |
| 123 | } |
| 124 | |
| 125 | HandleScope* link_; |
| 126 | uint32_t number_of_references_; |
| 127 | |
| 128 | // number_of_references_ are available if this is allocated and filled in by jni_compiler. |
| 129 | StackReference<mirror::Object> references_[0]; |
| 130 | |
| 131 | private: |
| 132 | template<size_t kNumReferences> friend class StackHandleScope; |
| 133 | DISALLOW_COPY_AND_ASSIGN(HandleScope); |
| 134 | }; |
| 135 | |
| 136 | // A wrapper which wraps around Object** and restores the pointer in the destructor. |
| 137 | // TODO: Add more functionality. |
| 138 | template<class T> |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 139 | class HandleWrapper : public Handle<T> { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 140 | public: |
| 141 | HandleWrapper(T** obj, const Handle<T>& handle) |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 142 | : Handle<T>(handle), obj_(obj) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | ~HandleWrapper() { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 146 | *obj_ = Handle<T>::Get(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | private: |
| 150 | T** obj_; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | // Scoped handle storage of a fixed size that is usually stack allocated. |
| 154 | template<size_t kNumReferences> |
| 155 | class StackHandleScope : public HandleScope { |
| 156 | public: |
| 157 | explicit StackHandleScope(Thread* self); |
| 158 | ~StackHandleScope(); |
| 159 | |
| 160 | template<class T> |
| 161 | Handle<T> NewHandle(T* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 162 | SetReference(pos_, object); |
| 163 | return Handle<T>(GetHandle(pos_++)); |
| 164 | } |
| 165 | |
| 166 | template<class T> |
| 167 | HandleWrapper<T> NewHandleWrapper(T** object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 168 | SetReference(pos_, *object); |
| 169 | Handle<T> h(GetHandle(pos_++)); |
| 170 | return HandleWrapper<T>(object, h); |
| 171 | } |
| 172 | |
| 173 | private: |
| 174 | // references_storage_ needs to be first so that it matches the address of references_. |
| 175 | StackReference<mirror::Object> references_storage_[kNumReferences]; |
| 176 | Thread* const self_; |
| 177 | size_t pos_; |
| 178 | |
| 179 | template<size_t kNumRefs> friend class StackHandleScope; |
| 180 | }; |
| 181 | |
| 182 | } // namespace art |
| 183 | |
| 184 | #endif // ART_RUNTIME_HANDLE_SCOPE_H_ |