diff options
Diffstat (limited to 'runtime/mirror/object_array-inl.h')
-rw-r--r-- | runtime/mirror/object_array-inl.h | 162 |
1 files changed, 110 insertions, 52 deletions
diff --git a/runtime/mirror/object_array-inl.h b/runtime/mirror/object_array-inl.h index 6a50dfe019..c3424793e9 100644 --- a/runtime/mirror/object_array-inl.h +++ b/runtime/mirror/object_array-inl.h @@ -25,6 +25,7 @@ #include "runtime.h" #include "sirt_ref.h" #include "thread.h" +#include <string> namespace art { namespace mirror { @@ -32,8 +33,8 @@ namespace mirror { template<class T> inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self, Class* object_array_class, int32_t length, gc::AllocatorType allocator_type) { - Array* array = Array::Alloc<true>(self, object_array_class, length, sizeof(Object*), - allocator_type); + Array* array = Array::Alloc<true>(self, object_array_class, length, + sizeof(HeapReference<Object>), allocator_type); if (UNLIKELY(array == nullptr)) { return nullptr; } else { @@ -49,12 +50,12 @@ inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self, Class* object_array_c } template<class T> -inline T* ObjectArray<T>::Get(int32_t i) const { +inline T* ObjectArray<T>::Get(int32_t i) { if (UNLIKELY(!CheckIsValidIndex(i))) { DCHECK(Thread::Current()->IsExceptionPending()); return NULL; } - return GetWithoutChecks(i); + return GetFieldObject<T>(OffsetOfElement(i), false); } template<class T> @@ -72,7 +73,7 @@ inline bool ObjectArray<T>::CheckAssignable(T* object) { template<class T> inline void ObjectArray<T>::Set(int32_t i, T* object) { if (LIKELY(CheckIsValidIndex(i) && CheckAssignable(object))) { - SetWithoutChecks(i, object); + SetFieldObject(OffsetOfElement(i), object, false); } else { DCHECK(Thread::Current()->IsExceptionPending()); } @@ -82,72 +83,123 @@ template<class T> inline void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) { DCHECK(CheckIsValidIndex(i)); DCHECK(CheckAssignable(object)); - MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*)); - SetFieldObject(data_offset, object, false); + SetFieldObject(OffsetOfElement(i), object, false); } template<class T> -inline void ObjectArray<T>::SetPtrWithoutChecks(int32_t i, T* object) { +inline void ObjectArray<T>::SetWithoutChecksAndWriteBarrier(int32_t i, T* object) { DCHECK(CheckIsValidIndex(i)); - // TODO enable this check. It fails when writing the image in ImageWriter::FixupObjectArray. + // TODO: enable this check. It fails when writing the image in ImageWriter::FixupObjectArray. // DCHECK(CheckAssignable(object)); - MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*)); - SetFieldPtr(data_offset, object, false); + SetFieldObjectWithoutWriteBarrier(OffsetOfElement(i), object, false); } template<class T> -inline T* ObjectArray<T>::GetWithoutChecks(int32_t i) const { +inline T* ObjectArray<T>::GetWithoutChecks(int32_t i) { DCHECK(CheckIsValidIndex(i)); - MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*)); - return GetFieldObject<T*>(data_offset, false); + return GetFieldObject<T>(OffsetOfElement(i), false); } template<class T> -inline void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos, - ObjectArray<T>* dst, int dst_pos, - size_t length) { - if (src->CheckIsValidIndex(src_pos) && - src->CheckIsValidIndex(src_pos + length - 1) && - dst->CheckIsValidIndex(dst_pos) && - dst->CheckIsValidIndex(dst_pos + length - 1)) { - MemberOffset src_offset(DataOffset(sizeof(Object*)).Int32Value() + src_pos * sizeof(Object*)); - MemberOffset dst_offset(DataOffset(sizeof(Object*)).Int32Value() + dst_pos * sizeof(Object*)); - Class* array_class = dst->GetClass(); - gc::Heap* heap = Runtime::Current()->GetHeap(); - if (array_class == src->GetClass()) { - // No need for array store checks if arrays are of the same type - for (size_t i = 0; i < length; i++) { - Object* object = src->GetFieldObject<Object*>(src_offset, false); - heap->VerifyObject(object); - // directly set field, we do a bulk write barrier at the end - dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true); - src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*)); - dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*)); - } +inline void ObjectArray<T>::AssignableMemmove(int32_t dst_pos, ObjectArray<T>* src, + int32_t src_pos, int32_t count) { + if (kIsDebugBuild) { + for (int i = 0; i < count; ++i) { + // The Get will perform the VerifyObject. + src->GetWithoutChecks(src_pos + i); + } + } + // Perform the memmove using int memmove then perform the write barrier. + CHECK_EQ(sizeof(HeapReference<T>), sizeof(uint32_t)); + IntArray* dstAsIntArray = reinterpret_cast<IntArray*>(this); + IntArray* srcAsIntArray = reinterpret_cast<IntArray*>(src); + dstAsIntArray->Memmove(dst_pos, srcAsIntArray, src_pos, count); + Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count); + if (kIsDebugBuild) { + for (int i = 0; i < count; ++i) { + // The Get will perform the VerifyObject. + GetWithoutChecks(dst_pos + i); + } + } +} + +template<class T> +inline void ObjectArray<T>::AssignableMemcpy(int32_t dst_pos, ObjectArray<T>* src, + int32_t src_pos, int32_t count) { + if (kIsDebugBuild) { + for (int i = 0; i < count; ++i) { + // The Get will perform the VerifyObject. + src->GetWithoutChecks(src_pos + i); + } + } + // Perform the memmove using int memcpy then perform the write barrier. + CHECK_EQ(sizeof(HeapReference<T>), sizeof(uint32_t)); + IntArray* dstAsIntArray = reinterpret_cast<IntArray*>(this); + IntArray* srcAsIntArray = reinterpret_cast<IntArray*>(src); + dstAsIntArray->Memcpy(dst_pos, srcAsIntArray, src_pos, count); + Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count); + if (kIsDebugBuild) { + for (int i = 0; i < count; ++i) { + // The Get will perform the VerifyObject. + GetWithoutChecks(dst_pos + i); + } + } +} + +template<class T> +inline void ObjectArray<T>::AssignableCheckingMemcpy(int32_t dst_pos, ObjectArray<T>* src, + int32_t src_pos, int32_t count, + bool throw_exception) { + DCHECK_NE(this, src) + << "This case should be handled with memmove that handles overlaps correctly"; + // We want to avoid redundant IsAssignableFrom checks where possible, so we cache a class that + // we know is assignable to the destination array's component type. + Class* dst_class = GetClass()->GetComponentType(); + Class* lastAssignableElementClass = dst_class; + + Object* o = nullptr; + int i = 0; + for (; i < count; ++i) { + // The follow get operations force the objects to be verified. + o = src->GetWithoutChecks(src_pos + i); + if (o == nullptr) { + // Null is always assignable. + SetWithoutChecks(dst_pos + i, nullptr); } else { - Class* element_class = array_class->GetComponentType(); - CHECK(!element_class->IsPrimitive()); - for (size_t i = 0; i < length; i++) { - Object* object = src->GetFieldObject<Object*>(src_offset, false); - if (object != NULL && !object->InstanceOf(element_class)) { - dst->ThrowArrayStoreException(object); - return; - } - heap->VerifyObject(object); - // directly set field, we do a bulk write barrier at the end - dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true); - src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*)); - dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*)); + // TODO: use the underlying class reference to avoid uncompression when not necessary. + Class* o_class = o->GetClass(); + if (LIKELY(lastAssignableElementClass == o_class)) { + SetWithoutChecks(dst_pos + i, o); + } else if (LIKELY(dst_class->IsAssignableFrom(o_class))) { + lastAssignableElementClass = o_class; + SetWithoutChecks(dst_pos + i, o); + } else { + // Can't put this element into the array, break to perform write-barrier and throw + // exception. + break; } } - heap->WriteBarrierArray(dst, dst_pos, length); - } else { - DCHECK(Thread::Current()->IsExceptionPending()); + } + Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count); + if (UNLIKELY(i != count)) { + std::string actualSrcType(PrettyTypeOf(o)); + std::string dstType(PrettyTypeOf(this)); + Thread* self = Thread::Current(); + ThrowLocation throw_location = self->GetCurrentLocationForThrow(); + if (throw_exception) { + self->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayStoreException;", + "source[%d] of type %s cannot be stored in destination array of type %s", + src_pos + i, actualSrcType.c_str(), dstType.c_str()); + } else { + LOG(FATAL) << StringPrintf("source[%d] of type %s cannot be stored in destination array of type %s", + src_pos + i, actualSrcType.c_str(), dstType.c_str()); + } } } template<class T> inline ObjectArray<T>* ObjectArray<T>::CopyOf(Thread* self, int32_t new_length) { + DCHECK_GE(new_length, 0); // We may get copied by a compacting GC. SirtRef<ObjectArray<T> > sirt_this(self, this); gc::Heap* heap = Runtime::Current()->GetHeap(); @@ -155,11 +207,17 @@ inline ObjectArray<T>* ObjectArray<T>::CopyOf(Thread* self, int32_t new_length) heap->GetCurrentNonMovingAllocator(); ObjectArray<T>* new_array = Alloc(self, GetClass(), new_length, allocator_type); if (LIKELY(new_array != nullptr)) { - Copy(sirt_this.get(), 0, new_array, 0, std::min(sirt_this->GetLength(), new_length)); + new_array->AssignableMemcpy(0, sirt_this.get(), 0, std::min(sirt_this->GetLength(), new_length)); } return new_array; } +template<class T> +inline MemberOffset ObjectArray<T>::OffsetOfElement(int32_t i) { + return MemberOffset(DataOffset(sizeof(HeapReference<Object>)).Int32Value() + + (i * sizeof(HeapReference<Object>))); +} + } // namespace mirror } // namespace art |