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