blob: c4f9a755b3135cf3e19e67a1a117f0c1ebce6e4d [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
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 Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_MIRROR_ARRAY_H_
18#define ART_RUNTIME_MIRROR_ARRAY_H_
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019
20#include "object.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080021#include "object_callbacks.h"
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080022#include "gc/heap.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010023#include "runtime.h"
Sebastien Hertzabff6432014-01-27 18:01:39 +010024#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025
26namespace art {
27namespace mirror {
28
29class MANAGED Array : public Object {
30 public:
Mathieu Chartier590fee92013-09-13 13:46:47 -070031 // A convenience for code that doesn't know the component size, and doesn't want to have to work
32 // it out itself.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080033 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 Rogers2dd0e2c2013-01-24 12:42:14 -080044 static Array* Alloc(Thread* self, Class* array_class, int32_t component_count)
45 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
46
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080047 template <bool kIsInstrumented>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048 static Array* Alloc(Thread* self, Class* array_class, int32_t component_count,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080049 size_t component_size)
50 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051
Mathieu Chartier5bb99032014-02-08 16:20:58 -080052 static Array* CreateMultiArray(Thread* self, const SirtRef<Class>& element_class,
53 const SirtRef<IntArray>& dimensions)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
55
Mathieu Chartier4e305412014-02-19 10:54:44 -080056 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -080057 size_t SizeOf() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier4e305412014-02-19 10:54:44 -080058 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -080059 int32_t GetLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier4e305412014-02-19 10:54:44 -080060 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061 }
62
Ian Rogersef7d42f2014-01-06 12:55:46 -080063 void SetLength(int32_t length) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064 CHECK_GE(length, 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010065 // 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 Chartier4e305412014-02-19 10:54:44 -080067 SetField32<false, false, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080068 }
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 Rogersef7d42f2014-01-06 12:55:46 -080083 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 Rogers2dd0e2c2013-01-24 12:42:14 -080087 return reinterpret_cast<void*>(data);
88 }
89
Ian Rogersef7d42f2014-01-06 12:55:46 -080090 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 Rogers2dd0e2c2013-01-24 12:42:14 -080094 }
95
Sebastien Hertzabff6432014-01-27 18:01:39 +010096 // Returns true if the index is valid. If not, throws an ArrayIndexOutOfBoundsException and
97 // returns false.
Mathieu Chartier4e305412014-02-19 10:54:44 -080098 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -080099 bool CheckIsValidIndex(int32_t index) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800100 if (UNLIKELY(static_cast<uint32_t>(index) >=
101 static_cast<uint32_t>(GetLength<kVerifyFlags>()))) {
Sebastien Hertz9897be92013-06-27 18:24:46 +0200102 ThrowArrayIndexOutOfBoundsException(index);
103 return false;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104 }
105 return true;
106 }
107
108 protected:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800109 void ThrowArrayStoreException(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800110
111 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800112 void ThrowArrayIndexOutOfBoundsException(int32_t index)
Sebastien Hertzabff6432014-01-27 18:01:39 +0100113 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
114
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115 // 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
123template<class T>
124class 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 Rogersef7d42f2014-01-06 12:55:46 -0800131 const T* GetData() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
132 return reinterpret_cast<const T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800133 }
134
Ian Rogersef7d42f2014-01-06 12:55:46 -0800135 T* GetData() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
136 return reinterpret_cast<T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800137 }
138
Ian Rogersef7d42f2014-01-06 12:55:46 -0800139 T Get(int32_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100140 if (UNLIKELY(!CheckIsValidIndex(i))) {
141 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800142 return T(0);
143 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100144 return GetWithoutChecks(i);
145 }
146
Ian Rogersef7d42f2014-01-06 12:55:46 -0800147 T GetWithoutChecks(int32_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100148 DCHECK(CheckIsValidIndex(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800149 return GetData()[i];
150 }
151
152 void Set(int32_t i, T value) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100153 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 Hertzabff6432014-01-27 18:01:39 +0100164 if (LIKELY(CheckIsValidIndex(i))) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100165 SetWithoutChecks<kTransactionActive, kCheckTransaction>(i, value);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100166 } else {
167 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800168 }
169 }
170
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100171 // 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 Hertzabff6432014-01-27 18:01:39 +0100181 DCHECK(CheckIsValidIndex(i));
182 GetData()[i] = value;
183 }
184
Ian Rogersef7d42f2014-01-06 12:55:46 -0800185 /*
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 Rogers2dd0e2c2013-01-24 12:42:14 -0800201 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 Chartier83c8ee02014-01-28 14:50:23 -0800212 static void VisitRoots(RootCallback* callback, void* arg)
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800213 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
214
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800215 private:
216 static Class* array_class_;
217
218 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
219};
220
221} // namespace mirror
222} // namespace art
223
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700224#endif // ART_RUNTIME_MIRROR_ARRAY_H_