blob: 19f9a927e3dfc340471bbbd5d4557991331c1868 [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_MIRROR_ARRAY_H_
18#define ART_RUNTIME_MIRROR_ARRAY_H_
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019
David Srbecky56de89a2018-10-01 15:32:20 +010020#include "base/bit_utils.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070021#include "base/enums.h"
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070022#include "obj_ptr.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "object.h"
24
25namespace art {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070026
Andreas Gampe391be3a2019-05-14 12:41:40 -070027namespace gc {
28enum AllocatorType : char;
29} // namespace gc
30
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070031template<class T> class Handle;
Andreas Gampe8e0f0432018-10-24 13:38:03 -070032class Thread;
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070033
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034namespace mirror {
35
36class MANAGED Array : public Object {
37 public:
Andreas Gampea2fed082019-02-01 09:34:43 -080038 static constexpr size_t kFirstElementOffset = 12u;
39
Mingyao Yang98d1cc82014-05-15 17:02:16 -070040 // The size of a java.lang.Class representing an array.
Andreas Gampe542451c2016-07-26 09:02:02 -070041 static uint32_t ClassSize(PointerSize pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070042
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070043 // Allocates an array with the given properties, if kFillUsable is true the array will be of at
Ian Rogers6fac4472014-02-25 17:01:10 -080044 // least component_count size, however, if there's usable space at the end of the allocation the
45 // array will fill it.
Vladimir Marko9b81ac32019-05-16 16:47:08 +010046 template <bool kIsInstrumented = true, bool kFillUsable = false>
Vladimir Markobcf17522018-06-01 13:14:32 +010047 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)
Mathieu Chartier31e88222016-10-14 18:43:19 -070052 REQUIRES_SHARED(Locks::mutator_lock_)
53 REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054
Vladimir Markobcf17522018-06-01 13:14:32 +010055 static ObjPtr<Array> CreateMultiArray(Thread* self,
56 Handle<Class> element_class,
57 Handle<IntArray> dimensions)
Mathieu Chartier31e88222016-10-14 18:43:19 -070058 REQUIRES_SHARED(Locks::mutator_lock_)
59 REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060
Vladimir Markod355acf2019-03-21 17:09:40 +000061 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070062 size_t SizeOf() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4e305412014-02-19 10:54:44 -080063 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070064 ALWAYS_INLINE int32_t GetLength() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070065 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Array, length_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066 }
67
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070068 void SetLength(int32_t length) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -070069 DCHECK_GE(length, 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010070 // We use non transactional version since we can't undo this write. We also disable checking
71 // since it would fail during a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070072 SetField32<false, false, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Array, length_), length);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073 }
74
David Srbecky56de89a2018-10-01 15:32:20 +010075 static constexpr MemberOffset LengthOffset() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
77 }
78
David Srbecky56de89a2018-10-01 15:32:20 +010079 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 }
Andreas Gampea2fed082019-02-01 09:34:43 -080086 template <size_t kComponentSize>
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
94 static constexpr size_t FirstElementOffset() {
95 return OFFSETOF_MEMBER(Array, first_element_);
96 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097
Ian Rogersef7d42f2014-01-06 12:55:46 -080098 void* GetRawData(size_t component_size, int32_t index)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070099 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800100 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
101 + (index * component_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800102 return reinterpret_cast<void*>(data);
103 }
Andreas Gampea2fed082019-02-01 09:34:43 -0800104 template <size_t kComponentSize>
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 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800110
Ian Rogersef7d42f2014-01-06 12:55:46 -0800111 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);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115 }
Andreas Gampea2fed082019-02-01 09:34:43 -0800116 template <size_t kComponentSize>
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 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122
Sebastien Hertzabff6432014-01-27 18:01:39 +0100123 // Returns true if the index is valid. If not, throws an ArrayIndexOutOfBoundsException and
124 // returns false.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800125 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700126 ALWAYS_INLINE bool CheckIsValidIndex(int32_t index) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800127
Vladimir Marko3068d582019-05-28 16:39:29 +0100128 static ObjPtr<Array> CopyOf(Handle<Array> h_this, Thread* self, int32_t new_length)
129 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700130
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131 protected:
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700132 void ThrowArrayStoreException(ObjPtr<Object> object) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700133 REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800134
135 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800136 void ThrowArrayIndexOutOfBoundsException(int32_t index)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700137 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100138
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139 // The number of array elements.
David Srbecky56de89a2018-10-01 15:32:20 +0100140 // We only use the field indirectly using the LengthOffset() method.
141 int32_t length_ ATTRIBUTE_UNUSED;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800142 // Marker for the data (used by generated code)
David Srbecky56de89a2018-10-01 15:32:20 +0100143 // We only use the field indirectly using the DataOffset() method.
144 uint32_t first_element_[0] ATTRIBUTE_UNUSED;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800145
146 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
147};
148
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700149template<typename T>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800150class MANAGED PrimitiveArray : public Array {
151 public:
152 typedef T ElementType;
153
Vladimir Markobcf17522018-06-01 13:14:32 +0100154 static ObjPtr<PrimitiveArray<T>> Alloc(Thread* self, size_t length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700155 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800156
Vladimir Markobcf17522018-06-01 13:14:32 +0100157 static ObjPtr<PrimitiveArray<T>> AllocateAndFill(Thread* self, const T* data, size_t length)
Alex Light440b5d92017-01-24 15:32:25 -0800158 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
159
160
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700161 const T* GetData() const ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampea2fed082019-02-01 09:34:43 -0800162 return reinterpret_cast<const T*>(GetRawData<sizeof(T)>(0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800163 }
164
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700165 T* GetData() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampea2fed082019-02-01 09:34:43 -0800166 return reinterpret_cast<T*>(GetRawData<sizeof(T)>(0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800167 }
168
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700169 T Get(int32_t i) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100170
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700171 T GetWithoutChecks(int32_t i) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000172 DCHECK(CheckIsValidIndex(i)) << "i=" << i << " length=" << GetLength();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173 return GetData()[i];
174 }
175
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700176 void Set(int32_t i, T value) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100177
178 // TODO fix thread safety analysis broken by the use of template. This should be
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700179 // REQUIRES_SHARED(Locks::mutator_lock_).
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100180 template<bool kTransactionActive, bool kCheckTransaction = true>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700181 void Set(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800182
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100183 // TODO fix thread safety analysis broken by the use of template. This should be
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700184 // REQUIRES_SHARED(Locks::mutator_lock_).
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700185 template<bool kTransactionActive,
186 bool kCheckTransaction = true,
187 VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700188 void SetWithoutChecks(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Sebastien Hertzabff6432014-01-27 18:01:39 +0100189
Ian Rogersef7d42f2014-01-06 12:55:46 -0800190 /*
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 */
Mathieu Chartier31e88222016-10-14 18:43:19 -0700195 void Memmove(int32_t dst_pos, ObjPtr<PrimitiveArray<T>> src, int32_t src_pos, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700196 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800197
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 */
Mathieu Chartier31e88222016-10-14 18:43:19 -0700203 void Memcpy(int32_t dst_pos, ObjPtr<PrimitiveArray<T>> src, int32_t src_pos, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700204 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800205
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800206 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800207 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
208};
209
Andreas Gampe2722f382017-06-08 18:03:25 -0700210// Declare the different primitive arrays. Instantiations will be in array.cc.
211extern template class PrimitiveArray<uint8_t>; // BooleanArray
212extern template class PrimitiveArray<int8_t>; // ByteArray
213extern template class PrimitiveArray<uint16_t>; // CharArray
214extern template class PrimitiveArray<double>; // DoubleArray
215extern template class PrimitiveArray<float>; // FloatArray
216extern template class PrimitiveArray<int32_t>; // IntArray
217extern template class PrimitiveArray<int64_t>; // LongArray
218extern template class PrimitiveArray<int16_t>; // ShortArray
219
Mathieu Chartiere401d142015-04-22 13:56:20 -0700220// Either an IntArray or a LongArray.
221class PointerArray : public Array {
222 public:
Vladimir Marko104883b2018-11-09 17:12:23 +0000223 template<typename T, VerifyObjectFlags kVerifyFlags = kVerifyNone>
Andreas Gampe542451c2016-07-26 09:02:02 -0700224 T GetElementPtrSize(uint32_t idx, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700225 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampea2fed082019-02-01 09:34:43 -0800226 template<typename T, PointerSize kPtrSize, VerifyObjectFlags kVerifyFlags = kVerifyNone>
227 T GetElementPtrSize(uint32_t idx)
228 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe3aa868a2019-02-04 11:18:43 -0800229 // 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_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700235
Vladimir Marko924ad502018-09-19 09:48:04 +0100236 template<VerifyObjectFlags kVerifyFlags = kVerifyNone>
Mathieu Chartier8c19d242017-03-06 12:35:10 -0800237 void** ElementAddress(size_t index, PointerSize ptr_size) REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko924ad502018-09-19 09:48:04 +0100238 DCHECK_LT(index, static_cast<size_t>(GetLength<kVerifyFlags>()));
Mathieu Chartier8c19d242017-03-06 12:35:10 -0800239 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
Mathieu Chartierd329a3b2016-01-27 15:30:10 -0800244 template<bool kTransactionActive = false, bool kUnchecked = false>
Andreas Gampe542451c2016-07-26 09:02:02 -0700245 void SetElementPtrSize(uint32_t idx, uint64_t element, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700246 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700247 template<bool kTransactionActive = false, bool kUnchecked = false, typename T>
Andreas Gampe542451c2016-07-26 09:02:02 -0700248 void SetElementPtrSize(uint32_t idx, T* element, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700249 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800250
251 // Fixup the pointers in the dest arrays by passing our pointers through the visitor. Only copies
252 // to dest if visitor(source_ptr) != source_ptr.
Vladimir Marko104883b2018-11-09 17:12:23 +0000253 template <VerifyObjectFlags kVerifyFlags = kVerifyNone, typename Visitor>
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000254 void Fixup(ObjPtr<mirror::PointerArray> dest, PointerSize pointer_size, const Visitor& visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700255 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lighta01de592016-11-15 10:43:06 -0800256
257 // Works like memcpy(), except we guarantee not to allow tearing of array values (ie using smaller
258 // than element size copies). Arguments are assumed to be within the bounds of the array and the
259 // arrays non-null. Cannot be called in an active transaction.
260 template<bool kUnchecked = false>
261 void Memcpy(int32_t dst_pos,
262 ObjPtr<PointerArray> src,
263 int32_t src_pos,
264 int32_t count,
265 PointerSize pointer_size)
266 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700267};
268
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800269} // namespace mirror
270} // namespace art
271
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700272#endif // ART_RUNTIME_MIRROR_ARRAY_H_