// Create a multi-dimensional array of Objects or primitive types. // // We have to generate the names for X[], X[][], X[][][], and so on. The // easiest way to deal with that is to create the full name once and then // subtract pieces off. Besides, we want to start with the outermost // piece and work our way in. // Recursively create an array with multiple dimensions. Elements may be // Objects or primitive types. static ObjPtr<Array> RecursiveCreateMultiArray(Thread* self,
Handle<Class> array_class, int current_dimension,
Handle<mirror::IntArray> dimensions)
REQUIRES_SHARED(Locks::mutator_lock_) {
int32_t array_length = dimensions->Get(current_dimension);
StackHandleScope<2> hs(self);
Handle<mirror::Class> h_component_type(hs.NewHandle(array_class->GetComponentType()));
size_t component_size_shift = h_component_type->GetPrimitiveTypeSizeShift();
gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
Handle<Array> new_array(hs.NewHandle(
Array::Alloc(self, array_class.Get(), array_length, component_size_shift, allocator_type))); if (UNLIKELY(new_array == nullptr)) {
CHECK(self->IsExceptionPending()); return nullptr;
} if (current_dimension + 1 < dimensions->GetLength()) { // Create a new sub-array in every element of the array. for (int32_t i = 0; i < array_length; i++) {
ObjPtr<Array> sub_array =
RecursiveCreateMultiArray(self, h_component_type, current_dimension + 1, dimensions); if (UNLIKELY(sub_array == nullptr)) {
CHECK(self->IsExceptionPending()); return nullptr;
} // Use non-transactional mode without check.
new_array->AsObjectArray<Array>()->Set<false, false>(i, sub_array);
}
} return new_array.Get();
}
ObjPtr<Array> Array::CreateMultiArray(Thread* self,
Handle<Class> element_class,
Handle<IntArray> dimensions) { // Verify dimensions. // // The caller is responsible for verifying that "dimArray" is non-null // and has a length > 0 and <= 255. int num_dimensions = dimensions->GetLength();
DCHECK_GT(num_dimensions, 0);
DCHECK_LE(num_dimensions, 255);
for (int i = 0; i < num_dimensions; i++) { int dimension = dimensions->Get(i); if (UNLIKELY(dimension < 0)) {
ThrowNegativeArraySizeException(StringPrintf("Dimension %d: %d", i, dimension).c_str()); return nullptr;
}
}
// Find/generate the array class.
ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
StackHandleScope<1> hs(self);
MutableHandle<mirror::Class> array_class(
hs.NewHandle(class_linker->FindArrayClass(self, element_class.Get()))); if (UNLIKELY(array_class == nullptr)) {
CHECK(self->IsExceptionPending()); return nullptr;
} for (int32_t i = 1; i < dimensions->GetLength(); ++i) {
array_class.Assign(class_linker->FindArrayClass(self, array_class.Get())); if (UNLIKELY(array_class == nullptr)) {
CHECK(self->IsExceptionPending()); return nullptr;
}
} // Create the array.
ObjPtr<Array> new_array = RecursiveCreateMultiArray(self, array_class, 0, dimensions); if (UNLIKELY(new_array == nullptr)) {
CHECK(self->IsExceptionPending());
} return new_array.Ptr();
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.