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