blob: 4dddd38b8057164c8a9d204e60da994c62f2e00d [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
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080022#include "base/stringprintf.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "class.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070024#include "gc/heap-inl.h"
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070025#include "thread.h"
26#include "utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027
28namespace art {
29namespace mirror {
30
Mingyao Yang98d1cc82014-05-15 17:02:16 -070031inline uint32_t Array::ClassSize() {
32 uint32_t vtable_entries = Object::kVTableLength;
Fred Shih37f05ef2014-07-16 18:38:08 -070033 return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 0, 0);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070034}
35
Hiroshi Yamauchi6e83c172014-05-01 21:25:41 -070036template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
Ian Rogersef7d42f2014-01-06 12:55:46 -080037inline size_t Array::SizeOf() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038 // This is safe from overflow because the array was already allocated, so we know it's sane.
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070039 size_t component_size_shift = GetClass<kVerifyFlags, kReadBarrierOption>()->
40 template GetComponentSizeShift<kReadBarrierOption>();
Mathieu Chartier4e305412014-02-19 10:54:44 -080041 // Don't need to check this since we already check this in GetClass.
42 int32_t component_count =
43 GetLength<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>();
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070044 size_t header_size = DataOffset(1U << component_size_shift).SizeValue();
45 size_t data_size = component_count << component_size_shift;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046 return header_size + data_size;
47}
48
Ian Rogers7e70b002014-10-08 11:47:24 -070049inline MemberOffset Array::DataOffset(size_t component_size) {
50 DCHECK(IsPowerOfTwo(component_size)) << component_size;
51 size_t data_offset = RoundUp(OFFSETOF_MEMBER(Array, first_element_), component_size);
52 DCHECK_EQ(RoundUp(data_offset, component_size), data_offset)
53 << "Array data offset isn't aligned with component size";
54 return MemberOffset(data_offset);
55}
56
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070057template<VerifyObjectFlags kVerifyFlags>
58inline bool Array::CheckIsValidIndex(int32_t index) {
59 if (UNLIKELY(static_cast<uint32_t>(index) >=
60 static_cast<uint32_t>(GetLength<kVerifyFlags>()))) {
61 ThrowArrayIndexOutOfBoundsException(index);
62 return false;
63 }
64 return true;
65}
66
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070067static inline size_t ComputeArraySize(Thread* self, Class* array_class, int32_t component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070068 size_t component_size_shift)
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070069 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070070 DCHECK(array_class != NULL);
71 DCHECK_GE(component_count, 0);
72 DCHECK(array_class->IsArrayClass());
73
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070074 size_t component_size = 1U << component_size_shift;
Hiroshi Yamauchiaa866f52014-03-21 16:18:30 -070075 size_t header_size = Array::DataOffset(component_size).SizeValue();
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070076 size_t data_size = static_cast<size_t>(component_count) << component_size_shift;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070077 size_t size = header_size + data_size;
78
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070079 // Check for size_t overflow and throw OutOfMemoryError if this was
80 // an unreasonable request.
81#ifdef __LP64__
82 // 64-bit. No overflow as component_count is 32-bit and the maximum
83 // component size is 8.
84 DCHECK_LE((1U << component_size_shift), 8U);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070085 UNUSED(self);
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070086#else
87 // 32-bit.
88 DCHECK_NE(header_size, 0U);
89 DCHECK_EQ(RoundUp(header_size, component_size), header_size);
90 // The array length limit (exclusive).
91 const size_t length_limit = (0U - header_size) >> component_size_shift;
92 if (UNLIKELY(length_limit <= static_cast<size_t>(component_count))) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070093 self->ThrowOutOfMemoryError(StringPrintf("%s of length %d would overflow",
94 PrettyDescriptor(array_class).c_str(),
95 component_count).c_str());
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070096 return 0; // failure
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070097 }
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070098#endif
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070099 return size;
100}
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700101
Ian Rogers6fac4472014-02-25 17:01:10 -0800102// Used for setting the array length in the allocation code path to ensure it is guarded by a
103// StoreStore fence.
Mathieu Chartier1febddf2013-11-20 12:33:14 -0800104class SetLengthVisitor {
105 public:
106 explicit SetLengthVisitor(int32_t length) : length_(length) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700107 }
Mathieu Chartier1febddf2013-11-20 12:33:14 -0800108
Ian Rogers6fac4472014-02-25 17:01:10 -0800109 void operator()(Object* obj, size_t usable_size) const
110 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
111 UNUSED(usable_size);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800112 // Avoid AsArray as object is not yet in live bitmap or allocation stack.
113 Array* array = down_cast<Array*>(obj);
114 // DCHECK(array->IsArrayInstance());
Mathieu Chartier1febddf2013-11-20 12:33:14 -0800115 array->SetLength(length_);
116 }
117
118 private:
119 const int32_t length_;
Ian Rogers6fac4472014-02-25 17:01:10 -0800120
121 DISALLOW_COPY_AND_ASSIGN(SetLengthVisitor);
122};
123
124// Similar to SetLengthVisitor, used for setting the array length to fill the usable size of an
125// array.
126class SetLengthToUsableSizeVisitor {
127 public:
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700128 SetLengthToUsableSizeVisitor(int32_t min_length, size_t header_size,
129 size_t component_size_shift) :
130 minimum_length_(min_length), header_size_(header_size),
131 component_size_shift_(component_size_shift) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800132 }
133
134 void operator()(Object* obj, size_t usable_size) const
135 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
136 // Avoid AsArray as object is not yet in live bitmap or allocation stack.
137 Array* array = down_cast<Array*>(obj);
Ian Rogers6fac4472014-02-25 17:01:10 -0800138 // DCHECK(array->IsArrayInstance());
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700139 int32_t length = (usable_size - header_size_) >> component_size_shift_;
Ian Rogersa55cf412014-02-27 00:31:26 -0800140 DCHECK_GE(length, minimum_length_);
Ian Rogers13735952014-10-08 12:43:28 -0700141 uint8_t* old_end = reinterpret_cast<uint8_t*>(array->GetRawData(1U << component_size_shift_,
142 minimum_length_));
143 uint8_t* new_end = reinterpret_cast<uint8_t*>(array->GetRawData(1U << component_size_shift_,
144 length));
Ian Rogersa55cf412014-02-27 00:31:26 -0800145 // Ensure space beyond original allocation is zeroed.
146 memset(old_end, 0, new_end - old_end);
Ian Rogers6fac4472014-02-25 17:01:10 -0800147 array->SetLength(length);
148 }
149
150 private:
Ian Rogersa55cf412014-02-27 00:31:26 -0800151 const int32_t minimum_length_;
Ian Rogers6fac4472014-02-25 17:01:10 -0800152 const size_t header_size_;
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700153 const size_t component_size_shift_;
Ian Rogers6fac4472014-02-25 17:01:10 -0800154
155 DISALLOW_COPY_AND_ASSIGN(SetLengthToUsableSizeVisitor);
Mathieu Chartier1febddf2013-11-20 12:33:14 -0800156};
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700157
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700158template <bool kIsInstrumented, bool kFillUsable>
Mathieu Chartier590fee92013-09-13 13:46:47 -0700159inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700160 size_t component_size_shift, gc::AllocatorType allocator_type) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800161 DCHECK(allocator_type != gc::kAllocatorTypeLOS);
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700162 DCHECK_EQ(array_class->GetComponentSizeShift(), component_size_shift);
163 DCHECK_EQ(array_class->GetComponentSize(), (1U << component_size_shift));
164 size_t size = ComputeArraySize(self, array_class, component_count, component_size_shift);
165#ifdef __LP64__
166 // 64-bit. No size_t overflow.
167 DCHECK_NE(size, 0U);
168#else
169 // 32-bit.
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700170 if (UNLIKELY(size == 0)) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800171 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700172 }
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700173#endif
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700174 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers6fac4472014-02-25 17:01:10 -0800175 Array* result;
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700176 if (!kFillUsable) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800177 SetLengthVisitor visitor(component_count);
178 result = down_cast<Array*>(
179 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size,
180 allocator_type, visitor));
181 } else {
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700182 SetLengthToUsableSizeVisitor visitor(component_count,
183 DataOffset(1U << component_size_shift).SizeValue(),
184 component_size_shift);
Ian Rogers6fac4472014-02-25 17:01:10 -0800185 result = down_cast<Array*>(
186 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size,
187 allocator_type, visitor));
188 }
189 if (kIsDebugBuild && result != nullptr && Runtime::Current()->IsStarted()) {
Mathieu Chartier85801542014-02-27 18:06:26 -0800190 array_class = result->GetClass(); // In case the array class moved.
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700191 CHECK_EQ(array_class->GetComponentSize(), 1U << component_size_shift);
192 if (!kFillUsable) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800193 CHECK_EQ(result->SizeOf(), size);
194 } else {
195 CHECK_GE(result->SizeOf(), size);
196 }
197 }
198 return result;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700199}
200
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800201template<class T>
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800202inline void PrimitiveArray<T>::VisitRoots(RootCallback* callback, void* arg) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700203 if (!array_class_.IsNull()) {
204 array_class_.VisitRoot(callback, arg, 0, kRootStickyClass);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800205 }
206}
207
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700208template<typename T>
209inline PrimitiveArray<T>* PrimitiveArray<T>::Alloc(Thread* self, size_t length) {
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700210 Array* raw_array = Array::Alloc<true>(self, GetArrayClass(), length,
211 ComponentSizeShiftWidth<sizeof(T)>(),
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700212 Runtime::Current()->GetHeap()->GetCurrentAllocator());
213 return down_cast<PrimitiveArray<T>*>(raw_array);
214}
215
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700216template<typename T>
217inline T PrimitiveArray<T>::Get(int32_t i) {
218 if (!CheckIsValidIndex(i)) {
219 DCHECK(Thread::Current()->IsExceptionPending());
220 return T(0);
221 }
222 return GetWithoutChecks(i);
223}
224
225template<typename T>
226inline void PrimitiveArray<T>::Set(int32_t i, T value) {
227 if (Runtime::Current()->IsActiveTransaction()) {
228 Set<true>(i, value);
229 } else {
230 Set<false>(i, value);
231 }
232}
233
234template<typename T>
235template<bool kTransactionActive, bool kCheckTransaction>
236inline void PrimitiveArray<T>::Set(int32_t i, T value) {
237 if (CheckIsValidIndex(i)) {
238 SetWithoutChecks<kTransactionActive, kCheckTransaction>(i, value);
239 } else {
240 DCHECK(Thread::Current()->IsExceptionPending());
241 }
242}
243
244template<typename T>
245template<bool kTransactionActive, bool kCheckTransaction>
246inline void PrimitiveArray<T>::SetWithoutChecks(int32_t i, T value) {
247 if (kCheckTransaction) {
248 DCHECK_EQ(kTransactionActive, Runtime::Current()->IsActiveTransaction());
249 }
250 if (kTransactionActive) {
251 Runtime::Current()->RecordWriteArray(this, i, GetWithoutChecks(i));
252 }
253 DCHECK(CheckIsValidIndex(i));
254 GetData()[i] = value;
255}
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700256// Backward copy where elements are of aligned appropriately for T. Count is in T sized units.
257// Copies are guaranteed not to tear when the sizeof T is less-than 64bit.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800258template<typename T>
259static inline void ArrayBackwardCopy(T* d, const T* s, int32_t count) {
260 d += count;
261 s += count;
262 for (int32_t i = 0; i < count; ++i) {
263 d--;
264 s--;
265 *d = *s;
266 }
267}
268
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700269// Forward copy where elements are of aligned appropriately for T. Count is in T sized units.
270// Copies are guaranteed not to tear when the sizeof T is less-than 64bit.
Ian Rogers6fac4472014-02-25 17:01:10 -0800271template<typename T>
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700272static inline void ArrayForwardCopy(T* d, const T* s, int32_t count) {
273 for (int32_t i = 0; i < count; ++i) {
274 *d = *s;
275 d++;
276 s++;
277 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800278}
279
Ian Rogersef7d42f2014-01-06 12:55:46 -0800280template<class T>
Ian Rogers6fac4472014-02-25 17:01:10 -0800281inline void PrimitiveArray<T>::Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos,
282 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800283 if (UNLIKELY(count == 0)) {
284 return;
285 }
286 DCHECK_GE(dst_pos, 0);
287 DCHECK_GE(src_pos, 0);
288 DCHECK_GT(count, 0);
289 DCHECK(src != nullptr);
290 DCHECK_LT(dst_pos, GetLength());
291 DCHECK_LE(dst_pos, GetLength() - count);
292 DCHECK_LT(src_pos, src->GetLength());
293 DCHECK_LE(src_pos, src->GetLength() - count);
294
295 // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
296 // in our implementation, because they may copy byte-by-byte.
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700297 if (LIKELY(src != this)) {
298 // Memcpy ok for guaranteed non-overlapping distinct arrays.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800299 Memcpy(dst_pos, src, src_pos, count);
300 } else {
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700301 // Handle copies within the same array using the appropriate direction copy.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800302 void* dst_raw = GetRawData(sizeof(T), dst_pos);
303 const void* src_raw = src->GetRawData(sizeof(T), src_pos);
304 if (sizeof(T) == sizeof(uint8_t)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800305 uint8_t* d = reinterpret_cast<uint8_t*>(dst_raw);
306 const uint8_t* s = reinterpret_cast<const uint8_t*>(src_raw);
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700307 memmove(d, s, count);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800308 } else {
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700309 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= count);
310 if (sizeof(T) == sizeof(uint16_t)) {
311 uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
312 const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
313 if (copy_forward) {
314 ArrayForwardCopy<uint16_t>(d, s, count);
315 } else {
316 ArrayBackwardCopy<uint16_t>(d, s, count);
317 }
318 } else if (sizeof(T) == sizeof(uint32_t)) {
319 uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
320 const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
321 if (copy_forward) {
322 ArrayForwardCopy<uint32_t>(d, s, count);
323 } else {
324 ArrayBackwardCopy<uint32_t>(d, s, count);
325 }
326 } else {
327 DCHECK_EQ(sizeof(T), sizeof(uint64_t));
328 uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
329 const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
330 if (copy_forward) {
331 ArrayForwardCopy<uint64_t>(d, s, count);
332 } else {
333 ArrayBackwardCopy<uint64_t>(d, s, count);
334 }
335 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800336 }
337 }
338}
339
Ian Rogersef7d42f2014-01-06 12:55:46 -0800340template<class T>
Ian Rogers6fac4472014-02-25 17:01:10 -0800341inline void PrimitiveArray<T>::Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos,
342 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800343 if (UNLIKELY(count == 0)) {
344 return;
345 }
346 DCHECK_GE(dst_pos, 0);
347 DCHECK_GE(src_pos, 0);
348 DCHECK_GT(count, 0);
349 DCHECK(src != nullptr);
350 DCHECK_LT(dst_pos, GetLength());
351 DCHECK_LE(dst_pos, GetLength() - count);
352 DCHECK_LT(src_pos, src->GetLength());
353 DCHECK_LE(src_pos, src->GetLength() - count);
354
355 // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
356 // in our implementation, because they may copy byte-by-byte.
357 void* dst_raw = GetRawData(sizeof(T), dst_pos);
358 const void* src_raw = src->GetRawData(sizeof(T), src_pos);
359 if (sizeof(T) == sizeof(uint8_t)) {
360 memcpy(dst_raw, src_raw, count);
361 } else if (sizeof(T) == sizeof(uint16_t)) {
362 uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
363 const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
364 ArrayForwardCopy<uint16_t>(d, s, count);
365 } else if (sizeof(T) == sizeof(uint32_t)) {
366 uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
367 const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
368 ArrayForwardCopy<uint32_t>(d, s, count);
369 } else {
370 DCHECK_EQ(sizeof(T), sizeof(uint64_t));
371 uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
372 const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
373 ArrayForwardCopy<uint64_t>(d, s, count);
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_ARRAY_INL_H_