diff options
author | Sebastien Hertz <shertz@google.com> | 2014-01-27 18:01:39 +0100 |
---|---|---|
committer | Sebastien Hertz <shertz@google.com> | 2014-01-28 18:18:14 +0100 |
commit | abff6439db28fbbed95490bfff7e24d1fdf5b771 (patch) | |
tree | 6ed749056b1d2e29f2bdf81c13e80e15849e9836 /runtime/mirror/array.h | |
parent | 7ea5dafc81b2bba7cabad26130bb75dc8f709803 (diff) | |
download | art-abff6439db28fbbed95490bfff7e24d1fdf5b771.tar.gz art-abff6439db28fbbed95490bfff7e24d1fdf5b771.tar.bz2 art-abff6439db28fbbed95490bfff7e24d1fdf5b771.zip |
Refactor array access for the interpreter.
Adds GetWithoutChecks and SetWithoutChecks methods in PrimitiveArray and use
them in the interpreter. Updates Get and Set methods to rely on them and adds
some DCHECK to control exception flow.
Renames IsValidIndex into CheckIsValidIndex to reflect it can throw an
exception. It's also more consistent with ObjectArray::CheckIsAssignable.
Make ThrowArrayIndexOutOfBoundsException private in Array since it's only used
by Array::CheckIsValidIndex.
Updates DoFilledNewArray to use SetWithoutChecks rather than Set.
Change-Id: I2fd314d77a67cf969843d499b86d04ca7b7a43e6
Diffstat (limited to 'runtime/mirror/array.h')
-rw-r--r-- | runtime/mirror/array.h | 29 |
1 files changed, 23 insertions, 6 deletions
diff --git a/runtime/mirror/array.h b/runtime/mirror/array.h index 52659464fc..207573fa00 100644 --- a/runtime/mirror/array.h +++ b/runtime/mirror/array.h @@ -19,6 +19,7 @@ #include "object.h" #include "gc/heap.h" +#include "thread.h" namespace art { namespace mirror { @@ -83,7 +84,9 @@ class MANAGED Array : public Object { return reinterpret_cast<const void*>(data); } - bool IsValidIndex(int32_t index) const + // Returns true if the index is valid. If not, throws an ArrayIndexOutOfBoundsException and + // returns false. + bool CheckIsValidIndex(int32_t index) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { if (UNLIKELY(static_cast<uint32_t>(index) >= static_cast<uint32_t>(GetLength()))) { ThrowArrayIndexOutOfBoundsException(index); @@ -93,12 +96,13 @@ class MANAGED Array : public Object { } protected: - void ThrowArrayIndexOutOfBoundsException(int32_t index) const - SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); void ThrowArrayStoreException(Object* object) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); private: + void ThrowArrayIndexOutOfBoundsException(int32_t index) const + SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); + // The number of array elements. int32_t length_; // Marker for the data (used by generated code) @@ -126,18 +130,31 @@ class MANAGED PrimitiveArray : public Array { } T Get(int32_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { - if (!IsValidIndex(i)) { + if (UNLIKELY(!CheckIsValidIndex(i))) { + DCHECK(Thread::Current()->IsExceptionPending()); return T(0); } + return GetWithoutChecks(i); + } + + T GetWithoutChecks(int32_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { + DCHECK(CheckIsValidIndex(i)); return GetData()[i]; } void Set(int32_t i, T value) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { - if (IsValidIndex(i)) { - GetData()[i] = value; + if (LIKELY(CheckIsValidIndex(i))) { + SetWithoutChecks(i, value); + } else { + DCHECK(Thread::Current()->IsExceptionPending()); } } + void SetWithoutChecks(int32_t i, T value) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { + DCHECK(CheckIsValidIndex(i)); + GetData()[i] = value; + } + static void SetArrayClass(Class* array_class) { CHECK(array_class_ == NULL); CHECK(array_class != NULL); |