blob: 16cf30f1e2f833dc52187e39ce4adc0015af7381 [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"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070021#include "gc_root.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"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080025#include "object_callbacks.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026
27namespace art {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070028
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070029template<class T> class Handle;
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>
Mathieu Chartier31e88222016-10-14 18:43:19 -070042 ALWAYS_INLINE static 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)
47 REQUIRES_SHARED(Locks::mutator_lock_)
48 REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049
Mathieu Chartier31e88222016-10-14 18:43:19 -070050 static Array* CreateMultiArray(Thread* self,
51 Handle<Class> element_class,
Mathieu Chartier0cd81352014-05-22 16:48:55 -070052 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
71 static MemberOffset LengthOffset() {
72 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
73 }
74
Ian Rogers7e70b002014-10-08 11:47:24 -070075 static MemberOffset DataOffset(size_t component_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076
Ian Rogersef7d42f2014-01-06 12:55:46 -080077 void* GetRawData(size_t component_size, int32_t index)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070078 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080079 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
80 + (index * component_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081 return reinterpret_cast<void*>(data);
82 }
83
Ian Rogersef7d42f2014-01-06 12:55:46 -080084 const void* GetRawData(size_t component_size, int32_t index) const {
85 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
86 + (index * component_size);
87 return reinterpret_cast<void*>(data);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080088 }
89
Sebastien Hertzabff6432014-01-27 18:01:39 +010090 // Returns true if the index is valid. If not, throws an ArrayIndexOutOfBoundsException and
91 // returns false.
Mathieu Chartier4e305412014-02-19 10:54:44 -080092 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070093 ALWAYS_INLINE bool CheckIsValidIndex(int32_t index) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070095 Array* CopyOf(Thread* self, int32_t new_length) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier4e2cb092015-07-22 16:17:51 -070096 REQUIRES(!Roles::uninterruptible_);
Mathieu Chartiere401d142015-04-22 13:56:20 -070097
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080098 protected:
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070099 void ThrowArrayStoreException(ObjPtr<Object> object) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700100 REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800101
102 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800103 void ThrowArrayIndexOutOfBoundsException(int32_t index)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700104 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100105
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106 // The number of array elements.
107 int32_t length_;
108 // Marker for the data (used by generated code)
109 uint32_t first_element_[0];
110
111 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
112};
113
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700114template<typename T>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115class MANAGED PrimitiveArray : public Array {
116 public:
117 typedef T ElementType;
118
119 static PrimitiveArray<T>* Alloc(Thread* self, size_t length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700120 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800121
Alex Light440b5d92017-01-24 15:32:25 -0800122 static PrimitiveArray<T>* AllocateAndFill(Thread* self, const T* data, size_t length)
123 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
124
125
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700126 const T* GetData() const ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800127 return reinterpret_cast<const T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800128 }
129
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700130 T* GetData() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800131 return reinterpret_cast<T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800132 }
133
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700134 T Get(int32_t i) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100135
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700136 T GetWithoutChecks(int32_t i) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000137 DCHECK(CheckIsValidIndex(i)) << "i=" << i << " length=" << GetLength();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138 return GetData()[i];
139 }
140
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700141 void Set(int32_t i, T value) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100142
143 // TODO fix thread safety analysis broken by the use of template. This should be
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700144 // REQUIRES_SHARED(Locks::mutator_lock_).
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100145 template<bool kTransactionActive, bool kCheckTransaction = true>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700146 void Set(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800147
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100148 // TODO fix thread safety analysis broken by the use of template. This should be
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700149 // REQUIRES_SHARED(Locks::mutator_lock_).
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700150 template<bool kTransactionActive,
151 bool kCheckTransaction = true,
152 VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700153 void SetWithoutChecks(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Sebastien Hertzabff6432014-01-27 18:01:39 +0100154
Ian Rogersef7d42f2014-01-06 12:55:46 -0800155 /*
156 * Works like memmove(), except we guarantee not to allow tearing of array values (ie using
157 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
158 * and the arrays non-null.
159 */
Mathieu Chartier31e88222016-10-14 18:43:19 -0700160 void Memmove(int32_t dst_pos, ObjPtr<PrimitiveArray<T>> src, int32_t src_pos, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700161 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800162
163 /*
164 * Works like memcpy(), 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 Memcpy(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
Mathieu Chartier31e88222016-10-14 18:43:19 -0700171 static void SetArrayClass(ObjPtr<Class> array_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800172
Mathieu Chartierd6636d32016-07-28 11:02:38 -0700173 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700174 static Class* GetArrayClass() REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700175 DCHECK(!array_class_.IsNull());
Mathieu Chartierd6636d32016-07-28 11:02:38 -0700176 return array_class_.Read<kReadBarrierOption>();
Ian Rogers2d10b202014-05-12 19:15:18 -0700177 }
178
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800179 static void ResetArrayClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700180 CHECK(!array_class_.IsNull());
181 array_class_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800182 }
183
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700184 static void VisitRoots(RootVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800185
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800186 private:
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700187 static GcRoot<Class> array_class_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800188
189 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
190};
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
Mathieu Chartierd329a3b2016-01-27 15:30:10 -0800201 template<bool kTransactionActive = false, bool kUnchecked = false>
Andreas Gampe542451c2016-07-26 09:02:02 -0700202 void SetElementPtrSize(uint32_t idx, uint64_t element, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700203 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700204 template<bool kTransactionActive = false, bool kUnchecked = false, typename T>
Andreas Gampe542451c2016-07-26 09:02:02 -0700205 void SetElementPtrSize(uint32_t idx, T* element, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700206 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800207
208 // Fixup the pointers in the dest arrays by passing our pointers through the visitor. Only copies
209 // to dest if visitor(source_ptr) != source_ptr.
Mathieu Chartierdfe02f62016-02-01 20:15:11 -0800210 template <VerifyObjectFlags kVerifyFlags = kVerifyNone,
211 ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
212 typename Visitor>
Andreas Gampe542451c2016-07-26 09:02:02 -0700213 void Fixup(mirror::PointerArray* dest, PointerSize pointer_size, const Visitor& visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700214 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lighta01de592016-11-15 10:43:06 -0800215
216 // Works like memcpy(), except we guarantee not to allow tearing of array values (ie using smaller
217 // than element size copies). Arguments are assumed to be within the bounds of the array and the
218 // arrays non-null. Cannot be called in an active transaction.
219 template<bool kUnchecked = false>
220 void Memcpy(int32_t dst_pos,
221 ObjPtr<PointerArray> src,
222 int32_t src_pos,
223 int32_t count,
224 PointerSize pointer_size)
225 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700226};
227
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800228} // namespace mirror
229} // namespace art
230
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700231#endif // ART_RUNTIME_MIRROR_ARRAY_H_