Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_MIRROR_ARRAY_H_ |
| 18 | #define ART_RUNTIME_MIRROR_ARRAY_H_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | |
| 20 | #include "object.h" |
| 21 | |
| 22 | namespace art { |
| 23 | namespace mirror { |
| 24 | |
| 25 | class MANAGED Array : public Object { |
| 26 | public: |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 27 | // A convenience for code that doesn't know the component size, and doesn't want to have to work |
| 28 | // it out itself. |
| 29 | template <bool kIsMovable, bool kIsInstrumented> |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 30 | static Array* Alloc(Thread* self, Class* array_class, int32_t component_count) |
| 31 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 32 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 33 | template <bool kIsMovable, bool kIsInstrumented> |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 34 | static Array* Alloc(Thread* self, Class* array_class, int32_t component_count, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 35 | size_t component_size) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 36 | |
| 37 | static Array* CreateMultiArray(Thread* self, Class* element_class, IntArray* dimensions) |
| 38 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 39 | |
| 40 | size_t SizeOf() const; |
| 41 | |
| 42 | int32_t GetLength() const { |
| 43 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false); |
| 44 | } |
| 45 | |
| 46 | void SetLength(int32_t length) { |
| 47 | CHECK_GE(length, 0); |
| 48 | SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false); |
| 49 | } |
| 50 | |
| 51 | static MemberOffset LengthOffset() { |
| 52 | return OFFSET_OF_OBJECT_MEMBER(Array, length_); |
| 53 | } |
| 54 | |
| 55 | static MemberOffset DataOffset(size_t component_size) { |
| 56 | if (component_size != sizeof(int64_t)) { |
| 57 | return OFFSET_OF_OBJECT_MEMBER(Array, first_element_); |
| 58 | } else { |
| 59 | // Align longs and doubles. |
| 60 | return MemberOffset(OFFSETOF_MEMBER(Array, first_element_) + 4); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void* GetRawData(size_t component_size) { |
| 65 | intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value(); |
| 66 | return reinterpret_cast<void*>(data); |
| 67 | } |
| 68 | |
| 69 | const void* GetRawData(size_t component_size) const { |
| 70 | intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value(); |
| 71 | return reinterpret_cast<const void*>(data); |
| 72 | } |
| 73 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 74 | bool IsValidIndex(int32_t index) const |
| 75 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 66d1aee | 2013-07-23 11:59:01 +0200 | [diff] [blame] | 76 | if (UNLIKELY(static_cast<uint32_t>(index) >= static_cast<uint32_t>(GetLength()))) { |
Sebastien Hertz | 9897be9 | 2013-06-27 18:24:46 +0200 | [diff] [blame] | 77 | ThrowArrayIndexOutOfBoundsException(index); |
| 78 | return false; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 79 | } |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | protected: |
Sebastien Hertz | 9897be9 | 2013-06-27 18:24:46 +0200 | [diff] [blame] | 84 | void ThrowArrayIndexOutOfBoundsException(int32_t index) const |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 85 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Sebastien Hertz | 9897be9 | 2013-06-27 18:24:46 +0200 | [diff] [blame] | 86 | void ThrowArrayStoreException(Object* object) const |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 87 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 88 | |
| 89 | private: |
| 90 | // The number of array elements. |
| 91 | int32_t length_; |
| 92 | // Marker for the data (used by generated code) |
| 93 | uint32_t first_element_[0]; |
| 94 | |
| 95 | DISALLOW_IMPLICIT_CONSTRUCTORS(Array); |
| 96 | }; |
| 97 | |
| 98 | template<class T> |
| 99 | class MANAGED PrimitiveArray : public Array { |
| 100 | public: |
| 101 | typedef T ElementType; |
| 102 | |
| 103 | static PrimitiveArray<T>* Alloc(Thread* self, size_t length) |
| 104 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 105 | |
| 106 | const T* GetData() const { |
| 107 | intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value(); |
| 108 | return reinterpret_cast<T*>(data); |
| 109 | } |
| 110 | |
| 111 | T* GetData() { |
| 112 | intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value(); |
| 113 | return reinterpret_cast<T*>(data); |
| 114 | } |
| 115 | |
| 116 | T Get(int32_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 117 | if (!IsValidIndex(i)) { |
| 118 | return T(0); |
| 119 | } |
| 120 | return GetData()[i]; |
| 121 | } |
| 122 | |
| 123 | void Set(int32_t i, T value) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 124 | if (IsValidIndex(i)) { |
| 125 | GetData()[i] = value; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | static void SetArrayClass(Class* array_class) { |
| 130 | CHECK(array_class_ == NULL); |
| 131 | CHECK(array_class != NULL); |
| 132 | array_class_ = array_class; |
| 133 | } |
| 134 | |
| 135 | static void ResetArrayClass() { |
| 136 | CHECK(array_class_ != NULL); |
| 137 | array_class_ = NULL; |
| 138 | } |
| 139 | |
| 140 | private: |
| 141 | static Class* array_class_; |
| 142 | |
| 143 | DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray); |
| 144 | }; |
| 145 | |
| 146 | } // namespace mirror |
| 147 | } // namespace art |
| 148 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 149 | #endif // ART_RUNTIME_MIRROR_ARRAY_H_ |