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_ARRAY_H_ 18 #define ART_RUNTIME_MIRROR_ARRAY_H_ 19 20 #include "base/bit_utils.h" 21 #include "base/enums.h" 22 #include "obj_ptr.h" 23 #include "object.h" 24 25 namespace art { 26 27 namespace gc { 28 enum AllocatorType : char; 29 } // namespace gc 30 31 template<class T> class Handle; 32 class Thread; 33 34 namespace mirror { 35 36 class MANAGED Array : public Object { 37 public: 38 static constexpr size_t kFirstElementOffset = 12u; 39 40 // The size of a java.lang.Class representing an array. 41 static uint32_t ClassSize(PointerSize pointer_size); 42 43 // Allocates an array with the given properties, if kFillUsable is true the array will be of at 44 // least component_count size, however, if there's usable space at the end of the allocation the 45 // array will fill it. 46 template <bool kIsInstrumented = true, bool kFillUsable = false> 47 ALWAYS_INLINE static ObjPtr<Array> Alloc(Thread* self, 48 ObjPtr<Class> array_class, 49 int32_t component_count, 50 size_t component_size_shift, 51 gc::AllocatorType allocator_type) 52 REQUIRES_SHARED(Locks::mutator_lock_) 53 REQUIRES(!Roles::uninterruptible_); 54 55 static ObjPtr<Array> CreateMultiArray(Thread* self, 56 Handle<Class> element_class, 57 Handle<IntArray> dimensions) 58 REQUIRES_SHARED(Locks::mutator_lock_) 59 REQUIRES(!Roles::uninterruptible_); 60 61 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 62 size_t SizeOf() REQUIRES_SHARED(Locks::mutator_lock_); 63 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> GetLength()64 ALWAYS_INLINE int32_t GetLength() REQUIRES_SHARED(Locks::mutator_lock_) { 65 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Array, length_)); 66 } 67 SetLength(int32_t length)68 void SetLength(int32_t length) REQUIRES_SHARED(Locks::mutator_lock_) { 69 DCHECK_GE(length, 0); 70 // We use non transactional version since we can't undo this write. We also disable checking 71 // since it would fail during a transaction. 72 SetField32<false, false, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Array, length_), length); 73 } 74 LengthOffset()75 static constexpr MemberOffset LengthOffset() { 76 return OFFSET_OF_OBJECT_MEMBER(Array, length_); 77 } 78 DataOffset(size_t component_size)79 static constexpr MemberOffset DataOffset(size_t component_size) { 80 DCHECK(IsPowerOfTwo(component_size)) << component_size; 81 size_t data_offset = RoundUp(OFFSETOF_MEMBER(Array, first_element_), component_size); 82 DCHECK_EQ(RoundUp(data_offset, component_size), data_offset) 83 << "Array data offset isn't aligned with component size"; 84 return MemberOffset(data_offset); 85 } 86 template <size_t kComponentSize> DataOffset()87 static constexpr MemberOffset DataOffset() { 88 static_assert(IsPowerOfTwo(kComponentSize), "Invalid component size"); 89 constexpr size_t data_offset = RoundUp(kFirstElementOffset, kComponentSize); 90 static_assert(RoundUp(data_offset, kComponentSize) == data_offset, "RoundUp fail"); 91 return MemberOffset(data_offset); 92 } 93 FirstElementOffset()94 static constexpr size_t FirstElementOffset() { 95 return OFFSETOF_MEMBER(Array, first_element_); 96 } 97 GetRawData(size_t component_size,int32_t index)98 void* GetRawData(size_t component_size, int32_t index) 99 REQUIRES_SHARED(Locks::mutator_lock_) { 100 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() + 101 + (index * component_size); 102 return reinterpret_cast<void*>(data); 103 } 104 template <size_t kComponentSize> GetRawData(int32_t index)105 void* GetRawData(int32_t index) REQUIRES_SHARED(Locks::mutator_lock_) { 106 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset<kComponentSize>().Int32Value() + 107 + (index * kComponentSize); 108 return reinterpret_cast<void*>(data); 109 } 110 GetRawData(size_t component_size,int32_t index)111 const void* GetRawData(size_t component_size, int32_t index) const { 112 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() + 113 + (index * component_size); 114 return reinterpret_cast<void*>(data); 115 } 116 template <size_t kComponentSize> GetRawData(int32_t index)117 const void* GetRawData(int32_t index) const { 118 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset<kComponentSize>().Int32Value() + 119 + (index * kComponentSize); 120 return reinterpret_cast<void*>(data); 121 } 122 123 // Returns true if the index is valid. If not, throws an ArrayIndexOutOfBoundsException and 124 // returns false. 125 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 126 ALWAYS_INLINE bool CheckIsValidIndex(int32_t index) REQUIRES_SHARED(Locks::mutator_lock_); 127 128 static ObjPtr<Array> CopyOf(Handle<Array> h_this, Thread* self, int32_t new_length) 129 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); 130 131 protected: 132 void ThrowArrayStoreException(ObjPtr<Object> object) REQUIRES_SHARED(Locks::mutator_lock_) 133 REQUIRES(!Roles::uninterruptible_); 134 135 private: 136 void ThrowArrayIndexOutOfBoundsException(int32_t index) 137 REQUIRES_SHARED(Locks::mutator_lock_); 138 139 // The number of array elements. 140 // We only use the field indirectly using the LengthOffset() method. 141 int32_t length_ ATTRIBUTE_UNUSED; 142 // Marker for the data (used by generated code) 143 // We only use the field indirectly using the DataOffset() method. 144 uint32_t first_element_[0] ATTRIBUTE_UNUSED; 145 146 DISALLOW_IMPLICIT_CONSTRUCTORS(Array); 147 }; 148 149 template<typename T> 150 class MANAGED PrimitiveArray : public Array { 151 public: 152 typedef T ElementType; 153 154 static ObjPtr<PrimitiveArray<T>> Alloc(Thread* self, size_t length) 155 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); 156 157 static ObjPtr<PrimitiveArray<T>> AllocateAndFill(Thread* self, const T* data, size_t length) 158 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); 159 160 GetData()161 const T* GetData() const ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) { 162 return reinterpret_cast<const T*>(GetRawData<sizeof(T)>(0)); 163 } 164 GetData()165 T* GetData() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) { 166 return reinterpret_cast<T*>(GetRawData<sizeof(T)>(0)); 167 } 168 169 T Get(int32_t i) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_); 170 GetWithoutChecks(int32_t i)171 T GetWithoutChecks(int32_t i) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) { 172 DCHECK(CheckIsValidIndex(i)) << "i=" << i << " length=" << GetLength(); 173 return GetData()[i]; 174 } 175 176 void Set(int32_t i, T value) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_); 177 178 // TODO fix thread safety analysis broken by the use of template. This should be 179 // REQUIRES_SHARED(Locks::mutator_lock_). 180 template<bool kTransactionActive, bool kCheckTransaction = true> 181 void Set(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS; 182 183 // TODO fix thread safety analysis broken by the use of template. This should be 184 // REQUIRES_SHARED(Locks::mutator_lock_). 185 template<bool kTransactionActive, 186 bool kCheckTransaction = true, 187 VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 188 void SetWithoutChecks(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS; 189 190 /* 191 * Works like memmove(), except we guarantee not to allow tearing of array values (ie using 192 * smaller than element size copies). Arguments are assumed to be within the bounds of the array 193 * and the arrays non-null. 194 */ 195 void Memmove(int32_t dst_pos, ObjPtr<PrimitiveArray<T>> src, int32_t src_pos, int32_t count) 196 REQUIRES_SHARED(Locks::mutator_lock_); 197 198 /* 199 * Works like memcpy(), except we guarantee not to allow tearing of array values (ie using 200 * smaller than element size copies). Arguments are assumed to be within the bounds of the array 201 * and the arrays non-null. 202 */ 203 void Memcpy(int32_t dst_pos, ObjPtr<PrimitiveArray<T>> src, int32_t src_pos, int32_t count) 204 REQUIRES_SHARED(Locks::mutator_lock_); 205 206 private: 207 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray); 208 }; 209 210 // Declare the different primitive arrays. Instantiations will be in array.cc. 211 extern template class PrimitiveArray<uint8_t>; // BooleanArray 212 extern template class PrimitiveArray<int8_t>; // ByteArray 213 extern template class PrimitiveArray<uint16_t>; // CharArray 214 extern template class PrimitiveArray<double>; // DoubleArray 215 extern template class PrimitiveArray<float>; // FloatArray 216 extern template class PrimitiveArray<int32_t>; // IntArray 217 extern template class PrimitiveArray<int64_t>; // LongArray 218 extern template class PrimitiveArray<int16_t>; // ShortArray 219 220 // Either an IntArray or a LongArray. 221 class PointerArray : public Array { 222 public: 223 template<typename T, VerifyObjectFlags kVerifyFlags = kVerifyNone> 224 T GetElementPtrSize(uint32_t idx, PointerSize ptr_size) 225 REQUIRES_SHARED(Locks::mutator_lock_); 226 template<typename T, PointerSize kPtrSize, VerifyObjectFlags kVerifyFlags = kVerifyNone> 227 T GetElementPtrSize(uint32_t idx) 228 REQUIRES_SHARED(Locks::mutator_lock_); 229 // Same as GetElementPtrSize, but uses unchecked version of array conversion. It is thus not 230 // checked whether kPtrSize matches the underlying array. Only use after at least one invocation 231 // of GetElementPtrSize! 232 template<typename T, PointerSize kPtrSize, VerifyObjectFlags kVerifyFlags = kVerifyNone> 233 T GetElementPtrSizeUnchecked(uint32_t idx) 234 REQUIRES_SHARED(Locks::mutator_lock_); 235 236 template<VerifyObjectFlags kVerifyFlags = kVerifyNone> ElementAddress(size_t index,PointerSize ptr_size)237 void** ElementAddress(size_t index, PointerSize ptr_size) REQUIRES_SHARED(Locks::mutator_lock_) { 238 DCHECK_LT(index, static_cast<size_t>(GetLength<kVerifyFlags>())); 239 return reinterpret_cast<void**>(reinterpret_cast<uint8_t*>(this) + 240 Array::DataOffset(static_cast<size_t>(ptr_size)).Uint32Value() + 241 static_cast<size_t>(ptr_size) * index); 242 } 243 244 template<bool kTransactionActive = false, bool kCheckTransaction = true, bool kUnchecked = false> 245 void SetElementPtrSize(uint32_t idx, uint64_t element, PointerSize ptr_size) 246 REQUIRES_SHARED(Locks::mutator_lock_); 247 template<bool kTransactionActive = false, 248 bool kCheckTransaction = true, 249 bool kUnchecked = false, 250 typename T> 251 void SetElementPtrSize(uint32_t idx, T* element, PointerSize ptr_size) 252 REQUIRES_SHARED(Locks::mutator_lock_); 253 254 // Fixup the pointers in the dest arrays by passing our pointers through the visitor. Only copies 255 // to dest if visitor(source_ptr) != source_ptr. 256 template <VerifyObjectFlags kVerifyFlags = kVerifyNone, typename Visitor> 257 void Fixup(ObjPtr<mirror::PointerArray> dest, PointerSize pointer_size, const Visitor& visitor) 258 REQUIRES_SHARED(Locks::mutator_lock_); 259 260 // Works like memcpy(), except we guarantee not to allow tearing of array values (ie using smaller 261 // than element size copies). Arguments are assumed to be within the bounds of the array and the 262 // arrays non-null. Cannot be called in an active transaction. 263 template<bool kUnchecked = false> 264 void Memcpy(int32_t dst_pos, 265 ObjPtr<PointerArray> src, 266 int32_t src_pos, 267 int32_t count, 268 PointerSize pointer_size) 269 REQUIRES_SHARED(Locks::mutator_lock_); 270 }; 271 272 } // namespace mirror 273 } // namespace art 274 275 #endif // ART_RUNTIME_MIRROR_ARRAY_H_ 276