blob: 6218dd9c9cb73f56bdf60b2717e9f51c05c952d5 [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
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019#include "class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070020#include "class.h"
Ian Rogers98379392014-02-24 16:53:16 -080021#include "class_linker-inl.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080022#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070023#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070024#include "gc/accounting/card_table-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include "handle_scope-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "object_array-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "thread.h"
29#include "utils.h"
30
31namespace art {
32namespace mirror {
33
Andreas Gampe46ee31b2016-12-14 10:11:49 -080034using android::base::StringPrintf;
35
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036// Create a multi-dimensional array of Objects or primitive types.
37//
38// We have to generate the names for X[], X[][], X[][][], and so on. The
39// easiest way to deal with that is to create the full name once and then
40// subtract pieces off. Besides, we want to start with the outermost
41// piece and work our way in.
42// Recursively create an array with multiple dimensions. Elements may be
43// Objects or primitive types.
Mathieu Chartier5bb99032014-02-08 16:20:58 -080044static Array* RecursiveCreateMultiArray(Thread* self,
Mathieu Chartier0cd81352014-05-22 16:48:55 -070045 Handle<Class> array_class, int current_dimension,
46 Handle<mirror::IntArray> dimensions)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070047 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048 int32_t array_length = dimensions->Get(current_dimension);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070049 StackHandleScope<1> hs(self);
50 Handle<Array> new_array(
51 hs.NewHandle(
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070052 Array::Alloc<true>(self, array_class.Get(), array_length,
53 array_class->GetComponentSizeShift(),
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070054 Runtime::Current()->GetHeap()->GetCurrentAllocator())));
Andreas Gampefa4333d2017-02-14 11:10:34 -080055 if (UNLIKELY(new_array == nullptr)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056 CHECK(self->IsExceptionPending());
Mathieu Chartier5bb99032014-02-08 16:20:58 -080057 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070059 if (current_dimension + 1 < dimensions->GetLength()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060 // Create a new sub-array in every element of the array.
61 for (int32_t i = 0; i < array_length; i++) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -080062 StackHandleScope<1> hs2(self);
63 Handle<mirror::Class> h_component_type(hs2.NewHandle(array_class->GetComponentType()));
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070064 ObjPtr<Array> sub_array = RecursiveCreateMultiArray(self, h_component_type,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080065 current_dimension + 1, dimensions);
Mathieu Chartier5bb99032014-02-08 16:20:58 -080066 if (UNLIKELY(sub_array == nullptr)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080067 CHECK(self->IsExceptionPending());
Mathieu Chartier5bb99032014-02-08 16:20:58 -080068 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010070 // Use non-transactional mode without check.
71 new_array->AsObjectArray<Array>()->Set<false, false>(i, sub_array);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072 }
73 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070074 return new_array.Get();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080075}
76
Mathieu Chartier0cd81352014-05-22 16:48:55 -070077Array* Array::CreateMultiArray(Thread* self, Handle<Class> element_class,
78 Handle<IntArray> dimensions) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079 // Verify dimensions.
80 //
81 // The caller is responsible for verifying that "dimArray" is non-null
82 // and has a length > 0 and <= 255.
83 int num_dimensions = dimensions->GetLength();
84 DCHECK_GT(num_dimensions, 0);
85 DCHECK_LE(num_dimensions, 255);
86
87 for (int i = 0; i < num_dimensions; i++) {
88 int dimension = dimensions->Get(i);
89 if (UNLIKELY(dimension < 0)) {
Ian Rogers62d6c772013-02-27 08:32:07 -080090 ThrowNegativeArraySizeException(StringPrintf("Dimension %d: %d", i, dimension).c_str());
Mathieu Chartier5bb99032014-02-08 16:20:58 -080091 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092 }
93 }
94
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080095 // Find/generate the array class.
96 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070097 ObjPtr<mirror::Class> element_class_ptr = element_class.Get();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070098 StackHandleScope<1> hs(self);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070099 MutableHandle<mirror::Class> array_class(
Mathieu Chartierb74cd292014-05-29 14:31:33 -0700100 hs.NewHandle(class_linker->FindArrayClass(self, &element_class_ptr)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800101 if (UNLIKELY(array_class == nullptr)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800102 CHECK(self->IsExceptionPending());
Mathieu Chartier5bb99032014-02-08 16:20:58 -0800103 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104 }
Ian Rogers98379392014-02-24 16:53:16 -0800105 for (int32_t i = 1; i < dimensions->GetLength(); ++i) {
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700106 ObjPtr<mirror::Class> array_class_ptr = array_class.Get();
Mathieu Chartierb74cd292014-05-29 14:31:33 -0700107 array_class.Assign(class_linker->FindArrayClass(self, &array_class_ptr));
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
Ian Rogersef7d42f2014-01-06 12:55:46 -0800121void Array::ThrowArrayIndexOutOfBoundsException(int32_t index) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800122 art::ThrowArrayIndexOutOfBoundsException(index, GetLength());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800123}
124
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700125void Array::ThrowArrayStoreException(ObjPtr<Object> object) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800126 art::ThrowArrayStoreException(object->GetClass(), this->GetClass());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800127}
128
Mathieu Chartiere401d142015-04-22 13:56:20 -0700129Array* Array::CopyOf(Thread* self, int32_t new_length) {
130 CHECK(GetClass()->GetComponentType()->IsPrimitive()) << "Will miss write barriers";
131 DCHECK_GE(new_length, 0);
132 // We may get copied by a compacting GC.
133 StackHandleScope<1> hs(self);
134 auto h_this(hs.NewHandle(this));
135 auto* heap = Runtime::Current()->GetHeap();
136 gc::AllocatorType allocator_type = heap->IsMovableObject(this) ? heap->GetCurrentAllocator() :
137 heap->GetCurrentNonMovingAllocator();
138 const auto component_size = GetClass()->GetComponentSize();
139 const auto component_shift = GetClass()->GetComponentSizeShift();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700140 ObjPtr<Array> new_array = Alloc<true>(self, GetClass(), new_length, component_shift, allocator_type);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700141 if (LIKELY(new_array != nullptr)) {
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700142 memcpy(new_array->GetRawData(component_size, 0),
143 h_this->GetRawData(component_size, 0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700144 std::min(h_this->GetLength(), new_length) << component_shift);
145 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700146 return new_array.Ptr();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700147}
148
149
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700150template <typename T> GcRoot<Class> PrimitiveArray<T>::array_class_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800151
152// Explicitly instantiate all the primitive array types.
153template class PrimitiveArray<uint8_t>; // BooleanArray
154template class PrimitiveArray<int8_t>; // ByteArray
155template class PrimitiveArray<uint16_t>; // CharArray
156template class PrimitiveArray<double>; // DoubleArray
157template class PrimitiveArray<float>; // FloatArray
158template class PrimitiveArray<int32_t>; // IntArray
159template class PrimitiveArray<int64_t>; // LongArray
160template class PrimitiveArray<int16_t>; // ShortArray
161
162} // namespace mirror
163} // namespace art