Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1 | /* |
| 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 Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 21 | #include "class_linker-inl.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 22 | #include "common_throws.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 23 | #include "dex_file-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 24 | #include "gc/accounting/card_table-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 25 | #include "object-inl.h" |
| 26 | #include "object_array.h" |
| 27 | #include "object_array-inl.h" |
| 28 | #include "object_utils.h" |
| 29 | #include "sirt_ref.h" |
| 30 | #include "thread.h" |
| 31 | #include "utils.h" |
| 32 | |
| 33 | namespace art { |
| 34 | namespace mirror { |
| 35 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 36 | // 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 Chartier | 5bb9903 | 2014-02-08 16:20:58 -0800 | [diff] [blame] | 44 | static Array* RecursiveCreateMultiArray(Thread* self, |
| 45 | const SirtRef<Class>& array_class, int current_dimension, |
| 46 | const SirtRef<mirror::IntArray>& dimensions) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 47 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 48 | int32_t array_length = dimensions->Get(current_dimension); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 49 | SirtRef<Array> new_array(self, Array::Alloc<true>(self, array_class.get(), array_length, |
| 50 | array_class->GetComponentSize(), |
| 51 | Runtime::Current()->GetHeap()->GetCurrentAllocator())); |
Mathieu Chartier | 5bb9903 | 2014-02-08 16:20:58 -0800 | [diff] [blame] | 52 | if (UNLIKELY(new_array.get() == nullptr)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 53 | CHECK(self->IsExceptionPending()); |
Mathieu Chartier | 5bb9903 | 2014-02-08 16:20:58 -0800 | [diff] [blame] | 54 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 55 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 56 | if (current_dimension + 1 < dimensions->GetLength()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 57 | // Create a new sub-array in every element of the array. |
| 58 | for (int32_t i = 0; i < array_length; i++) { |
Mathieu Chartier | 5bb9903 | 2014-02-08 16:20:58 -0800 | [diff] [blame] | 59 | SirtRef<mirror::Class> sirt_component_type(self, array_class->GetComponentType()); |
| 60 | Array* sub_array = RecursiveCreateMultiArray(self, sirt_component_type, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 61 | current_dimension + 1, dimensions); |
Mathieu Chartier | 5bb9903 | 2014-02-08 16:20:58 -0800 | [diff] [blame] | 62 | if (UNLIKELY(sub_array == nullptr)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 63 | CHECK(self->IsExceptionPending()); |
Mathieu Chartier | 5bb9903 | 2014-02-08 16:20:58 -0800 | [diff] [blame] | 64 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 65 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 66 | // Use non-transactional mode without check. |
| 67 | new_array->AsObjectArray<Array>()->Set<false, false>(i, sub_array); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | return new_array.get(); |
| 71 | } |
| 72 | |
Mathieu Chartier | 5bb9903 | 2014-02-08 16:20:58 -0800 | [diff] [blame] | 73 | Array* Array::CreateMultiArray(Thread* self, const SirtRef<Class>& element_class, |
| 74 | const SirtRef<IntArray>& dimensions) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 75 | // Verify dimensions. |
| 76 | // |
| 77 | // The caller is responsible for verifying that "dimArray" is non-null |
| 78 | // and has a length > 0 and <= 255. |
| 79 | int num_dimensions = dimensions->GetLength(); |
| 80 | DCHECK_GT(num_dimensions, 0); |
| 81 | DCHECK_LE(num_dimensions, 255); |
| 82 | |
| 83 | for (int i = 0; i < num_dimensions; i++) { |
| 84 | int dimension = dimensions->Get(i); |
| 85 | if (UNLIKELY(dimension < 0)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 86 | ThrowNegativeArraySizeException(StringPrintf("Dimension %d: %d", i, dimension).c_str()); |
Mathieu Chartier | 5bb9903 | 2014-02-08 16:20:58 -0800 | [diff] [blame] | 87 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 91 | // Find/generate the array class. |
| 92 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | 5bb9903 | 2014-02-08 16:20:58 -0800 | [diff] [blame] | 93 | SirtRef<mirror::Class> array_class(self, |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 94 | class_linker->FindArrayClass(self, element_class.get())); |
Mathieu Chartier | 5bb9903 | 2014-02-08 16:20:58 -0800 | [diff] [blame] | 95 | if (UNLIKELY(array_class.get() == nullptr)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 96 | CHECK(self->IsExceptionPending()); |
Mathieu Chartier | 5bb9903 | 2014-02-08 16:20:58 -0800 | [diff] [blame] | 97 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 98 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 99 | for (int32_t i = 1; i < dimensions->GetLength(); ++i) { |
| 100 | array_class.reset(class_linker->FindArrayClass(self, array_class.get())); |
| 101 | if (UNLIKELY(array_class.get() == nullptr)) { |
| 102 | CHECK(self->IsExceptionPending()); |
| 103 | return nullptr; |
| 104 | } |
| 105 | } |
| 106 | // Create the array. |
Mathieu Chartier | 5bb9903 | 2014-02-08 16:20:58 -0800 | [diff] [blame] | 107 | Array* new_array = RecursiveCreateMultiArray(self, array_class, 0, dimensions); |
| 108 | if (UNLIKELY(new_array == nullptr)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 109 | CHECK(self->IsExceptionPending()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 110 | } |
| 111 | return new_array; |
| 112 | } |
| 113 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 114 | void Array::ThrowArrayIndexOutOfBoundsException(int32_t index) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 115 | art::ThrowArrayIndexOutOfBoundsException(index, GetLength()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 118 | void Array::ThrowArrayStoreException(Object* object) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 119 | art::ThrowArrayStoreException(object->GetClass(), this->GetClass()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 122 | template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL; |
| 123 | |
| 124 | // Explicitly instantiate all the primitive array types. |
| 125 | template class PrimitiveArray<uint8_t>; // BooleanArray |
| 126 | template class PrimitiveArray<int8_t>; // ByteArray |
| 127 | template class PrimitiveArray<uint16_t>; // CharArray |
| 128 | template class PrimitiveArray<double>; // DoubleArray |
| 129 | template class PrimitiveArray<float>; // FloatArray |
| 130 | template class PrimitiveArray<int32_t>; // IntArray |
| 131 | template class PrimitiveArray<int64_t>; // LongArray |
| 132 | template class PrimitiveArray<int16_t>; // ShortArray |
| 133 | |
| 134 | } // namespace mirror |
| 135 | } // namespace art |