blob: 8bdd561ec4938501b94aa1e705df00964acc815f [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"
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
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070027template<class T> class Handle;
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070028
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029namespace mirror {
30
31class MANAGED Array : public Object {
32 public:
Mingyao Yang98d1cc82014-05-15 17:02:16 -070033 // The size of a java.lang.Class representing an array.
Andreas Gampe542451c2016-07-26 09:02:02 -070034 static uint32_t ClassSize(PointerSize pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070035
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070036 // Allocates an array with the given properties, if kFillUsable is true the array will be of at
Ian Rogers6fac4472014-02-25 17:01:10 -080037 // least component_count size, however, if there's usable space at the end of the allocation the
38 // array will fill it.
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070039 template <bool kIsInstrumented, bool kFillUsable = false>
Vladimir Markobcf17522018-06-01 13:14:32 +010040 ALWAYS_INLINE static ObjPtr<Array> Alloc(Thread* self,
41 ObjPtr<Class> array_class,
42 int32_t component_count,
43 size_t component_size_shift,
44 gc::AllocatorType allocator_type)
Mathieu Chartier31e88222016-10-14 18:43:19 -070045 REQUIRES_SHARED(Locks::mutator_lock_)
46 REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047
Vladimir Markobcf17522018-06-01 13:14:32 +010048 static ObjPtr<Array> CreateMultiArray(Thread* self,
49 Handle<Class> element_class,
50 Handle<IntArray> dimensions)
Mathieu Chartier31e88222016-10-14 18:43:19 -070051 REQUIRES_SHARED(Locks::mutator_lock_)
52 REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053
Hiroshi Yamauchi6e83c172014-05-01 21:25:41 -070054 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
55 ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070056 size_t SizeOf() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4e305412014-02-19 10:54:44 -080057 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070058 ALWAYS_INLINE int32_t GetLength() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070059 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Array, length_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060 }
61
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070062 void SetLength(int32_t length) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -070063 DCHECK_GE(length, 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010064 // We use non transactional version since we can't undo this write. We also disable checking
65 // since it would fail during a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070066 SetField32<false, false, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Array, length_), length);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080067 }
68
69 static MemberOffset LengthOffset() {
70 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
71 }
72
Ian Rogers7e70b002014-10-08 11:47:24 -070073 static MemberOffset DataOffset(size_t component_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074
Ian Rogersef7d42f2014-01-06 12:55:46 -080075 void* GetRawData(size_t component_size, int32_t index)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070076 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080077 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
78 + (index * component_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079 return reinterpret_cast<void*>(data);
80 }
81
Ian Rogersef7d42f2014-01-06 12:55:46 -080082 const void* GetRawData(size_t component_size, int32_t index) const {
83 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
84 + (index * component_size);
85 return reinterpret_cast<void*>(data);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086 }
87
Sebastien Hertzabff6432014-01-27 18:01:39 +010088 // Returns true if the index is valid. If not, throws an ArrayIndexOutOfBoundsException and
89 // returns false.
Mathieu Chartier4e305412014-02-19 10:54:44 -080090 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070091 ALWAYS_INLINE bool CheckIsValidIndex(int32_t index) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092
Vladimir Markobcf17522018-06-01 13:14:32 +010093 ObjPtr<Array> CopyOf(Thread* self, int32_t new_length) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier4e2cb092015-07-22 16:17:51 -070094 REQUIRES(!Roles::uninterruptible_);
Mathieu Chartiere401d142015-04-22 13:56:20 -070095
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080096 protected:
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070097 void ThrowArrayStoreException(ObjPtr<Object> object) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartiered8990a2015-07-23 14:11:16 -070098 REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099
100 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800101 void ThrowArrayIndexOutOfBoundsException(int32_t index)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700102 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100103
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104 // The number of array elements.
105 int32_t length_;
106 // Marker for the data (used by generated code)
107 uint32_t first_element_[0];
108
109 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
110};
111
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700112template<typename T>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800113class MANAGED PrimitiveArray : public Array {
114 public:
115 typedef T ElementType;
116
Vladimir Markobcf17522018-06-01 13:14:32 +0100117 static ObjPtr<PrimitiveArray<T>> Alloc(Thread* self, size_t length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700118 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800119
Vladimir Markobcf17522018-06-01 13:14:32 +0100120 static ObjPtr<PrimitiveArray<T>> AllocateAndFill(Thread* self, const T* data, size_t length)
Alex Light440b5d92017-01-24 15:32:25 -0800121 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
122
123
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700124 const T* GetData() const ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800125 return reinterpret_cast<const T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126 }
127
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700128 T* GetData() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800129 return reinterpret_cast<T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800130 }
131
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700132 T Get(int32_t i) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100133
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700134 T GetWithoutChecks(int32_t i) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000135 DCHECK(CheckIsValidIndex(i)) << "i=" << i << " length=" << GetLength();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800136 return GetData()[i];
137 }
138
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700139 void Set(int32_t i, T value) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100140
141 // TODO fix thread safety analysis broken by the use of template. This should be
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700142 // REQUIRES_SHARED(Locks::mutator_lock_).
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100143 template<bool kTransactionActive, bool kCheckTransaction = true>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700144 void Set(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800145
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100146 // TODO fix thread safety analysis broken by the use of template. This should be
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700147 // REQUIRES_SHARED(Locks::mutator_lock_).
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700148 template<bool kTransactionActive,
149 bool kCheckTransaction = true,
150 VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700151 void SetWithoutChecks(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Sebastien Hertzabff6432014-01-27 18:01:39 +0100152
Ian Rogersef7d42f2014-01-06 12:55:46 -0800153 /*
154 * Works like memmove(), except we guarantee not to allow tearing of array values (ie using
155 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
156 * and the arrays non-null.
157 */
Mathieu Chartier31e88222016-10-14 18:43:19 -0700158 void Memmove(int32_t dst_pos, ObjPtr<PrimitiveArray<T>> src, int32_t src_pos, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700159 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800160
161 /*
162 * Works like memcpy(), except we guarantee not to allow tearing of array values (ie using
163 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
164 * and the arrays non-null.
165 */
Mathieu Chartier31e88222016-10-14 18:43:19 -0700166 void Memcpy(int32_t dst_pos, ObjPtr<PrimitiveArray<T>> src, int32_t src_pos, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700167 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800168
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800170 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
171};
172
Andreas Gampe2722f382017-06-08 18:03:25 -0700173// Declare the different primitive arrays. Instantiations will be in array.cc.
174extern template class PrimitiveArray<uint8_t>; // BooleanArray
175extern template class PrimitiveArray<int8_t>; // ByteArray
176extern template class PrimitiveArray<uint16_t>; // CharArray
177extern template class PrimitiveArray<double>; // DoubleArray
178extern template class PrimitiveArray<float>; // FloatArray
179extern template class PrimitiveArray<int32_t>; // IntArray
180extern template class PrimitiveArray<int64_t>; // LongArray
181extern template class PrimitiveArray<int16_t>; // ShortArray
182
Mathieu Chartiere401d142015-04-22 13:56:20 -0700183// Either an IntArray or a LongArray.
184class PointerArray : public Array {
185 public:
Mathieu Chartierdfe02f62016-02-01 20:15:11 -0800186 template<typename T,
187 VerifyObjectFlags kVerifyFlags = kVerifyNone,
188 ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Andreas Gampe542451c2016-07-26 09:02:02 -0700189 T GetElementPtrSize(uint32_t idx, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700190 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700191
Mathieu Chartier8c19d242017-03-06 12:35:10 -0800192 void** ElementAddress(size_t index, PointerSize ptr_size) REQUIRES_SHARED(Locks::mutator_lock_) {
193 DCHECK_LT(index, static_cast<size_t>(GetLength()));
194 return reinterpret_cast<void**>(reinterpret_cast<uint8_t*>(this) +
195 Array::DataOffset(static_cast<size_t>(ptr_size)).Uint32Value() +
196 static_cast<size_t>(ptr_size) * index);
197 }
198
Mathieu Chartierd329a3b2016-01-27 15:30:10 -0800199 template<bool kTransactionActive = false, bool kUnchecked = false>
Andreas Gampe542451c2016-07-26 09:02:02 -0700200 void SetElementPtrSize(uint32_t idx, uint64_t element, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700201 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700202 template<bool kTransactionActive = false, bool kUnchecked = false, typename T>
Andreas Gampe542451c2016-07-26 09:02:02 -0700203 void SetElementPtrSize(uint32_t idx, T* element, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700204 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800205
206 // Fixup the pointers in the dest arrays by passing our pointers through the visitor. Only copies
207 // to dest if visitor(source_ptr) != source_ptr.
Mathieu Chartierdfe02f62016-02-01 20:15:11 -0800208 template <VerifyObjectFlags kVerifyFlags = kVerifyNone,
209 ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
210 typename Visitor>
Andreas Gampe542451c2016-07-26 09:02:02 -0700211 void Fixup(mirror::PointerArray* dest, PointerSize pointer_size, const Visitor& visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700212 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lighta01de592016-11-15 10:43:06 -0800213
214 // Works like memcpy(), except we guarantee not to allow tearing of array values (ie using smaller
215 // than element size copies). Arguments are assumed to be within the bounds of the array and the
216 // arrays non-null. Cannot be called in an active transaction.
217 template<bool kUnchecked = false>
218 void Memcpy(int32_t dst_pos,
219 ObjPtr<PointerArray> src,
220 int32_t src_pos,
221 int32_t count,
222 PointerSize pointer_size)
223 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700224};
225
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800226} // namespace mirror
227} // namespace art
228
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700229#endif // ART_RUNTIME_MIRROR_ARRAY_H_