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