From 104883b04617a850adf11f05e57c2fd29e09c83c Mon Sep 17 00:00:00 2001 From: Vladimir Marko Date: Fri, 9 Nov 2018 17:12:23 +0000 Subject: Clean up primitive array helpers in Object. Refactor these helpers and avoid read barriers. Remove Class::Is{Int,Long}ArrayClass() and use the Object helpers instead. Remove the AsByteSizedArray() and AsShortSizedArray() helpers that essentially break the type system and rewrite their users, adding appropriate notes. {Float,Double}Array uses in Unsafe would have previously failed a DCHECK(). Test: Additional test in 004-UnsafeTest. Test: m test-art-host-gtest Test: testrunner.py --host --optimizing Change-Id: I88b7e3df7de883f64cfc5eb437a40646f2884685 --- runtime/mirror/object-inl.h | 91 ++++++++++++++++++++------------------------- 1 file changed, 40 insertions(+), 51 deletions(-) (limited to 'runtime/mirror/object-inl.h') diff --git a/runtime/mirror/object-inl.h b/runtime/mirror/object-inl.h index 8ae79a8c66..fcb7479d5d 100644 --- a/runtime/mirror/object-inl.h +++ b/runtime/mirror/object-inl.h @@ -192,113 +192,102 @@ inline Array* Object::AsArray() { return down_cast(this); } +template +ALWAYS_INLINE bool Object::IsSpecificPrimitiveArray() { + // We do not need a read barrier here as the primitive type is constant, + // both from-space and to-space component type classes shall yield the same result. + ObjPtr klass = GetClass(); + constexpr auto kNewFlags = RemoveThisFlags(kVerifyFlags); + ObjPtr const component_type = klass->GetComponentType(); + return component_type != nullptr && + component_type->GetPrimitiveType() == kType; +} + +template +inline bool Object::IsBooleanArray() { + return IsSpecificPrimitiveArray(); +} + template inline BooleanArray* Object::AsBooleanArray() { - constexpr auto kNewFlags = RemoveThisFlags(kVerifyFlags); - DCHECK(GetClass()->IsArrayClass()); - DCHECK(GetClass()->GetComponentType()->IsPrimitiveBoolean()); + DCHECK(IsBooleanArray()); return down_cast(this); } +template +inline bool Object::IsByteArray() { + return IsSpecificPrimitiveArray(); +} + template inline ByteArray* Object::AsByteArray() { - constexpr auto kNewFlags = RemoveThisFlags(kVerifyFlags); - DCHECK(GetClass()->IsArrayClass()); - DCHECK(GetClass()->template GetComponentType()->IsPrimitiveByte()); + DCHECK(IsByteArray()); return down_cast(this); } template -inline ByteArray* Object::AsByteSizedArray() { - constexpr auto kNewFlags = RemoveThisFlags(kVerifyFlags); - DCHECK(GetClass()->IsArrayClass()); - DCHECK(GetClass()->template GetComponentType()->IsPrimitiveByte() || - GetClass()->template GetComponentType()->IsPrimitiveBoolean()); - return down_cast(this); +inline bool Object::IsCharArray() { + return IsSpecificPrimitiveArray(); } template inline CharArray* Object::AsCharArray() { - constexpr auto kNewFlags = RemoveThisFlags(kVerifyFlags); - DCHECK(GetClass()->IsArrayClass()); - DCHECK(GetClass()->template GetComponentType()->IsPrimitiveChar()); + DCHECK(IsCharArray()); return down_cast(this); } template -inline ShortArray* Object::AsShortArray() { - constexpr auto kNewFlags = RemoveThisFlags(kVerifyFlags); - DCHECK(GetClass()->IsArrayClass()); - DCHECK(GetClass()->template GetComponentType()->IsPrimitiveShort()); - return down_cast(this); +inline bool Object::IsShortArray() { + return IsSpecificPrimitiveArray(); } template -inline ShortArray* Object::AsShortSizedArray() { - constexpr auto kNewFlags = RemoveThisFlags(kVerifyFlags); - DCHECK(GetClass()->IsArrayClass()); - DCHECK(GetClass()->template GetComponentType()->IsPrimitiveShort() || - GetClass()->template GetComponentType()->IsPrimitiveChar()); +inline ShortArray* Object::AsShortArray() { + DCHECK(IsShortArray()); return down_cast(this); } -template +template inline bool Object::IsIntArray() { - constexpr auto kNewFlags = RemoveThisFlags(kVerifyFlags); - ObjPtr klass = GetClass(); - ObjPtr component_type = klass->GetComponentType(); - return component_type != nullptr && component_type->template IsPrimitiveInt(); + return IsSpecificPrimitiveArray(); } -template +template inline IntArray* Object::AsIntArray() { - DCHECK((IsIntArray())); + DCHECK((IsIntArray())); return down_cast(this); } -template +template inline bool Object::IsLongArray() { - constexpr auto kNewFlags = RemoveThisFlags(kVerifyFlags); - ObjPtr klass = GetClass(); - ObjPtr component_type = klass->GetComponentType(); - return component_type != nullptr && component_type->template IsPrimitiveLong(); + return IsSpecificPrimitiveArray(); } -template +template inline LongArray* Object::AsLongArray() { - DCHECK((IsLongArray())); + DCHECK((IsLongArray())); return down_cast(this); } template inline bool Object::IsFloatArray() { - constexpr auto kNewFlags = RemoveThisFlags(kVerifyFlags); - auto* component_type = GetClass()->GetComponentType(); - return component_type != nullptr && component_type->template IsPrimitiveFloat(); + return IsSpecificPrimitiveArray(); } template inline FloatArray* Object::AsFloatArray() { DCHECK(IsFloatArray()); - constexpr auto kNewFlags = RemoveThisFlags(kVerifyFlags); - DCHECK(GetClass()->IsArrayClass()); - DCHECK(GetClass()->template GetComponentType()->IsPrimitiveFloat()); return down_cast(this); } template inline bool Object::IsDoubleArray() { - constexpr auto kNewFlags = RemoveThisFlags(kVerifyFlags); - auto* component_type = GetClass()->GetComponentType(); - return component_type != nullptr && component_type->template IsPrimitiveDouble(); + return IsSpecificPrimitiveArray(); } template inline DoubleArray* Object::AsDoubleArray() { DCHECK(IsDoubleArray()); - constexpr auto kNewFlags = RemoveThisFlags(kVerifyFlags); - DCHECK(GetClass()->IsArrayClass()); - DCHECK(GetClass()->template GetComponentType()->IsPrimitiveDouble()); return down_cast(this); } -- cgit v1.2.3-59-g8ed1b