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-inl.h"
18
19 #include "array-alloc-inl.h"
20 #include "base/utils.h"
21 #include "class-inl.h"
22 #include "class.h"
23 #include "class_linker-inl.h"
24 #include "class_root.h"
25 #include "common_throws.h"
26 #include "dex/dex_file-inl.h"
27 #include "gc/accounting/card_table-inl.h"
28 #include "handle_scope-inl.h"
29 #include "object-inl.h"
30 #include "object_array-alloc-inl.h"
31 #include "object_array-inl.h"
32 #include "thread.h"
33
34 namespace art {
35 namespace mirror {
36
37 using android::base::StringPrintf;
38
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.
RecursiveCreateMultiArray(Thread * self,Handle<Class> array_class,int current_dimension,Handle<mirror::IntArray> dimensions)47 static ObjPtr<Array> RecursiveCreateMultiArray(Thread* self,
48 Handle<Class> array_class,
49 int current_dimension,
50 Handle<mirror::IntArray> dimensions)
51 REQUIRES_SHARED(Locks::mutator_lock_) {
52 int32_t array_length = dimensions->Get(current_dimension);
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)));
59 if (UNLIKELY(new_array == nullptr)) {
60 CHECK(self->IsExceptionPending());
61 return nullptr;
62 }
63 if (current_dimension + 1 < dimensions->GetLength()) {
64 // Create a new sub-array in every element of the array.
65 for (int32_t i = 0; i < array_length; i++) {
66 ObjPtr<Array> sub_array =
67 RecursiveCreateMultiArray(self, h_component_type, current_dimension + 1, dimensions);
68 if (UNLIKELY(sub_array == nullptr)) {
69 CHECK(self->IsExceptionPending());
70 return nullptr;
71 }
72 // Use non-transactional mode without check.
73 new_array->AsObjectArray<Array>()->Set<false, false>(i, sub_array);
74 }
75 }
76 return new_array.Get();
77 }
78
CreateMultiArray(Thread * self,Handle<Class> element_class,Handle<IntArray> dimensions)79 ObjPtr<Array> Array::CreateMultiArray(Thread* self,
80 Handle<Class> element_class,
81 Handle<IntArray> dimensions) {
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)) {
93 ThrowNegativeArraySizeException(StringPrintf("Dimension %d: %d", i, dimension).c_str());
94 return nullptr;
95 }
96 }
97
98 // Find/generate the array class.
99 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
100 StackHandleScope<1> hs(self);
101 MutableHandle<mirror::Class> array_class(
102 hs.NewHandle(class_linker->FindArrayClass(self, element_class.Get())));
103 if (UNLIKELY(array_class == nullptr)) {
104 CHECK(self->IsExceptionPending());
105 return nullptr;
106 }
107 for (int32_t i = 1; i < dimensions->GetLength(); ++i) {
108 array_class.Assign(class_linker->FindArrayClass(self, array_class.Get()));
109 if (UNLIKELY(array_class == nullptr)) {
110 CHECK(self->IsExceptionPending());
111 return nullptr;
112 }
113 }
114 // Create the array.
115 ObjPtr<Array> new_array = RecursiveCreateMultiArray(self, array_class, 0, dimensions);
116 if (UNLIKELY(new_array == nullptr)) {
117 CHECK(self->IsExceptionPending());
118 }
119 return new_array.Ptr();
120 }
121
122 template<typename T>
Alloc(Thread * self,size_t length)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);
131 }
132
ThrowArrayIndexOutOfBoundsException(int32_t index)133 void Array::ThrowArrayIndexOutOfBoundsException(int32_t index) {
134 art::ThrowArrayIndexOutOfBoundsException(index, GetLength());
135 }
136
ThrowArrayStoreException(ObjPtr<Object> object)137 void Array::ThrowArrayStoreException(ObjPtr<Object> object) {
138 art::ThrowArrayStoreException(object->GetClass(), this->GetClass());
139 }
140
CopyOf(Thread * self,int32_t new_length)141 ObjPtr<Array> Array::CopyOf(Thread* self, int32_t new_length) {
142 ObjPtr<Class> klass = GetClass();
143 CHECK(klass->IsPrimitiveArray()) << "Will miss write barriers";
144 DCHECK_GE(new_length, 0);
145 // We may get copied by a compacting GC.
146 StackHandleScope<1> hs(self);
147 auto h_this(hs.NewHandle(this));
148 auto* heap = Runtime::Current()->GetHeap();
149 gc::AllocatorType allocator_type = heap->IsMovableObject(this) ? heap->GetCurrentAllocator() :
150 heap->GetCurrentNonMovingAllocator();
151 const auto component_size = klass->GetComponentSize();
152 const auto component_shift = klass->GetComponentSizeShift();
153 ObjPtr<Array> new_array =
154 Alloc<true>(self, klass, new_length, component_shift, allocator_type); // Invalidates klass.
155 if (LIKELY(new_array != nullptr)) {
156 memcpy(new_array->GetRawData(component_size, 0),
157 h_this->GetRawData(component_size, 0),
158 std::min(h_this->GetLength(), new_length) << component_shift);
159 }
160 return new_array;
161 }
162
163 // Explicitly instantiate all the primitive array types.
164 template class PrimitiveArray<uint8_t>; // BooleanArray
165 template class PrimitiveArray<int8_t>; // ByteArray
166 template class PrimitiveArray<uint16_t>; // CharArray
167 template class PrimitiveArray<double>; // DoubleArray
168 template class PrimitiveArray<float>; // FloatArray
169 template class PrimitiveArray<int32_t>; // IntArray
170 template class PrimitiveArray<int64_t>; // LongArray
171 template class PrimitiveArray<int16_t>; // ShortArray
172
173 } // namespace mirror
174 } // namespace art
175