array.h revision e401d146407d61eeb99f8d6176b2ac13c4df1e33
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 "gc_root.h" 21#include "gc/allocator_type.h" 22#include "object.h" 23#include "object_callbacks.h" 24 25namespace art { 26 27template<class T> class Handle; 28 29namespace mirror { 30 31class MANAGED Array : public Object { 32 public: 33 // The size of a java.lang.Class representing an array. 34 static uint32_t ClassSize(size_t pointer_size); 35 36 // Allocates an array with the given properties, if kFillUsable is true the array will be of at 37 // least component_count size, however, if there's usable space at the end of the allocation the 38 // array will fill it. 39 template <bool kIsInstrumented, bool kFillUsable = false> 40 ALWAYS_INLINE static Array* Alloc(Thread* self, Class* array_class, int32_t component_count, 41 size_t component_size_shift, gc::AllocatorType allocator_type) 42 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 43 44 static Array* CreateMultiArray(Thread* self, Handle<Class> element_class, 45 Handle<IntArray> dimensions) 46 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 47 48 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, 49 ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 50 size_t SizeOf() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 51 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 52 ALWAYS_INLINE int32_t GetLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 53 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Array, length_)); 54 } 55 56 void SetLength(int32_t length) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 57 DCHECK_GE(length, 0); 58 // We use non transactional version since we can't undo this write. We also disable checking 59 // since it would fail during a transaction. 60 SetField32<false, false, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Array, length_), length); 61 } 62 63 static MemberOffset LengthOffset() { 64 return OFFSET_OF_OBJECT_MEMBER(Array, length_); 65 } 66 67 static MemberOffset DataOffset(size_t component_size); 68 69 void* GetRawData(size_t component_size, int32_t index) 70 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 71 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() + 72 + (index * component_size); 73 return reinterpret_cast<void*>(data); 74 } 75 76 const void* GetRawData(size_t component_size, int32_t index) const { 77 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() + 78 + (index * component_size); 79 return reinterpret_cast<void*>(data); 80 } 81 82 // Returns true if the index is valid. If not, throws an ArrayIndexOutOfBoundsException and 83 // returns false. 84 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 85 ALWAYS_INLINE bool CheckIsValidIndex(int32_t index) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 86 87 Array* CopyOf(Thread* self, int32_t new_length) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 88 89 protected: 90 void ThrowArrayStoreException(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 91 92 private: 93 void ThrowArrayIndexOutOfBoundsException(int32_t index) 94 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 95 96 // The number of array elements. 97 int32_t length_; 98 // Marker for the data (used by generated code) 99 uint32_t first_element_[0]; 100 101 DISALLOW_IMPLICIT_CONSTRUCTORS(Array); 102}; 103 104template<typename T> 105class MANAGED PrimitiveArray : public Array { 106 public: 107 typedef T ElementType; 108 109 static PrimitiveArray<T>* Alloc(Thread* self, size_t length) 110 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 111 112 const T* GetData() const ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 113 return reinterpret_cast<const T*>(GetRawData(sizeof(T), 0)); 114 } 115 116 T* GetData() ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 117 return reinterpret_cast<T*>(GetRawData(sizeof(T), 0)); 118 } 119 120 T Get(int32_t i) ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 121 122 T GetWithoutChecks(int32_t i) ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 123 DCHECK(CheckIsValidIndex(i)); 124 return GetData()[i]; 125 } 126 127 void Set(int32_t i, T value) ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 128 129 // TODO fix thread safety analysis broken by the use of template. This should be 130 // SHARED_LOCKS_REQUIRED(Locks::mutator_lock_). 131 template<bool kTransactionActive, bool kCheckTransaction = true> 132 void Set(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS; 133 134 // TODO fix thread safety analysis broken by the use of template. This should be 135 // SHARED_LOCKS_REQUIRED(Locks::mutator_lock_). 136 template<bool kTransactionActive, 137 bool kCheckTransaction = true, 138 VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 139 void SetWithoutChecks(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS; 140 141 /* 142 * Works like memmove(), except we guarantee not to allow tearing of array values (ie using 143 * smaller than element size copies). Arguments are assumed to be within the bounds of the array 144 * and the arrays non-null. 145 */ 146 void Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count) 147 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 148 149 /* 150 * Works like memcpy(), except we guarantee not to allow tearing of array values (ie using 151 * smaller than element size copies). Arguments are assumed to be within the bounds of the array 152 * and the arrays non-null. 153 */ 154 void Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count) 155 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 156 157 static void SetArrayClass(Class* array_class) { 158 CHECK(array_class_.IsNull()); 159 CHECK(array_class != nullptr); 160 array_class_ = GcRoot<Class>(array_class); 161 } 162 163 static Class* GetArrayClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 164 DCHECK(!array_class_.IsNull()); 165 return array_class_.Read(); 166 } 167 168 static void ResetArrayClass() { 169 CHECK(!array_class_.IsNull()); 170 array_class_ = GcRoot<Class>(nullptr); 171 } 172 173 static void VisitRoots(RootVisitor* visitor) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 174 175 private: 176 static GcRoot<Class> array_class_; 177 178 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray); 179}; 180 181// Either an IntArray or a LongArray. 182class PointerArray : public Array { 183 public: 184 template<typename T> 185 T GetElementPtrSize(uint32_t idx, size_t ptr_size) 186 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 187 188 template<bool kTransactionActive = false, bool kUnchecked = false, typename T> 189 void SetElementPtrSize(uint32_t idx, T element, size_t ptr_size) 190 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 191}; 192 193} // namespace mirror 194} // namespace art 195 196#endif // ART_RUNTIME_MIRROR_ARRAY_H_ 197