From ef7d42fca18c16fbaf103822ad16f23246e2905d Mon Sep 17 00:00:00 2001 From: Ian Rogers Date: Mon, 6 Jan 2014 12:55:46 -0800 Subject: Object model changes to support 64bit. Modify mirror objects so that references between them use an ObjectReference value type rather than an Object* so that functionality to compress larger references can be captured in the ObjectRefererence implementation. ObjectReferences are 32bit and all other aspects of object layout remain as they are currently. Expand fields in objects holding pointers so they can hold 64bit pointers. Its expected the size of these will come down by improving where we hold compiler meta-data. Stub out x86_64 architecture specific runtime implementation. Modify OutputStream so that reads and writes are of unsigned quantities. Make the use of portable or quick code more explicit. Templatize AtomicInteger to support more than just int32_t as a type. Add missing, and fix issues relating to, missing annotalysis information on the mutator lock. Refactor and share implementations for array copy between System and uses elsewhere in the runtime. Fix numerous 64bit build issues. Change-Id: I1a5694c251a42c9eff71084dfdd4b51fff716822 --- runtime/mirror/object_array-inl.h | 162 ++++++++++++++++++++++++++------------ 1 file changed, 110 insertions(+), 52 deletions(-) (limited to 'runtime/mirror/object_array-inl.h') 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 namespace art { namespace mirror { @@ -32,8 +33,8 @@ namespace mirror { template inline ObjectArray* ObjectArray::Alloc(Thread* self, Class* object_array_class, int32_t length, gc::AllocatorType allocator_type) { - Array* array = Array::Alloc(self, object_array_class, length, sizeof(Object*), - allocator_type); + Array* array = Array::Alloc(self, object_array_class, length, + sizeof(HeapReference), allocator_type); if (UNLIKELY(array == nullptr)) { return nullptr; } else { @@ -49,12 +50,12 @@ inline ObjectArray* ObjectArray::Alloc(Thread* self, Class* object_array_c } template -inline T* ObjectArray::Get(int32_t i) const { +inline T* ObjectArray::Get(int32_t i) { if (UNLIKELY(!CheckIsValidIndex(i))) { DCHECK(Thread::Current()->IsExceptionPending()); return NULL; } - return GetWithoutChecks(i); + return GetFieldObject(OffsetOfElement(i), false); } template @@ -72,7 +73,7 @@ inline bool ObjectArray::CheckAssignable(T* object) { template inline void ObjectArray::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 inline void ObjectArray::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 -inline void ObjectArray::SetPtrWithoutChecks(int32_t i, T* object) { +inline void ObjectArray::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 -inline T* ObjectArray::GetWithoutChecks(int32_t i) const { +inline T* ObjectArray::GetWithoutChecks(int32_t i) { DCHECK(CheckIsValidIndex(i)); - MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*)); - return GetFieldObject(data_offset, false); + return GetFieldObject(OffsetOfElement(i), false); } template -inline void ObjectArray::Copy(const ObjectArray* src, int src_pos, - ObjectArray* 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(src_offset, false); - heap->VerifyObject(object); - // directly set field, we do a bulk write barrier at the end - dst->SetField32(dst_offset, reinterpret_cast(object), false, true); - src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*)); - dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*)); - } +inline void ObjectArray::AssignableMemmove(int32_t dst_pos, ObjectArray* 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), sizeof(uint32_t)); + IntArray* dstAsIntArray = reinterpret_cast(this); + IntArray* srcAsIntArray = reinterpret_cast(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 +inline void ObjectArray::AssignableMemcpy(int32_t dst_pos, ObjectArray* 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), sizeof(uint32_t)); + IntArray* dstAsIntArray = reinterpret_cast(this); + IntArray* srcAsIntArray = reinterpret_cast(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 +inline void ObjectArray::AssignableCheckingMemcpy(int32_t dst_pos, ObjectArray* 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(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(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 inline ObjectArray* ObjectArray::CopyOf(Thread* self, int32_t new_length) { + DCHECK_GE(new_length, 0); // We may get copied by a compacting GC. SirtRef > sirt_this(self, this); gc::Heap* heap = Runtime::Current()->GetHeap(); @@ -155,11 +207,17 @@ inline ObjectArray* ObjectArray::CopyOf(Thread* self, int32_t new_length) heap->GetCurrentNonMovingAllocator(); ObjectArray* 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 +inline MemberOffset ObjectArray::OffsetOfElement(int32_t i) { + return MemberOffset(DataOffset(sizeof(HeapReference)).Int32Value() + + (i * sizeof(HeapReference))); +} + } // namespace mirror } // namespace art -- cgit v1.2.3-59-g8ed1b