blob: 13b5a8ba27cf223299119fbdf1d75b023bb9c98e [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 Rogers7e70b002014-10-08 11:47:24 -070048inline MemberOffset Array::DataOffset(size_t component_size) {
49 DCHECK(IsPowerOfTwo(component_size)) << component_size;
50 size_t data_offset = RoundUp(OFFSETOF_MEMBER(Array, first_element_), component_size);
51 DCHECK_EQ(RoundUp(data_offset, component_size), data_offset)
52 << "Array data offset isn't aligned with component size";
53 return MemberOffset(data_offset);
54}
55
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070056template<VerifyObjectFlags kVerifyFlags>
57inline bool Array::CheckIsValidIndex(int32_t index) {
58 if (UNLIKELY(static_cast<uint32_t>(index) >=
59 static_cast<uint32_t>(GetLength<kVerifyFlags>()))) {
60 ThrowArrayIndexOutOfBoundsException(index);
61 return false;
62 }
63 return true;
64}
65
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070066static inline size_t ComputeArraySize(Thread* self, Class* array_class, int32_t component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070067 size_t component_size_shift)
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070068 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070069 DCHECK(array_class != NULL);
70 DCHECK_GE(component_count, 0);
71 DCHECK(array_class->IsArrayClass());
72
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
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070078 // Check for size_t overflow and throw OutOfMemoryError if this was
79 // an unreasonable request.
80#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 Yamauchi967a0ad2013-09-10 16:24:21 -070091 self->ThrowOutOfMemoryError(StringPrintf("%s of length %d would overflow",
92 PrettyDescriptor(array_class).c_str(),
93 component_count).c_str());
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070094 return 0; // failure
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070095 }
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070096#endif
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070097 return size;
98}
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070099
Ian Rogers6fac4472014-02-25 17:01:10 -0800100// Used for setting the array length in the allocation code path to ensure it is guarded by a
101// StoreStore fence.
Mathieu Chartier1febddf2013-11-20 12:33:14 -0800102class SetLengthVisitor {
103 public:
104 explicit SetLengthVisitor(int32_t length) : length_(length) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700105 }
Mathieu Chartier1febddf2013-11-20 12:33:14 -0800106
Ian Rogers6fac4472014-02-25 17:01:10 -0800107 void operator()(Object* obj, size_t usable_size) const
108 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
109 UNUSED(usable_size);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800110 // Avoid AsArray as object is not yet in live bitmap or allocation stack.
111 Array* array = down_cast<Array*>(obj);
112 // DCHECK(array->IsArrayInstance());
Mathieu Chartier1febddf2013-11-20 12:33:14 -0800113 array->SetLength(length_);
114 }
115
116 private:
117 const int32_t length_;
Ian Rogers6fac4472014-02-25 17:01:10 -0800118
119 DISALLOW_COPY_AND_ASSIGN(SetLengthVisitor);
120};
121
122// Similar to SetLengthVisitor, used for setting the array length to fill the usable size of an
123// array.
124class SetLengthToUsableSizeVisitor {
125 public:
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700126 SetLengthToUsableSizeVisitor(int32_t min_length, size_t header_size,
127 size_t component_size_shift) :
128 minimum_length_(min_length), header_size_(header_size),
129 component_size_shift_(component_size_shift) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800130 }
131
132 void operator()(Object* obj, size_t usable_size) const
133 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
134 // Avoid AsArray as object is not yet in live bitmap or allocation stack.
135 Array* array = down_cast<Array*>(obj);
Ian Rogers6fac4472014-02-25 17:01:10 -0800136 // DCHECK(array->IsArrayInstance());
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700137 int32_t length = (usable_size - header_size_) >> component_size_shift_;
Ian Rogersa55cf412014-02-27 00:31:26 -0800138 DCHECK_GE(length, minimum_length_);
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700139 byte* old_end = reinterpret_cast<byte*>(array->GetRawData(1U << component_size_shift_,
140 minimum_length_));
141 byte* new_end = reinterpret_cast<byte*>(array->GetRawData(1U << component_size_shift_,
142 length));
Ian Rogersa55cf412014-02-27 00:31:26 -0800143 // Ensure space beyond original allocation is zeroed.
144 memset(old_end, 0, new_end - old_end);
Ian Rogers6fac4472014-02-25 17:01:10 -0800145 array->SetLength(length);
146 }
147
148 private:
Ian Rogersa55cf412014-02-27 00:31:26 -0800149 const int32_t minimum_length_;
Ian Rogers6fac4472014-02-25 17:01:10 -0800150 const size_t header_size_;
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700151 const size_t component_size_shift_;
Ian Rogers6fac4472014-02-25 17:01:10 -0800152
153 DISALLOW_COPY_AND_ASSIGN(SetLengthToUsableSizeVisitor);
Mathieu Chartier1febddf2013-11-20 12:33:14 -0800154};
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700155
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700156template <bool kIsInstrumented, bool kFillUsable>
Mathieu Chartier590fee92013-09-13 13:46:47 -0700157inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700158 size_t component_size_shift, gc::AllocatorType allocator_type) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800159 DCHECK(allocator_type != gc::kAllocatorTypeLOS);
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700160 DCHECK_EQ(array_class->GetComponentSizeShift(), component_size_shift);
161 DCHECK_EQ(array_class->GetComponentSize(), (1U << component_size_shift));
162 size_t size = ComputeArraySize(self, array_class, component_count, component_size_shift);
163#ifdef __LP64__
164 // 64-bit. No size_t overflow.
165 DCHECK_NE(size, 0U);
166#else
167 // 32-bit.
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700168 if (UNLIKELY(size == 0)) {
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 Chartier83c8ee02014-01-28 14:50:23 -0800200inline void PrimitiveArray<T>::VisitRoots(RootCallback* callback, void* arg) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700201 if (!array_class_.IsNull()) {
202 array_class_.VisitRoot(callback, arg, 0, kRootStickyClass);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800203 }
204}
205
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700206template<typename T>
207inline PrimitiveArray<T>* PrimitiveArray<T>::Alloc(Thread* self, size_t length) {
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700208 Array* raw_array = Array::Alloc<true>(self, GetArrayClass(), length,
209 ComponentSizeShiftWidth<sizeof(T)>(),
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700210 Runtime::Current()->GetHeap()->GetCurrentAllocator());
211 return down_cast<PrimitiveArray<T>*>(raw_array);
212}
213
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700214template<typename T>
215inline T PrimitiveArray<T>::Get(int32_t i) {
216 if (!CheckIsValidIndex(i)) {
217 DCHECK(Thread::Current()->IsExceptionPending());
218 return T(0);
219 }
220 return GetWithoutChecks(i);
221}
222
223template<typename T>
224inline void PrimitiveArray<T>::Set(int32_t i, T value) {
225 if (Runtime::Current()->IsActiveTransaction()) {
226 Set<true>(i, value);
227 } else {
228 Set<false>(i, value);
229 }
230}
231
232template<typename T>
233template<bool kTransactionActive, bool kCheckTransaction>
234inline void PrimitiveArray<T>::Set(int32_t i, T value) {
235 if (CheckIsValidIndex(i)) {
236 SetWithoutChecks<kTransactionActive, kCheckTransaction>(i, value);
237 } else {
238 DCHECK(Thread::Current()->IsExceptionPending());
239 }
240}
241
242template<typename T>
243template<bool kTransactionActive, bool kCheckTransaction>
244inline void PrimitiveArray<T>::SetWithoutChecks(int32_t i, T value) {
245 if (kCheckTransaction) {
246 DCHECK_EQ(kTransactionActive, Runtime::Current()->IsActiveTransaction());
247 }
248 if (kTransactionActive) {
249 Runtime::Current()->RecordWriteArray(this, i, GetWithoutChecks(i));
250 }
251 DCHECK(CheckIsValidIndex(i));
252 GetData()[i] = value;
253}
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700254// Backward copy where elements are of aligned appropriately for T. Count is in T sized units.
255// Copies are guaranteed not to tear when the sizeof T is less-than 64bit.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800256template<typename T>
257static inline void ArrayBackwardCopy(T* d, const T* s, int32_t count) {
258 d += count;
259 s += count;
260 for (int32_t i = 0; i < count; ++i) {
261 d--;
262 s--;
263 *d = *s;
264 }
265}
266
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700267// Forward copy where elements are of aligned appropriately for T. Count is in T sized units.
268// Copies are guaranteed not to tear when the sizeof T is less-than 64bit.
Ian Rogers6fac4472014-02-25 17:01:10 -0800269template<typename T>
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700270static inline void ArrayForwardCopy(T* d, const T* s, int32_t count) {
271 for (int32_t i = 0; i < count; ++i) {
272 *d = *s;
273 d++;
274 s++;
275 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800276}
277
Ian Rogersef7d42f2014-01-06 12:55:46 -0800278template<class T>
Ian Rogers6fac4472014-02-25 17:01:10 -0800279inline void PrimitiveArray<T>::Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos,
280 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800281 if (UNLIKELY(count == 0)) {
282 return;
283 }
284 DCHECK_GE(dst_pos, 0);
285 DCHECK_GE(src_pos, 0);
286 DCHECK_GT(count, 0);
287 DCHECK(src != nullptr);
288 DCHECK_LT(dst_pos, GetLength());
289 DCHECK_LE(dst_pos, GetLength() - count);
290 DCHECK_LT(src_pos, src->GetLength());
291 DCHECK_LE(src_pos, src->GetLength() - count);
292
293 // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
294 // in our implementation, because they may copy byte-by-byte.
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700295 if (LIKELY(src != this)) {
296 // Memcpy ok for guaranteed non-overlapping distinct arrays.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800297 Memcpy(dst_pos, src, src_pos, count);
298 } else {
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700299 // Handle copies within the same array using the appropriate direction copy.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800300 void* dst_raw = GetRawData(sizeof(T), dst_pos);
301 const void* src_raw = src->GetRawData(sizeof(T), src_pos);
302 if (sizeof(T) == sizeof(uint8_t)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800303 uint8_t* d = reinterpret_cast<uint8_t*>(dst_raw);
304 const uint8_t* s = reinterpret_cast<const uint8_t*>(src_raw);
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700305 memmove(d, s, count);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800306 } else {
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700307 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= count);
308 if (sizeof(T) == sizeof(uint16_t)) {
309 uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
310 const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
311 if (copy_forward) {
312 ArrayForwardCopy<uint16_t>(d, s, count);
313 } else {
314 ArrayBackwardCopy<uint16_t>(d, s, count);
315 }
316 } else if (sizeof(T) == sizeof(uint32_t)) {
317 uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
318 const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
319 if (copy_forward) {
320 ArrayForwardCopy<uint32_t>(d, s, count);
321 } else {
322 ArrayBackwardCopy<uint32_t>(d, s, count);
323 }
324 } else {
325 DCHECK_EQ(sizeof(T), sizeof(uint64_t));
326 uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
327 const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
328 if (copy_forward) {
329 ArrayForwardCopy<uint64_t>(d, s, count);
330 } else {
331 ArrayBackwardCopy<uint64_t>(d, s, count);
332 }
333 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800334 }
335 }
336}
337
Ian Rogersef7d42f2014-01-06 12:55:46 -0800338template<class T>
Ian Rogers6fac4472014-02-25 17:01:10 -0800339inline void PrimitiveArray<T>::Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos,
340 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800341 if (UNLIKELY(count == 0)) {
342 return;
343 }
344 DCHECK_GE(dst_pos, 0);
345 DCHECK_GE(src_pos, 0);
346 DCHECK_GT(count, 0);
347 DCHECK(src != nullptr);
348 DCHECK_LT(dst_pos, GetLength());
349 DCHECK_LE(dst_pos, GetLength() - count);
350 DCHECK_LT(src_pos, src->GetLength());
351 DCHECK_LE(src_pos, src->GetLength() - count);
352
353 // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
354 // in our implementation, because they may copy byte-by-byte.
355 void* dst_raw = GetRawData(sizeof(T), dst_pos);
356 const void* src_raw = src->GetRawData(sizeof(T), src_pos);
357 if (sizeof(T) == sizeof(uint8_t)) {
358 memcpy(dst_raw, src_raw, count);
359 } else if (sizeof(T) == sizeof(uint16_t)) {
360 uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
361 const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
362 ArrayForwardCopy<uint16_t>(d, s, count);
363 } else if (sizeof(T) == sizeof(uint32_t)) {
364 uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
365 const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
366 ArrayForwardCopy<uint32_t>(d, s, count);
367 } else {
368 DCHECK_EQ(sizeof(T), sizeof(uint64_t));
369 uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
370 const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
371 ArrayForwardCopy<uint64_t>(d, s, count);
372 }
373}
374
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800375} // namespace mirror
376} // namespace art
377
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700378#endif // ART_RUNTIME_MIRROR_ARRAY_INL_H_