blob: e67c7139ab291b9cd6e52d748a9140d6c408a15c [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
Andreas Gampe46ee31b2016-12-14 10:11:49 -080017#include "array-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080018
Andreas Gampe8e0f0432018-10-24 13:38:03 -070019#include "array-alloc-inl.h"
David Sehrc431b9d2018-03-02 12:01:51 -080020#include "base/utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070022#include "class.h"
Ian Rogers98379392014-02-24 16:53:16 -080023#include "class_linker-inl.h"
Vladimir Marko0278be72018-05-24 13:30:24 +010024#include "class_root.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080025#include "common_throws.h"
David Sehr9e734c72018-01-04 17:56:19 -080026#include "dex/dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070027#include "gc/accounting/card_table-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070028#include "handle_scope-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "object_array-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032
33namespace art {
34namespace mirror {
35
Andreas Gampe46ee31b2016-12-14 10:11:49 -080036using android::base::StringPrintf;
37
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038// Create a multi-dimensional array of Objects or primitive types.
39//
40// We have to generate the names for X[], X[][], X[][][], and so on. The
41// easiest way to deal with that is to create the full name once and then
42// subtract pieces off. Besides, we want to start with the outermost
43// piece and work our way in.
44// Recursively create an array with multiple dimensions. Elements may be
45// Objects or primitive types.
Vladimir Markobcf17522018-06-01 13:14:32 +010046static ObjPtr<Array> RecursiveCreateMultiArray(Thread* self,
47 Handle<Class> array_class,
48 int current_dimension,
49 Handle<mirror::IntArray> dimensions)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070050 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051 int32_t array_length = dimensions->Get(current_dimension);
Vladimir Markobcf17522018-06-01 13:14:32 +010052 StackHandleScope<2> hs(self);
53 Handle<mirror::Class> h_component_type(hs.NewHandle(array_class->GetComponentType()));
54 size_t component_size_shift = h_component_type->GetPrimitiveTypeSizeShift();
55 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
56 Handle<Array> new_array(hs.NewHandle(Array::Alloc<true>(
57 self, array_class.Get(), array_length, component_size_shift, allocator_type)));
Andreas Gampefa4333d2017-02-14 11:10:34 -080058 if (UNLIKELY(new_array == nullptr)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059 CHECK(self->IsExceptionPending());
Mathieu Chartier5bb99032014-02-08 16:20:58 -080060 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070062 if (current_dimension + 1 < dimensions->GetLength()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063 // Create a new sub-array in every element of the array.
64 for (int32_t i = 0; i < array_length; i++) {
Vladimir Markobcf17522018-06-01 13:14:32 +010065 ObjPtr<Array> sub_array =
66 RecursiveCreateMultiArray(self, h_component_type, current_dimension + 1, dimensions);
Mathieu Chartier5bb99032014-02-08 16:20:58 -080067 if (UNLIKELY(sub_array == nullptr)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080068 CHECK(self->IsExceptionPending());
Mathieu Chartier5bb99032014-02-08 16:20:58 -080069 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080070 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010071 // Use non-transactional mode without check.
72 new_array->AsObjectArray<Array>()->Set<false, false>(i, sub_array);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073 }
74 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070075 return new_array.Get();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076}
77
Vladimir Markobcf17522018-06-01 13:14:32 +010078ObjPtr<Array> Array::CreateMultiArray(Thread* self,
79 Handle<Class> element_class,
80 Handle<IntArray> dimensions) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081 // Verify dimensions.
82 //
83 // The caller is responsible for verifying that "dimArray" is non-null
84 // and has a length > 0 and <= 255.
85 int num_dimensions = dimensions->GetLength();
86 DCHECK_GT(num_dimensions, 0);
87 DCHECK_LE(num_dimensions, 255);
88
89 for (int i = 0; i < num_dimensions; i++) {
90 int dimension = dimensions->Get(i);
91 if (UNLIKELY(dimension < 0)) {
Ian Rogers62d6c772013-02-27 08:32:07 -080092 ThrowNegativeArraySizeException(StringPrintf("Dimension %d: %d", i, dimension).c_str());
Mathieu Chartier5bb99032014-02-08 16:20:58 -080093 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094 }
95 }
96
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097 // Find/generate the array class.
98 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070099 StackHandleScope<1> hs(self);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700100 MutableHandle<mirror::Class> array_class(
Vladimir Markobcf17522018-06-01 13:14:32 +0100101 hs.NewHandle(class_linker->FindArrayClass(self, element_class.Get())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800102 if (UNLIKELY(array_class == nullptr)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103 CHECK(self->IsExceptionPending());
Mathieu Chartier5bb99032014-02-08 16:20:58 -0800104 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800105 }
Ian Rogers98379392014-02-24 16:53:16 -0800106 for (int32_t i = 1; i < dimensions->GetLength(); ++i) {
Vladimir Markobcf17522018-06-01 13:14:32 +0100107 array_class.Assign(class_linker->FindArrayClass(self, array_class.Get()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800108 if (UNLIKELY(array_class == nullptr)) {
Ian Rogers98379392014-02-24 16:53:16 -0800109 CHECK(self->IsExceptionPending());
110 return nullptr;
111 }
112 }
113 // Create the array.
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700114 ObjPtr<Array> new_array = RecursiveCreateMultiArray(self, array_class, 0, dimensions);
Mathieu Chartier5bb99032014-02-08 16:20:58 -0800115 if (UNLIKELY(new_array == nullptr)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800116 CHECK(self->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800117 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700118 return new_array.Ptr();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800119}
120
Vladimir Marko0278be72018-05-24 13:30:24 +0100121template<typename T>
Vladimir Markobcf17522018-06-01 13:14:32 +0100122ObjPtr<PrimitiveArray<T>> PrimitiveArray<T>::Alloc(Thread* self, size_t length) {
123 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
124 ObjPtr<Array> raw_array = Array::Alloc<true>(self,
125 GetClassRoot<PrimitiveArray<T>>(),
126 length,
127 ComponentSizeShiftWidth(sizeof(T)),
128 allocator_type);
129 return ObjPtr<PrimitiveArray<T>>::DownCast(raw_array);
Vladimir Marko0278be72018-05-24 13:30:24 +0100130}
131
Ian Rogersef7d42f2014-01-06 12:55:46 -0800132void Array::ThrowArrayIndexOutOfBoundsException(int32_t index) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800133 art::ThrowArrayIndexOutOfBoundsException(index, GetLength());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800134}
135
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700136void Array::ThrowArrayStoreException(ObjPtr<Object> object) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800137 art::ThrowArrayStoreException(object->GetClass(), this->GetClass());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138}
139
Vladimir Markobcf17522018-06-01 13:14:32 +0100140ObjPtr<Array> Array::CopyOf(Thread* self, int32_t new_length) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700141 CHECK(GetClass()->GetComponentType()->IsPrimitive()) << "Will miss write barriers";
142 DCHECK_GE(new_length, 0);
143 // We may get copied by a compacting GC.
144 StackHandleScope<1> hs(self);
145 auto h_this(hs.NewHandle(this));
146 auto* heap = Runtime::Current()->GetHeap();
147 gc::AllocatorType allocator_type = heap->IsMovableObject(this) ? heap->GetCurrentAllocator() :
148 heap->GetCurrentNonMovingAllocator();
149 const auto component_size = GetClass()->GetComponentSize();
150 const auto component_shift = GetClass()->GetComponentSizeShift();
Vladimir Markobcf17522018-06-01 13:14:32 +0100151 ObjPtr<Array> new_array =
152 Alloc<true>(self, GetClass(), new_length, component_shift, allocator_type);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700153 if (LIKELY(new_array != nullptr)) {
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700154 memcpy(new_array->GetRawData(component_size, 0),
155 h_this->GetRawData(component_size, 0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700156 std::min(h_this->GetLength(), new_length) << component_shift);
157 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700158 return new_array.Ptr();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700159}
160
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161// Explicitly instantiate all the primitive array types.
162template class PrimitiveArray<uint8_t>; // BooleanArray
163template class PrimitiveArray<int8_t>; // ByteArray
164template class PrimitiveArray<uint16_t>; // CharArray
165template class PrimitiveArray<double>; // DoubleArray
166template class PrimitiveArray<float>; // FloatArray
167template class PrimitiveArray<int32_t>; // IntArray
168template class PrimitiveArray<int64_t>; // LongArray
169template class PrimitiveArray<int16_t>; // ShortArray
170
171} // namespace mirror
172} // namespace art