blob: c9c99e8621692122129f0a46a031e2c411aa8962 [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
Andreas Gampe542451c2016-07-26 09:02:02 -070020#include "base/enums.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070021#include "gc/allocator_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070022#include "gc_root.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>
Mathieu Chartier31e88222016-10-14 18:43:19 -070041 ALWAYS_INLINE static 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)
46 REQUIRES_SHARED(Locks::mutator_lock_)
47 REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048
Mathieu Chartier31e88222016-10-14 18:43:19 -070049 static Array* CreateMultiArray(Thread* self,
50 Handle<Class> element_class,
Mathieu Chartier0cd81352014-05-22 16:48:55 -070051 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
70 static MemberOffset LengthOffset() {
71 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
72 }
73
Ian Rogers7e70b002014-10-08 11:47:24 -070074 static MemberOffset DataOffset(size_t component_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080075
Ian Rogersef7d42f2014-01-06 12:55:46 -080076 void* GetRawData(size_t component_size, int32_t index)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070077 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080078 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
79 + (index * component_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080 return reinterpret_cast<void*>(data);
81 }
82
Ian Rogersef7d42f2014-01-06 12:55:46 -080083 const void* GetRawData(size_t component_size, int32_t index) const {
84 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
85 + (index * component_size);
86 return reinterpret_cast<void*>(data);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087 }
88
Sebastien Hertzabff6432014-01-27 18:01:39 +010089 // Returns true if the index is valid. If not, throws an ArrayIndexOutOfBoundsException and
90 // returns false.
Mathieu Chartier4e305412014-02-19 10:54:44 -080091 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070092 ALWAYS_INLINE bool CheckIsValidIndex(int32_t index) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080093
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070094 Array* CopyOf(Thread* self, int32_t new_length) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier4e2cb092015-07-22 16:17:51 -070095 REQUIRES(!Roles::uninterruptible_);
Mathieu Chartiere401d142015-04-22 13:56:20 -070096
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097 protected:
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070098 void ThrowArrayStoreException(ObjPtr<Object> object) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartiered8990a2015-07-23 14:11:16 -070099 REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100
101 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800102 void ThrowArrayIndexOutOfBoundsException(int32_t index)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700103 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100104
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800105 // The number of array elements.
106 int32_t length_;
107 // Marker for the data (used by generated code)
108 uint32_t first_element_[0];
109
110 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
111};
112
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700113template<typename T>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800114class MANAGED PrimitiveArray : public Array {
115 public:
116 typedef T ElementType;
117
118 static PrimitiveArray<T>* Alloc(Thread* self, size_t length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700119 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800120
Alex Light440b5d92017-01-24 15:32:25 -0800121 static PrimitiveArray<T>* AllocateAndFill(Thread* self, const T* data, size_t length)
122 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
123
124
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700125 const T* GetData() const ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800126 return reinterpret_cast<const T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800127 }
128
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700129 T* GetData() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800130 return reinterpret_cast<T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131 }
132
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700133 T Get(int32_t i) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100134
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700135 T GetWithoutChecks(int32_t i) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000136 DCHECK(CheckIsValidIndex(i)) << "i=" << i << " length=" << GetLength();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800137 return GetData()[i];
138 }
139
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700140 void Set(int32_t i, T value) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100141
142 // TODO fix thread safety analysis broken by the use of template. This should be
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700143 // REQUIRES_SHARED(Locks::mutator_lock_).
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100144 template<bool kTransactionActive, bool kCheckTransaction = true>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700145 void Set(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800146
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100147 // TODO fix thread safety analysis broken by the use of template. This should be
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700148 // REQUIRES_SHARED(Locks::mutator_lock_).
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700149 template<bool kTransactionActive,
150 bool kCheckTransaction = true,
151 VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700152 void SetWithoutChecks(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Sebastien Hertzabff6432014-01-27 18:01:39 +0100153
Ian Rogersef7d42f2014-01-06 12:55:46 -0800154 /*
155 * Works like memmove(), except we guarantee not to allow tearing of array values (ie using
156 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
157 * and the arrays non-null.
158 */
Mathieu Chartier31e88222016-10-14 18:43:19 -0700159 void Memmove(int32_t dst_pos, ObjPtr<PrimitiveArray<T>> src, int32_t src_pos, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700160 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800161
162 /*
163 * Works like memcpy(), 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 Memcpy(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
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800170 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
172};
173
Andreas Gampe2722f382017-06-08 18:03:25 -0700174// Declare the different primitive arrays. Instantiations will be in array.cc.
175extern template class PrimitiveArray<uint8_t>; // BooleanArray
176extern template class PrimitiveArray<int8_t>; // ByteArray
177extern template class PrimitiveArray<uint16_t>; // CharArray
178extern template class PrimitiveArray<double>; // DoubleArray
179extern template class PrimitiveArray<float>; // FloatArray
180extern template class PrimitiveArray<int32_t>; // IntArray
181extern template class PrimitiveArray<int64_t>; // LongArray
182extern template class PrimitiveArray<int16_t>; // ShortArray
183
Mathieu Chartiere401d142015-04-22 13:56:20 -0700184// Either an IntArray or a LongArray.
185class PointerArray : public Array {
186 public:
Mathieu Chartierdfe02f62016-02-01 20:15:11 -0800187 template<typename T,
188 VerifyObjectFlags kVerifyFlags = kVerifyNone,
189 ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Andreas Gampe542451c2016-07-26 09:02:02 -0700190 T GetElementPtrSize(uint32_t idx, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700191 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700192
Mathieu Chartier8c19d242017-03-06 12:35:10 -0800193 void** ElementAddress(size_t index, PointerSize ptr_size) REQUIRES_SHARED(Locks::mutator_lock_) {
194 DCHECK_LT(index, static_cast<size_t>(GetLength()));
195 return reinterpret_cast<void**>(reinterpret_cast<uint8_t*>(this) +
196 Array::DataOffset(static_cast<size_t>(ptr_size)).Uint32Value() +
197 static_cast<size_t>(ptr_size) * index);
198 }
199
Mathieu Chartierd329a3b2016-01-27 15:30:10 -0800200 template<bool kTransactionActive = false, bool kUnchecked = false>
Andreas Gampe542451c2016-07-26 09:02:02 -0700201 void SetElementPtrSize(uint32_t idx, uint64_t element, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700202 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700203 template<bool kTransactionActive = false, bool kUnchecked = false, typename T>
Andreas Gampe542451c2016-07-26 09:02:02 -0700204 void SetElementPtrSize(uint32_t idx, T* element, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700205 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800206
207 // Fixup the pointers in the dest arrays by passing our pointers through the visitor. Only copies
208 // to dest if visitor(source_ptr) != source_ptr.
Mathieu Chartierdfe02f62016-02-01 20:15:11 -0800209 template <VerifyObjectFlags kVerifyFlags = kVerifyNone,
210 ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
211 typename Visitor>
Andreas Gampe542451c2016-07-26 09:02:02 -0700212 void Fixup(mirror::PointerArray* dest, PointerSize pointer_size, const Visitor& visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700213 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lighta01de592016-11-15 10:43:06 -0800214
215 // Works like memcpy(), except we guarantee not to allow tearing of array values (ie using smaller
216 // than element size copies). Arguments are assumed to be within the bounds of the array and the
217 // arrays non-null. Cannot be called in an active transaction.
218 template<bool kUnchecked = false>
219 void Memcpy(int32_t dst_pos,
220 ObjPtr<PointerArray> src,
221 int32_t src_pos,
222 int32_t count,
223 PointerSize pointer_size)
224 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700225};
226
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800227} // namespace mirror
228} // namespace art
229
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700230#endif // ART_RUNTIME_MIRROR_ARRAY_H_