blob: 0fdf1323eb0c019bd4de30aa6a1e6d7ee3458ecc [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"
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070028#include "obj_ptr-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "runtime.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070030#include "handle_scope-inl.h"
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020031#include "thread.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010032#include "utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033
34namespace art {
35namespace mirror {
36
37template<class T>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070038inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self,
39 ObjPtr<Class> object_array_class,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080040 int32_t length, gc::AllocatorType allocator_type) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070041 Array* array = Array::Alloc<true>(self,
42 object_array_class.Ptr(),
43 length,
44 ComponentSizeShiftWidth(kHeapReferenceSize),
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070045 allocator_type);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080046 if (UNLIKELY(array == nullptr)) {
47 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048 }
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070049 DCHECK_EQ(array->GetClass()->GetComponentSizeShift(),
50 ComponentSizeShiftWidth(kHeapReferenceSize));
51 return array->AsObjectArray<T>();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052}
53
54template<class T>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070055inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self,
56 ObjPtr<Class> object_array_class,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080057 int32_t length) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070058 return Alloc(self,
59 object_array_class,
60 length,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080061 Runtime::Current()->GetHeap()->GetCurrentAllocator());
62}
63
Mathieu Chartierfbc31082016-01-24 11:59:56 -080064template<class T> template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
Ian Rogersef7d42f2014-01-06 12:55:46 -080065inline T* ObjectArray<T>::Get(int32_t i) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070066 if (!CheckIsValidIndex(i)) {
Sebastien Hertzabff6432014-01-27 18:01:39 +010067 DCHECK(Thread::Current()->IsExceptionPending());
Mathieu Chartier2cebb242015-04-21 16:50:40 -070068 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -080070 return GetFieldObject<T, kVerifyFlags, kReadBarrierOption>(OffsetOfElement(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071}
72
Mathieu Chartier4e305412014-02-19 10:54:44 -080073template<class T> template<VerifyObjectFlags kVerifyFlags>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070074inline bool ObjectArray<T>::CheckAssignable(ObjPtr<T> object) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070075 if (object != nullptr) {
Mathieu Chartier4e305412014-02-19 10:54:44 -080076 Class* element_class = GetClass<kVerifyFlags>()->GetComponentType();
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020077 if (UNLIKELY(!object->InstanceOf(element_class))) {
78 ThrowArrayStoreException(object);
79 return false;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080 }
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020081 }
82 return true;
83}
84
85template<class T>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070086inline void ObjectArray<T>::Set(int32_t i, ObjPtr<T> object) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010087 if (Runtime::Current()->IsActiveTransaction()) {
88 Set<true>(i, object);
89 } else {
90 Set<false>(i, object);
91 }
92}
93
94template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -080095template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070096inline void ObjectArray<T>::Set(int32_t i, ObjPtr<T> object) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070097 if (CheckIsValidIndex(i) && CheckAssignable<kVerifyFlags>(object)) {
98 SetFieldObject<kTransactionActive, kCheckTransaction, kVerifyFlags>(OffsetOfElement(i), object);
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020099 } else {
100 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800101 }
102}
103
104template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -0800105template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700106inline void ObjectArray<T>::SetWithoutChecks(int32_t i, ObjPtr<T> object) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800107 DCHECK(CheckIsValidIndex<kVerifyFlags>(i));
108 DCHECK(CheckAssignable<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>(object));
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700109 SetFieldObject<kTransactionActive, kCheckTransaction, kVerifyFlags>(OffsetOfElement(i), object);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800110}
111
112template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -0800113template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700114inline void ObjectArray<T>::SetWithoutChecksAndWriteBarrier(int32_t i, ObjPtr<T> object) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800115 DCHECK(CheckIsValidIndex<kVerifyFlags>(i));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800116 // TODO: enable this check. It fails when writing the image in ImageWriter::FixupObjectArray.
Sebastien Hertzabff6432014-01-27 18:01:39 +0100117 // DCHECK(CheckAssignable(object));
Mathieu Chartier4e305412014-02-19 10:54:44 -0800118 SetFieldObjectWithoutWriteBarrier<kTransactionActive, kCheckTransaction, kVerifyFlags>(
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700119 OffsetOfElement(i), object);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800120}
121
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800122template<class T> template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800123inline T* ObjectArray<T>::GetWithoutChecks(int32_t i) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100124 DCHECK(CheckIsValidIndex(i));
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800125 return GetFieldObject<T, kVerifyFlags, kReadBarrierOption>(OffsetOfElement(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126}
127
128template<class T>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700129inline void ObjectArray<T>::AssignableMemmove(int32_t dst_pos,
130 ObjPtr<ObjectArray<T>> src,
131 int32_t src_pos,
132 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800133 if (kIsDebugBuild) {
134 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700135 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800136 src->GetWithoutChecks(src_pos + i);
137 }
138 }
139 // Perform the memmove using int memmove then perform the write barrier.
Roland Levillain33d69032015-06-18 18:20:59 +0100140 static_assert(sizeof(HeapReference<T>) == sizeof(uint32_t),
141 "art::mirror::HeapReference<T> and uint32_t have different sizes.");
Mathieu Chartierfec13d42016-10-07 12:59:33 -0700142 // TODO: Optimize this later?
143 // We can't use memmove since it does not handle read barriers and may do by per byte copying.
144 // See b/32012820.
145 const bool copy_forward = (src != this) || (dst_pos < src_pos) || (dst_pos - src_pos >= count);
146 if (copy_forward) {
147 // Forward copy.
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800148 bool baker_non_gray_case = false;
149 if (kUseReadBarrier && kUseBakerReadBarrier) {
150 uintptr_t fake_address_dependency;
151 if (!ReadBarrier::IsGray(src.Ptr(), &fake_address_dependency)) {
152 baker_non_gray_case = true;
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800153 DCHECK_EQ(fake_address_dependency, 0U);
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800154 src.Assign(reinterpret_cast<ObjectArray<T>*>(
155 reinterpret_cast<uintptr_t>(src.Ptr()) | fake_address_dependency));
156 for (int i = 0; i < count; ++i) {
157 // We can skip the RB here because 'src' isn't gray.
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800158 T* obj = src->template GetWithoutChecks<kDefaultVerifyFlags, kWithoutReadBarrier>(
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800159 src_pos + i);
160 SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj);
161 }
162 }
163 }
164 if (!baker_non_gray_case) {
165 for (int i = 0; i < count; ++i) {
166 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800167 T* obj = src->GetWithoutChecks(src_pos + i);
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800168 SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj);
169 }
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700170 }
171 } else {
Mathieu Chartierfec13d42016-10-07 12:59:33 -0700172 // Backward copy.
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800173 bool baker_non_gray_case = false;
174 if (kUseReadBarrier && kUseBakerReadBarrier) {
175 uintptr_t fake_address_dependency;
176 if (!ReadBarrier::IsGray(src.Ptr(), &fake_address_dependency)) {
177 baker_non_gray_case = true;
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800178 DCHECK_EQ(fake_address_dependency, 0U);
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800179 src.Assign(reinterpret_cast<ObjectArray<T>*>(
180 reinterpret_cast<uintptr_t>(src.Ptr()) | fake_address_dependency));
181 for (int i = count - 1; i >= 0; --i) {
182 // We can skip the RB here because 'src' isn't gray.
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800183 T* obj = src->template GetWithoutChecks<kDefaultVerifyFlags, kWithoutReadBarrier>(
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800184 src_pos + i);
185 SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj);
186 }
187 }
188 }
189 if (!baker_non_gray_case) {
190 for (int i = count - 1; i >= 0; --i) {
191 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800192 T* obj = src->GetWithoutChecks(src_pos + i);
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800193 SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj);
194 }
Mathieu Chartierfec13d42016-10-07 12:59:33 -0700195 }
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700196 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800197 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
198 if (kIsDebugBuild) {
199 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700200 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800201 GetWithoutChecks(dst_pos + i);
202 }
203 }
204}
205
206template<class T>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700207inline void ObjectArray<T>::AssignableMemcpy(int32_t dst_pos,
208 ObjPtr<ObjectArray<T>> src,
209 int32_t src_pos,
210 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800211 if (kIsDebugBuild) {
212 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700213 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800214 src->GetWithoutChecks(src_pos + i);
215 }
216 }
217 // Perform the memmove using int memcpy then perform the write barrier.
Roland Levillain33d69032015-06-18 18:20:59 +0100218 static_assert(sizeof(HeapReference<T>) == sizeof(uint32_t),
219 "art::mirror::HeapReference<T> and uint32_t have different sizes.");
Mathieu Chartierfec13d42016-10-07 12:59:33 -0700220 // TODO: Optimize this later?
221 // We can't use memmove since it does not handle read barriers and may do by per byte copying.
222 // See b/32012820.
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800223 bool baker_non_gray_case = false;
224 if (kUseReadBarrier && kUseBakerReadBarrier) {
225 uintptr_t fake_address_dependency;
226 if (!ReadBarrier::IsGray(src.Ptr(), &fake_address_dependency)) {
227 baker_non_gray_case = true;
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800228 DCHECK_EQ(fake_address_dependency, 0U);
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800229 src.Assign(reinterpret_cast<ObjectArray<T>*>(
230 reinterpret_cast<uintptr_t>(src.Ptr()) | fake_address_dependency));
231 for (int i = 0; i < count; ++i) {
232 // We can skip the RB here because 'src' isn't gray.
233 Object* obj = src->template GetWithoutChecks<kDefaultVerifyFlags, kWithoutReadBarrier>(
234 src_pos + i);
235 SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj);
236 }
237 }
238 }
239 if (!baker_non_gray_case) {
240 for (int i = 0; i < count; ++i) {
241 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
242 T* obj = src->GetWithoutChecks(src_pos + i);
243 SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj);
244 }
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700245 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800246 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
247 if (kIsDebugBuild) {
248 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700249 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800250 GetWithoutChecks(dst_pos + i);
251 }
252 }
253}
254
255template<class T>
Andreas Gampe85a098a2016-03-31 13:30:53 -0700256template<bool kTransactionActive>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700257inline void ObjectArray<T>::AssignableCheckingMemcpy(int32_t dst_pos,
258 ObjPtr<ObjectArray<T>> src,
259 int32_t src_pos,
260 int32_t count,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800261 bool throw_exception) {
262 DCHECK_NE(this, src)
263 << "This case should be handled with memmove that handles overlaps correctly";
264 // We want to avoid redundant IsAssignableFrom checks where possible, so we cache a class that
265 // we know is assignable to the destination array's component type.
266 Class* dst_class = GetClass()->GetComponentType();
267 Class* lastAssignableElementClass = dst_class;
268
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800269 T* o = nullptr;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800270 int i = 0;
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800271 bool baker_non_gray_case = false;
272 if (kUseReadBarrier && kUseBakerReadBarrier) {
273 uintptr_t fake_address_dependency;
274 if (!ReadBarrier::IsGray(src.Ptr(), &fake_address_dependency)) {
275 baker_non_gray_case = true;
Hiroshi Yamauchi6013f772016-11-16 13:30:17 -0800276 DCHECK_EQ(fake_address_dependency, 0U);
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800277 src.Assign(reinterpret_cast<ObjectArray<T>*>(
278 reinterpret_cast<uintptr_t>(src.Ptr()) | fake_address_dependency));
279 for (; i < count; ++i) {
280 // The follow get operations force the objects to be verified.
281 // We can skip the RB here because 'src' isn't gray.
282 o = src->template GetWithoutChecks<kDefaultVerifyFlags, kWithoutReadBarrier>(
283 src_pos + i);
284 if (o == nullptr) {
285 // Null is always assignable.
286 SetWithoutChecks<kTransactionActive>(dst_pos + i, nullptr);
287 } else {
288 // TODO: use the underlying class reference to avoid uncompression when not necessary.
289 Class* o_class = o->GetClass();
290 if (LIKELY(lastAssignableElementClass == o_class)) {
291 SetWithoutChecks<kTransactionActive>(dst_pos + i, o);
292 } else if (LIKELY(dst_class->IsAssignableFrom(o_class))) {
293 lastAssignableElementClass = o_class;
294 SetWithoutChecks<kTransactionActive>(dst_pos + i, o);
295 } else {
296 // Can't put this element into the array, break to perform write-barrier and throw
297 // exception.
298 break;
299 }
300 }
301 }
302 }
303 }
304 if (!baker_non_gray_case) {
305 for (; i < count; ++i) {
306 // The follow get operations force the objects to be verified.
307 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
308 o = src->GetWithoutChecks(src_pos + i);
309 if (o == nullptr) {
310 // Null is always assignable.
311 SetWithoutChecks<kTransactionActive>(dst_pos + i, nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800312 } else {
Hiroshi Yamauchie43b80e2016-11-14 13:42:50 -0800313 // TODO: use the underlying class reference to avoid uncompression when not necessary.
314 Class* o_class = o->GetClass();
315 if (LIKELY(lastAssignableElementClass == o_class)) {
316 SetWithoutChecks<kTransactionActive>(dst_pos + i, o);
317 } else if (LIKELY(dst_class->IsAssignableFrom(o_class))) {
318 lastAssignableElementClass = o_class;
319 SetWithoutChecks<kTransactionActive>(dst_pos + i, o);
320 } else {
321 // Can't put this element into the array, break to perform write-barrier and throw
322 // exception.
323 break;
324 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800325 }
326 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800327 }
328 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
329 if (UNLIKELY(i != count)) {
David Sehr709b0702016-10-13 09:12:37 -0700330 std::string actualSrcType(mirror::Object::PrettyTypeOf(o));
331 std::string dstType(PrettyTypeOf());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800332 Thread* self = Thread::Current();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800333 if (throw_exception) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000334 self->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Ian Rogersef7d42f2014-01-06 12:55:46 -0800335 "source[%d] of type %s cannot be stored in destination array of type %s",
336 src_pos + i, actualSrcType.c_str(), dstType.c_str());
337 } else {
338 LOG(FATAL) << StringPrintf("source[%d] of type %s cannot be stored in destination array of type %s",
339 src_pos + i, actualSrcType.c_str(), dstType.c_str());
340 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800341 }
342}
343
344template<class T>
345inline ObjectArray<T>* ObjectArray<T>::CopyOf(Thread* self, int32_t new_length) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800346 DCHECK_GE(new_length, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700347 // We may get copied by a compacting GC.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700348 StackHandleScope<1> hs(self);
Ian Rogers700a4022014-05-19 16:49:03 -0700349 Handle<ObjectArray<T>> h_this(hs.NewHandle(this));
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800350 gc::Heap* heap = Runtime::Current()->GetHeap();
351 gc::AllocatorType allocator_type = heap->IsMovableObject(this) ? heap->GetCurrentAllocator() :
352 heap->GetCurrentNonMovingAllocator();
353 ObjectArray<T>* new_array = Alloc(self, GetClass(), new_length, allocator_type);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700354 if (LIKELY(new_array != nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700355 new_array->AssignableMemcpy(0, h_this.Get(), 0, std::min(h_this->GetLength(), new_length));
Ian Rogersa436fde2013-08-27 23:34:06 -0700356 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800357 return new_array;
358}
359
Ian Rogersef7d42f2014-01-06 12:55:46 -0800360template<class T>
361inline MemberOffset ObjectArray<T>::OffsetOfElement(int32_t i) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700362 return MemberOffset(DataOffset(kHeapReferenceSize).Int32Value() + (i * kHeapReferenceSize));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800363}
364
Mathieu Chartier059ef3d2015-08-18 13:54:21 -0700365template<class T> template<typename Visitor>
Hiroshi Yamauchi723e6ce2015-10-28 20:59:47 -0700366inline void ObjectArray<T>::VisitReferences(const Visitor& visitor) {
Mathieu Chartier407f7022014-02-18 14:37:05 -0800367 const size_t length = static_cast<size_t>(GetLength());
368 for (size_t i = 0; i < length; ++i) {
369 visitor(this, OffsetOfElement(i), false);
370 }
371}
372
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800373} // namespace mirror
374} // namespace art
375
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700376#endif // ART_RUNTIME_MIRROR_OBJECT_ARRAY_INL_H_