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 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 17 | #include "array-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 18 | |
Andreas Gampe | 8e0f043 | 2018-10-24 13:38:03 -0700 | [diff] [blame] | 19 | #include "array-alloc-inl.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 20 | #include "base/utils.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 21 | #include "class-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 22 | #include "class.h" |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 23 | #include "class_linker-inl.h" |
Vladimir Marko | 0278be7 | 2018-05-24 13:30:24 +0100 | [diff] [blame] | 24 | #include "class_root.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 25 | #include "common_throws.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 26 | #include "dex/dex_file-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 27 | #include "gc/accounting/card_table-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 28 | #include "handle_scope-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | #include "object-inl.h" |
Andreas Gampe | 52ecb65 | 2018-10-24 15:18:21 -0700 | [diff] [blame] | 30 | #include "object_array-alloc-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 31 | #include "object_array-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 32 | #include "thread.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | namespace mirror { |
| 36 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 37 | using android::base::StringPrintf; |
| 38 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 39 | // Create a multi-dimensional array of Objects or primitive types. |
| 40 | // |
| 41 | // We have to generate the names for X[], X[][], X[][][], and so on. The |
| 42 | // easiest way to deal with that is to create the full name once and then |
| 43 | // subtract pieces off. Besides, we want to start with the outermost |
| 44 | // piece and work our way in. |
| 45 | // Recursively create an array with multiple dimensions. Elements may be |
| 46 | // Objects or primitive types. |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 47 | static ObjPtr<Array> RecursiveCreateMultiArray(Thread* self, |
| 48 | Handle<Class> array_class, |
| 49 | int current_dimension, |
| 50 | Handle<mirror::IntArray> dimensions) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 51 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 52 | int32_t array_length = dimensions->Get(current_dimension); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 53 | StackHandleScope<2> hs(self); |
| 54 | Handle<mirror::Class> h_component_type(hs.NewHandle(array_class->GetComponentType())); |
| 55 | size_t component_size_shift = h_component_type->GetPrimitiveTypeSizeShift(); |
| 56 | gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator(); |
| 57 | Handle<Array> new_array(hs.NewHandle(Array::Alloc<true>( |
| 58 | self, array_class.Get(), array_length, component_size_shift, allocator_type))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 59 | if (UNLIKELY(new_array == nullptr)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 60 | CHECK(self->IsExceptionPending()); |
Mathieu Chartier | 5bb9903 | 2014-02-08 16:20:58 -0800 | [diff] [blame] | 61 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 62 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 63 | if (current_dimension + 1 < dimensions->GetLength()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 64 | // Create a new sub-array in every element of the array. |
| 65 | for (int32_t i = 0; i < array_length; i++) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 66 | ObjPtr<Array> sub_array = |
| 67 | RecursiveCreateMultiArray(self, h_component_type, current_dimension + 1, dimensions); |
Mathieu Chartier | 5bb9903 | 2014-02-08 16:20:58 -0800 | [diff] [blame] | 68 | if (UNLIKELY(sub_array == nullptr)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 69 | CHECK(self->IsExceptionPending()); |
Mathieu Chartier | 5bb9903 | 2014-02-08 16:20:58 -0800 | [diff] [blame] | 70 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 71 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 72 | // Use non-transactional mode without check. |
| 73 | new_array->AsObjectArray<Array>()->Set<false, false>(i, sub_array); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 74 | } |
| 75 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 76 | return new_array.Get(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 79 | ObjPtr<Array> Array::CreateMultiArray(Thread* self, |
| 80 | Handle<Class> element_class, |
| 81 | Handle<IntArray> dimensions) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 82 | // Verify dimensions. |
| 83 | // |
| 84 | // The caller is responsible for verifying that "dimArray" is non-null |
| 85 | // and has a length > 0 and <= 255. |
| 86 | int num_dimensions = dimensions->GetLength(); |
| 87 | DCHECK_GT(num_dimensions, 0); |
| 88 | DCHECK_LE(num_dimensions, 255); |
| 89 | |
| 90 | for (int i = 0; i < num_dimensions; i++) { |
| 91 | int dimension = dimensions->Get(i); |
| 92 | if (UNLIKELY(dimension < 0)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 93 | ThrowNegativeArraySizeException(StringPrintf("Dimension %d: %d", i, dimension).c_str()); |
Mathieu Chartier | 5bb9903 | 2014-02-08 16:20:58 -0800 | [diff] [blame] | 94 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 98 | // Find/generate the array class. |
| 99 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 100 | StackHandleScope<1> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 101 | MutableHandle<mirror::Class> array_class( |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 102 | hs.NewHandle(class_linker->FindArrayClass(self, element_class.Get()))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 103 | if (UNLIKELY(array_class == nullptr)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 104 | CHECK(self->IsExceptionPending()); |
Mathieu Chartier | 5bb9903 | 2014-02-08 16:20:58 -0800 | [diff] [blame] | 105 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 106 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 107 | for (int32_t i = 1; i < dimensions->GetLength(); ++i) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 108 | array_class.Assign(class_linker->FindArrayClass(self, array_class.Get())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 109 | if (UNLIKELY(array_class == nullptr)) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 110 | CHECK(self->IsExceptionPending()); |
| 111 | return nullptr; |
| 112 | } |
| 113 | } |
| 114 | // Create the array. |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 115 | ObjPtr<Array> new_array = RecursiveCreateMultiArray(self, array_class, 0, dimensions); |
Mathieu Chartier | 5bb9903 | 2014-02-08 16:20:58 -0800 | [diff] [blame] | 116 | if (UNLIKELY(new_array == nullptr)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 117 | CHECK(self->IsExceptionPending()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 118 | } |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 119 | return new_array.Ptr(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Vladimir Marko | 0278be7 | 2018-05-24 13:30:24 +0100 | [diff] [blame] | 122 | template<typename T> |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 123 | ObjPtr<PrimitiveArray<T>> PrimitiveArray<T>::Alloc(Thread* self, size_t length) { |
| 124 | gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator(); |
| 125 | ObjPtr<Array> raw_array = Array::Alloc<true>(self, |
| 126 | GetClassRoot<PrimitiveArray<T>>(), |
| 127 | length, |
| 128 | ComponentSizeShiftWidth(sizeof(T)), |
| 129 | allocator_type); |
| 130 | return ObjPtr<PrimitiveArray<T>>::DownCast(raw_array); |
Vladimir Marko | 0278be7 | 2018-05-24 13:30:24 +0100 | [diff] [blame] | 131 | } |
| 132 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 133 | void Array::ThrowArrayIndexOutOfBoundsException(int32_t index) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 134 | art::ThrowArrayIndexOutOfBoundsException(index, GetLength()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Mathieu Chartier | 1a5337f | 2016-10-13 13:48:23 -0700 | [diff] [blame] | 137 | void Array::ThrowArrayStoreException(ObjPtr<Object> object) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 138 | art::ThrowArrayStoreException(object->GetClass(), this->GetClass()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 141 | ObjPtr<Array> Array::CopyOf(Thread* self, int32_t new_length) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 142 | CHECK(GetClass()->GetComponentType()->IsPrimitive()) << "Will miss write barriers"; |
| 143 | DCHECK_GE(new_length, 0); |
| 144 | // We may get copied by a compacting GC. |
| 145 | StackHandleScope<1> hs(self); |
| 146 | auto h_this(hs.NewHandle(this)); |
| 147 | auto* heap = Runtime::Current()->GetHeap(); |
| 148 | gc::AllocatorType allocator_type = heap->IsMovableObject(this) ? heap->GetCurrentAllocator() : |
| 149 | heap->GetCurrentNonMovingAllocator(); |
| 150 | const auto component_size = GetClass()->GetComponentSize(); |
| 151 | const auto component_shift = GetClass()->GetComponentSizeShift(); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 152 | ObjPtr<Array> new_array = |
| 153 | Alloc<true>(self, GetClass(), new_length, component_shift, allocator_type); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 154 | if (LIKELY(new_array != nullptr)) { |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 155 | memcpy(new_array->GetRawData(component_size, 0), |
| 156 | h_this->GetRawData(component_size, 0), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 157 | std::min(h_this->GetLength(), new_length) << component_shift); |
| 158 | } |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 159 | return new_array.Ptr(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 162 | // Explicitly instantiate all the primitive array types. |
| 163 | template class PrimitiveArray<uint8_t>; // BooleanArray |
| 164 | template class PrimitiveArray<int8_t>; // ByteArray |
| 165 | template class PrimitiveArray<uint16_t>; // CharArray |
| 166 | template class PrimitiveArray<double>; // DoubleArray |
| 167 | template class PrimitiveArray<float>; // FloatArray |
| 168 | template class PrimitiveArray<int32_t>; // IntArray |
| 169 | template class PrimitiveArray<int64_t>; // LongArray |
| 170 | template class PrimitiveArray<int16_t>; // ShortArray |
| 171 | |
| 172 | } // namespace mirror |
| 173 | } // namespace art |