blob: b11dad820352426480f049d9c8c2f485051d00ba [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_INL_H_
18#define ART_RUNTIME_MIRROR_ARRAY_INL_H_
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019
20#include "array.h"
21
Vladimir Marko80afd022015-05-19 18:08:00 +010022#include "base/bit_utils.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070023#include "base/casts.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010024#include "base/logging.h"
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080025#include "base/stringprintf.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070026#include "class-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070027#include "gc/heap-inl.h"
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070028#include "obj_ptr-inl.h"
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070029#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030
31namespace art {
32namespace mirror {
33
Andreas Gampe542451c2016-07-26 09:02:02 -070034inline uint32_t Array::ClassSize(PointerSize pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070035 uint32_t vtable_entries = Object::kVTableLength;
Mathieu Chartiere401d142015-04-22 13:56:20 -070036 return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 0, 0, pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070037}
38
Hiroshi Yamauchi6e83c172014-05-01 21:25:41 -070039template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
Ian Rogersef7d42f2014-01-06 12:55:46 -080040inline size_t Array::SizeOf() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041 // This is safe from overflow because the array was already allocated, so we know it's sane.
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070042 size_t component_size_shift = GetClass<kVerifyFlags, kReadBarrierOption>()->
43 template GetComponentSizeShift<kReadBarrierOption>();
Mathieu Chartier4e305412014-02-19 10:54:44 -080044 // Don't need to check this since we already check this in GetClass.
45 int32_t component_count =
46 GetLength<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>();
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070047 size_t header_size = DataOffset(1U << component_size_shift).SizeValue();
48 size_t data_size = component_count << component_size_shift;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049 return header_size + data_size;
50}
51
Ian Rogers7e70b002014-10-08 11:47:24 -070052inline MemberOffset Array::DataOffset(size_t component_size) {
53 DCHECK(IsPowerOfTwo(component_size)) << component_size;
54 size_t data_offset = RoundUp(OFFSETOF_MEMBER(Array, first_element_), component_size);
55 DCHECK_EQ(RoundUp(data_offset, component_size), data_offset)
56 << "Array data offset isn't aligned with component size";
57 return MemberOffset(data_offset);
58}
59
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070060template<VerifyObjectFlags kVerifyFlags>
61inline bool Array::CheckIsValidIndex(int32_t index) {
62 if (UNLIKELY(static_cast<uint32_t>(index) >=
63 static_cast<uint32_t>(GetLength<kVerifyFlags>()))) {
64 ThrowArrayIndexOutOfBoundsException(index);
65 return false;
66 }
67 return true;
68}
69
Vladimir Marko20f85592015-03-19 10:07:02 +000070static inline size_t ComputeArraySize(int32_t component_count, size_t component_size_shift) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070071 DCHECK_GE(component_count, 0);
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070072
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070073 size_t component_size = 1U << component_size_shift;
Hiroshi Yamauchiaa866f52014-03-21 16:18:30 -070074 size_t header_size = Array::DataOffset(component_size).SizeValue();
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070075 size_t data_size = static_cast<size_t>(component_count) << component_size_shift;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070076 size_t size = header_size + data_size;
77
Vladimir Marko20f85592015-03-19 10:07:02 +000078 // Check for size_t overflow if this was an unreasonable request
79 // but let the caller throw OutOfMemoryError.
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070080#ifdef __LP64__
81 // 64-bit. No overflow as component_count is 32-bit and the maximum
82 // component size is 8.
83 DCHECK_LE((1U << component_size_shift), 8U);
84#else
85 // 32-bit.
86 DCHECK_NE(header_size, 0U);
87 DCHECK_EQ(RoundUp(header_size, component_size), header_size);
88 // The array length limit (exclusive).
89 const size_t length_limit = (0U - header_size) >> component_size_shift;
90 if (UNLIKELY(length_limit <= static_cast<size_t>(component_count))) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070091 return 0; // failure
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070092 }
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070093#endif
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070094 return size;
95}
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070096
Ian Rogers6fac4472014-02-25 17:01:10 -080097// Used for setting the array length in the allocation code path to ensure it is guarded by a
98// StoreStore fence.
Mathieu Chartier1febddf2013-11-20 12:33:14 -080099class SetLengthVisitor {
100 public:
101 explicit SetLengthVisitor(int32_t length) : length_(length) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700102 }
Mathieu Chartier1febddf2013-11-20 12:33:14 -0800103
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700104 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700105 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800106 // Avoid AsArray as object is not yet in live bitmap or allocation stack.
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700107 ObjPtr<Array> array = ObjPtr<Array>::DownCast(obj);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800108 // DCHECK(array->IsArrayInstance());
Mathieu Chartier1febddf2013-11-20 12:33:14 -0800109 array->SetLength(length_);
110 }
111
112 private:
113 const int32_t length_;
Ian Rogers6fac4472014-02-25 17:01:10 -0800114
115 DISALLOW_COPY_AND_ASSIGN(SetLengthVisitor);
116};
117
118// Similar to SetLengthVisitor, used for setting the array length to fill the usable size of an
119// array.
120class SetLengthToUsableSizeVisitor {
121 public:
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700122 SetLengthToUsableSizeVisitor(int32_t min_length, size_t header_size,
123 size_t component_size_shift) :
124 minimum_length_(min_length), header_size_(header_size),
125 component_size_shift_(component_size_shift) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800126 }
127
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700128 void operator()(ObjPtr<Object> obj, size_t usable_size) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700129 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800130 // Avoid AsArray as object is not yet in live bitmap or allocation stack.
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700131 ObjPtr<Array> array = ObjPtr<Array>::DownCast(obj);
Ian Rogers6fac4472014-02-25 17:01:10 -0800132 // DCHECK(array->IsArrayInstance());
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700133 int32_t length = (usable_size - header_size_) >> component_size_shift_;
Ian Rogersa55cf412014-02-27 00:31:26 -0800134 DCHECK_GE(length, minimum_length_);
Ian Rogers13735952014-10-08 12:43:28 -0700135 uint8_t* old_end = reinterpret_cast<uint8_t*>(array->GetRawData(1U << component_size_shift_,
136 minimum_length_));
137 uint8_t* new_end = reinterpret_cast<uint8_t*>(array->GetRawData(1U << component_size_shift_,
138 length));
Ian Rogersa55cf412014-02-27 00:31:26 -0800139 // Ensure space beyond original allocation is zeroed.
140 memset(old_end, 0, new_end - old_end);
Ian Rogers6fac4472014-02-25 17:01:10 -0800141 array->SetLength(length);
142 }
143
144 private:
Ian Rogersa55cf412014-02-27 00:31:26 -0800145 const int32_t minimum_length_;
Ian Rogers6fac4472014-02-25 17:01:10 -0800146 const size_t header_size_;
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700147 const size_t component_size_shift_;
Ian Rogers6fac4472014-02-25 17:01:10 -0800148
149 DISALLOW_COPY_AND_ASSIGN(SetLengthToUsableSizeVisitor);
Mathieu Chartier1febddf2013-11-20 12:33:14 -0800150};
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700151
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700152template <bool kIsInstrumented, bool kFillUsable>
Mathieu Chartier31e88222016-10-14 18:43:19 -0700153inline Array* Array::Alloc(Thread* self,
154 ObjPtr<Class> array_class,
155 int32_t component_count,
156 size_t component_size_shift,
157 gc::AllocatorType allocator_type) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800158 DCHECK(allocator_type != gc::kAllocatorTypeLOS);
Vladimir Marko20f85592015-03-19 10:07:02 +0000159 DCHECK(array_class != nullptr);
160 DCHECK(array_class->IsArrayClass());
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700161 DCHECK_EQ(array_class->GetComponentSizeShift(), component_size_shift);
162 DCHECK_EQ(array_class->GetComponentSize(), (1U << component_size_shift));
Vladimir Marko20f85592015-03-19 10:07:02 +0000163 size_t size = ComputeArraySize(component_count, component_size_shift);
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700164#ifdef __LP64__
165 // 64-bit. No size_t overflow.
166 DCHECK_NE(size, 0U);
167#else
168 // 32-bit.
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700169 if (UNLIKELY(size == 0)) {
Vladimir Marko20f85592015-03-19 10:07:02 +0000170 self->ThrowOutOfMemoryError(StringPrintf("%s of length %d would overflow",
David Sehr709b0702016-10-13 09:12:37 -0700171 array_class->PrettyDescriptor().c_str(),
Vladimir Marko20f85592015-03-19 10:07:02 +0000172 component_count).c_str());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800173 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700174 }
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700175#endif
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700176 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers6fac4472014-02-25 17:01:10 -0800177 Array* result;
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700178 if (!kFillUsable) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800179 SetLengthVisitor visitor(component_count);
180 result = down_cast<Array*>(
181 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size,
182 allocator_type, visitor));
183 } else {
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700184 SetLengthToUsableSizeVisitor visitor(component_count,
185 DataOffset(1U << component_size_shift).SizeValue(),
186 component_size_shift);
Ian Rogers6fac4472014-02-25 17:01:10 -0800187 result = down_cast<Array*>(
188 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size,
189 allocator_type, visitor));
190 }
191 if (kIsDebugBuild && result != nullptr && Runtime::Current()->IsStarted()) {
Mathieu Chartier85801542014-02-27 18:06:26 -0800192 array_class = result->GetClass(); // In case the array class moved.
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700193 CHECK_EQ(array_class->GetComponentSize(), 1U << component_size_shift);
194 if (!kFillUsable) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800195 CHECK_EQ(result->SizeOf(), size);
196 } else {
197 CHECK_GE(result->SizeOf(), size);
198 }
199 }
200 return result;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700201}
202
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800203template<class T>
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700204inline void PrimitiveArray<T>::VisitRoots(RootVisitor* visitor) {
205 array_class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800206}
207
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700208template<typename T>
209inline PrimitiveArray<T>* PrimitiveArray<T>::Alloc(Thread* self, size_t length) {
Mathieu Chartier31e88222016-10-14 18:43:19 -0700210 Array* raw_array = Array::Alloc<true>(self,
211 GetArrayClass(),
212 length,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700213 ComponentSizeShiftWidth(sizeof(T)),
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700214 Runtime::Current()->GetHeap()->GetCurrentAllocator());
215 return down_cast<PrimitiveArray<T>*>(raw_array);
216}
217
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700218template<typename T>
219inline T PrimitiveArray<T>::Get(int32_t i) {
220 if (!CheckIsValidIndex(i)) {
221 DCHECK(Thread::Current()->IsExceptionPending());
222 return T(0);
223 }
224 return GetWithoutChecks(i);
225}
226
227template<typename T>
228inline void PrimitiveArray<T>::Set(int32_t i, T value) {
229 if (Runtime::Current()->IsActiveTransaction()) {
230 Set<true>(i, value);
231 } else {
232 Set<false>(i, value);
233 }
234}
235
236template<typename T>
237template<bool kTransactionActive, bool kCheckTransaction>
238inline void PrimitiveArray<T>::Set(int32_t i, T value) {
239 if (CheckIsValidIndex(i)) {
240 SetWithoutChecks<kTransactionActive, kCheckTransaction>(i, value);
241 } else {
242 DCHECK(Thread::Current()->IsExceptionPending());
243 }
244}
245
246template<typename T>
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700247template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700248inline void PrimitiveArray<T>::SetWithoutChecks(int32_t i, T value) {
249 if (kCheckTransaction) {
250 DCHECK_EQ(kTransactionActive, Runtime::Current()->IsActiveTransaction());
251 }
252 if (kTransactionActive) {
253 Runtime::Current()->RecordWriteArray(this, i, GetWithoutChecks(i));
254 }
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700255 DCHECK(CheckIsValidIndex<kVerifyFlags>(i));
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700256 GetData()[i] = value;
257}
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700258// Backward copy where elements are of aligned appropriately for T. Count is in T sized units.
259// Copies are guaranteed not to tear when the sizeof T is less-than 64bit.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800260template<typename T>
261static inline void ArrayBackwardCopy(T* d, const T* s, int32_t count) {
262 d += count;
263 s += count;
264 for (int32_t i = 0; i < count; ++i) {
265 d--;
266 s--;
267 *d = *s;
268 }
269}
270
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700271// Forward copy where elements are of aligned appropriately for T. Count is in T sized units.
272// Copies are guaranteed not to tear when the sizeof T is less-than 64bit.
Ian Rogers6fac4472014-02-25 17:01:10 -0800273template<typename T>
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700274static inline void ArrayForwardCopy(T* d, const T* s, int32_t count) {
275 for (int32_t i = 0; i < count; ++i) {
276 *d = *s;
277 d++;
278 s++;
279 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800280}
281
Ian Rogersef7d42f2014-01-06 12:55:46 -0800282template<class T>
Mathieu Chartier31e88222016-10-14 18:43:19 -0700283inline void PrimitiveArray<T>::Memmove(int32_t dst_pos,
284 ObjPtr<PrimitiveArray<T>> src,
285 int32_t src_pos,
Ian Rogers6fac4472014-02-25 17:01:10 -0800286 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800287 if (UNLIKELY(count == 0)) {
288 return;
289 }
290 DCHECK_GE(dst_pos, 0);
291 DCHECK_GE(src_pos, 0);
292 DCHECK_GT(count, 0);
293 DCHECK(src != nullptr);
294 DCHECK_LT(dst_pos, GetLength());
295 DCHECK_LE(dst_pos, GetLength() - count);
296 DCHECK_LT(src_pos, src->GetLength());
297 DCHECK_LE(src_pos, src->GetLength() - count);
298
299 // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
300 // in our implementation, because they may copy byte-by-byte.
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700301 if (LIKELY(src != this)) {
302 // Memcpy ok for guaranteed non-overlapping distinct arrays.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800303 Memcpy(dst_pos, src, src_pos, count);
304 } else {
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700305 // Handle copies within the same array using the appropriate direction copy.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800306 void* dst_raw = GetRawData(sizeof(T), dst_pos);
307 const void* src_raw = src->GetRawData(sizeof(T), src_pos);
308 if (sizeof(T) == sizeof(uint8_t)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800309 uint8_t* d = reinterpret_cast<uint8_t*>(dst_raw);
310 const uint8_t* s = reinterpret_cast<const uint8_t*>(src_raw);
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700311 memmove(d, s, count);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800312 } else {
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700313 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= count);
314 if (sizeof(T) == sizeof(uint16_t)) {
315 uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
316 const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
317 if (copy_forward) {
318 ArrayForwardCopy<uint16_t>(d, s, count);
319 } else {
320 ArrayBackwardCopy<uint16_t>(d, s, count);
321 }
322 } else if (sizeof(T) == sizeof(uint32_t)) {
323 uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
324 const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
325 if (copy_forward) {
326 ArrayForwardCopy<uint32_t>(d, s, count);
327 } else {
328 ArrayBackwardCopy<uint32_t>(d, s, count);
329 }
330 } else {
331 DCHECK_EQ(sizeof(T), sizeof(uint64_t));
332 uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
333 const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
334 if (copy_forward) {
335 ArrayForwardCopy<uint64_t>(d, s, count);
336 } else {
337 ArrayBackwardCopy<uint64_t>(d, s, count);
338 }
339 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800340 }
341 }
342}
343
Ian Rogersef7d42f2014-01-06 12:55:46 -0800344template<class T>
Mathieu Chartier31e88222016-10-14 18:43:19 -0700345inline void PrimitiveArray<T>::Memcpy(int32_t dst_pos,
346 ObjPtr<PrimitiveArray<T>> src,
347 int32_t src_pos,
Ian Rogers6fac4472014-02-25 17:01:10 -0800348 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800349 if (UNLIKELY(count == 0)) {
350 return;
351 }
352 DCHECK_GE(dst_pos, 0);
353 DCHECK_GE(src_pos, 0);
354 DCHECK_GT(count, 0);
355 DCHECK(src != nullptr);
356 DCHECK_LT(dst_pos, GetLength());
357 DCHECK_LE(dst_pos, GetLength() - count);
358 DCHECK_LT(src_pos, src->GetLength());
359 DCHECK_LE(src_pos, src->GetLength() - count);
360
361 // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
362 // in our implementation, because they may copy byte-by-byte.
363 void* dst_raw = GetRawData(sizeof(T), dst_pos);
364 const void* src_raw = src->GetRawData(sizeof(T), src_pos);
365 if (sizeof(T) == sizeof(uint8_t)) {
366 memcpy(dst_raw, src_raw, count);
367 } else if (sizeof(T) == sizeof(uint16_t)) {
368 uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
369 const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
370 ArrayForwardCopy<uint16_t>(d, s, count);
371 } else if (sizeof(T) == sizeof(uint32_t)) {
372 uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
373 const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
374 ArrayForwardCopy<uint32_t>(d, s, count);
375 } else {
376 DCHECK_EQ(sizeof(T), sizeof(uint64_t));
377 uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
378 const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
379 ArrayForwardCopy<uint64_t>(d, s, count);
380 }
381}
382
Mathieu Chartierdfe02f62016-02-01 20:15:11 -0800383template<typename T, VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
Andreas Gampe542451c2016-07-26 09:02:02 -0700384inline T PointerArray::GetElementPtrSize(uint32_t idx, PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700385 // C style casts here since we sometimes have T be a pointer, or sometimes an integer
386 // (for stack traces).
Andreas Gampe542451c2016-07-26 09:02:02 -0700387 if (ptr_size == PointerSize::k64) {
Mathieu Chartierdfe02f62016-02-01 20:15:11 -0800388 return (T)static_cast<uintptr_t>(
389 AsLongArray<kVerifyFlags, kReadBarrierOption>()->GetWithoutChecks(idx));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700390 }
Mathieu Chartierdfe02f62016-02-01 20:15:11 -0800391 return (T)static_cast<uintptr_t>(
392 AsIntArray<kVerifyFlags, kReadBarrierOption>()->GetWithoutChecks(idx));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700393}
394
Mathieu Chartierd329a3b2016-01-27 15:30:10 -0800395template<bool kTransactionActive, bool kUnchecked>
Andreas Gampe542451c2016-07-26 09:02:02 -0700396inline void PointerArray::SetElementPtrSize(uint32_t idx, uint64_t element, PointerSize ptr_size) {
397 if (ptr_size == PointerSize::k64) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700398 (kUnchecked ? down_cast<LongArray*>(static_cast<Object*>(this)) : AsLongArray())->
Mathieu Chartierd329a3b2016-01-27 15:30:10 -0800399 SetWithoutChecks<kTransactionActive>(idx, element);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700400 } else {
Mathieu Chartierd329a3b2016-01-27 15:30:10 -0800401 DCHECK_LE(element, static_cast<uint64_t>(0xFFFFFFFFu));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700402 (kUnchecked ? down_cast<IntArray*>(static_cast<Object*>(this)) : AsIntArray())
Mathieu Chartierd329a3b2016-01-27 15:30:10 -0800403 ->SetWithoutChecks<kTransactionActive>(idx, static_cast<uint32_t>(element));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700404 }
405}
406
Mathieu Chartierd329a3b2016-01-27 15:30:10 -0800407template<bool kTransactionActive, bool kUnchecked, typename T>
Andreas Gampe542451c2016-07-26 09:02:02 -0700408inline void PointerArray::SetElementPtrSize(uint32_t idx, T* element, PointerSize ptr_size) {
Mathieu Chartier1bbfab62016-01-27 16:37:19 -0800409 SetElementPtrSize<kTransactionActive, kUnchecked>(idx,
410 reinterpret_cast<uintptr_t>(element),
411 ptr_size);
Mathieu Chartierd329a3b2016-01-27 15:30:10 -0800412}
413
Mathieu Chartierdfe02f62016-02-01 20:15:11 -0800414template <VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption, typename Visitor>
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800415inline void PointerArray::Fixup(mirror::PointerArray* dest,
Andreas Gampe542451c2016-07-26 09:02:02 -0700416 PointerSize pointer_size,
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800417 const Visitor& visitor) {
418 for (size_t i = 0, count = GetLength(); i < count; ++i) {
Mathieu Chartierdfe02f62016-02-01 20:15:11 -0800419 void* ptr = GetElementPtrSize<void*, kVerifyFlags, kReadBarrierOption>(i, pointer_size);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800420 void* new_ptr = visitor(ptr);
421 if (ptr != new_ptr) {
422 dest->SetElementPtrSize<false, true>(i, new_ptr, pointer_size);
423 }
424 }
425}
426
Mathieu Chartier31e88222016-10-14 18:43:19 -0700427template<typename T>
428inline void PrimitiveArray<T>::SetArrayClass(ObjPtr<Class> array_class) {
429 CHECK(array_class_.IsNull());
430 CHECK(array_class != nullptr);
431 array_class_ = GcRoot<Class>(array_class);
432}
433
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800434} // namespace mirror
435} // namespace art
436
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700437#endif // ART_RUNTIME_MIRROR_ARRAY_INL_H_