blob: c6fa15de8cfb182945e0e04c4d2a8423b356642f [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"
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070028#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029
30namespace art {
31namespace mirror {
32
Mathieu Chartiere401d142015-04-22 13:56:20 -070033inline uint32_t Array::ClassSize(size_t pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070034 uint32_t vtable_entries = Object::kVTableLength;
Mathieu Chartiere401d142015-04-22 13:56:20 -070035 return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 0, 0, pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070036}
37
Hiroshi Yamauchi6e83c172014-05-01 21:25:41 -070038template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
Ian Rogersef7d42f2014-01-06 12:55:46 -080039inline size_t Array::SizeOf() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040 // This is safe from overflow because the array was already allocated, so we know it's sane.
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070041 size_t component_size_shift = GetClass<kVerifyFlags, kReadBarrierOption>()->
42 template GetComponentSizeShift<kReadBarrierOption>();
Mathieu Chartier4e305412014-02-19 10:54:44 -080043 // Don't need to check this since we already check this in GetClass.
44 int32_t component_count =
45 GetLength<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>();
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070046 size_t header_size = DataOffset(1U << component_size_shift).SizeValue();
47 size_t data_size = component_count << component_size_shift;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048 return header_size + data_size;
49}
50
Ian Rogers7e70b002014-10-08 11:47:24 -070051inline MemberOffset Array::DataOffset(size_t component_size) {
52 DCHECK(IsPowerOfTwo(component_size)) << component_size;
53 size_t data_offset = RoundUp(OFFSETOF_MEMBER(Array, first_element_), component_size);
54 DCHECK_EQ(RoundUp(data_offset, component_size), data_offset)
55 << "Array data offset isn't aligned with component size";
56 return MemberOffset(data_offset);
57}
58
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070059template<VerifyObjectFlags kVerifyFlags>
60inline bool Array::CheckIsValidIndex(int32_t index) {
61 if (UNLIKELY(static_cast<uint32_t>(index) >=
62 static_cast<uint32_t>(GetLength<kVerifyFlags>()))) {
63 ThrowArrayIndexOutOfBoundsException(index);
64 return false;
65 }
66 return true;
67}
68
Vladimir Marko20f85592015-03-19 10:07:02 +000069static inline size_t ComputeArraySize(int32_t component_count, size_t component_size_shift) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070070 DCHECK_GE(component_count, 0);
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070071
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070072 size_t component_size = 1U << component_size_shift;
Hiroshi Yamauchiaa866f52014-03-21 16:18:30 -070073 size_t header_size = Array::DataOffset(component_size).SizeValue();
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070074 size_t data_size = static_cast<size_t>(component_count) << component_size_shift;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070075 size_t size = header_size + data_size;
76
Vladimir Marko20f85592015-03-19 10:07:02 +000077 // Check for size_t overflow if this was an unreasonable request
78 // but let the caller throw OutOfMemoryError.
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070079#ifdef __LP64__
80 // 64-bit. No overflow as component_count is 32-bit and the maximum
81 // component size is 8.
82 DCHECK_LE((1U << component_size_shift), 8U);
83#else
84 // 32-bit.
85 DCHECK_NE(header_size, 0U);
86 DCHECK_EQ(RoundUp(header_size, component_size), header_size);
87 // The array length limit (exclusive).
88 const size_t length_limit = (0U - header_size) >> component_size_shift;
89 if (UNLIKELY(length_limit <= static_cast<size_t>(component_count))) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070090 return 0; // failure
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070091 }
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070092#endif
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070093 return size;
94}
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070095
Ian Rogers6fac4472014-02-25 17:01:10 -080096// Used for setting the array length in the allocation code path to ensure it is guarded by a
97// StoreStore fence.
Mathieu Chartier1febddf2013-11-20 12:33:14 -080098class SetLengthVisitor {
99 public:
100 explicit SetLengthVisitor(int32_t length) : length_(length) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700101 }
Mathieu Chartier1febddf2013-11-20 12:33:14 -0800102
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100103 void operator()(Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700104 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800105 // Avoid AsArray as object is not yet in live bitmap or allocation stack.
106 Array* array = down_cast<Array*>(obj);
107 // DCHECK(array->IsArrayInstance());
Mathieu Chartier1febddf2013-11-20 12:33:14 -0800108 array->SetLength(length_);
109 }
110
111 private:
112 const int32_t length_;
Ian Rogers6fac4472014-02-25 17:01:10 -0800113
114 DISALLOW_COPY_AND_ASSIGN(SetLengthVisitor);
115};
116
117// Similar to SetLengthVisitor, used for setting the array length to fill the usable size of an
118// array.
119class SetLengthToUsableSizeVisitor {
120 public:
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700121 SetLengthToUsableSizeVisitor(int32_t min_length, size_t header_size,
122 size_t component_size_shift) :
123 minimum_length_(min_length), header_size_(header_size),
124 component_size_shift_(component_size_shift) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800125 }
126
127 void operator()(Object* obj, size_t usable_size) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700128 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800129 // Avoid AsArray as object is not yet in live bitmap or allocation stack.
130 Array* array = down_cast<Array*>(obj);
Ian Rogers6fac4472014-02-25 17:01:10 -0800131 // DCHECK(array->IsArrayInstance());
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700132 int32_t length = (usable_size - header_size_) >> component_size_shift_;
Ian Rogersa55cf412014-02-27 00:31:26 -0800133 DCHECK_GE(length, minimum_length_);
Ian Rogers13735952014-10-08 12:43:28 -0700134 uint8_t* old_end = reinterpret_cast<uint8_t*>(array->GetRawData(1U << component_size_shift_,
135 minimum_length_));
136 uint8_t* new_end = reinterpret_cast<uint8_t*>(array->GetRawData(1U << component_size_shift_,
137 length));
Ian Rogersa55cf412014-02-27 00:31:26 -0800138 // Ensure space beyond original allocation is zeroed.
139 memset(old_end, 0, new_end - old_end);
Ian Rogers6fac4472014-02-25 17:01:10 -0800140 array->SetLength(length);
141 }
142
143 private:
Ian Rogersa55cf412014-02-27 00:31:26 -0800144 const int32_t minimum_length_;
Ian Rogers6fac4472014-02-25 17:01:10 -0800145 const size_t header_size_;
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700146 const size_t component_size_shift_;
Ian Rogers6fac4472014-02-25 17:01:10 -0800147
148 DISALLOW_COPY_AND_ASSIGN(SetLengthToUsableSizeVisitor);
Mathieu Chartier1febddf2013-11-20 12:33:14 -0800149};
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700150
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700151template <bool kIsInstrumented, bool kFillUsable>
Mathieu Chartier590fee92013-09-13 13:46:47 -0700152inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700153 size_t component_size_shift, gc::AllocatorType allocator_type) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800154 DCHECK(allocator_type != gc::kAllocatorTypeLOS);
Vladimir Marko20f85592015-03-19 10:07:02 +0000155 DCHECK(array_class != nullptr);
156 DCHECK(array_class->IsArrayClass());
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700157 DCHECK_EQ(array_class->GetComponentSizeShift(), component_size_shift);
158 DCHECK_EQ(array_class->GetComponentSize(), (1U << component_size_shift));
Vladimir Marko20f85592015-03-19 10:07:02 +0000159 size_t size = ComputeArraySize(component_count, component_size_shift);
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700160#ifdef __LP64__
161 // 64-bit. No size_t overflow.
162 DCHECK_NE(size, 0U);
163#else
164 // 32-bit.
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700165 if (UNLIKELY(size == 0)) {
Vladimir Marko20f85592015-03-19 10:07:02 +0000166 self->ThrowOutOfMemoryError(StringPrintf("%s of length %d would overflow",
167 PrettyDescriptor(array_class).c_str(),
168 component_count).c_str());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800169 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700170 }
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700171#endif
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700172 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers6fac4472014-02-25 17:01:10 -0800173 Array* result;
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700174 if (!kFillUsable) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800175 SetLengthVisitor visitor(component_count);
176 result = down_cast<Array*>(
177 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size,
178 allocator_type, visitor));
179 } else {
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700180 SetLengthToUsableSizeVisitor visitor(component_count,
181 DataOffset(1U << component_size_shift).SizeValue(),
182 component_size_shift);
Ian Rogers6fac4472014-02-25 17:01:10 -0800183 result = down_cast<Array*>(
184 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size,
185 allocator_type, visitor));
186 }
187 if (kIsDebugBuild && result != nullptr && Runtime::Current()->IsStarted()) {
Mathieu Chartier85801542014-02-27 18:06:26 -0800188 array_class = result->GetClass(); // In case the array class moved.
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700189 CHECK_EQ(array_class->GetComponentSize(), 1U << component_size_shift);
190 if (!kFillUsable) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800191 CHECK_EQ(result->SizeOf(), size);
192 } else {
193 CHECK_GE(result->SizeOf(), size);
194 }
195 }
196 return result;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700197}
198
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800199template<class T>
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700200inline void PrimitiveArray<T>::VisitRoots(RootVisitor* visitor) {
201 array_class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800202}
203
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700204template<typename T>
205inline PrimitiveArray<T>* PrimitiveArray<T>::Alloc(Thread* self, size_t length) {
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700206 Array* raw_array = Array::Alloc<true>(self, GetArrayClass(), length,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700207 ComponentSizeShiftWidth(sizeof(T)),
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700208 Runtime::Current()->GetHeap()->GetCurrentAllocator());
209 return down_cast<PrimitiveArray<T>*>(raw_array);
210}
211
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700212template<typename T>
213inline T PrimitiveArray<T>::Get(int32_t i) {
214 if (!CheckIsValidIndex(i)) {
215 DCHECK(Thread::Current()->IsExceptionPending());
216 return T(0);
217 }
218 return GetWithoutChecks(i);
219}
220
221template<typename T>
222inline void PrimitiveArray<T>::Set(int32_t i, T value) {
223 if (Runtime::Current()->IsActiveTransaction()) {
224 Set<true>(i, value);
225 } else {
226 Set<false>(i, value);
227 }
228}
229
230template<typename T>
231template<bool kTransactionActive, bool kCheckTransaction>
232inline void PrimitiveArray<T>::Set(int32_t i, T value) {
233 if (CheckIsValidIndex(i)) {
234 SetWithoutChecks<kTransactionActive, kCheckTransaction>(i, value);
235 } else {
236 DCHECK(Thread::Current()->IsExceptionPending());
237 }
238}
239
240template<typename T>
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700241template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700242inline void PrimitiveArray<T>::SetWithoutChecks(int32_t i, T value) {
243 if (kCheckTransaction) {
244 DCHECK_EQ(kTransactionActive, Runtime::Current()->IsActiveTransaction());
245 }
246 if (kTransactionActive) {
247 Runtime::Current()->RecordWriteArray(this, i, GetWithoutChecks(i));
248 }
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700249 DCHECK(CheckIsValidIndex<kVerifyFlags>(i));
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700250 GetData()[i] = value;
251}
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700252// Backward copy where elements are of aligned appropriately for T. Count is in T sized units.
253// Copies are guaranteed not to tear when the sizeof T is less-than 64bit.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800254template<typename T>
255static inline void ArrayBackwardCopy(T* d, const T* s, int32_t count) {
256 d += count;
257 s += count;
258 for (int32_t i = 0; i < count; ++i) {
259 d--;
260 s--;
261 *d = *s;
262 }
263}
264
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700265// Forward copy where elements are of aligned appropriately for T. Count is in T sized units.
266// Copies are guaranteed not to tear when the sizeof T is less-than 64bit.
Ian Rogers6fac4472014-02-25 17:01:10 -0800267template<typename T>
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700268static inline void ArrayForwardCopy(T* d, const T* s, int32_t count) {
269 for (int32_t i = 0; i < count; ++i) {
270 *d = *s;
271 d++;
272 s++;
273 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800274}
275
Ian Rogersef7d42f2014-01-06 12:55:46 -0800276template<class T>
Ian Rogers6fac4472014-02-25 17:01:10 -0800277inline void PrimitiveArray<T>::Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos,
278 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800279 if (UNLIKELY(count == 0)) {
280 return;
281 }
282 DCHECK_GE(dst_pos, 0);
283 DCHECK_GE(src_pos, 0);
284 DCHECK_GT(count, 0);
285 DCHECK(src != nullptr);
286 DCHECK_LT(dst_pos, GetLength());
287 DCHECK_LE(dst_pos, GetLength() - count);
288 DCHECK_LT(src_pos, src->GetLength());
289 DCHECK_LE(src_pos, src->GetLength() - count);
290
291 // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
292 // in our implementation, because they may copy byte-by-byte.
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700293 if (LIKELY(src != this)) {
294 // Memcpy ok for guaranteed non-overlapping distinct arrays.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800295 Memcpy(dst_pos, src, src_pos, count);
296 } else {
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700297 // Handle copies within the same array using the appropriate direction copy.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800298 void* dst_raw = GetRawData(sizeof(T), dst_pos);
299 const void* src_raw = src->GetRawData(sizeof(T), src_pos);
300 if (sizeof(T) == sizeof(uint8_t)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800301 uint8_t* d = reinterpret_cast<uint8_t*>(dst_raw);
302 const uint8_t* s = reinterpret_cast<const uint8_t*>(src_raw);
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700303 memmove(d, s, count);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800304 } else {
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700305 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= count);
306 if (sizeof(T) == sizeof(uint16_t)) {
307 uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
308 const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
309 if (copy_forward) {
310 ArrayForwardCopy<uint16_t>(d, s, count);
311 } else {
312 ArrayBackwardCopy<uint16_t>(d, s, count);
313 }
314 } else if (sizeof(T) == sizeof(uint32_t)) {
315 uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
316 const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
317 if (copy_forward) {
318 ArrayForwardCopy<uint32_t>(d, s, count);
319 } else {
320 ArrayBackwardCopy<uint32_t>(d, s, count);
321 }
322 } else {
323 DCHECK_EQ(sizeof(T), sizeof(uint64_t));
324 uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
325 const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
326 if (copy_forward) {
327 ArrayForwardCopy<uint64_t>(d, s, count);
328 } else {
329 ArrayBackwardCopy<uint64_t>(d, s, count);
330 }
331 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800332 }
333 }
334}
335
Ian Rogersef7d42f2014-01-06 12:55:46 -0800336template<class T>
Ian Rogers6fac4472014-02-25 17:01:10 -0800337inline void PrimitiveArray<T>::Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos,
338 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800339 if (UNLIKELY(count == 0)) {
340 return;
341 }
342 DCHECK_GE(dst_pos, 0);
343 DCHECK_GE(src_pos, 0);
344 DCHECK_GT(count, 0);
345 DCHECK(src != nullptr);
346 DCHECK_LT(dst_pos, GetLength());
347 DCHECK_LE(dst_pos, GetLength() - count);
348 DCHECK_LT(src_pos, src->GetLength());
349 DCHECK_LE(src_pos, src->GetLength() - count);
350
351 // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
352 // in our implementation, because they may copy byte-by-byte.
353 void* dst_raw = GetRawData(sizeof(T), dst_pos);
354 const void* src_raw = src->GetRawData(sizeof(T), src_pos);
355 if (sizeof(T) == sizeof(uint8_t)) {
356 memcpy(dst_raw, src_raw, count);
357 } else if (sizeof(T) == sizeof(uint16_t)) {
358 uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
359 const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
360 ArrayForwardCopy<uint16_t>(d, s, count);
361 } else if (sizeof(T) == sizeof(uint32_t)) {
362 uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
363 const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
364 ArrayForwardCopy<uint32_t>(d, s, count);
365 } else {
366 DCHECK_EQ(sizeof(T), sizeof(uint64_t));
367 uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
368 const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
369 ArrayForwardCopy<uint64_t>(d, s, count);
370 }
371}
372
Mathieu Chartierdfe02f62016-02-01 20:15:11 -0800373template<typename T, VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700374inline T PointerArray::GetElementPtrSize(uint32_t idx, size_t ptr_size) {
375 // C style casts here since we sometimes have T be a pointer, or sometimes an integer
376 // (for stack traces).
377 if (ptr_size == 8) {
Mathieu Chartierdfe02f62016-02-01 20:15:11 -0800378 return (T)static_cast<uintptr_t>(
379 AsLongArray<kVerifyFlags, kReadBarrierOption>()->GetWithoutChecks(idx));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700380 }
381 DCHECK_EQ(ptr_size, 4u);
Mathieu Chartierdfe02f62016-02-01 20:15:11 -0800382 return (T)static_cast<uintptr_t>(
383 AsIntArray<kVerifyFlags, kReadBarrierOption>()->GetWithoutChecks(idx));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700384}
385
Mathieu Chartierd329a3b2016-01-27 15:30:10 -0800386template<bool kTransactionActive, bool kUnchecked>
387inline void PointerArray::SetElementPtrSize(uint32_t idx, uint64_t element, size_t ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700388 if (ptr_size == 8) {
389 (kUnchecked ? down_cast<LongArray*>(static_cast<Object*>(this)) : AsLongArray())->
Mathieu Chartierd329a3b2016-01-27 15:30:10 -0800390 SetWithoutChecks<kTransactionActive>(idx, element);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700391 } else {
392 DCHECK_EQ(ptr_size, 4u);
Mathieu Chartierd329a3b2016-01-27 15:30:10 -0800393 DCHECK_LE(element, static_cast<uint64_t>(0xFFFFFFFFu));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700394 (kUnchecked ? down_cast<IntArray*>(static_cast<Object*>(this)) : AsIntArray())
Mathieu Chartierd329a3b2016-01-27 15:30:10 -0800395 ->SetWithoutChecks<kTransactionActive>(idx, static_cast<uint32_t>(element));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700396 }
397}
398
Mathieu Chartierd329a3b2016-01-27 15:30:10 -0800399template<bool kTransactionActive, bool kUnchecked, typename T>
400inline void PointerArray::SetElementPtrSize(uint32_t idx, T* element, size_t ptr_size) {
Mathieu Chartier1bbfab62016-01-27 16:37:19 -0800401 SetElementPtrSize<kTransactionActive, kUnchecked>(idx,
402 reinterpret_cast<uintptr_t>(element),
403 ptr_size);
Mathieu Chartierd329a3b2016-01-27 15:30:10 -0800404}
405
Mathieu Chartierdfe02f62016-02-01 20:15:11 -0800406template <VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption, typename Visitor>
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800407inline void PointerArray::Fixup(mirror::PointerArray* dest,
408 size_t pointer_size,
409 const Visitor& visitor) {
410 for (size_t i = 0, count = GetLength(); i < count; ++i) {
Mathieu Chartierdfe02f62016-02-01 20:15:11 -0800411 void* ptr = GetElementPtrSize<void*, kVerifyFlags, kReadBarrierOption>(i, pointer_size);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800412 void* new_ptr = visitor(ptr);
413 if (ptr != new_ptr) {
414 dest->SetElementPtrSize<false, true>(i, new_ptr, pointer_size);
415 }
416 }
417}
418
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800419} // namespace mirror
420} // namespace art
421
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700422#endif // ART_RUNTIME_MIRROR_ARRAY_INL_H_