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