blob: 3e04bf6474d134c09ad38fa7a2732d6f67854f23 [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
20#include "object_array.h"
21
Andreas Gampe46ee31b2016-12-14 10:11:49 -080022#include <string>
23
24#include "android-base/stringprintf.h"
25
Ian Rogers7e70b002014-10-08 11:47:24 -070026#include "array-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070027#include "gc/heap.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/class.h"
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070029#include "obj_ptr-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "runtime.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070031#include "handle_scope-inl.h"
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020032#include "thread.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010033#include "utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034
35namespace art {
36namespace mirror {
37
38template<class T>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070039inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self,
40 ObjPtr<Class> object_array_class,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080041 int32_t length, gc::AllocatorType allocator_type) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070042 Array* array = Array::Alloc<true>(self,
43 object_array_class.Ptr(),
44 length,
45 ComponentSizeShiftWidth(kHeapReferenceSize),
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070046 allocator_type);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080047 if (UNLIKELY(array == nullptr)) {
48 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049 }
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070050 DCHECK_EQ(array->GetClass()->GetComponentSizeShift(),
51 ComponentSizeShiftWidth(kHeapReferenceSize));
52 return array->AsObjectArray<T>();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053}
54
55template<class T>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070056inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self,
57 ObjPtr<Class> object_array_class,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080058 int32_t length) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070059 return Alloc(self,
60 object_array_class,
61 length,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080062 Runtime::Current()->GetHeap()->GetCurrentAllocator());
63}
64
Mathieu Chartierfbc31082016-01-24 11:59:56 -080065template<class T> template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
Ian Rogersef7d42f2014-01-06 12:55:46 -080066inline T* ObjectArray<T>::Get(int32_t i) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070067 if (!CheckIsValidIndex(i)) {
Sebastien Hertzabff6432014-01-27 18:01:39 +010068 DCHECK(Thread::Current()->IsExceptionPending());
Mathieu Chartier2cebb242015-04-21 16:50:40 -070069 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080070 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -080071 return GetFieldObject<T, kVerifyFlags, kReadBarrierOption>(OffsetOfElement(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072}
73
Mathieu Chartier4e305412014-02-19 10:54:44 -080074template<class T> template<VerifyObjectFlags kVerifyFlags>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070075inline bool ObjectArray<T>::CheckAssignable(ObjPtr<T> object) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070076 if (object != nullptr) {
Mathieu Chartier4e305412014-02-19 10:54:44 -080077 Class* element_class = GetClass<kVerifyFlags>()->GetComponentType();
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020078 if (UNLIKELY(!object->InstanceOf(element_class))) {
79 ThrowArrayStoreException(object);
80 return false;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081 }
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020082 }
83 return true;
84}
85
86template<class T>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070087inline void ObjectArray<T>::Set(int32_t i, ObjPtr<T> object) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010088 if (Runtime::Current()->IsActiveTransaction()) {
89 Set<true>(i, object);
90 } else {
91 Set<false>(i, object);
92 }
93}
94
95template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -080096template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070097inline void ObjectArray<T>::Set(int32_t i, ObjPtr<T> object) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070098 if (CheckIsValidIndex(i) && CheckAssignable<kVerifyFlags>(object)) {
99 SetFieldObject<kTransactionActive, kCheckTransaction, kVerifyFlags>(OffsetOfElement(i), object);
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +0200100 } else {
101 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800102 }
103}
104
105template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -0800106template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700107inline void ObjectArray<T>::SetWithoutChecks(int32_t i, ObjPtr<T> object) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800108 DCHECK(CheckIsValidIndex<kVerifyFlags>(i));
109 DCHECK(CheckAssignable<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>(object));
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700110 SetFieldObject<kTransactionActive, kCheckTransaction, kVerifyFlags>(OffsetOfElement(i), object);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111}
112
113template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -0800114template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700115inline void ObjectArray<T>::SetWithoutChecksAndWriteBarrier(int32_t i, ObjPtr<T> object) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800116 DCHECK(CheckIsValidIndex<kVerifyFlags>(i));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800117 // TODO: enable this check. It fails when writing the image in ImageWriter::FixupObjectArray.
Sebastien Hertzabff6432014-01-27 18:01:39 +0100118 // DCHECK(CheckAssignable(object));
Mathieu Chartier4e305412014-02-19 10:54:44 -0800119 SetFieldObjectWithoutWriteBarrier<kTransactionActive, kCheckTransaction, kVerifyFlags>(
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700120 OffsetOfElement(i), object);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800121}
122
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800123template<class T> template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800124inline T* ObjectArray<T>::GetWithoutChecks(int32_t i) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100125 DCHECK(CheckIsValidIndex(i));
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800126 return GetFieldObject<T, kVerifyFlags, kReadBarrierOption>(OffsetOfElement(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800127}
128
129template<class T>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700130inline void ObjectArray<T>::AssignableMemmove(int32_t dst_pos,
131 ObjPtr<ObjectArray<T>> src,
132 int32_t src_pos,
133 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800134 if (kIsDebugBuild) {
135 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700136 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800137 src->GetWithoutChecks(src_pos + i);
138 }
139 }
140 // Perform the memmove using int memmove then perform the write barrier.
Roland Levillain33d69032015-06-18 18:20:59 +0100141 static_assert(sizeof(HeapReference<T>) == sizeof(uint32_t),
142 "art::mirror::HeapReference<T> and uint32_t have different sizes.");
Mathieu Chartierfec13d42016-10-07 12:59:33 -0700143 // TODO: Optimize this later?
144 // We can't use memmove since it does not handle read barriers and may do by per byte copying.
145 // See b/32012820.
146 const bool copy_forward = (src != this) || (dst_pos < src_pos) || (dst_pos - src_pos >= count);
147 if (copy_forward) {
148 // Forward copy.
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800149 bool baker_non_gray_case = false;
150 if (kUseReadBarrier && kUseBakerReadBarrier) {
151 uintptr_t fake_address_dependency;
152 if (!ReadBarrier::IsGray(src.Ptr(), &fake_address_dependency)) {
153 baker_non_gray_case = true;
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800154 DCHECK_EQ(fake_address_dependency, 0U);
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800155 src.Assign(reinterpret_cast<ObjectArray<T>*>(
156 reinterpret_cast<uintptr_t>(src.Ptr()) | fake_address_dependency));
157 for (int i = 0; i < count; ++i) {
158 // We can skip the RB here because 'src' isn't gray.
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800159 T* obj = src->template GetWithoutChecks<kDefaultVerifyFlags, kWithoutReadBarrier>(
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800160 src_pos + i);
161 SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj);
162 }
163 }
164 }
165 if (!baker_non_gray_case) {
166 for (int i = 0; i < count; ++i) {
167 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800168 T* obj = src->GetWithoutChecks(src_pos + i);
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800169 SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj);
170 }
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700171 }
172 } else {
Mathieu Chartierfec13d42016-10-07 12:59:33 -0700173 // Backward copy.
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800174 bool baker_non_gray_case = false;
175 if (kUseReadBarrier && kUseBakerReadBarrier) {
176 uintptr_t fake_address_dependency;
177 if (!ReadBarrier::IsGray(src.Ptr(), &fake_address_dependency)) {
178 baker_non_gray_case = true;
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800179 DCHECK_EQ(fake_address_dependency, 0U);
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800180 src.Assign(reinterpret_cast<ObjectArray<T>*>(
181 reinterpret_cast<uintptr_t>(src.Ptr()) | fake_address_dependency));
182 for (int i = count - 1; i >= 0; --i) {
183 // We can skip the RB here because 'src' isn't gray.
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800184 T* obj = src->template GetWithoutChecks<kDefaultVerifyFlags, kWithoutReadBarrier>(
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800185 src_pos + i);
186 SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj);
187 }
188 }
189 }
190 if (!baker_non_gray_case) {
191 for (int i = count - 1; i >= 0; --i) {
192 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800193 T* obj = src->GetWithoutChecks(src_pos + i);
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800194 SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj);
195 }
Mathieu Chartierfec13d42016-10-07 12:59:33 -0700196 }
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700197 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800198 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
199 if (kIsDebugBuild) {
200 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700201 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800202 GetWithoutChecks(dst_pos + i);
203 }
204 }
205}
206
207template<class T>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700208inline void ObjectArray<T>::AssignableMemcpy(int32_t dst_pos,
209 ObjPtr<ObjectArray<T>> src,
210 int32_t src_pos,
211 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800212 if (kIsDebugBuild) {
213 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700214 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800215 src->GetWithoutChecks(src_pos + i);
216 }
217 }
218 // Perform the memmove using int memcpy then perform the write barrier.
Roland Levillain33d69032015-06-18 18:20:59 +0100219 static_assert(sizeof(HeapReference<T>) == sizeof(uint32_t),
220 "art::mirror::HeapReference<T> and uint32_t have different sizes.");
Mathieu Chartierfec13d42016-10-07 12:59:33 -0700221 // TODO: Optimize this later?
222 // We can't use memmove since it does not handle read barriers and may do by per byte copying.
223 // See b/32012820.
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800224 bool baker_non_gray_case = false;
225 if (kUseReadBarrier && kUseBakerReadBarrier) {
226 uintptr_t fake_address_dependency;
227 if (!ReadBarrier::IsGray(src.Ptr(), &fake_address_dependency)) {
228 baker_non_gray_case = true;
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800229 DCHECK_EQ(fake_address_dependency, 0U);
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800230 src.Assign(reinterpret_cast<ObjectArray<T>*>(
231 reinterpret_cast<uintptr_t>(src.Ptr()) | fake_address_dependency));
232 for (int i = 0; i < count; ++i) {
233 // We can skip the RB here because 'src' isn't gray.
234 Object* obj = src->template GetWithoutChecks<kDefaultVerifyFlags, kWithoutReadBarrier>(
235 src_pos + i);
236 SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj);
237 }
238 }
239 }
240 if (!baker_non_gray_case) {
241 for (int i = 0; i < count; ++i) {
242 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
243 T* obj = src->GetWithoutChecks(src_pos + i);
244 SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj);
245 }
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700246 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800247 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
248 if (kIsDebugBuild) {
249 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700250 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800251 GetWithoutChecks(dst_pos + i);
252 }
253 }
254}
255
256template<class T>
Andreas Gampe85a098a2016-03-31 13:30:53 -0700257template<bool kTransactionActive>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700258inline void ObjectArray<T>::AssignableCheckingMemcpy(int32_t dst_pos,
259 ObjPtr<ObjectArray<T>> src,
260 int32_t src_pos,
261 int32_t count,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800262 bool throw_exception) {
263 DCHECK_NE(this, src)
264 << "This case should be handled with memmove that handles overlaps correctly";
265 // We want to avoid redundant IsAssignableFrom checks where possible, so we cache a class that
266 // we know is assignable to the destination array's component type.
267 Class* dst_class = GetClass()->GetComponentType();
268 Class* lastAssignableElementClass = dst_class;
269
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800270 T* o = nullptr;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800271 int i = 0;
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800272 bool baker_non_gray_case = false;
273 if (kUseReadBarrier && kUseBakerReadBarrier) {
274 uintptr_t fake_address_dependency;
275 if (!ReadBarrier::IsGray(src.Ptr(), &fake_address_dependency)) {
276 baker_non_gray_case = true;
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800277 DCHECK_EQ(fake_address_dependency, 0U);
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800278 src.Assign(reinterpret_cast<ObjectArray<T>*>(
279 reinterpret_cast<uintptr_t>(src.Ptr()) | fake_address_dependency));
280 for (; i < count; ++i) {
281 // The follow get operations force the objects to be verified.
282 // We can skip the RB here because 'src' isn't gray.
283 o = src->template GetWithoutChecks<kDefaultVerifyFlags, kWithoutReadBarrier>(
284 src_pos + i);
285 if (o == nullptr) {
286 // Null is always assignable.
287 SetWithoutChecks<kTransactionActive>(dst_pos + i, nullptr);
288 } else {
289 // TODO: use the underlying class reference to avoid uncompression when not necessary.
290 Class* o_class = o->GetClass();
291 if (LIKELY(lastAssignableElementClass == o_class)) {
292 SetWithoutChecks<kTransactionActive>(dst_pos + i, o);
293 } else if (LIKELY(dst_class->IsAssignableFrom(o_class))) {
294 lastAssignableElementClass = o_class;
295 SetWithoutChecks<kTransactionActive>(dst_pos + i, o);
296 } else {
297 // Can't put this element into the array, break to perform write-barrier and throw
298 // exception.
299 break;
300 }
301 }
302 }
303 }
304 }
305 if (!baker_non_gray_case) {
306 for (; i < count; ++i) {
307 // The follow get operations force the objects to be verified.
308 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
309 o = src->GetWithoutChecks(src_pos + i);
310 if (o == nullptr) {
311 // Null is always assignable.
312 SetWithoutChecks<kTransactionActive>(dst_pos + i, nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800313 } else {
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800314 // TODO: use the underlying class reference to avoid uncompression when not necessary.
315 Class* o_class = o->GetClass();
316 if (LIKELY(lastAssignableElementClass == o_class)) {
317 SetWithoutChecks<kTransactionActive>(dst_pos + i, o);
318 } else if (LIKELY(dst_class->IsAssignableFrom(o_class))) {
319 lastAssignableElementClass = o_class;
320 SetWithoutChecks<kTransactionActive>(dst_pos + i, o);
321 } else {
322 // Can't put this element into the array, break to perform write-barrier and throw
323 // exception.
324 break;
325 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800326 }
327 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800328 }
329 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
330 if (UNLIKELY(i != count)) {
David Sehr709b0702016-10-13 09:12:37 -0700331 std::string actualSrcType(mirror::Object::PrettyTypeOf(o));
332 std::string dstType(PrettyTypeOf());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800333 Thread* self = Thread::Current();
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800334 std::string msg = android::base::StringPrintf(
335 "source[%d] of type %s cannot be stored in destination array of type %s",
336 src_pos + i,
337 actualSrcType.c_str(),
338 dstType.c_str());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800339 if (throw_exception) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800340 self->ThrowNewException("Ljava/lang/ArrayStoreException;", msg.c_str());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800341 } else {
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800342 LOG(FATAL) << msg;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800343 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800344 }
345}
346
347template<class T>
348inline ObjectArray<T>* ObjectArray<T>::CopyOf(Thread* self, int32_t new_length) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800349 DCHECK_GE(new_length, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700350 // We may get copied by a compacting GC.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700351 StackHandleScope<1> hs(self);
Ian Rogers700a4022014-05-19 16:49:03 -0700352 Handle<ObjectArray<T>> h_this(hs.NewHandle(this));
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800353 gc::Heap* heap = Runtime::Current()->GetHeap();
354 gc::AllocatorType allocator_type = heap->IsMovableObject(this) ? heap->GetCurrentAllocator() :
355 heap->GetCurrentNonMovingAllocator();
356 ObjectArray<T>* new_array = Alloc(self, GetClass(), new_length, allocator_type);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700357 if (LIKELY(new_array != nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700358 new_array->AssignableMemcpy(0, h_this.Get(), 0, std::min(h_this->GetLength(), new_length));
Ian Rogersa436fde2013-08-27 23:34:06 -0700359 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800360 return new_array;
361}
362
Ian Rogersef7d42f2014-01-06 12:55:46 -0800363template<class T>
364inline MemberOffset ObjectArray<T>::OffsetOfElement(int32_t i) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700365 return MemberOffset(DataOffset(kHeapReferenceSize).Int32Value() + (i * kHeapReferenceSize));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800366}
367
Mathieu Chartier059ef3d2015-08-18 13:54:21 -0700368template<class T> template<typename Visitor>
Hiroshi Yamauchi723e6ce2015-10-28 20:59:47 -0700369inline void ObjectArray<T>::VisitReferences(const Visitor& visitor) {
Mathieu Chartier407f7022014-02-18 14:37:05 -0800370 const size_t length = static_cast<size_t>(GetLength());
371 for (size_t i = 0; i < length; ++i) {
372 visitor(this, OffsetOfElement(i), false);
373 }
374}
375
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800376} // namespace mirror
377} // namespace art
378
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700379#endif // ART_RUNTIME_MIRROR_OBJECT_ARRAY_INL_H_