blob: 218085701961eea9bf8a1ddadd272270c3c52a6e [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 Rogers62d6c772013-02-27 08:32:07 -080021#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070022#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070023#include "gc/accounting/card_table-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "object-inl.h"
25#include "object_array.h"
26#include "object_array-inl.h"
27#include "object_utils.h"
28#include "sirt_ref.h"
29#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,
44 const SirtRef<Class>& array_class, int current_dimension,
45 const SirtRef<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 Chartier5bb99032014-02-08 16:20:58 -080048 SirtRef<Array> new_array(self, Array::Alloc<true>(self, array_class.get(), array_length));
49 if (UNLIKELY(new_array.get() == nullptr)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050 CHECK(self->IsExceptionPending());
Mathieu Chartier5bb99032014-02-08 16:20:58 -080051 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070053 if (current_dimension + 1 < dimensions->GetLength()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054 // Create a new sub-array in every element of the array.
55 for (int32_t i = 0; i < array_length; i++) {
Mathieu Chartier5bb99032014-02-08 16:20:58 -080056 SirtRef<mirror::Class> sirt_component_type(self, array_class->GetComponentType());
57 Array* sub_array = RecursiveCreateMultiArray(self, sirt_component_type,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058 current_dimension + 1, dimensions);
Mathieu Chartier5bb99032014-02-08 16:20:58 -080059 if (UNLIKELY(sub_array == nullptr)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060 CHECK(self->IsExceptionPending());
Mathieu Chartier5bb99032014-02-08 16:20:58 -080061 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080062 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010063 // Use non-transactional mode without check.
64 new_array->AsObjectArray<Array>()->Set<false, false>(i, sub_array);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080065 }
66 }
67 return new_array.get();
68}
69
Mathieu Chartier5bb99032014-02-08 16:20:58 -080070Array* Array::CreateMultiArray(Thread* self, const SirtRef<Class>& element_class,
71 const SirtRef<IntArray>& dimensions) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072 // Verify dimensions.
73 //
74 // The caller is responsible for verifying that "dimArray" is non-null
75 // and has a length > 0 and <= 255.
76 int num_dimensions = dimensions->GetLength();
77 DCHECK_GT(num_dimensions, 0);
78 DCHECK_LE(num_dimensions, 255);
79
80 for (int i = 0; i < num_dimensions; i++) {
81 int dimension = dimensions->Get(i);
82 if (UNLIKELY(dimension < 0)) {
Ian Rogers62d6c772013-02-27 08:32:07 -080083 ThrowNegativeArraySizeException(StringPrintf("Dimension %d: %d", i, dimension).c_str());
Mathieu Chartier5bb99032014-02-08 16:20:58 -080084 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085 }
86 }
87
88 // Generate the full name of the array class.
89 std::string descriptor(num_dimensions, '[');
Mathieu Chartier5bb99032014-02-08 16:20:58 -080090 descriptor += ClassHelper(element_class.get()).GetDescriptor();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091
92 // Find/generate the array class.
93 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier590fee92013-09-13 13:46:47 -070094 SirtRef<mirror::ClassLoader> class_loader(self, element_class->GetClassLoader());
Mathieu Chartier5bb99032014-02-08 16:20:58 -080095 SirtRef<mirror::Class> array_class(self,
96 class_linker->FindClass(descriptor.c_str(), class_loader));
97 if (UNLIKELY(array_class.get() == nullptr)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080098 CHECK(self->IsExceptionPending());
Mathieu Chartier5bb99032014-02-08 16:20:58 -080099 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100 }
101 // create the array
Mathieu Chartier5bb99032014-02-08 16:20:58 -0800102 Array* new_array = RecursiveCreateMultiArray(self, array_class, 0, dimensions);
103 if (UNLIKELY(new_array == nullptr)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104 CHECK(self->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800105 }
106 return new_array;
107}
108
Ian Rogersef7d42f2014-01-06 12:55:46 -0800109void Array::ThrowArrayIndexOutOfBoundsException(int32_t index) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800110 art::ThrowArrayIndexOutOfBoundsException(index, GetLength());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111}
112
Ian Rogersef7d42f2014-01-06 12:55:46 -0800113void Array::ThrowArrayStoreException(Object* object) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800114 art::ThrowArrayStoreException(object->GetClass(), this->GetClass());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115}
116
117template<typename T>
118PrimitiveArray<T>* PrimitiveArray<T>::Alloc(Thread* self, size_t length) {
119 DCHECK(array_class_ != NULL);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800120 Array* raw_array = Array::Alloc<true>(self, array_class_, length, sizeof(T));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800121 return down_cast<PrimitiveArray<T>*>(raw_array);
122}
123
124template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
125
126// Explicitly instantiate all the primitive array types.
127template class PrimitiveArray<uint8_t>; // BooleanArray
128template class PrimitiveArray<int8_t>; // ByteArray
129template class PrimitiveArray<uint16_t>; // CharArray
130template class PrimitiveArray<double>; // DoubleArray
131template class PrimitiveArray<float>; // FloatArray
132template class PrimitiveArray<int32_t>; // IntArray
133template class PrimitiveArray<int64_t>; // LongArray
134template class PrimitiveArray<int16_t>; // ShortArray
135
136} // namespace mirror
137} // namespace art