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" |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 21 | #include "object_callbacks.h" |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 22 | #include "gc/heap.h" |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 23 | #include "runtime.h" |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 24 | #include "thread.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | namespace mirror { |
| 28 | |
| 29 | class MANAGED Array : public Object { |
| 30 | public: |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 31 | // A convenience for code that doesn't know the component size, and doesn't want to have to work |
| 32 | // it out itself. |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 33 | template <bool kIsInstrumented> |
| 34 | static Array* Alloc(Thread* self, Class* array_class, int32_t component_count, |
| 35 | gc::AllocatorType allocator_type) |
| 36 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 37 | |
| 38 | template <bool kIsInstrumented> |
| 39 | static Array* Alloc(Thread* self, Class* array_class, int32_t component_count, |
| 40 | size_t component_size, gc::AllocatorType allocator_type) |
| 41 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 42 | |
| 43 | template <bool kIsInstrumented> |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 44 | static Array* Alloc(Thread* self, Class* array_class, int32_t component_count) |
| 45 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 46 | |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 47 | template <bool kIsInstrumented> |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 48 | static Array* Alloc(Thread* self, Class* array_class, int32_t component_count, |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 49 | size_t component_size) |
| 50 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 51 | |
Mathieu Chartier | 5bb9903 | 2014-02-08 16:20:58 -0800 | [diff] [blame] | 52 | static Array* CreateMultiArray(Thread* self, const SirtRef<Class>& element_class, |
| 53 | const SirtRef<IntArray>& dimensions) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 54 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 55 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame^] | 56 | template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 57 | size_t SizeOf() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame^] | 58 | template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 59 | int32_t GetLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame^] | 60 | return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Array, length_), false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 63 | void SetLength(int32_t length) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 64 | CHECK_GE(length, 0); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 65 | // We use non transactional version since we can't undo this write. We also disable checking |
| 66 | // since it would fail during a transaction. |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame^] | 67 | SetField32<false, false, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static MemberOffset LengthOffset() { |
| 71 | return OFFSET_OF_OBJECT_MEMBER(Array, length_); |
| 72 | } |
| 73 | |
| 74 | static MemberOffset DataOffset(size_t component_size) { |
| 75 | if (component_size != sizeof(int64_t)) { |
| 76 | return OFFSET_OF_OBJECT_MEMBER(Array, first_element_); |
| 77 | } else { |
| 78 | // Align longs and doubles. |
| 79 | return MemberOffset(OFFSETOF_MEMBER(Array, first_element_) + 4); |
| 80 | } |
| 81 | } |
| 82 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 83 | void* GetRawData(size_t component_size, int32_t index) |
| 84 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 85 | intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() + |
| 86 | + (index * component_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 87 | return reinterpret_cast<void*>(data); |
| 88 | } |
| 89 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 90 | const void* GetRawData(size_t component_size, int32_t index) const { |
| 91 | intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() + |
| 92 | + (index * component_size); |
| 93 | return reinterpret_cast<void*>(data); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 96 | // Returns true if the index is valid. If not, throws an ArrayIndexOutOfBoundsException and |
| 97 | // returns false. |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame^] | 98 | template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 99 | bool CheckIsValidIndex(int32_t index) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame^] | 100 | if (UNLIKELY(static_cast<uint32_t>(index) >= |
| 101 | static_cast<uint32_t>(GetLength<kVerifyFlags>()))) { |
Sebastien Hertz | 9897be9 | 2013-06-27 18:24:46 +0200 | [diff] [blame] | 102 | ThrowArrayIndexOutOfBoundsException(index); |
| 103 | return false; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 104 | } |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | protected: |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 109 | void ThrowArrayStoreException(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 110 | |
| 111 | private: |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 112 | void ThrowArrayIndexOutOfBoundsException(int32_t index) |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 113 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 114 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 115 | // The number of array elements. |
| 116 | int32_t length_; |
| 117 | // Marker for the data (used by generated code) |
| 118 | uint32_t first_element_[0]; |
| 119 | |
| 120 | DISALLOW_IMPLICIT_CONSTRUCTORS(Array); |
| 121 | }; |
| 122 | |
| 123 | template<class T> |
| 124 | class MANAGED PrimitiveArray : public Array { |
| 125 | public: |
| 126 | typedef T ElementType; |
| 127 | |
| 128 | static PrimitiveArray<T>* Alloc(Thread* self, size_t length) |
| 129 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 130 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 131 | const T* GetData() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 132 | return reinterpret_cast<const T*>(GetRawData(sizeof(T), 0)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 135 | T* GetData() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 136 | return reinterpret_cast<T*>(GetRawData(sizeof(T), 0)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 139 | T Get(int32_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 140 | if (UNLIKELY(!CheckIsValidIndex(i))) { |
| 141 | DCHECK(Thread::Current()->IsExceptionPending()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 142 | return T(0); |
| 143 | } |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 144 | return GetWithoutChecks(i); |
| 145 | } |
| 146 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 147 | T GetWithoutChecks(int32_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 148 | DCHECK(CheckIsValidIndex(i)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 149 | return GetData()[i]; |
| 150 | } |
| 151 | |
| 152 | void Set(int32_t i, T value) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 153 | if (Runtime::Current()->IsActiveTransaction()) { |
| 154 | Set<true>(i, value); |
| 155 | } else { |
| 156 | Set<false>(i, value); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // TODO fix thread safety analysis broken by the use of template. This should be |
| 161 | // SHARED_LOCKS_REQUIRED(Locks::mutator_lock_). |
| 162 | template<bool kTransactionActive, bool kCheckTransaction = true> |
| 163 | void Set(int32_t i, T value) NO_THREAD_SAFETY_ANALYSIS { |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 164 | if (LIKELY(CheckIsValidIndex(i))) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 165 | SetWithoutChecks<kTransactionActive, kCheckTransaction>(i, value); |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 166 | } else { |
| 167 | DCHECK(Thread::Current()->IsExceptionPending()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 171 | // TODO fix thread safety analysis broken by the use of template. This should be |
| 172 | // SHARED_LOCKS_REQUIRED(Locks::mutator_lock_). |
| 173 | template<bool kTransactionActive, bool kCheckTransaction = true> |
| 174 | void SetWithoutChecks(int32_t i, T value) NO_THREAD_SAFETY_ANALYSIS { |
| 175 | if (kCheckTransaction) { |
| 176 | DCHECK_EQ(kTransactionActive, Runtime::Current()->IsActiveTransaction()); |
| 177 | } |
| 178 | if (kTransactionActive) { |
| 179 | Runtime::Current()->RecordWriteArray(this, i, GetWithoutChecks(i)); |
| 180 | } |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 181 | DCHECK(CheckIsValidIndex(i)); |
| 182 | GetData()[i] = value; |
| 183 | } |
| 184 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 185 | /* |
| 186 | * Works like memmove(), except we guarantee not to allow tearing of array values (ie using |
| 187 | * smaller than element size copies). Arguments are assumed to be within the bounds of the array |
| 188 | * and the arrays non-null. |
| 189 | */ |
| 190 | void Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count) |
| 191 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 192 | |
| 193 | /* |
| 194 | * Works like memcpy(), except we guarantee not to allow tearing of array values (ie using |
| 195 | * smaller than element size copies). Arguments are assumed to be within the bounds of the array |
| 196 | * and the arrays non-null. |
| 197 | */ |
| 198 | void Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count) |
| 199 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 200 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 201 | static void SetArrayClass(Class* array_class) { |
| 202 | CHECK(array_class_ == NULL); |
| 203 | CHECK(array_class != NULL); |
| 204 | array_class_ = array_class; |
| 205 | } |
| 206 | |
| 207 | static void ResetArrayClass() { |
| 208 | CHECK(array_class_ != NULL); |
| 209 | array_class_ = NULL; |
| 210 | } |
| 211 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 212 | static void VisitRoots(RootCallback* callback, void* arg) |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 213 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 214 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 215 | private: |
| 216 | static Class* array_class_; |
| 217 | |
| 218 | DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray); |
| 219 | }; |
| 220 | |
| 221 | } // namespace mirror |
| 222 | } // namespace art |
| 223 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 224 | #endif // ART_RUNTIME_MIRROR_ARRAY_H_ |