1 /* 2 * Copyright (C) 2011 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_MIRROR_OBJECT_ARRAY_H_ 18 #define ART_RUNTIME_MIRROR_OBJECT_ARRAY_H_ 19 20 #include <iterator> 21 #include "array.h" 22 #include "base/iteration_range.h" 23 #include "base/macros.h" 24 #include "obj_ptr.h" 25 26 namespace art HIDDEN { 27 namespace mirror { 28 29 template<typename T, typename Container> class ArrayIter; 30 template <typename T> using ConstObjPtrArrayIter = ArrayIter<T, const ObjPtr<ObjectArray<T>>>; 31 template <typename T> using ConstHandleArrayIter = ArrayIter<T, const Handle<ObjectArray<T>>>; 32 template <typename T> using ObjPtrArrayIter = ArrayIter<T, ObjPtr<ObjectArray<T>>>; 33 template <typename T> using HandleArrayIter = ArrayIter<T, Handle<ObjectArray<T>>>; 34 35 template<class T> 36 class MANAGED ObjectArray: public Array { 37 public: 38 MIRROR_CLASS("[Ljava/lang/Object;"); 39 40 // The size of Object[].class. ClassSize(PointerSize pointer_size)41 static uint32_t ClassSize(PointerSize pointer_size) { 42 return Array::ClassSize(pointer_size); 43 } 44 45 static ObjPtr<ObjectArray<T>> Alloc(Thread* self, 46 ObjPtr<Class> object_array_class, 47 int32_t length, 48 gc::AllocatorType allocator_type) 49 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); 50 51 static ObjPtr<ObjectArray<T>> Alloc(Thread* self, 52 ObjPtr<Class> object_array_class, 53 int32_t length) 54 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); 55 56 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, 57 ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 58 ALWAYS_INLINE ObjPtr<T> Get(int32_t i) REQUIRES_SHARED(Locks::mutator_lock_); 59 60 // Returns true if the object can be stored into the array. If not, throws 61 // an ArrayStoreException and returns false. 62 // TODO fix thread safety analysis: should be REQUIRES_SHARED(Locks::mutator_lock_). 63 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 64 bool CheckAssignable(ObjPtr<T> object) NO_THREAD_SAFETY_ANALYSIS; 65 66 ALWAYS_INLINE void Set(int32_t i, ObjPtr<T> object) REQUIRES_SHARED(Locks::mutator_lock_); 67 // TODO fix thread safety analysis: should be REQUIRES_SHARED(Locks::mutator_lock_). 68 template<bool kTransactionActive, bool kCheckTransaction = true, 69 VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 70 ALWAYS_INLINE void Set(int32_t i, ObjPtr<T> object) NO_THREAD_SAFETY_ANALYSIS; 71 72 // Set element without bound and element type checks, to be used in limited 73 // circumstances, such as during boot image writing. 74 // TODO fix thread safety analysis broken by the use of template. This should be 75 // REQUIRES_SHARED(Locks::mutator_lock_). 76 template<bool kTransactionActive, bool kCheckTransaction = true, 77 VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 78 ALWAYS_INLINE void SetWithoutChecks(int32_t i, ObjPtr<T> object) NO_THREAD_SAFETY_ANALYSIS; 79 // TODO fix thread safety analysis broken by the use of template. This should be 80 // REQUIRES_SHARED(Locks::mutator_lock_). 81 template<bool kTransactionActive, bool kCheckTransaction = true, 82 VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 83 ALWAYS_INLINE void SetWithoutChecksAndWriteBarrier(int32_t i, ObjPtr<T> object) 84 NO_THREAD_SAFETY_ANALYSIS; 85 86 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, 87 ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 88 ALWAYS_INLINE ObjPtr<T> GetWithoutChecks(int32_t i) REQUIRES_SHARED(Locks::mutator_lock_); 89 90 // Copy src into this array (dealing with overlaps as memmove does) without assignability checks. 91 void AssignableMemmove(int32_t dst_pos, 92 ObjPtr<ObjectArray<T>> src, 93 int32_t src_pos, 94 int32_t count) 95 REQUIRES_SHARED(Locks::mutator_lock_); 96 97 // Copy src into this array assuming no overlap and without assignability checks. 98 void AssignableMemcpy(int32_t dst_pos, 99 ObjPtr<ObjectArray<T>> src, 100 int32_t src_pos, 101 int32_t count) 102 REQUIRES_SHARED(Locks::mutator_lock_); 103 104 // Copy src into this array with assignability checks. 105 template<bool kTransactionActive> 106 void AssignableCheckingMemcpy(int32_t dst_pos, 107 ObjPtr<ObjectArray<T>> src, 108 int32_t src_pos, 109 int32_t count, 110 bool throw_exception) 111 REQUIRES_SHARED(Locks::mutator_lock_); 112 113 static ObjPtr<ObjectArray<T>> CopyOf(Handle<ObjectArray<T>> h_this, 114 Thread* self, 115 int32_t new_length) 116 REQUIRES_SHARED(Locks::mutator_lock_) 117 REQUIRES(!Roles::uninterruptible_); 118 119 static MemberOffset OffsetOfElement(int32_t i); 120 121 inline ConstObjPtrArrayIter<T> cbegin() const REQUIRES_SHARED(Locks::mutator_lock_); 122 inline ConstObjPtrArrayIter<T> cend() const REQUIRES_SHARED(Locks::mutator_lock_); ConstIterate()123 inline IterationRange<ConstObjPtrArrayIter<T>> ConstIterate() const REQUIRES_SHARED(Locks::mutator_lock_) { 124 return IterationRange(cbegin(), cend()); 125 } 126 inline ObjPtrArrayIter<T> begin() REQUIRES_SHARED(Locks::mutator_lock_); 127 inline ObjPtrArrayIter<T> end() REQUIRES_SHARED(Locks::mutator_lock_); Iterate()128 inline IterationRange<ObjPtrArrayIter<T>> Iterate() REQUIRES_SHARED(Locks::mutator_lock_) { 129 return IterationRange(begin(), end()); 130 } 131 132 static inline ConstHandleArrayIter<T> cbegin(const Handle<ObjectArray<T>>& h_this) 133 REQUIRES_SHARED(Locks::mutator_lock_); 134 static inline ConstHandleArrayIter<T> cend(const Handle<ObjectArray<T>>& h_this) 135 REQUIRES_SHARED(Locks::mutator_lock_); ConstIterate(const Handle<ObjectArray<T>> & h_this)136 static inline IterationRange<ConstHandleArrayIter<T>> ConstIterate( 137 const Handle<ObjectArray<T>>& h_this) REQUIRES_SHARED(Locks::mutator_lock_) { 138 return IterationRange(cbegin(h_this), cend(h_this)); 139 } 140 static inline HandleArrayIter<T> begin(Handle<ObjectArray<T>>& h_this) 141 REQUIRES_SHARED(Locks::mutator_lock_); 142 static inline HandleArrayIter<T> end(Handle<ObjectArray<T>>& h_this) 143 REQUIRES_SHARED(Locks::mutator_lock_); Iterate(Handle<ObjectArray<T>> & h_this)144 static inline IterationRange<HandleArrayIter<T>> Iterate(Handle<ObjectArray<T>>& h_this) 145 REQUIRES_SHARED(Locks::mutator_lock_) { 146 return IterationRange(begin(h_this), end(h_this)); 147 } 148 149 private: 150 // TODO fix thread safety analysis broken by the use of template. This should be 151 // REQUIRES_SHARED(Locks::mutator_lock_). 152 template<typename Visitor> 153 void VisitReferences(const Visitor& visitor) NO_THREAD_SAFETY_ANALYSIS; 154 template<typename Visitor> 155 void VisitReferences(const Visitor& visitor, 156 MemberOffset begin, 157 MemberOffset end) NO_THREAD_SAFETY_ANALYSIS; 158 159 friend class Object; // For VisitReferences 160 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray); 161 }; 162 163 // Everything is NO_THREAD_SAFETY_ANALYSIS to work-around STL incompat with thread-annotations. 164 // Everything should have REQUIRES_SHARED(Locks::mutator_lock_). 165 template <typename T, typename Container> 166 class ArrayIter { 167 private: 168 using Iter = ArrayIter<T, Container>; 169 170 public: 171 using iterator_category = std::forward_iterator_tag; 172 using value_type = ObjPtr<T>; 173 using difference_type = ptrdiff_t; 174 using pointer = value_type*; 175 using reference = value_type&; 176 ArrayIter(Container array,int32_t idx)177 ArrayIter(Container array, int32_t idx) NO_THREAD_SAFETY_ANALYSIS : array_(array), idx_(idx) { 178 CheckIdx(); 179 } 180 181 ArrayIter(const Iter& other) = default; // NOLINT(runtime/explicit) 182 Iter& operator=(const Iter& other) = default; 183 184 bool operator!=(const Iter& other) const NO_THREAD_SAFETY_ANALYSIS { 185 CheckIdx(); 186 return !(*this == other); 187 } 188 bool operator==(const Iter& other) const NO_THREAD_SAFETY_ANALYSIS { 189 return Ptr(other.array_) == Ptr(array_) && other.idx_ == idx_; 190 } 191 Iter& operator++() NO_THREAD_SAFETY_ANALYSIS { 192 idx_++; 193 CheckIdx(); 194 return *this; 195 } 196 Iter operator++(int) NO_THREAD_SAFETY_ANALYSIS { 197 Iter res(this); 198 idx_++; 199 CheckIdx(); 200 return res; 201 } 202 ObjPtr<T> operator->() const NO_THREAD_SAFETY_ANALYSIS { 203 CheckIdx(); 204 return array_->GetWithoutChecks(idx_); 205 } 206 ObjPtr<T> operator*() const NO_THREAD_SAFETY_ANALYSIS { 207 CheckIdx(); 208 return array_->GetWithoutChecks(idx_); 209 } 210 211 private: 212 // Checks current index and that locks are properly held. 213 void CheckIdx() const REQUIRES_SHARED(Locks::mutator_lock_); 214 Ptr(const Handle<ObjectArray<T>> & p)215 static ObjectArray<T>* Ptr(const Handle<ObjectArray<T>>& p) 216 REQUIRES_SHARED(Locks::mutator_lock_) { 217 return p.Get(); 218 } Ptr(const ObjPtr<ObjectArray<T>> & p)219 static ObjectArray<T>* Ptr(const ObjPtr<ObjectArray<T>>& p) 220 REQUIRES_SHARED(Locks::mutator_lock_) { 221 return p.Ptr(); 222 } 223 224 Container array_; 225 int32_t idx_; 226 }; 227 228 } // namespace mirror 229 } // namespace art 230 231 #endif // ART_RUNTIME_MIRROR_OBJECT_ARRAY_H_ 232