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" |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 25 | #include "verify_object.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | namespace mirror { |
| 29 | class Object; |
| 30 | } |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 31 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 32 | class Thread; |
| 33 | |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 34 | // HandleScopes are scoped objects containing a number of Handles. They are used to allocate |
| 35 | // handles, for these handles (and the objects contained within them) to be visible/roots for the |
| 36 | // GC. It is most common to stack allocate HandleScopes using StackHandleScope. |
Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 37 | class PACKED(4) HandleScope { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 38 | public: |
| 39 | ~HandleScope() {} |
| 40 | |
| 41 | // Number of references contained within this handle scope. |
| 42 | uint32_t NumberOfReferences() const { |
| 43 | return number_of_references_; |
| 44 | } |
| 45 | |
| 46 | // We have versions with and without explicit pointer size of the following. The first two are |
| 47 | // used at runtime, so OFFSETOF_MEMBER computes the right offsets automatically. The last one |
| 48 | // takes the pointer size explicitly so that at compile time we can cross-compile correctly. |
| 49 | |
| 50 | // Returns the size of a HandleScope containing num_references handles. |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 51 | static size_t SizeOf(uint32_t num_references); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 52 | |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 53 | // Returns the size of a HandleScope containing num_references handles. |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 54 | static size_t SizeOf(size_t pointer_size, uint32_t num_references); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 55 | |
| 56 | // Link to previous HandleScope or null. |
| 57 | HandleScope* GetLink() const { |
| 58 | return link_; |
| 59 | } |
| 60 | |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 61 | ALWAYS_INLINE mirror::Object* GetReference(size_t i) const |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 62 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 63 | |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 64 | ALWAYS_INLINE Handle<mirror::Object> GetHandle(size_t i) |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 65 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 66 | |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 67 | ALWAYS_INLINE MutableHandle<mirror::Object> GetMutableHandle(size_t i) |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 68 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 69 | |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 70 | ALWAYS_INLINE void SetReference(size_t i, mirror::Object* object) |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 71 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 72 | |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 73 | ALWAYS_INLINE bool Contains(StackReference<mirror::Object>* handle_scope_entry) const; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 74 | |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 75 | // Offset of link within HandleScope, used by generated code. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 76 | static size_t LinkOffset(size_t pointer_size ATTRIBUTE_UNUSED) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 80 | // Offset of length within handle scope, used by generated code. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 81 | static size_t NumberOfReferencesOffset(size_t pointer_size) { |
| 82 | return pointer_size; |
| 83 | } |
| 84 | |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 85 | // Offset of link within handle scope, used by generated code. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 86 | static size_t ReferencesOffset(size_t pointer_size) { |
| 87 | return pointer_size + sizeof(number_of_references_); |
| 88 | } |
| 89 | |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 90 | // Placement new creation. |
| 91 | static HandleScope* Create(void* storage, HandleScope* link, uint32_t num_references) |
| 92 | WARN_UNUSED { |
| 93 | return new (storage) HandleScope(link, num_references); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 96 | protected: |
| 97 | // Return backing storage used for references. |
| 98 | ALWAYS_INLINE StackReference<mirror::Object>* GetReferences() const { |
| 99 | uintptr_t address = reinterpret_cast<uintptr_t>(this) + ReferencesOffset(sizeof(void*)); |
| 100 | return reinterpret_cast<StackReference<mirror::Object>*>(address); |
| 101 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 102 | |
Mathieu Chartier | d035c2d | 2014-10-27 17:30:20 -0700 | [diff] [blame] | 103 | explicit HandleScope(size_t number_of_references) : |
| 104 | link_(nullptr), number_of_references_(number_of_references) { |
| 105 | } |
| 106 | |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 107 | // Semi-hidden constructor. Construction expected by generated code and StackHandleScope. |
| 108 | explicit HandleScope(HandleScope* link, uint32_t num_references) : |
| 109 | link_(link), number_of_references_(num_references) { |
| 110 | } |
| 111 | |
| 112 | // Link-list of handle scopes. The root is held by a Thread. |
| 113 | HandleScope* const link_; |
| 114 | |
| 115 | // Number of handlerized references. |
| 116 | const uint32_t number_of_references_; |
| 117 | |
| 118 | // Storage for references. |
| 119 | // StackReference<mirror::Object> references_[number_of_references_] |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 120 | |
| 121 | private: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 122 | DISALLOW_COPY_AND_ASSIGN(HandleScope); |
| 123 | }; |
| 124 | |
| 125 | // A wrapper which wraps around Object** and restores the pointer in the destructor. |
| 126 | // TODO: Add more functionality. |
| 127 | template<class T> |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 128 | class HandleWrapper : public MutableHandle<T> { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 129 | public: |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 130 | HandleWrapper(T** obj, const MutableHandle<T>& handle) |
| 131 | : MutableHandle<T>(handle), obj_(obj) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | ~HandleWrapper() { |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 135 | *obj_ = MutableHandle<T>::Get(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | private: |
Ian Rogers | b5cb18a | 2014-10-21 15:05:36 -0700 | [diff] [blame] | 139 | T** const obj_; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | // Scoped handle storage of a fixed size that is usually stack allocated. |
| 143 | template<size_t kNumReferences> |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 144 | class PACKED(4) StackHandleScope FINAL : public HandleScope { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 145 | public: |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 146 | explicit ALWAYS_INLINE StackHandleScope(Thread* self, mirror::Object* fill_value = nullptr); |
| 147 | ALWAYS_INLINE ~StackHandleScope(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 148 | |
Ian Rogers | b5cb18a | 2014-10-21 15:05:36 -0700 | [diff] [blame] | 149 | template<class T> |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 150 | ALWAYS_INLINE MutableHandle<T> NewHandle(T* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 151 | |
Ian Rogers | b5cb18a | 2014-10-21 15:05:36 -0700 | [diff] [blame] | 152 | template<class T> |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 153 | ALWAYS_INLINE HandleWrapper<T> NewHandleWrapper(T** object) |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 154 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | b5cb18a | 2014-10-21 15:05:36 -0700 | [diff] [blame] | 155 | |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 156 | ALWAYS_INLINE void SetReference(size_t i, mirror::Object* object) |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 157 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 158 | |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 159 | private: |
| 160 | template<class T> |
| 161 | ALWAYS_INLINE MutableHandle<T> GetHandle(size_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 162 | DCHECK_LT(i, kNumReferences); |
| 163 | return MutableHandle<T>(&GetReferences()[i]); |
| 164 | } |
| 165 | |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 166 | // Reference storage needs to be first as expected by the HandleScope layout. |
| 167 | StackReference<mirror::Object> storage_[kNumReferences]; |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 168 | |
| 169 | // The thread that the stack handle scope is a linked list upon. The stack handle scope will |
| 170 | // push and pop itself from this thread. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 171 | Thread* const self_; |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 172 | |
| 173 | // Position new handles will be created. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 174 | size_t pos_; |
| 175 | |
| 176 | template<size_t kNumRefs> friend class StackHandleScope; |
| 177 | }; |
| 178 | |
| 179 | } // namespace art |
| 180 | |
| 181 | #endif // ART_RUNTIME_HANDLE_SCOPE_H_ |