blob: 636be3346a1675186a2b9fd3a09639da89ade46b [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
17#include "array.h"
18
19#include "class.h"
20#include "class-inl.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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "object-inl.h"
26#include "object_array.h"
27#include "object_array-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070028#include "handle_scope-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "thread.h"
30#include "utils.h"
31
32namespace art {
33namespace mirror {
34
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035// Create a multi-dimensional array of Objects or primitive types.
36//
37// We have to generate the names for X[], X[][], X[][][], and so on. The
38// easiest way to deal with that is to create the full name once and then
39// subtract pieces off. Besides, we want to start with the outermost
40// piece and work our way in.
41// Recursively create an array with multiple dimensions. Elements may be
42// Objects or primitive types.
Mathieu Chartier5bb99032014-02-08 16:20:58 -080043static Array* RecursiveCreateMultiArray(Thread* self,
Mathieu Chartier0cd81352014-05-22 16:48:55 -070044 Handle<Class> array_class, int current_dimension,
45 Handle<mirror::IntArray> dimensions)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
47 int32_t array_length = dimensions->Get(current_dimension);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070048 StackHandleScope<1> hs(self);
49 Handle<Array> new_array(
50 hs.NewHandle(
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070051 Array::Alloc<true>(self, array_class.Get(), array_length,
52 array_class->GetComponentSizeShift(),
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070053 Runtime::Current()->GetHeap()->GetCurrentAllocator())));
54 if (UNLIKELY(new_array.Get() == nullptr)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055 CHECK(self->IsExceptionPending());
Mathieu Chartier5bb99032014-02-08 16:20:58 -080056 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070058 if (current_dimension + 1 < dimensions->GetLength()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059 // Create a new sub-array in every element of the array.
60 for (int32_t i = 0; i < array_length; i++) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070061 StackHandleScope<1> hs(self);
62 Handle<mirror::Class> h_component_type(hs.NewHandle(array_class->GetComponentType()));
63 Array* sub_array = RecursiveCreateMultiArray(self, h_component_type,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064 current_dimension + 1, dimensions);
Mathieu Chartier5bb99032014-02-08 16:20:58 -080065 if (UNLIKELY(sub_array == nullptr)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066 CHECK(self->IsExceptionPending());
Mathieu Chartier5bb99032014-02-08 16:20:58 -080067 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080068 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010069 // Use non-transactional mode without check.
70 new_array->AsObjectArray<Array>()->Set<false, false>(i, sub_array);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071 }
72 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070073 return new_array.Get();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074}
75
Mathieu Chartier0cd81352014-05-22 16:48:55 -070076Array* Array::CreateMultiArray(Thread* self, Handle<Class> element_class,
77 Handle<IntArray> dimensions) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078 // Verify dimensions.
79 //
80 // The caller is responsible for verifying that "dimArray" is non-null
81 // and has a length > 0 and <= 255.
82 int num_dimensions = dimensions->GetLength();
83 DCHECK_GT(num_dimensions, 0);
84 DCHECK_LE(num_dimensions, 255);
85
86 for (int i = 0; i < num_dimensions; i++) {
87 int dimension = dimensions->Get(i);
88 if (UNLIKELY(dimension < 0)) {
Ian Rogers62d6c772013-02-27 08:32:07 -080089 ThrowNegativeArraySizeException(StringPrintf("Dimension %d: %d", i, dimension).c_str());
Mathieu Chartier5bb99032014-02-08 16:20:58 -080090 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091 }
92 }
93
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094 // Find/generate the array class.
95 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -070096 mirror::Class* element_class_ptr = element_class.Get();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070097 StackHandleScope<1> hs(self);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070098 MutableHandle<mirror::Class> array_class(
Mathieu Chartierb74cd292014-05-29 14:31:33 -070099 hs.NewHandle(class_linker->FindArrayClass(self, &element_class_ptr)));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700100 if (UNLIKELY(array_class.Get() == nullptr)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800101 CHECK(self->IsExceptionPending());
Mathieu Chartier5bb99032014-02-08 16:20:58 -0800102 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103 }
Ian Rogers98379392014-02-24 16:53:16 -0800104 for (int32_t i = 1; i < dimensions->GetLength(); ++i) {
Mathieu Chartierb74cd292014-05-29 14:31:33 -0700105 mirror::Class* array_class_ptr = array_class.Get();
106 array_class.Assign(class_linker->FindArrayClass(self, &array_class_ptr));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700107 if (UNLIKELY(array_class.Get() == nullptr)) {
Ian Rogers98379392014-02-24 16:53:16 -0800108 CHECK(self->IsExceptionPending());
109 return nullptr;
110 }
111 }
112 // Create the array.
Mathieu Chartier5bb99032014-02-08 16:20:58 -0800113 Array* new_array = RecursiveCreateMultiArray(self, array_class, 0, dimensions);
114 if (UNLIKELY(new_array == nullptr)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115 CHECK(self->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800116 }
117 return new_array;
118}
119
Ian Rogersef7d42f2014-01-06 12:55:46 -0800120void Array::ThrowArrayIndexOutOfBoundsException(int32_t index) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800121 art::ThrowArrayIndexOutOfBoundsException(index, GetLength());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122}
123
Ian Rogersef7d42f2014-01-06 12:55:46 -0800124void Array::ThrowArrayStoreException(Object* object) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800125 art::ThrowArrayStoreException(object->GetClass(), this->GetClass());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126}
127
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700128template <typename T> GcRoot<Class> PrimitiveArray<T>::array_class_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800129
130// Explicitly instantiate all the primitive array types.
131template class PrimitiveArray<uint8_t>; // BooleanArray
132template class PrimitiveArray<int8_t>; // ByteArray
133template class PrimitiveArray<uint16_t>; // CharArray
134template class PrimitiveArray<double>; // DoubleArray
135template class PrimitiveArray<float>; // FloatArray
136template class PrimitiveArray<int32_t>; // IntArray
137template class PrimitiveArray<int64_t>; // LongArray
138template class PrimitiveArray<int16_t>; // ShortArray
139
140} // namespace mirror
141} // namespace art