blob: 103efa36b5a92d2214151b32fcbd8e44e47a70ac [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"
21#include "gc/card_table-inl.h"
22#include "object-inl.h"
23#include "object_array.h"
24#include "object_array-inl.h"
25#include "object_utils.h"
26#include "sirt_ref.h"
27#include "thread.h"
28#include "utils.h"
29
30namespace art {
31namespace mirror {
32
33Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
34 size_t component_size) {
35 DCHECK(array_class != NULL);
36 DCHECK_GE(component_count, 0);
37 DCHECK(array_class->IsArrayClass());
38
39 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
40 size_t data_size = component_count * component_size;
41 size_t size = header_size + data_size;
42
43 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
44 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
45 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
46 self->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
47 "%s of length %d would overflow",
48 PrettyDescriptor(array_class).c_str(), component_count);
49 return NULL;
50 }
51
52 Heap* heap = Runtime::Current()->GetHeap();
53 Array* array = down_cast<Array*>(heap->AllocObject(self, array_class, size));
54 if (array != NULL) {
55 DCHECK(array->IsArrayInstance());
56 array->SetLength(component_count);
57 }
58 return array;
59}
60
61Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count) {
62 DCHECK(array_class->IsArrayClass());
63 return Alloc(self, array_class, component_count, array_class->GetComponentSize());
64}
65
66// Create a multi-dimensional array of Objects or primitive types.
67//
68// We have to generate the names for X[], X[][], X[][][], and so on. The
69// easiest way to deal with that is to create the full name once and then
70// subtract pieces off. Besides, we want to start with the outermost
71// piece and work our way in.
72// Recursively create an array with multiple dimensions. Elements may be
73// Objects or primitive types.
74static Array* RecursiveCreateMultiArray(Thread* self, Class* array_class, int current_dimension,
75 IntArray* dimensions)
76 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
77 int32_t array_length = dimensions->Get(current_dimension);
78 SirtRef<Array> new_array(self, Array::Alloc(self, array_class, array_length));
79 if (UNLIKELY(new_array.get() == NULL)) {
80 CHECK(self->IsExceptionPending());
81 return NULL;
82 }
83 if ((current_dimension + 1) < dimensions->GetLength()) {
84 // Create a new sub-array in every element of the array.
85 for (int32_t i = 0; i < array_length; i++) {
86 Array* sub_array = RecursiveCreateMultiArray(self, array_class->GetComponentType(),
87 current_dimension + 1, dimensions);
88 if (UNLIKELY(sub_array == NULL)) {
89 CHECK(self->IsExceptionPending());
90 return NULL;
91 }
92 new_array->AsObjectArray<Array>()->Set(i, sub_array);
93 }
94 }
95 return new_array.get();
96}
97
98Array* Array::CreateMultiArray(Thread* self, Class* element_class, IntArray* dimensions) {
99 // Verify dimensions.
100 //
101 // The caller is responsible for verifying that "dimArray" is non-null
102 // and has a length > 0 and <= 255.
103 int num_dimensions = dimensions->GetLength();
104 DCHECK_GT(num_dimensions, 0);
105 DCHECK_LE(num_dimensions, 255);
106
107 for (int i = 0; i < num_dimensions; i++) {
108 int dimension = dimensions->Get(i);
109 if (UNLIKELY(dimension < 0)) {
110 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;",
111 "Dimension %d: %d", i, dimension);
112 return NULL;
113 }
114 }
115
116 // Generate the full name of the array class.
117 std::string descriptor(num_dimensions, '[');
118 descriptor += ClassHelper(element_class).GetDescriptor();
119
120 // Find/generate the array class.
121 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
122 Class* array_class = class_linker->FindClass(descriptor.c_str(), element_class->GetClassLoader());
123 if (UNLIKELY(array_class == NULL)) {
124 CHECK(self->IsExceptionPending());
125 return NULL;
126 }
127 // create the array
128 Array* new_array = RecursiveCreateMultiArray(self, array_class, 0, dimensions);
129 if (UNLIKELY(new_array == NULL)) {
130 CHECK(self->IsExceptionPending());
131 return NULL;
132 }
133 return new_array;
134}
135
136bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
137 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
138 "length=%i; index=%i", length_, index);
139 return false;
140}
141
142bool Array::ThrowArrayStoreException(Object* object) const {
143 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
144 "%s cannot be stored in an array of type %s",
145 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
146 return false;
147}
148
149template<typename T>
150PrimitiveArray<T>* PrimitiveArray<T>::Alloc(Thread* self, size_t length) {
151 DCHECK(array_class_ != NULL);
152 Array* raw_array = Array::Alloc(self, array_class_, length, sizeof(T));
153 return down_cast<PrimitiveArray<T>*>(raw_array);
154}
155
156template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
157
158// Explicitly instantiate all the primitive array types.
159template class PrimitiveArray<uint8_t>; // BooleanArray
160template class PrimitiveArray<int8_t>; // ByteArray
161template class PrimitiveArray<uint16_t>; // CharArray
162template class PrimitiveArray<double>; // DoubleArray
163template class PrimitiveArray<float>; // FloatArray
164template class PrimitiveArray<int32_t>; // IntArray
165template class PrimitiveArray<int64_t>; // LongArray
166template class PrimitiveArray<int16_t>; // ShortArray
167
168} // namespace mirror
169} // namespace art