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