blob: 6582226dd542379d9a19caf97a18d3b20a0a6fc7 [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
22#include "class.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070023#include "gc/heap-inl.h"
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070024#include "thread.h"
25#include "utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026
27namespace art {
28namespace mirror {
29
Mingyao Yang98d1cc82014-05-15 17:02:16 -070030inline uint32_t Array::ClassSize() {
31 uint32_t vtable_entries = Object::kVTableLength;
Fred Shih37f05ef2014-07-16 18:38:08 -070032 return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 0, 0);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070033}
34
Hiroshi Yamauchi6e83c172014-05-01 21:25:41 -070035template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
Ian Rogersef7d42f2014-01-06 12:55:46 -080036inline size_t Array::SizeOf() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037 // This is safe from overflow because the array was already allocated, so we know it's sane.
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070038 size_t component_size_shift = GetClass<kVerifyFlags, kReadBarrierOption>()->
39 template GetComponentSizeShift<kReadBarrierOption>();
Mathieu Chartier4e305412014-02-19 10:54:44 -080040 // Don't need to check this since we already check this in GetClass.
41 int32_t component_count =
42 GetLength<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>();
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070043 size_t header_size = DataOffset(1U << component_size_shift).SizeValue();
44 size_t data_size = component_count << component_size_shift;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045 return header_size + data_size;
46}
47
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070048template<VerifyObjectFlags kVerifyFlags>
49inline bool Array::CheckIsValidIndex(int32_t index) {
50 if (UNLIKELY(static_cast<uint32_t>(index) >=
51 static_cast<uint32_t>(GetLength<kVerifyFlags>()))) {
52 ThrowArrayIndexOutOfBoundsException(index);
53 return false;
54 }
55 return true;
56}
57
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070058static inline size_t ComputeArraySize(Thread* self, Class* array_class, int32_t component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070059 size_t component_size_shift)
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070060 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070061 DCHECK(array_class != NULL);
62 DCHECK_GE(component_count, 0);
63 DCHECK(array_class->IsArrayClass());
64
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070065 size_t component_size = 1U << component_size_shift;
Hiroshi Yamauchiaa866f52014-03-21 16:18:30 -070066 size_t header_size = Array::DataOffset(component_size).SizeValue();
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070067 size_t data_size = static_cast<size_t>(component_count) << component_size_shift;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070068 size_t size = header_size + data_size;
69
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070070 // Check for size_t overflow and throw OutOfMemoryError if this was
71 // an unreasonable request.
72#ifdef __LP64__
73 // 64-bit. No overflow as component_count is 32-bit and the maximum
74 // component size is 8.
75 DCHECK_LE((1U << component_size_shift), 8U);
76#else
77 // 32-bit.
78 DCHECK_NE(header_size, 0U);
79 DCHECK_EQ(RoundUp(header_size, component_size), header_size);
80 // The array length limit (exclusive).
81 const size_t length_limit = (0U - header_size) >> component_size_shift;
82 if (UNLIKELY(length_limit <= static_cast<size_t>(component_count))) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070083 self->ThrowOutOfMemoryError(StringPrintf("%s of length %d would overflow",
84 PrettyDescriptor(array_class).c_str(),
85 component_count).c_str());
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070086 return 0; // failure
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070087 }
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070088#endif
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070089 return size;
90}
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070091
Ian Rogers6fac4472014-02-25 17:01:10 -080092// Used for setting the array length in the allocation code path to ensure it is guarded by a
93// StoreStore fence.
Mathieu Chartier1febddf2013-11-20 12:33:14 -080094class SetLengthVisitor {
95 public:
96 explicit SetLengthVisitor(int32_t length) : length_(length) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070097 }
Mathieu Chartier1febddf2013-11-20 12:33:14 -080098
Ian Rogers6fac4472014-02-25 17:01:10 -080099 void operator()(Object* obj, size_t usable_size) const
100 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
101 UNUSED(usable_size);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800102 // Avoid AsArray as object is not yet in live bitmap or allocation stack.
103 Array* array = down_cast<Array*>(obj);
104 // DCHECK(array->IsArrayInstance());
Mathieu Chartier1febddf2013-11-20 12:33:14 -0800105 array->SetLength(length_);
106 }
107
108 private:
109 const int32_t length_;
Ian Rogers6fac4472014-02-25 17:01:10 -0800110
111 DISALLOW_COPY_AND_ASSIGN(SetLengthVisitor);
112};
113
114// Similar to SetLengthVisitor, used for setting the array length to fill the usable size of an
115// array.
116class SetLengthToUsableSizeVisitor {
117 public:
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700118 SetLengthToUsableSizeVisitor(int32_t min_length, size_t header_size,
119 size_t component_size_shift) :
120 minimum_length_(min_length), header_size_(header_size),
121 component_size_shift_(component_size_shift) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800122 }
123
124 void operator()(Object* obj, size_t usable_size) const
125 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
126 // Avoid AsArray as object is not yet in live bitmap or allocation stack.
127 Array* array = down_cast<Array*>(obj);
Ian Rogers6fac4472014-02-25 17:01:10 -0800128 // DCHECK(array->IsArrayInstance());
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700129 int32_t length = (usable_size - header_size_) >> component_size_shift_;
Ian Rogersa55cf412014-02-27 00:31:26 -0800130 DCHECK_GE(length, minimum_length_);
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700131 byte* old_end = reinterpret_cast<byte*>(array->GetRawData(1U << component_size_shift_,
132 minimum_length_));
133 byte* new_end = reinterpret_cast<byte*>(array->GetRawData(1U << component_size_shift_,
134 length));
Ian Rogersa55cf412014-02-27 00:31:26 -0800135 // Ensure space beyond original allocation is zeroed.
136 memset(old_end, 0, new_end - old_end);
Ian Rogers6fac4472014-02-25 17:01:10 -0800137 array->SetLength(length);
138 }
139
140 private:
Ian Rogersa55cf412014-02-27 00:31:26 -0800141 const int32_t minimum_length_;
Ian Rogers6fac4472014-02-25 17:01:10 -0800142 const size_t header_size_;
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700143 const size_t component_size_shift_;
Ian Rogers6fac4472014-02-25 17:01:10 -0800144
145 DISALLOW_COPY_AND_ASSIGN(SetLengthToUsableSizeVisitor);
Mathieu Chartier1febddf2013-11-20 12:33:14 -0800146};
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700147
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700148template <bool kIsInstrumented, bool kFillUsable>
Mathieu Chartier590fee92013-09-13 13:46:47 -0700149inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700150 size_t component_size_shift, gc::AllocatorType allocator_type) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800151 DCHECK(allocator_type != gc::kAllocatorTypeLOS);
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700152 DCHECK_EQ(array_class->GetComponentSizeShift(), component_size_shift);
153 DCHECK_EQ(array_class->GetComponentSize(), (1U << component_size_shift));
154 size_t size = ComputeArraySize(self, array_class, component_count, component_size_shift);
155#ifdef __LP64__
156 // 64-bit. No size_t overflow.
157 DCHECK_NE(size, 0U);
158#else
159 // 32-bit.
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700160 if (UNLIKELY(size == 0)) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800161 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700162 }
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700163#endif
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700164 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers6fac4472014-02-25 17:01:10 -0800165 Array* result;
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700166 if (!kFillUsable) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800167 SetLengthVisitor visitor(component_count);
168 result = down_cast<Array*>(
169 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size,
170 allocator_type, visitor));
171 } else {
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700172 SetLengthToUsableSizeVisitor visitor(component_count,
173 DataOffset(1U << component_size_shift).SizeValue(),
174 component_size_shift);
Ian Rogers6fac4472014-02-25 17:01:10 -0800175 result = down_cast<Array*>(
176 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size,
177 allocator_type, visitor));
178 }
179 if (kIsDebugBuild && result != nullptr && Runtime::Current()->IsStarted()) {
Mathieu Chartier85801542014-02-27 18:06:26 -0800180 array_class = result->GetClass(); // In case the array class moved.
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700181 CHECK_EQ(array_class->GetComponentSize(), 1U << component_size_shift);
182 if (!kFillUsable) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800183 CHECK_EQ(result->SizeOf(), size);
184 } else {
185 CHECK_GE(result->SizeOf(), size);
186 }
187 }
188 return result;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700189}
190
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800191template<class T>
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800192inline void PrimitiveArray<T>::VisitRoots(RootCallback* callback, void* arg) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700193 if (!array_class_.IsNull()) {
194 array_class_.VisitRoot(callback, arg, 0, kRootStickyClass);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800195 }
196}
197
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700198template<typename T>
199inline PrimitiveArray<T>* PrimitiveArray<T>::Alloc(Thread* self, size_t length) {
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700200 Array* raw_array = Array::Alloc<true>(self, GetArrayClass(), length,
201 ComponentSizeShiftWidth<sizeof(T)>(),
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700202 Runtime::Current()->GetHeap()->GetCurrentAllocator());
203 return down_cast<PrimitiveArray<T>*>(raw_array);
204}
205
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700206template<typename T>
207inline T PrimitiveArray<T>::Get(int32_t i) {
208 if (!CheckIsValidIndex(i)) {
209 DCHECK(Thread::Current()->IsExceptionPending());
210 return T(0);
211 }
212 return GetWithoutChecks(i);
213}
214
215template<typename T>
216inline void PrimitiveArray<T>::Set(int32_t i, T value) {
217 if (Runtime::Current()->IsActiveTransaction()) {
218 Set<true>(i, value);
219 } else {
220 Set<false>(i, value);
221 }
222}
223
224template<typename T>
225template<bool kTransactionActive, bool kCheckTransaction>
226inline void PrimitiveArray<T>::Set(int32_t i, T value) {
227 if (CheckIsValidIndex(i)) {
228 SetWithoutChecks<kTransactionActive, kCheckTransaction>(i, value);
229 } else {
230 DCHECK(Thread::Current()->IsExceptionPending());
231 }
232}
233
234template<typename T>
235template<bool kTransactionActive, bool kCheckTransaction>
236inline void PrimitiveArray<T>::SetWithoutChecks(int32_t i, T value) {
237 if (kCheckTransaction) {
238 DCHECK_EQ(kTransactionActive, Runtime::Current()->IsActiveTransaction());
239 }
240 if (kTransactionActive) {
241 Runtime::Current()->RecordWriteArray(this, i, GetWithoutChecks(i));
242 }
243 DCHECK(CheckIsValidIndex(i));
244 GetData()[i] = value;
245}
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700246// Backward copy where elements are of aligned appropriately for T. Count is in T sized units.
247// Copies are guaranteed not to tear when the sizeof T is less-than 64bit.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800248template<typename T>
249static inline void ArrayBackwardCopy(T* d, const T* s, int32_t count) {
250 d += count;
251 s += count;
252 for (int32_t i = 0; i < count; ++i) {
253 d--;
254 s--;
255 *d = *s;
256 }
257}
258
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700259// Forward copy where elements are of aligned appropriately for T. Count is in T sized units.
260// Copies are guaranteed not to tear when the sizeof T is less-than 64bit.
Ian Rogers6fac4472014-02-25 17:01:10 -0800261template<typename T>
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700262static inline void ArrayForwardCopy(T* d, const T* s, int32_t count) {
263 for (int32_t i = 0; i < count; ++i) {
264 *d = *s;
265 d++;
266 s++;
267 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800268}
269
Ian Rogersef7d42f2014-01-06 12:55:46 -0800270template<class T>
Ian Rogers6fac4472014-02-25 17:01:10 -0800271inline void PrimitiveArray<T>::Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos,
272 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800273 if (UNLIKELY(count == 0)) {
274 return;
275 }
276 DCHECK_GE(dst_pos, 0);
277 DCHECK_GE(src_pos, 0);
278 DCHECK_GT(count, 0);
279 DCHECK(src != nullptr);
280 DCHECK_LT(dst_pos, GetLength());
281 DCHECK_LE(dst_pos, GetLength() - count);
282 DCHECK_LT(src_pos, src->GetLength());
283 DCHECK_LE(src_pos, src->GetLength() - count);
284
285 // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
286 // in our implementation, because they may copy byte-by-byte.
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700287 if (LIKELY(src != this)) {
288 // Memcpy ok for guaranteed non-overlapping distinct arrays.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800289 Memcpy(dst_pos, src, src_pos, count);
290 } else {
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700291 // Handle copies within the same array using the appropriate direction copy.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800292 void* dst_raw = GetRawData(sizeof(T), dst_pos);
293 const void* src_raw = src->GetRawData(sizeof(T), src_pos);
294 if (sizeof(T) == sizeof(uint8_t)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800295 uint8_t* d = reinterpret_cast<uint8_t*>(dst_raw);
296 const uint8_t* s = reinterpret_cast<const uint8_t*>(src_raw);
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700297 memmove(d, s, count);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800298 } else {
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700299 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= count);
300 if (sizeof(T) == sizeof(uint16_t)) {
301 uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
302 const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
303 if (copy_forward) {
304 ArrayForwardCopy<uint16_t>(d, s, count);
305 } else {
306 ArrayBackwardCopy<uint16_t>(d, s, count);
307 }
308 } else if (sizeof(T) == sizeof(uint32_t)) {
309 uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
310 const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
311 if (copy_forward) {
312 ArrayForwardCopy<uint32_t>(d, s, count);
313 } else {
314 ArrayBackwardCopy<uint32_t>(d, s, count);
315 }
316 } else {
317 DCHECK_EQ(sizeof(T), sizeof(uint64_t));
318 uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
319 const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
320 if (copy_forward) {
321 ArrayForwardCopy<uint64_t>(d, s, count);
322 } else {
323 ArrayBackwardCopy<uint64_t>(d, s, count);
324 }
325 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800326 }
327 }
328}
329
Ian Rogersef7d42f2014-01-06 12:55:46 -0800330template<class T>
Ian Rogers6fac4472014-02-25 17:01:10 -0800331inline void PrimitiveArray<T>::Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos,
332 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800333 if (UNLIKELY(count == 0)) {
334 return;
335 }
336 DCHECK_GE(dst_pos, 0);
337 DCHECK_GE(src_pos, 0);
338 DCHECK_GT(count, 0);
339 DCHECK(src != nullptr);
340 DCHECK_LT(dst_pos, GetLength());
341 DCHECK_LE(dst_pos, GetLength() - count);
342 DCHECK_LT(src_pos, src->GetLength());
343 DCHECK_LE(src_pos, src->GetLength() - count);
344
345 // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
346 // in our implementation, because they may copy byte-by-byte.
347 void* dst_raw = GetRawData(sizeof(T), dst_pos);
348 const void* src_raw = src->GetRawData(sizeof(T), src_pos);
349 if (sizeof(T) == sizeof(uint8_t)) {
350 memcpy(dst_raw, src_raw, count);
351 } else if (sizeof(T) == sizeof(uint16_t)) {
352 uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
353 const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
354 ArrayForwardCopy<uint16_t>(d, s, count);
355 } else if (sizeof(T) == sizeof(uint32_t)) {
356 uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
357 const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
358 ArrayForwardCopy<uint32_t>(d, s, count);
359 } else {
360 DCHECK_EQ(sizeof(T), sizeof(uint64_t));
361 uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
362 const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
363 ArrayForwardCopy<uint64_t>(d, s, count);
364 }
365}
366
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800367} // namespace mirror
368} // namespace art
369
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700370#endif // ART_RUNTIME_MIRROR_ARRAY_INL_H_