blob: 7edc851d4f47b6ea6049fabf521d0f05f396877d [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;
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070029
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030namespace mirror {
31
32class MANAGED Array : public Object {
33 public:
Mingyao Yang98d1cc82014-05-15 17:02:16 -070034 // The size of a java.lang.Class representing an array.
Andreas Gampe542451c2016-07-26 09:02:02 -070035 static uint32_t ClassSize(PointerSize pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070036
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070037 // Allocates an array with the given properties, if kFillUsable is true the array will be of at
Ian Rogers6fac4472014-02-25 17:01:10 -080038 // least component_count size, however, if there's usable space at the end of the allocation the
39 // array will fill it.
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070040 template <bool kIsInstrumented, bool kFillUsable = false>
Vladimir Markobcf17522018-06-01 13:14:32 +010041 ALWAYS_INLINE static ObjPtr<Array> Alloc(Thread* self,
42 ObjPtr<Class> array_class,
43 int32_t component_count,
44 size_t component_size_shift,
45 gc::AllocatorType allocator_type)
Mathieu Chartier31e88222016-10-14 18:43:19 -070046 REQUIRES_SHARED(Locks::mutator_lock_)
47 REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048
Vladimir Markobcf17522018-06-01 13:14:32 +010049 static ObjPtr<Array> CreateMultiArray(Thread* self,
50 Handle<Class> element_class,
51 Handle<IntArray> dimensions)
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
Hiroshi Yamauchi6e83c172014-05-01 21:25:41 -070055 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
56 ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070057 size_t SizeOf() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4e305412014-02-19 10:54:44 -080058 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070059 ALWAYS_INLINE int32_t GetLength() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070060 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Array, length_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061 }
62
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070063 void SetLength(int32_t length) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -070064 DCHECK_GE(length, 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010065 // We use non transactional version since we can't undo this write. We also disable checking
66 // since it would fail during a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070067 SetField32<false, false, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Array, length_), length);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080068 }
69
David Srbecky56de89a2018-10-01 15:32:20 +010070 static constexpr MemberOffset LengthOffset() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
72 }
73
David Srbecky56de89a2018-10-01 15:32:20 +010074 static constexpr MemberOffset DataOffset(size_t component_size) {
75 DCHECK(IsPowerOfTwo(component_size)) << component_size;
76 size_t data_offset = RoundUp(OFFSETOF_MEMBER(Array, first_element_), component_size);
77 DCHECK_EQ(RoundUp(data_offset, component_size), data_offset)
78 << "Array data offset isn't aligned with component size";
79 return MemberOffset(data_offset);
80 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081
Ian Rogersef7d42f2014-01-06 12:55:46 -080082 void* GetRawData(size_t component_size, int32_t index)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070083 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080084 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
85 + (index * component_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086 return reinterpret_cast<void*>(data);
87 }
88
Ian Rogersef7d42f2014-01-06 12:55:46 -080089 const void* GetRawData(size_t component_size, int32_t index) const {
90 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
91 + (index * component_size);
92 return reinterpret_cast<void*>(data);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080093 }
94
Sebastien Hertzabff6432014-01-27 18:01:39 +010095 // Returns true if the index is valid. If not, throws an ArrayIndexOutOfBoundsException and
96 // returns false.
Mathieu Chartier4e305412014-02-19 10:54:44 -080097 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070098 ALWAYS_INLINE bool CheckIsValidIndex(int32_t index) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099
Vladimir Markobcf17522018-06-01 13:14:32 +0100100 ObjPtr<Array> CopyOf(Thread* self, int32_t new_length) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier4e2cb092015-07-22 16:17:51 -0700101 REQUIRES(!Roles::uninterruptible_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700102
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103 protected:
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700104 void ThrowArrayStoreException(ObjPtr<Object> object) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700105 REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106
107 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800108 void ThrowArrayIndexOutOfBoundsException(int32_t index)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700109 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100110
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111 // The number of array elements.
David Srbecky56de89a2018-10-01 15:32:20 +0100112 // We only use the field indirectly using the LengthOffset() method.
113 int32_t length_ ATTRIBUTE_UNUSED;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800114 // Marker for the data (used by generated code)
David Srbecky56de89a2018-10-01 15:32:20 +0100115 // We only use the field indirectly using the DataOffset() method.
116 uint32_t first_element_[0] ATTRIBUTE_UNUSED;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800117
118 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
119};
120
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700121template<typename T>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122class MANAGED PrimitiveArray : public Array {
123 public:
124 typedef T ElementType;
125
Vladimir Markobcf17522018-06-01 13:14:32 +0100126 static ObjPtr<PrimitiveArray<T>> Alloc(Thread* self, size_t length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700127 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800128
Vladimir Markobcf17522018-06-01 13:14:32 +0100129 static ObjPtr<PrimitiveArray<T>> AllocateAndFill(Thread* self, const T* data, size_t length)
Alex Light440b5d92017-01-24 15:32:25 -0800130 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
131
132
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700133 const T* GetData() const ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800134 return reinterpret_cast<const T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800135 }
136
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700137 T* GetData() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800138 return reinterpret_cast<T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139 }
140
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700141 T Get(int32_t i) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100142
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700143 T GetWithoutChecks(int32_t i) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000144 DCHECK(CheckIsValidIndex(i)) << "i=" << i << " length=" << GetLength();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800145 return GetData()[i];
146 }
147
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700148 void Set(int32_t i, T value) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100149
150 // TODO fix thread safety analysis broken by the use of template. This should be
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700151 // REQUIRES_SHARED(Locks::mutator_lock_).
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100152 template<bool kTransactionActive, bool kCheckTransaction = true>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700153 void Set(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800154
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100155 // TODO fix thread safety analysis broken by the use of template. This should be
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700156 // REQUIRES_SHARED(Locks::mutator_lock_).
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700157 template<bool kTransactionActive,
158 bool kCheckTransaction = true,
159 VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700160 void SetWithoutChecks(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Sebastien Hertzabff6432014-01-27 18:01:39 +0100161
Ian Rogersef7d42f2014-01-06 12:55:46 -0800162 /*
163 * Works like memmove(), except we guarantee not to allow tearing of array values (ie using
164 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
165 * and the arrays non-null.
166 */
Mathieu Chartier31e88222016-10-14 18:43:19 -0700167 void Memmove(int32_t dst_pos, ObjPtr<PrimitiveArray<T>> src, int32_t src_pos, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700168 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800169
170 /*
171 * Works like memcpy(), except we guarantee not to allow tearing of array values (ie using
172 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
173 * and the arrays non-null.
174 */
Mathieu Chartier31e88222016-10-14 18:43:19 -0700175 void Memcpy(int32_t dst_pos, ObjPtr<PrimitiveArray<T>> src, int32_t src_pos, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700176 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800177
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800178 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800179 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
180};
181
Andreas Gampe2722f382017-06-08 18:03:25 -0700182// Declare the different primitive arrays. Instantiations will be in array.cc.
183extern template class PrimitiveArray<uint8_t>; // BooleanArray
184extern template class PrimitiveArray<int8_t>; // ByteArray
185extern template class PrimitiveArray<uint16_t>; // CharArray
186extern template class PrimitiveArray<double>; // DoubleArray
187extern template class PrimitiveArray<float>; // FloatArray
188extern template class PrimitiveArray<int32_t>; // IntArray
189extern template class PrimitiveArray<int64_t>; // LongArray
190extern template class PrimitiveArray<int16_t>; // ShortArray
191
Mathieu Chartiere401d142015-04-22 13:56:20 -0700192// Either an IntArray or a LongArray.
193class PointerArray : public Array {
194 public:
Mathieu Chartierdfe02f62016-02-01 20:15:11 -0800195 template<typename T,
196 VerifyObjectFlags kVerifyFlags = kVerifyNone,
197 ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Andreas Gampe542451c2016-07-26 09:02:02 -0700198 T GetElementPtrSize(uint32_t idx, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700199 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700200
Vladimir Marko924ad502018-09-19 09:48:04 +0100201 template<VerifyObjectFlags kVerifyFlags = kVerifyNone>
Mathieu Chartier8c19d242017-03-06 12:35:10 -0800202 void** ElementAddress(size_t index, PointerSize ptr_size) REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko924ad502018-09-19 09:48:04 +0100203 DCHECK_LT(index, static_cast<size_t>(GetLength<kVerifyFlags>()));
Mathieu Chartier8c19d242017-03-06 12:35:10 -0800204 return reinterpret_cast<void**>(reinterpret_cast<uint8_t*>(this) +
205 Array::DataOffset(static_cast<size_t>(ptr_size)).Uint32Value() +
206 static_cast<size_t>(ptr_size) * index);
207 }
208
Mathieu Chartierd329a3b2016-01-27 15:30:10 -0800209 template<bool kTransactionActive = false, bool kUnchecked = false>
Andreas Gampe542451c2016-07-26 09:02:02 -0700210 void SetElementPtrSize(uint32_t idx, uint64_t element, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700211 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700212 template<bool kTransactionActive = false, bool kUnchecked = false, typename T>
Andreas Gampe542451c2016-07-26 09:02:02 -0700213 void SetElementPtrSize(uint32_t idx, T* element, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700214 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800215
216 // Fixup the pointers in the dest arrays by passing our pointers through the visitor. Only copies
217 // to dest if visitor(source_ptr) != source_ptr.
Mathieu Chartierdfe02f62016-02-01 20:15:11 -0800218 template <VerifyObjectFlags kVerifyFlags = kVerifyNone,
219 ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
220 typename Visitor>
Andreas Gampe542451c2016-07-26 09:02:02 -0700221 void Fixup(mirror::PointerArray* dest, PointerSize pointer_size, const Visitor& visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700222 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lighta01de592016-11-15 10:43:06 -0800223
224 // Works like memcpy(), except we guarantee not to allow tearing of array values (ie using smaller
225 // than element size copies). Arguments are assumed to be within the bounds of the array and the
226 // arrays non-null. Cannot be called in an active transaction.
227 template<bool kUnchecked = false>
228 void Memcpy(int32_t dst_pos,
229 ObjPtr<PointerArray> src,
230 int32_t src_pos,
231 int32_t count,
232 PointerSize pointer_size)
233 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700234};
235
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800236} // namespace mirror
237} // namespace art
238
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700239#endif // ART_RUNTIME_MIRROR_ARRAY_H_