blob: c3c523187ddc51b007ebbc20ce7f9a687d2d75f9 [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_OBJECT_ARRAY_INL_H_
18#define ART_RUNTIME_MIRROR_OBJECT_ARRAY_INL_H_
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019
Vladimir Marko80afd022015-05-19 18:08:00 +010020#include <string>
21
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "object_array.h"
23
Ian Rogers7e70b002014-10-08 11:47:24 -070024#include "array-inl.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070025#include "base/stringprintf.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070026#include "gc/heap.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/class.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "runtime.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070029#include "handle_scope-inl.h"
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020030#include "thread.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010031#include "utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032
33namespace art {
34namespace mirror {
35
36template<class T>
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080037inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self, Class* object_array_class,
38 int32_t length, gc::AllocatorType allocator_type) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080039 Array* array = Array::Alloc<true>(self, object_array_class, length,
Mathieu Chartierc7853442015-03-27 14:35:38 -070040 ComponentSizeShiftWidth(sizeof(HeapReference<Object>)),
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070041 allocator_type);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080042 if (UNLIKELY(array == nullptr)) {
43 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044 } else {
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070045 DCHECK_EQ(array->GetClass()->GetComponentSizeShift(),
Mathieu Chartierc7853442015-03-27 14:35:38 -070046 ComponentSizeShiftWidth(sizeof(HeapReference<Object>)));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047 return array->AsObjectArray<T>();
48 }
49}
50
51template<class T>
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080052inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self, Class* object_array_class,
53 int32_t length) {
54 return Alloc(self, object_array_class, length,
55 Runtime::Current()->GetHeap()->GetCurrentAllocator());
56}
57
Mathieu Chartierfbc31082016-01-24 11:59:56 -080058template<class T> template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
Ian Rogersef7d42f2014-01-06 12:55:46 -080059inline T* ObjectArray<T>::Get(int32_t i) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070060 if (!CheckIsValidIndex(i)) {
Sebastien Hertzabff6432014-01-27 18:01:39 +010061 DCHECK(Thread::Current()->IsExceptionPending());
Mathieu Chartier2cebb242015-04-21 16:50:40 -070062 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -080064 return GetFieldObject<T, kVerifyFlags, kReadBarrierOption>(OffsetOfElement(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080065}
66
Mathieu Chartier4e305412014-02-19 10:54:44 -080067template<class T> template<VerifyObjectFlags kVerifyFlags>
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020068inline bool ObjectArray<T>::CheckAssignable(T* object) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070069 if (object != nullptr) {
Mathieu Chartier4e305412014-02-19 10:54:44 -080070 Class* element_class = GetClass<kVerifyFlags>()->GetComponentType();
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020071 if (UNLIKELY(!object->InstanceOf(element_class))) {
72 ThrowArrayStoreException(object);
73 return false;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074 }
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020075 }
76 return true;
77}
78
79template<class T>
80inline void ObjectArray<T>::Set(int32_t i, T* object) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010081 if (Runtime::Current()->IsActiveTransaction()) {
82 Set<true>(i, object);
83 } else {
84 Set<false>(i, object);
85 }
86}
87
88template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -080089template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010090inline void ObjectArray<T>::Set(int32_t i, T* object) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070091 if (CheckIsValidIndex(i) && CheckAssignable<kVerifyFlags>(object)) {
92 SetFieldObject<kTransactionActive, kCheckTransaction, kVerifyFlags>(OffsetOfElement(i), object);
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020093 } else {
94 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080095 }
96}
97
98template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -080099template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100inline void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800101 DCHECK(CheckIsValidIndex<kVerifyFlags>(i));
102 DCHECK(CheckAssignable<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>(object));
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700103 SetFieldObject<kTransactionActive, kCheckTransaction, kVerifyFlags>(OffsetOfElement(i), object);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104}
105
106template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -0800107template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800108inline void ObjectArray<T>::SetWithoutChecksAndWriteBarrier(int32_t i, T* object) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800109 DCHECK(CheckIsValidIndex<kVerifyFlags>(i));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800110 // TODO: enable this check. It fails when writing the image in ImageWriter::FixupObjectArray.
Sebastien Hertzabff6432014-01-27 18:01:39 +0100111 // DCHECK(CheckAssignable(object));
Mathieu Chartier4e305412014-02-19 10:54:44 -0800112 SetFieldObjectWithoutWriteBarrier<kTransactionActive, kCheckTransaction, kVerifyFlags>(
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700113 OffsetOfElement(i), object);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800114}
115
116template<class T>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800117inline T* ObjectArray<T>::GetWithoutChecks(int32_t i) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100118 DCHECK(CheckIsValidIndex(i));
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700119 return GetFieldObject<T>(OffsetOfElement(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800120}
121
122template<class T>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800123inline void ObjectArray<T>::AssignableMemmove(int32_t dst_pos, ObjectArray<T>* src,
124 int32_t src_pos, int32_t count) {
125 if (kIsDebugBuild) {
126 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700127 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800128 src->GetWithoutChecks(src_pos + i);
129 }
130 }
131 // Perform the memmove using int memmove then perform the write barrier.
Roland Levillain33d69032015-06-18 18:20:59 +0100132 static_assert(sizeof(HeapReference<T>) == sizeof(uint32_t),
133 "art::mirror::HeapReference<T> and uint32_t have different sizes.");
Ian Rogersef7d42f2014-01-06 12:55:46 -0800134 IntArray* dstAsIntArray = reinterpret_cast<IntArray*>(this);
135 IntArray* srcAsIntArray = reinterpret_cast<IntArray*>(src);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800136 if (kUseReadBarrier) {
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700137 // TODO: Optimize this later?
138 const bool copy_forward = (src != this) || (dst_pos < src_pos) || (dst_pos - src_pos >= count);
139 if (copy_forward) {
140 // Forward copy.
141 for (int i = 0; i < count; ++i) {
142 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
143 Object* obj = src->GetWithoutChecks(src_pos + i);
144 SetWithoutChecks<false>(dst_pos + i, obj);
145 }
146 } else {
147 // Backward copy.
148 for (int i = count - 1; i >= 0; --i) {
149 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
150 Object* obj = src->GetWithoutChecks(src_pos + i);
151 SetWithoutChecks<false>(dst_pos + i, obj);
152 }
153 }
154 } else {
155 dstAsIntArray->Memmove(dst_pos, srcAsIntArray, src_pos, count);
156 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800157 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
158 if (kIsDebugBuild) {
159 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700160 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800161 GetWithoutChecks(dst_pos + i);
162 }
163 }
164}
165
166template<class T>
167inline void ObjectArray<T>::AssignableMemcpy(int32_t dst_pos, ObjectArray<T>* src,
168 int32_t src_pos, int32_t count) {
169 if (kIsDebugBuild) {
170 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700171 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800172 src->GetWithoutChecks(src_pos + i);
173 }
174 }
175 // Perform the memmove using int memcpy then perform the write barrier.
Roland Levillain33d69032015-06-18 18:20:59 +0100176 static_assert(sizeof(HeapReference<T>) == sizeof(uint32_t),
177 "art::mirror::HeapReference<T> and uint32_t have different sizes.");
Ian Rogersef7d42f2014-01-06 12:55:46 -0800178 IntArray* dstAsIntArray = reinterpret_cast<IntArray*>(this);
179 IntArray* srcAsIntArray = reinterpret_cast<IntArray*>(src);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800180 if (kUseReadBarrier) {
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700181 // TODO: Optimize this later?
182 for (int i = 0; i < count; ++i) {
183 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
184 T* obj = src->GetWithoutChecks(src_pos + i);
185 SetWithoutChecks<false>(dst_pos + i, obj);
186 }
187 } else {
188 dstAsIntArray->Memcpy(dst_pos, srcAsIntArray, src_pos, count);
189 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800190 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
191 if (kIsDebugBuild) {
192 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700193 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800194 GetWithoutChecks(dst_pos + i);
195 }
196 }
197}
198
199template<class T>
Andreas Gampe85a098a2016-03-31 13:30:53 -0700200template<bool kTransactionActive>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800201inline void ObjectArray<T>::AssignableCheckingMemcpy(int32_t dst_pos, ObjectArray<T>* src,
202 int32_t src_pos, int32_t count,
203 bool throw_exception) {
204 DCHECK_NE(this, src)
205 << "This case should be handled with memmove that handles overlaps correctly";
206 // We want to avoid redundant IsAssignableFrom checks where possible, so we cache a class that
207 // we know is assignable to the destination array's component type.
208 Class* dst_class = GetClass()->GetComponentType();
209 Class* lastAssignableElementClass = dst_class;
210
211 Object* o = nullptr;
212 int i = 0;
213 for (; i < count; ++i) {
214 // The follow get operations force the objects to be verified.
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700215 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800216 o = src->GetWithoutChecks(src_pos + i);
217 if (o == nullptr) {
218 // Null is always assignable.
Andreas Gampe85a098a2016-03-31 13:30:53 -0700219 SetWithoutChecks<kTransactionActive>(dst_pos + i, nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800221 // TODO: use the underlying class reference to avoid uncompression when not necessary.
222 Class* o_class = o->GetClass();
223 if (LIKELY(lastAssignableElementClass == o_class)) {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700224 SetWithoutChecks<kTransactionActive>(dst_pos + i, o);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800225 } else if (LIKELY(dst_class->IsAssignableFrom(o_class))) {
226 lastAssignableElementClass = o_class;
Andreas Gampe85a098a2016-03-31 13:30:53 -0700227 SetWithoutChecks<kTransactionActive>(dst_pos + i, o);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800228 } else {
229 // Can't put this element into the array, break to perform write-barrier and throw
230 // exception.
231 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800232 }
233 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800234 }
235 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
236 if (UNLIKELY(i != count)) {
237 std::string actualSrcType(PrettyTypeOf(o));
238 std::string dstType(PrettyTypeOf(this));
239 Thread* self = Thread::Current();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800240 if (throw_exception) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000241 self->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Ian Rogersef7d42f2014-01-06 12:55:46 -0800242 "source[%d] of type %s cannot be stored in destination array of type %s",
243 src_pos + i, actualSrcType.c_str(), dstType.c_str());
244 } else {
245 LOG(FATAL) << StringPrintf("source[%d] of type %s cannot be stored in destination array of type %s",
246 src_pos + i, actualSrcType.c_str(), dstType.c_str());
247 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800248 }
249}
250
251template<class T>
252inline ObjectArray<T>* ObjectArray<T>::CopyOf(Thread* self, int32_t new_length) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800253 DCHECK_GE(new_length, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700254 // We may get copied by a compacting GC.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700255 StackHandleScope<1> hs(self);
Ian Rogers700a4022014-05-19 16:49:03 -0700256 Handle<ObjectArray<T>> h_this(hs.NewHandle(this));
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800257 gc::Heap* heap = Runtime::Current()->GetHeap();
258 gc::AllocatorType allocator_type = heap->IsMovableObject(this) ? heap->GetCurrentAllocator() :
259 heap->GetCurrentNonMovingAllocator();
260 ObjectArray<T>* new_array = Alloc(self, GetClass(), new_length, allocator_type);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700261 if (LIKELY(new_array != nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700262 new_array->AssignableMemcpy(0, h_this.Get(), 0, std::min(h_this->GetLength(), new_length));
Ian Rogersa436fde2013-08-27 23:34:06 -0700263 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800264 return new_array;
265}
266
Ian Rogersef7d42f2014-01-06 12:55:46 -0800267template<class T>
268inline MemberOffset ObjectArray<T>::OffsetOfElement(int32_t i) {
269 return MemberOffset(DataOffset(sizeof(HeapReference<Object>)).Int32Value() +
270 (i * sizeof(HeapReference<Object>)));
271}
272
Mathieu Chartier059ef3d2015-08-18 13:54:21 -0700273template<class T> template<typename Visitor>
Hiroshi Yamauchi723e6ce2015-10-28 20:59:47 -0700274inline void ObjectArray<T>::VisitReferences(const Visitor& visitor) {
Mathieu Chartier407f7022014-02-18 14:37:05 -0800275 const size_t length = static_cast<size_t>(GetLength());
276 for (size_t i = 0; i < length; ++i) {
277 visitor(this, OffsetOfElement(i), false);
278 }
279}
280
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800281} // namespace mirror
282} // namespace art
283
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700284#endif // ART_RUNTIME_MIRROR_OBJECT_ARRAY_INL_H_