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 | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 21 | #include "common_throws.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 22 | #include "dex_file-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 23 | #include "gc/accounting/card_table-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 24 | #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 | |
| 32 | namespace art { |
| 33 | namespace mirror { |
| 34 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 35 | // 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. |
| 43 | static Array* RecursiveCreateMultiArray(Thread* self, Class* array_class, int current_dimension, |
| 44 | IntArray* dimensions) |
| 45 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 46 | int32_t array_length = dimensions->Get(current_dimension); |
| 47 | SirtRef<Array> new_array(self, Array::Alloc(self, array_class, array_length)); |
| 48 | if (UNLIKELY(new_array.get() == NULL)) { |
| 49 | CHECK(self->IsExceptionPending()); |
| 50 | return NULL; |
| 51 | } |
| 52 | if ((current_dimension + 1) < dimensions->GetLength()) { |
| 53 | // Create a new sub-array in every element of the array. |
| 54 | for (int32_t i = 0; i < array_length; i++) { |
| 55 | Array* sub_array = RecursiveCreateMultiArray(self, array_class->GetComponentType(), |
| 56 | current_dimension + 1, dimensions); |
| 57 | if (UNLIKELY(sub_array == NULL)) { |
| 58 | CHECK(self->IsExceptionPending()); |
| 59 | return NULL; |
| 60 | } |
| 61 | new_array->AsObjectArray<Array>()->Set(i, sub_array); |
| 62 | } |
| 63 | } |
| 64 | return new_array.get(); |
| 65 | } |
| 66 | |
| 67 | Array* Array::CreateMultiArray(Thread* self, Class* element_class, IntArray* dimensions) { |
| 68 | // Verify dimensions. |
| 69 | // |
| 70 | // The caller is responsible for verifying that "dimArray" is non-null |
| 71 | // and has a length > 0 and <= 255. |
| 72 | int num_dimensions = dimensions->GetLength(); |
| 73 | DCHECK_GT(num_dimensions, 0); |
| 74 | DCHECK_LE(num_dimensions, 255); |
| 75 | |
| 76 | for (int i = 0; i < num_dimensions; i++) { |
| 77 | int dimension = dimensions->Get(i); |
| 78 | if (UNLIKELY(dimension < 0)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 79 | ThrowNegativeArraySizeException(StringPrintf("Dimension %d: %d", i, dimension).c_str()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 80 | return NULL; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // Generate the full name of the array class. |
| 85 | std::string descriptor(num_dimensions, '['); |
| 86 | descriptor += ClassHelper(element_class).GetDescriptor(); |
| 87 | |
| 88 | // Find/generate the array class. |
| 89 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 90 | Class* array_class = class_linker->FindClass(descriptor.c_str(), element_class->GetClassLoader()); |
| 91 | if (UNLIKELY(array_class == NULL)) { |
| 92 | CHECK(self->IsExceptionPending()); |
| 93 | return NULL; |
| 94 | } |
| 95 | // create the array |
| 96 | Array* new_array = RecursiveCreateMultiArray(self, array_class, 0, dimensions); |
| 97 | if (UNLIKELY(new_array == NULL)) { |
| 98 | CHECK(self->IsExceptionPending()); |
| 99 | return NULL; |
| 100 | } |
| 101 | return new_array; |
| 102 | } |
| 103 | |
Sebastien Hertz | 9897be9 | 2013-06-27 18:24:46 +0200 | [diff] [blame] | 104 | void Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 105 | art::ThrowArrayIndexOutOfBoundsException(index, GetLength()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Sebastien Hertz | 9897be9 | 2013-06-27 18:24:46 +0200 | [diff] [blame] | 108 | void Array::ThrowArrayStoreException(Object* object) const { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 109 | art::ThrowArrayStoreException(object->GetClass(), this->GetClass()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | template<typename T> |
| 113 | PrimitiveArray<T>* PrimitiveArray<T>::Alloc(Thread* self, size_t length) { |
| 114 | DCHECK(array_class_ != NULL); |
| 115 | Array* raw_array = Array::Alloc(self, array_class_, length, sizeof(T)); |
| 116 | return down_cast<PrimitiveArray<T>*>(raw_array); |
| 117 | } |
| 118 | |
| 119 | template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL; |
| 120 | |
| 121 | // Explicitly instantiate all the primitive array types. |
| 122 | template class PrimitiveArray<uint8_t>; // BooleanArray |
| 123 | template class PrimitiveArray<int8_t>; // ByteArray |
| 124 | template class PrimitiveArray<uint16_t>; // CharArray |
| 125 | template class PrimitiveArray<double>; // DoubleArray |
| 126 | template class PrimitiveArray<float>; // FloatArray |
| 127 | template class PrimitiveArray<int32_t>; // IntArray |
| 128 | template class PrimitiveArray<int64_t>; // LongArray |
| 129 | template class PrimitiveArray<int16_t>; // ShortArray |
| 130 | |
| 131 | } // namespace mirror |
| 132 | } // namespace art |