blob: 8816c619df8d994c3b0d979299ffc701fd10ec43 [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"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070022#include "gc/allocator_type.h"
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070023#include "obj_ptr.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "object.h"
25
26namespace art {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070027
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070028template<class T> class Handle;
Andreas Gampe8e0f0432018-10-24 13:38:03 -070029class Thread;
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070030
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031namespace mirror {
32
33class MANAGED Array : public Object {
34 public:
Mingyao Yang98d1cc82014-05-15 17:02:16 -070035 // The size of a java.lang.Class representing an array.
Andreas Gampe542451c2016-07-26 09:02:02 -070036 static uint32_t ClassSize(PointerSize pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070037
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070038 // Allocates an array with the given properties, if kFillUsable is true the array will be of at
Ian Rogers6fac4472014-02-25 17:01:10 -080039 // least component_count size, however, if there's usable space at the end of the allocation the
40 // array will fill it.
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070041 template <bool kIsInstrumented, bool kFillUsable = false>
Vladimir Markobcf17522018-06-01 13:14:32 +010042 ALWAYS_INLINE static ObjPtr<Array> Alloc(Thread* self,
43 ObjPtr<Class> array_class,
44 int32_t component_count,
45 size_t component_size_shift,
46 gc::AllocatorType allocator_type)
Mathieu Chartier31e88222016-10-14 18:43:19 -070047 REQUIRES_SHARED(Locks::mutator_lock_)
48 REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049
Vladimir Markobcf17522018-06-01 13:14:32 +010050 static ObjPtr<Array> CreateMultiArray(Thread* self,
51 Handle<Class> element_class,
52 Handle<IntArray> dimensions)
Mathieu Chartier31e88222016-10-14 18:43:19 -070053 REQUIRES_SHARED(Locks::mutator_lock_)
54 REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055
Hiroshi Yamauchi6e83c172014-05-01 21:25:41 -070056 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
57 ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070058 size_t SizeOf() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4e305412014-02-19 10:54:44 -080059 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070060 ALWAYS_INLINE int32_t GetLength() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070061 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Array, length_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080062 }
63
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070064 void SetLength(int32_t length) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -070065 DCHECK_GE(length, 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010066 // We use non transactional version since we can't undo this write. We also disable checking
67 // since it would fail during a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070068 SetField32<false, false, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Array, length_), length);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069 }
70
David Srbecky56de89a2018-10-01 15:32:20 +010071 static constexpr MemberOffset LengthOffset() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
73 }
74
David Srbecky56de89a2018-10-01 15:32:20 +010075 static constexpr MemberOffset DataOffset(size_t component_size) {
76 DCHECK(IsPowerOfTwo(component_size)) << component_size;
77 size_t data_offset = RoundUp(OFFSETOF_MEMBER(Array, first_element_), component_size);
78 DCHECK_EQ(RoundUp(data_offset, component_size), data_offset)
79 << "Array data offset isn't aligned with component size";
80 return MemberOffset(data_offset);
81 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080082
Ian Rogersef7d42f2014-01-06 12:55:46 -080083 void* GetRawData(size_t component_size, int32_t index)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070084 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080085 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
86 + (index * component_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087 return reinterpret_cast<void*>(data);
88 }
89
Ian Rogersef7d42f2014-01-06 12:55:46 -080090 const void* GetRawData(size_t component_size, int32_t index) const {
91 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
92 + (index * component_size);
93 return reinterpret_cast<void*>(data);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094 }
95
Sebastien Hertzabff6432014-01-27 18:01:39 +010096 // Returns true if the index is valid. If not, throws an ArrayIndexOutOfBoundsException and
97 // returns false.
Mathieu Chartier4e305412014-02-19 10:54:44 -080098 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070099 ALWAYS_INLINE bool CheckIsValidIndex(int32_t index) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100
Vladimir Markobcf17522018-06-01 13:14:32 +0100101 ObjPtr<Array> CopyOf(Thread* self, int32_t new_length) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier4e2cb092015-07-22 16:17:51 -0700102 REQUIRES(!Roles::uninterruptible_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700103
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104 protected:
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700105 void ThrowArrayStoreException(ObjPtr<Object> object) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700106 REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107
108 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800109 void ThrowArrayIndexOutOfBoundsException(int32_t index)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700110 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100111
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800112 // The number of array elements.
David Srbecky56de89a2018-10-01 15:32:20 +0100113 // We only use the field indirectly using the LengthOffset() method.
114 int32_t length_ ATTRIBUTE_UNUSED;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115 // Marker for the data (used by generated code)
David Srbecky56de89a2018-10-01 15:32:20 +0100116 // We only use the field indirectly using the DataOffset() method.
117 uint32_t first_element_[0] ATTRIBUTE_UNUSED;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800118
119 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
120};
121
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700122template<typename T>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800123class MANAGED PrimitiveArray : public Array {
124 public:
125 typedef T ElementType;
126
Vladimir Markobcf17522018-06-01 13:14:32 +0100127 static ObjPtr<PrimitiveArray<T>> Alloc(Thread* self, size_t length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700128 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800129
Vladimir Markobcf17522018-06-01 13:14:32 +0100130 static ObjPtr<PrimitiveArray<T>> AllocateAndFill(Thread* self, const T* data, size_t length)
Alex Light440b5d92017-01-24 15:32:25 -0800131 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
132
133
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700134 const T* GetData() const ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800135 return reinterpret_cast<const T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800136 }
137
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700138 T* GetData() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800139 return reinterpret_cast<T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140 }
141
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700142 T Get(int32_t i) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100143
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700144 T GetWithoutChecks(int32_t i) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000145 DCHECK(CheckIsValidIndex(i)) << "i=" << i << " length=" << GetLength();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800146 return GetData()[i];
147 }
148
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700149 void Set(int32_t i, T value) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100150
151 // TODO fix thread safety analysis broken by the use of template. This should be
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700152 // REQUIRES_SHARED(Locks::mutator_lock_).
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100153 template<bool kTransactionActive, bool kCheckTransaction = true>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700154 void Set(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100156 // TODO fix thread safety analysis broken by the use of template. This should be
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700157 // REQUIRES_SHARED(Locks::mutator_lock_).
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700158 template<bool kTransactionActive,
159 bool kCheckTransaction = true,
160 VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700161 void SetWithoutChecks(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Sebastien Hertzabff6432014-01-27 18:01:39 +0100162
Ian Rogersef7d42f2014-01-06 12:55:46 -0800163 /*
164 * Works like memmove(), except we guarantee not to allow tearing of array values (ie using
165 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
166 * and the arrays non-null.
167 */
Mathieu Chartier31e88222016-10-14 18:43:19 -0700168 void Memmove(int32_t dst_pos, ObjPtr<PrimitiveArray<T>> src, int32_t src_pos, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700169 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800170
171 /*
172 * Works like memcpy(), except we guarantee not to allow tearing of array values (ie using
173 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
174 * and the arrays non-null.
175 */
Mathieu Chartier31e88222016-10-14 18:43:19 -0700176 void Memcpy(int32_t dst_pos, ObjPtr<PrimitiveArray<T>> src, int32_t src_pos, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700177 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800178
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800179 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800180 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
181};
182
Andreas Gampe2722f382017-06-08 18:03:25 -0700183// Declare the different primitive arrays. Instantiations will be in array.cc.
184extern template class PrimitiveArray<uint8_t>; // BooleanArray
185extern template class PrimitiveArray<int8_t>; // ByteArray
186extern template class PrimitiveArray<uint16_t>; // CharArray
187extern template class PrimitiveArray<double>; // DoubleArray
188extern template class PrimitiveArray<float>; // FloatArray
189extern template class PrimitiveArray<int32_t>; // IntArray
190extern template class PrimitiveArray<int64_t>; // LongArray
191extern template class PrimitiveArray<int16_t>; // ShortArray
192
Mathieu Chartiere401d142015-04-22 13:56:20 -0700193// Either an IntArray or a LongArray.
194class PointerArray : public Array {
195 public:
Vladimir Marko104883b2018-11-09 17:12:23 +0000196 template<typename T, VerifyObjectFlags kVerifyFlags = kVerifyNone>
Andreas Gampe542451c2016-07-26 09:02:02 -0700197 T GetElementPtrSize(uint32_t idx, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700198 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700199
Vladimir Marko924ad502018-09-19 09:48:04 +0100200 template<VerifyObjectFlags kVerifyFlags = kVerifyNone>
Mathieu Chartier8c19d242017-03-06 12:35:10 -0800201 void** ElementAddress(size_t index, PointerSize ptr_size) REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko924ad502018-09-19 09:48:04 +0100202 DCHECK_LT(index, static_cast<size_t>(GetLength<kVerifyFlags>()));
Mathieu Chartier8c19d242017-03-06 12:35:10 -0800203 return reinterpret_cast<void**>(reinterpret_cast<uint8_t*>(this) +
204 Array::DataOffset(static_cast<size_t>(ptr_size)).Uint32Value() +
205 static_cast<size_t>(ptr_size) * index);
206 }
207
Mathieu Chartierd329a3b2016-01-27 15:30:10 -0800208 template<bool kTransactionActive = false, bool kUnchecked = false>
Andreas Gampe542451c2016-07-26 09:02:02 -0700209 void SetElementPtrSize(uint32_t idx, uint64_t element, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700210 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700211 template<bool kTransactionActive = false, bool kUnchecked = false, typename T>
Andreas Gampe542451c2016-07-26 09:02:02 -0700212 void SetElementPtrSize(uint32_t idx, T* element, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700213 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800214
215 // Fixup the pointers in the dest arrays by passing our pointers through the visitor. Only copies
216 // to dest if visitor(source_ptr) != source_ptr.
Vladimir Marko104883b2018-11-09 17:12:23 +0000217 template <VerifyObjectFlags kVerifyFlags = kVerifyNone, typename Visitor>
Andreas Gampe542451c2016-07-26 09:02:02 -0700218 void Fixup(mirror::PointerArray* dest, PointerSize pointer_size, const Visitor& visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700219 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lighta01de592016-11-15 10:43:06 -0800220
221 // Works like memcpy(), except we guarantee not to allow tearing of array values (ie using smaller
222 // than element size copies). Arguments are assumed to be within the bounds of the array and the
223 // arrays non-null. Cannot be called in an active transaction.
224 template<bool kUnchecked = false>
225 void Memcpy(int32_t dst_pos,
226 ObjPtr<PointerArray> src,
227 int32_t src_pos,
228 int32_t count,
229 PointerSize pointer_size)
230 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700231};
232
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800233} // namespace mirror
234} // namespace art
235
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700236#endif // ART_RUNTIME_MIRROR_ARRAY_H_