blob: 9696dcd75480fc9938aa942e6348fcba6b0f73f3 [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
Vladimir Marko20f85592015-03-19 10:07:02 +000067static inline size_t ComputeArraySize(int32_t component_count, size_t component_size_shift) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070068 DCHECK_GE(component_count, 0);
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070069
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070070 size_t component_size = 1U << component_size_shift;
Hiroshi Yamauchiaa866f52014-03-21 16:18:30 -070071 size_t header_size = Array::DataOffset(component_size).SizeValue();
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070072 size_t data_size = static_cast<size_t>(component_count) << component_size_shift;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070073 size_t size = header_size + data_size;
74
Vladimir Marko20f85592015-03-19 10:07:02 +000075 // Check for size_t overflow if this was an unreasonable request
76 // but let the caller throw OutOfMemoryError.
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070077#ifdef __LP64__
78 // 64-bit. No overflow as component_count is 32-bit and the maximum
79 // component size is 8.
80 DCHECK_LE((1U << component_size_shift), 8U);
81#else
82 // 32-bit.
83 DCHECK_NE(header_size, 0U);
84 DCHECK_EQ(RoundUp(header_size, component_size), header_size);
85 // The array length limit (exclusive).
86 const size_t length_limit = (0U - header_size) >> component_size_shift;
87 if (UNLIKELY(length_limit <= static_cast<size_t>(component_count))) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070088 return 0; // failure
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070089 }
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070090#endif
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070091 return size;
92}
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070093
Ian Rogers6fac4472014-02-25 17:01:10 -080094// Used for setting the array length in the allocation code path to ensure it is guarded by a
95// StoreStore fence.
Mathieu Chartier1febddf2013-11-20 12:33:14 -080096class SetLengthVisitor {
97 public:
98 explicit SetLengthVisitor(int32_t length) : length_(length) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070099 }
Mathieu Chartier1febddf2013-11-20 12:33:14 -0800100
Ian Rogers6fac4472014-02-25 17:01:10 -0800101 void operator()(Object* obj, size_t usable_size) const
102 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
103 UNUSED(usable_size);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800104 // Avoid AsArray as object is not yet in live bitmap or allocation stack.
105 Array* array = down_cast<Array*>(obj);
106 // DCHECK(array->IsArrayInstance());
Mathieu Chartier1febddf2013-11-20 12:33:14 -0800107 array->SetLength(length_);
108 }
109
110 private:
111 const int32_t length_;
Ian Rogers6fac4472014-02-25 17:01:10 -0800112
113 DISALLOW_COPY_AND_ASSIGN(SetLengthVisitor);
114};
115
116// Similar to SetLengthVisitor, used for setting the array length to fill the usable size of an
117// array.
118class SetLengthToUsableSizeVisitor {
119 public:
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700120 SetLengthToUsableSizeVisitor(int32_t min_length, size_t header_size,
121 size_t component_size_shift) :
122 minimum_length_(min_length), header_size_(header_size),
123 component_size_shift_(component_size_shift) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800124 }
125
126 void operator()(Object* obj, size_t usable_size) const
127 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
128 // Avoid AsArray as object is not yet in live bitmap or allocation stack.
129 Array* array = down_cast<Array*>(obj);
Ian Rogers6fac4472014-02-25 17:01:10 -0800130 // DCHECK(array->IsArrayInstance());
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700131 int32_t length = (usable_size - header_size_) >> component_size_shift_;
Ian Rogersa55cf412014-02-27 00:31:26 -0800132 DCHECK_GE(length, minimum_length_);
Ian Rogers13735952014-10-08 12:43:28 -0700133 uint8_t* old_end = reinterpret_cast<uint8_t*>(array->GetRawData(1U << component_size_shift_,
134 minimum_length_));
135 uint8_t* new_end = reinterpret_cast<uint8_t*>(array->GetRawData(1U << component_size_shift_,
136 length));
Ian Rogersa55cf412014-02-27 00:31:26 -0800137 // Ensure space beyond original allocation is zeroed.
138 memset(old_end, 0, new_end - old_end);
Ian Rogers6fac4472014-02-25 17:01:10 -0800139 array->SetLength(length);
140 }
141
142 private:
Ian Rogersa55cf412014-02-27 00:31:26 -0800143 const int32_t minimum_length_;
Ian Rogers6fac4472014-02-25 17:01:10 -0800144 const size_t header_size_;
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700145 const size_t component_size_shift_;
Ian Rogers6fac4472014-02-25 17:01:10 -0800146
147 DISALLOW_COPY_AND_ASSIGN(SetLengthToUsableSizeVisitor);
Mathieu Chartier1febddf2013-11-20 12:33:14 -0800148};
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700149
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700150template <bool kIsInstrumented, bool kFillUsable>
Mathieu Chartier590fee92013-09-13 13:46:47 -0700151inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700152 size_t component_size_shift, gc::AllocatorType allocator_type) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800153 DCHECK(allocator_type != gc::kAllocatorTypeLOS);
Vladimir Marko20f85592015-03-19 10:07:02 +0000154 DCHECK(array_class != nullptr);
155 DCHECK(array_class->IsArrayClass());
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700156 DCHECK_EQ(array_class->GetComponentSizeShift(), component_size_shift);
157 DCHECK_EQ(array_class->GetComponentSize(), (1U << component_size_shift));
Vladimir Marko20f85592015-03-19 10:07:02 +0000158 size_t size = ComputeArraySize(component_count, component_size_shift);
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700159#ifdef __LP64__
160 // 64-bit. No size_t overflow.
161 DCHECK_NE(size, 0U);
162#else
163 // 32-bit.
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700164 if (UNLIKELY(size == 0)) {
Vladimir Marko20f85592015-03-19 10:07:02 +0000165 self->ThrowOutOfMemoryError(StringPrintf("%s of length %d would overflow",
166 PrettyDescriptor(array_class).c_str(),
167 component_count).c_str());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800168 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700169 }
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700170#endif
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700171 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers6fac4472014-02-25 17:01:10 -0800172 Array* result;
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700173 if (!kFillUsable) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800174 SetLengthVisitor visitor(component_count);
175 result = down_cast<Array*>(
176 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size,
177 allocator_type, visitor));
178 } else {
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700179 SetLengthToUsableSizeVisitor visitor(component_count,
180 DataOffset(1U << component_size_shift).SizeValue(),
181 component_size_shift);
Ian Rogers6fac4472014-02-25 17:01:10 -0800182 result = down_cast<Array*>(
183 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size,
184 allocator_type, visitor));
185 }
186 if (kIsDebugBuild && result != nullptr && Runtime::Current()->IsStarted()) {
Mathieu Chartier85801542014-02-27 18:06:26 -0800187 array_class = result->GetClass(); // In case the array class moved.
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700188 CHECK_EQ(array_class->GetComponentSize(), 1U << component_size_shift);
189 if (!kFillUsable) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800190 CHECK_EQ(result->SizeOf(), size);
191 } else {
192 CHECK_GE(result->SizeOf(), size);
193 }
194 }
195 return result;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700196}
197
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800198template<class T>
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700199inline void PrimitiveArray<T>::VisitRoots(RootVisitor* visitor) {
200 array_class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800201}
202
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700203template<typename T>
204inline PrimitiveArray<T>* PrimitiveArray<T>::Alloc(Thread* self, size_t length) {
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700205 Array* raw_array = Array::Alloc<true>(self, GetArrayClass(), length,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700206 ComponentSizeShiftWidth(sizeof(T)),
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700207 Runtime::Current()->GetHeap()->GetCurrentAllocator());
208 return down_cast<PrimitiveArray<T>*>(raw_array);
209}
210
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700211template<typename T>
212inline T PrimitiveArray<T>::Get(int32_t i) {
213 if (!CheckIsValidIndex(i)) {
214 DCHECK(Thread::Current()->IsExceptionPending());
215 return T(0);
216 }
217 return GetWithoutChecks(i);
218}
219
220template<typename T>
221inline void PrimitiveArray<T>::Set(int32_t i, T value) {
222 if (Runtime::Current()->IsActiveTransaction()) {
223 Set<true>(i, value);
224 } else {
225 Set<false>(i, value);
226 }
227}
228
229template<typename T>
230template<bool kTransactionActive, bool kCheckTransaction>
231inline void PrimitiveArray<T>::Set(int32_t i, T value) {
232 if (CheckIsValidIndex(i)) {
233 SetWithoutChecks<kTransactionActive, kCheckTransaction>(i, value);
234 } else {
235 DCHECK(Thread::Current()->IsExceptionPending());
236 }
237}
238
239template<typename T>
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700240template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700241inline void PrimitiveArray<T>::SetWithoutChecks(int32_t i, T value) {
242 if (kCheckTransaction) {
243 DCHECK_EQ(kTransactionActive, Runtime::Current()->IsActiveTransaction());
244 }
245 if (kTransactionActive) {
246 Runtime::Current()->RecordWriteArray(this, i, GetWithoutChecks(i));
247 }
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700248 DCHECK(CheckIsValidIndex<kVerifyFlags>(i));
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700249 GetData()[i] = value;
250}
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700251// Backward copy where elements are of aligned appropriately for T. Count is in T sized units.
252// Copies are guaranteed not to tear when the sizeof T is less-than 64bit.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800253template<typename T>
254static inline void ArrayBackwardCopy(T* d, const T* s, int32_t count) {
255 d += count;
256 s += count;
257 for (int32_t i = 0; i < count; ++i) {
258 d--;
259 s--;
260 *d = *s;
261 }
262}
263
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700264// Forward copy where elements are of aligned appropriately for T. Count is in T sized units.
265// Copies are guaranteed not to tear when the sizeof T is less-than 64bit.
Ian Rogers6fac4472014-02-25 17:01:10 -0800266template<typename T>
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700267static inline void ArrayForwardCopy(T* d, const T* s, int32_t count) {
268 for (int32_t i = 0; i < count; ++i) {
269 *d = *s;
270 d++;
271 s++;
272 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800273}
274
Ian Rogersef7d42f2014-01-06 12:55:46 -0800275template<class T>
Ian Rogers6fac4472014-02-25 17:01:10 -0800276inline void PrimitiveArray<T>::Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos,
277 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800278 if (UNLIKELY(count == 0)) {
279 return;
280 }
281 DCHECK_GE(dst_pos, 0);
282 DCHECK_GE(src_pos, 0);
283 DCHECK_GT(count, 0);
284 DCHECK(src != nullptr);
285 DCHECK_LT(dst_pos, GetLength());
286 DCHECK_LE(dst_pos, GetLength() - count);
287 DCHECK_LT(src_pos, src->GetLength());
288 DCHECK_LE(src_pos, src->GetLength() - count);
289
290 // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
291 // in our implementation, because they may copy byte-by-byte.
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700292 if (LIKELY(src != this)) {
293 // Memcpy ok for guaranteed non-overlapping distinct arrays.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800294 Memcpy(dst_pos, src, src_pos, count);
295 } else {
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700296 // Handle copies within the same array using the appropriate direction copy.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800297 void* dst_raw = GetRawData(sizeof(T), dst_pos);
298 const void* src_raw = src->GetRawData(sizeof(T), src_pos);
299 if (sizeof(T) == sizeof(uint8_t)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800300 uint8_t* d = reinterpret_cast<uint8_t*>(dst_raw);
301 const uint8_t* s = reinterpret_cast<const uint8_t*>(src_raw);
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700302 memmove(d, s, count);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800303 } else {
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700304 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= count);
305 if (sizeof(T) == sizeof(uint16_t)) {
306 uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
307 const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
308 if (copy_forward) {
309 ArrayForwardCopy<uint16_t>(d, s, count);
310 } else {
311 ArrayBackwardCopy<uint16_t>(d, s, count);
312 }
313 } else if (sizeof(T) == sizeof(uint32_t)) {
314 uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
315 const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
316 if (copy_forward) {
317 ArrayForwardCopy<uint32_t>(d, s, count);
318 } else {
319 ArrayBackwardCopy<uint32_t>(d, s, count);
320 }
321 } else {
322 DCHECK_EQ(sizeof(T), sizeof(uint64_t));
323 uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
324 const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
325 if (copy_forward) {
326 ArrayForwardCopy<uint64_t>(d, s, count);
327 } else {
328 ArrayBackwardCopy<uint64_t>(d, s, count);
329 }
330 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800331 }
332 }
333}
334
Ian Rogersef7d42f2014-01-06 12:55:46 -0800335template<class T>
Ian Rogers6fac4472014-02-25 17:01:10 -0800336inline void PrimitiveArray<T>::Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos,
337 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800338 if (UNLIKELY(count == 0)) {
339 return;
340 }
341 DCHECK_GE(dst_pos, 0);
342 DCHECK_GE(src_pos, 0);
343 DCHECK_GT(count, 0);
344 DCHECK(src != nullptr);
345 DCHECK_LT(dst_pos, GetLength());
346 DCHECK_LE(dst_pos, GetLength() - count);
347 DCHECK_LT(src_pos, src->GetLength());
348 DCHECK_LE(src_pos, src->GetLength() - count);
349
350 // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
351 // in our implementation, because they may copy byte-by-byte.
352 void* dst_raw = GetRawData(sizeof(T), dst_pos);
353 const void* src_raw = src->GetRawData(sizeof(T), src_pos);
354 if (sizeof(T) == sizeof(uint8_t)) {
355 memcpy(dst_raw, src_raw, count);
356 } else if (sizeof(T) == sizeof(uint16_t)) {
357 uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
358 const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
359 ArrayForwardCopy<uint16_t>(d, s, count);
360 } else if (sizeof(T) == sizeof(uint32_t)) {
361 uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
362 const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
363 ArrayForwardCopy<uint32_t>(d, s, count);
364 } else {
365 DCHECK_EQ(sizeof(T), sizeof(uint64_t));
366 uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
367 const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
368 ArrayForwardCopy<uint64_t>(d, s, count);
369 }
370}
371
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800372} // namespace mirror
373} // namespace art
374
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700375#endif // ART_RUNTIME_MIRROR_ARRAY_INL_H_