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