blob: 5cfc987e44e70ef60fdf057fa4cb26f331dc3131 [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"
Andreas Gampec15a2f42017-04-21 12:09:39 -070027#include "class.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "gc/heap.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070029#include "handle_scope-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070030#include "obj_ptr-inl.h"
31#include "object-inl.h"
32#include "runtime.h"
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020033#include "thread.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010034#include "utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035
36namespace art {
37namespace mirror {
38
39template<class T>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070040inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self,
41 ObjPtr<Class> object_array_class,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080042 int32_t length, gc::AllocatorType allocator_type) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070043 Array* array = Array::Alloc<true>(self,
44 object_array_class.Ptr(),
45 length,
46 ComponentSizeShiftWidth(kHeapReferenceSize),
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070047 allocator_type);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080048 if (UNLIKELY(array == nullptr)) {
49 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050 }
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070051 DCHECK_EQ(array->GetClass()->GetComponentSizeShift(),
52 ComponentSizeShiftWidth(kHeapReferenceSize));
53 return array->AsObjectArray<T>();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054}
55
56template<class T>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070057inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self,
58 ObjPtr<Class> object_array_class,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080059 int32_t length) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070060 return Alloc(self,
61 object_array_class,
62 length,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080063 Runtime::Current()->GetHeap()->GetCurrentAllocator());
64}
65
Mathieu Chartierfbc31082016-01-24 11:59:56 -080066template<class T> template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
Ian Rogersef7d42f2014-01-06 12:55:46 -080067inline T* ObjectArray<T>::Get(int32_t i) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070068 if (!CheckIsValidIndex(i)) {
Sebastien Hertzabff6432014-01-27 18:01:39 +010069 DCHECK(Thread::Current()->IsExceptionPending());
Mathieu Chartier2cebb242015-04-21 16:50:40 -070070 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -080072 return GetFieldObject<T, kVerifyFlags, kReadBarrierOption>(OffsetOfElement(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073}
74
Mathieu Chartier4e305412014-02-19 10:54:44 -080075template<class T> template<VerifyObjectFlags kVerifyFlags>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070076inline bool ObjectArray<T>::CheckAssignable(ObjPtr<T> object) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070077 if (object != nullptr) {
Mathieu Chartier4e305412014-02-19 10:54:44 -080078 Class* element_class = GetClass<kVerifyFlags>()->GetComponentType();
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020079 if (UNLIKELY(!object->InstanceOf(element_class))) {
80 ThrowArrayStoreException(object);
81 return false;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080082 }
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020083 }
84 return true;
85}
86
87template<class T>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070088inline void ObjectArray<T>::Set(int32_t i, ObjPtr<T> object) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010089 if (Runtime::Current()->IsActiveTransaction()) {
90 Set<true>(i, object);
91 } else {
92 Set<false>(i, object);
93 }
94}
95
96template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -080097template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070098inline void ObjectArray<T>::Set(int32_t i, ObjPtr<T> object) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070099 if (CheckIsValidIndex(i) && CheckAssignable<kVerifyFlags>(object)) {
100 SetFieldObject<kTransactionActive, kCheckTransaction, kVerifyFlags>(OffsetOfElement(i), object);
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +0200101 } else {
102 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103 }
104}
105
106template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -0800107template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700108inline void ObjectArray<T>::SetWithoutChecks(int32_t i, ObjPtr<T> object) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800109 DCHECK(CheckIsValidIndex<kVerifyFlags>(i));
110 DCHECK(CheckAssignable<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>(object));
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700111 SetFieldObject<kTransactionActive, kCheckTransaction, kVerifyFlags>(OffsetOfElement(i), object);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800112}
113
114template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -0800115template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700116inline void ObjectArray<T>::SetWithoutChecksAndWriteBarrier(int32_t i, ObjPtr<T> object) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800117 DCHECK(CheckIsValidIndex<kVerifyFlags>(i));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800118 // TODO: enable this check. It fails when writing the image in ImageWriter::FixupObjectArray.
Sebastien Hertzabff6432014-01-27 18:01:39 +0100119 // DCHECK(CheckAssignable(object));
Mathieu Chartier4e305412014-02-19 10:54:44 -0800120 SetFieldObjectWithoutWriteBarrier<kTransactionActive, kCheckTransaction, kVerifyFlags>(
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700121 OffsetOfElement(i), object);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122}
123
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800124template<class T> template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800125inline T* ObjectArray<T>::GetWithoutChecks(int32_t i) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100126 DCHECK(CheckIsValidIndex(i));
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800127 return GetFieldObject<T, kVerifyFlags, kReadBarrierOption>(OffsetOfElement(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800128}
129
130template<class T>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700131inline void ObjectArray<T>::AssignableMemmove(int32_t dst_pos,
132 ObjPtr<ObjectArray<T>> src,
133 int32_t src_pos,
134 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800135 if (kIsDebugBuild) {
136 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700137 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800138 src->GetWithoutChecks(src_pos + i);
139 }
140 }
141 // Perform the memmove using int memmove then perform the write barrier.
Roland Levillain33d69032015-06-18 18:20:59 +0100142 static_assert(sizeof(HeapReference<T>) == sizeof(uint32_t),
143 "art::mirror::HeapReference<T> and uint32_t have different sizes.");
Mathieu Chartierfec13d42016-10-07 12:59:33 -0700144 // TODO: Optimize this later?
145 // We can't use memmove since it does not handle read barriers and may do by per byte copying.
146 // See b/32012820.
147 const bool copy_forward = (src != this) || (dst_pos < src_pos) || (dst_pos - src_pos >= count);
148 if (copy_forward) {
149 // Forward copy.
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800150 bool baker_non_gray_case = false;
151 if (kUseReadBarrier && kUseBakerReadBarrier) {
152 uintptr_t fake_address_dependency;
153 if (!ReadBarrier::IsGray(src.Ptr(), &fake_address_dependency)) {
154 baker_non_gray_case = true;
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800155 DCHECK_EQ(fake_address_dependency, 0U);
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800156 src.Assign(reinterpret_cast<ObjectArray<T>*>(
157 reinterpret_cast<uintptr_t>(src.Ptr()) | fake_address_dependency));
158 for (int i = 0; i < count; ++i) {
159 // We can skip the RB here because 'src' isn't gray.
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800160 T* obj = src->template GetWithoutChecks<kDefaultVerifyFlags, kWithoutReadBarrier>(
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800161 src_pos + i);
162 SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj);
163 }
164 }
165 }
166 if (!baker_non_gray_case) {
167 for (int i = 0; i < count; ++i) {
168 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800169 T* obj = src->GetWithoutChecks(src_pos + i);
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800170 SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj);
171 }
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700172 }
173 } else {
Mathieu Chartierfec13d42016-10-07 12:59:33 -0700174 // Backward copy.
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800175 bool baker_non_gray_case = false;
176 if (kUseReadBarrier && kUseBakerReadBarrier) {
177 uintptr_t fake_address_dependency;
178 if (!ReadBarrier::IsGray(src.Ptr(), &fake_address_dependency)) {
179 baker_non_gray_case = true;
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800180 DCHECK_EQ(fake_address_dependency, 0U);
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800181 src.Assign(reinterpret_cast<ObjectArray<T>*>(
182 reinterpret_cast<uintptr_t>(src.Ptr()) | fake_address_dependency));
183 for (int i = count - 1; i >= 0; --i) {
184 // We can skip the RB here because 'src' isn't gray.
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800185 T* obj = src->template GetWithoutChecks<kDefaultVerifyFlags, kWithoutReadBarrier>(
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800186 src_pos + i);
187 SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj);
188 }
189 }
190 }
191 if (!baker_non_gray_case) {
192 for (int i = count - 1; i >= 0; --i) {
193 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800194 T* obj = src->GetWithoutChecks(src_pos + i);
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800195 SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj);
196 }
Mathieu Chartierfec13d42016-10-07 12:59:33 -0700197 }
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700198 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800199 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
200 if (kIsDebugBuild) {
201 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700202 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800203 GetWithoutChecks(dst_pos + i);
204 }
205 }
206}
207
208template<class T>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700209inline void ObjectArray<T>::AssignableMemcpy(int32_t dst_pos,
210 ObjPtr<ObjectArray<T>> src,
211 int32_t src_pos,
212 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800213 if (kIsDebugBuild) {
214 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700215 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800216 src->GetWithoutChecks(src_pos + i);
217 }
218 }
219 // Perform the memmove using int memcpy then perform the write barrier.
Roland Levillain33d69032015-06-18 18:20:59 +0100220 static_assert(sizeof(HeapReference<T>) == sizeof(uint32_t),
221 "art::mirror::HeapReference<T> and uint32_t have different sizes.");
Mathieu Chartierfec13d42016-10-07 12:59:33 -0700222 // TODO: Optimize this later?
223 // We can't use memmove since it does not handle read barriers and may do by per byte copying.
224 // See b/32012820.
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800225 bool baker_non_gray_case = false;
226 if (kUseReadBarrier && kUseBakerReadBarrier) {
227 uintptr_t fake_address_dependency;
228 if (!ReadBarrier::IsGray(src.Ptr(), &fake_address_dependency)) {
229 baker_non_gray_case = true;
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800230 DCHECK_EQ(fake_address_dependency, 0U);
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800231 src.Assign(reinterpret_cast<ObjectArray<T>*>(
232 reinterpret_cast<uintptr_t>(src.Ptr()) | fake_address_dependency));
233 for (int i = 0; i < count; ++i) {
234 // We can skip the RB here because 'src' isn't gray.
235 Object* obj = src->template GetWithoutChecks<kDefaultVerifyFlags, kWithoutReadBarrier>(
236 src_pos + i);
237 SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj);
238 }
239 }
240 }
241 if (!baker_non_gray_case) {
242 for (int i = 0; i < count; ++i) {
243 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
244 T* obj = src->GetWithoutChecks(src_pos + i);
245 SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj);
246 }
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700247 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800248 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
249 if (kIsDebugBuild) {
250 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700251 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800252 GetWithoutChecks(dst_pos + i);
253 }
254 }
255}
256
257template<class T>
Andreas Gampe85a098a2016-03-31 13:30:53 -0700258template<bool kTransactionActive>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700259inline void ObjectArray<T>::AssignableCheckingMemcpy(int32_t dst_pos,
260 ObjPtr<ObjectArray<T>> src,
261 int32_t src_pos,
262 int32_t count,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800263 bool throw_exception) {
264 DCHECK_NE(this, src)
265 << "This case should be handled with memmove that handles overlaps correctly";
266 // We want to avoid redundant IsAssignableFrom checks where possible, so we cache a class that
267 // we know is assignable to the destination array's component type.
268 Class* dst_class = GetClass()->GetComponentType();
269 Class* lastAssignableElementClass = dst_class;
270
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800271 T* o = nullptr;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800272 int i = 0;
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800273 bool baker_non_gray_case = false;
274 if (kUseReadBarrier && kUseBakerReadBarrier) {
275 uintptr_t fake_address_dependency;
276 if (!ReadBarrier::IsGray(src.Ptr(), &fake_address_dependency)) {
277 baker_non_gray_case = true;
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800278 DCHECK_EQ(fake_address_dependency, 0U);
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800279 src.Assign(reinterpret_cast<ObjectArray<T>*>(
280 reinterpret_cast<uintptr_t>(src.Ptr()) | fake_address_dependency));
281 for (; i < count; ++i) {
282 // The follow get operations force the objects to be verified.
283 // We can skip the RB here because 'src' isn't gray.
284 o = src->template GetWithoutChecks<kDefaultVerifyFlags, kWithoutReadBarrier>(
285 src_pos + i);
286 if (o == nullptr) {
287 // Null is always assignable.
288 SetWithoutChecks<kTransactionActive>(dst_pos + i, nullptr);
289 } else {
290 // TODO: use the underlying class reference to avoid uncompression when not necessary.
291 Class* o_class = o->GetClass();
292 if (LIKELY(lastAssignableElementClass == o_class)) {
293 SetWithoutChecks<kTransactionActive>(dst_pos + i, o);
294 } else if (LIKELY(dst_class->IsAssignableFrom(o_class))) {
295 lastAssignableElementClass = o_class;
296 SetWithoutChecks<kTransactionActive>(dst_pos + i, o);
297 } else {
298 // Can't put this element into the array, break to perform write-barrier and throw
299 // exception.
300 break;
301 }
302 }
303 }
304 }
305 }
306 if (!baker_non_gray_case) {
307 for (; i < count; ++i) {
308 // The follow get operations force the objects to be verified.
309 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
310 o = src->GetWithoutChecks(src_pos + i);
311 if (o == nullptr) {
312 // Null is always assignable.
313 SetWithoutChecks<kTransactionActive>(dst_pos + i, nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800314 } else {
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800315 // TODO: use the underlying class reference to avoid uncompression when not necessary.
316 Class* o_class = o->GetClass();
317 if (LIKELY(lastAssignableElementClass == o_class)) {
318 SetWithoutChecks<kTransactionActive>(dst_pos + i, o);
319 } else if (LIKELY(dst_class->IsAssignableFrom(o_class))) {
320 lastAssignableElementClass = o_class;
321 SetWithoutChecks<kTransactionActive>(dst_pos + i, o);
322 } else {
323 // Can't put this element into the array, break to perform write-barrier and throw
324 // exception.
325 break;
326 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800327 }
328 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800329 }
330 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
331 if (UNLIKELY(i != count)) {
David Sehr709b0702016-10-13 09:12:37 -0700332 std::string actualSrcType(mirror::Object::PrettyTypeOf(o));
333 std::string dstType(PrettyTypeOf());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800334 Thread* self = Thread::Current();
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800335 std::string msg = android::base::StringPrintf(
336 "source[%d] of type %s cannot be stored in destination array of type %s",
337 src_pos + i,
338 actualSrcType.c_str(),
339 dstType.c_str());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800340 if (throw_exception) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800341 self->ThrowNewException("Ljava/lang/ArrayStoreException;", msg.c_str());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800342 } else {
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800343 LOG(FATAL) << msg;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800344 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800345 }
346}
347
348template<class T>
349inline ObjectArray<T>* ObjectArray<T>::CopyOf(Thread* self, int32_t new_length) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800350 DCHECK_GE(new_length, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700351 // We may get copied by a compacting GC.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700352 StackHandleScope<1> hs(self);
Ian Rogers700a4022014-05-19 16:49:03 -0700353 Handle<ObjectArray<T>> h_this(hs.NewHandle(this));
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800354 gc::Heap* heap = Runtime::Current()->GetHeap();
355 gc::AllocatorType allocator_type = heap->IsMovableObject(this) ? heap->GetCurrentAllocator() :
356 heap->GetCurrentNonMovingAllocator();
357 ObjectArray<T>* new_array = Alloc(self, GetClass(), new_length, allocator_type);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700358 if (LIKELY(new_array != nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700359 new_array->AssignableMemcpy(0, h_this.Get(), 0, std::min(h_this->GetLength(), new_length));
Ian Rogersa436fde2013-08-27 23:34:06 -0700360 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800361 return new_array;
362}
363
Ian Rogersef7d42f2014-01-06 12:55:46 -0800364template<class T>
365inline MemberOffset ObjectArray<T>::OffsetOfElement(int32_t i) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700366 return MemberOffset(DataOffset(kHeapReferenceSize).Int32Value() + (i * kHeapReferenceSize));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800367}
368
Mathieu Chartier059ef3d2015-08-18 13:54:21 -0700369template<class T> template<typename Visitor>
Hiroshi Yamauchi723e6ce2015-10-28 20:59:47 -0700370inline void ObjectArray<T>::VisitReferences(const Visitor& visitor) {
Mathieu Chartier407f7022014-02-18 14:37:05 -0800371 const size_t length = static_cast<size_t>(GetLength());
372 for (size_t i = 0; i < length; ++i) {
373 visitor(this, OffsetOfElement(i), false);
374 }
375}
376
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800377} // namespace mirror
378} // namespace art
379
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700380#endif // ART_RUNTIME_MIRROR_OBJECT_ARRAY_INL_H_