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