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