handle_scope.h revision bc56fc3242ea2fad6630abdee1657f444eee8d30
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 26namespace art { 27namespace mirror { 28class Object; 29} 30class 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. 34class PACKED(4) 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 = sizeof(HandleScope); 50 size_t data_size = sizeof(StackReference<mirror::Object>) * num_references; 51 return header_size + data_size; 52 } 53 54 // Returns the size of a HandleScope containing num_references handles. 55 static size_t SizeOf(size_t pointer_size, uint32_t num_references) { 56 // Assume that the layout is packed. 57 size_t header_size = pointer_size + sizeof(number_of_references_); 58 size_t data_size = sizeof(StackReference<mirror::Object>) * num_references; 59 return header_size + data_size; 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. 138template<class T> 139class HandleWrapper : public Handle<T> { 140 public: 141 HandleWrapper(T** obj, const Handle<T>& handle) 142 : Handle<T>(handle), obj_(obj) { 143 } 144 145 ~HandleWrapper() { 146 *obj_ = Handle<T>::Get(); 147 } 148 149 private: 150 T** obj_; 151}; 152 153// Scoped handle storage of a fixed size that is usually stack allocated. 154template<size_t kNumReferences> 155class PACKED(4) StackHandleScope : public HandleScope { 156 public: 157 explicit StackHandleScope(Thread* self); 158 ~StackHandleScope(); 159 160 // Currently unused, using this GetReference instead of the one in HandleScope is preferred to 161 // avoid compiler optimizations incorrectly optimizing out of bound array accesses. 162 // TODO: Remove this when it is un-necessary. 163 mirror::Object* GetReference(size_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) 164 ALWAYS_INLINE { 165 DCHECK_LT(i, number_of_references_); 166 return references_storage_[i].AsMirrorPtr(); 167 } 168 169 Handle<mirror::Object> GetHandle(size_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) 170 ALWAYS_INLINE { 171 DCHECK_LT(i, number_of_references_); 172 return Handle<mirror::Object>(&references_storage_[i]); 173 } 174 175 void SetReference(size_t i, mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) 176 ALWAYS_INLINE { 177 DCHECK_LT(i, number_of_references_); 178 references_storage_[i].Assign(object); 179 } 180 181 template<class T> 182 Handle<T> NewHandle(T* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 183 SetReference(pos_, object); 184 return Handle<T>(GetHandle(pos_++)); 185 } 186 187 template<class T> 188 HandleWrapper<T> NewHandleWrapper(T** object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 189 SetReference(pos_, *object); 190 Handle<T> h(GetHandle(pos_++)); 191 return HandleWrapper<T>(object, h); 192 } 193 194 private: 195 // references_storage_ needs to be first so that it matches the address of references_. 196 StackReference<mirror::Object> references_storage_[kNumReferences]; 197 Thread* const self_; 198 size_t pos_; 199 200 template<size_t kNumRefs> friend class StackHandleScope; 201}; 202 203} // namespace art 204 205#endif // ART_RUNTIME_HANDLE_SCOPE_H_ 206