blob: a9dcfe6aeed44ddde6d626506b1efca0572bc538 [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:
Ian Rogers6fac4472014-02-25 17:01:10 -080031 // Allocates an array with the given properties, if fill_usable is true the array will be of at
32 // least component_count size, however, if there's usable space at the end of the allocation the
33 // array will fill it.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080034 template <bool kIsInstrumented>
35 static Array* Alloc(Thread* self, Class* array_class, int32_t component_count,
Ian Rogers6fac4472014-02-25 17:01:10 -080036 size_t component_size, gc::AllocatorType allocator_type,
37 bool fill_usable = false)
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080038 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039
Mathieu Chartier5bb99032014-02-08 16:20:58 -080040 static Array* CreateMultiArray(Thread* self, const SirtRef<Class>& element_class,
41 const SirtRef<IntArray>& dimensions)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
43
Mathieu Chartier4e305412014-02-19 10:54:44 -080044 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -080045 size_t SizeOf() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier4e305412014-02-19 10:54:44 -080046 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -080047 int32_t GetLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier4e305412014-02-19 10:54:44 -080048 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049 }
50
Ian Rogersef7d42f2014-01-06 12:55:46 -080051 void SetLength(int32_t length) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052 CHECK_GE(length, 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010053 // We use non transactional version since we can't undo this write. We also disable checking
54 // since it would fail during a transaction.
Mathieu Chartier4e305412014-02-19 10:54:44 -080055 SetField32<false, false, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056 }
57
58 static MemberOffset LengthOffset() {
59 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
60 }
61
62 static MemberOffset DataOffset(size_t component_size) {
63 if (component_size != sizeof(int64_t)) {
64 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
65 } else {
66 // Align longs and doubles.
67 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_) + 4);
68 }
69 }
70
Dmitry Petrochenko973cc952014-04-18 14:53:09 +070071 template<class MirrorType>
72 static int32_t DataOffsetOfType(uint32_t index) {
73 return DataOffset(sizeof(HeapReference<MirrorType>)).Int32Value() +
74 (sizeof(HeapReference<MirrorType>) * index);
75 }
76
Ian Rogersef7d42f2014-01-06 12:55:46 -080077 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 Rogers2dd0e2c2013-01-24 12:42:14 -080081 return reinterpret_cast<void*>(data);
82 }
83
Ian Rogersef7d42f2014-01-06 12:55:46 -080084 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 Rogers2dd0e2c2013-01-24 12:42:14 -080088 }
89
Sebastien Hertzabff6432014-01-27 18:01:39 +010090 // Returns true if the index is valid. If not, throws an ArrayIndexOutOfBoundsException and
91 // returns false.
Mathieu Chartier4e305412014-02-19 10:54:44 -080092 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -080093 bool CheckIsValidIndex(int32_t index) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier4e305412014-02-19 10:54:44 -080094 if (UNLIKELY(static_cast<uint32_t>(index) >=
95 static_cast<uint32_t>(GetLength<kVerifyFlags>()))) {
Sebastien Hertz9897be92013-06-27 18:24:46 +020096 ThrowArrayIndexOutOfBoundsException(index);
97 return false;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080098 }
99 return true;
100 }
101
102 protected:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800103 void ThrowArrayStoreException(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104
105 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800106 void ThrowArrayIndexOutOfBoundsException(int32_t index)
Sebastien Hertzabff6432014-01-27 18:01:39 +0100107 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
108
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109 // The number of array elements.
110 int32_t length_;
111 // Marker for the data (used by generated code)
112 uint32_t first_element_[0];
113
114 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
115};
116
117template<class T>
118class MANAGED PrimitiveArray : public Array {
119 public:
120 typedef T ElementType;
121
122 static PrimitiveArray<T>* Alloc(Thread* self, size_t length)
123 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
124
Ian Rogersef7d42f2014-01-06 12:55:46 -0800125 const T* GetData() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
126 return reinterpret_cast<const T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800127 }
128
Ian Rogersef7d42f2014-01-06 12:55:46 -0800129 T* GetData() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
130 return reinterpret_cast<T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131 }
132
Ian Rogersef7d42f2014-01-06 12:55:46 -0800133 T Get(int32_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100134 if (UNLIKELY(!CheckIsValidIndex(i))) {
135 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800136 return T(0);
137 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100138 return GetWithoutChecks(i);
139 }
140
Ian Rogersef7d42f2014-01-06 12:55:46 -0800141 T GetWithoutChecks(int32_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100142 DCHECK(CheckIsValidIndex(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800143 return GetData()[i];
144 }
145
146 void Set(int32_t i, T value) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100147 if (Runtime::Current()->IsActiveTransaction()) {
148 Set<true>(i, value);
149 } else {
150 Set<false>(i, value);
151 }
152 }
153
154 // TODO fix thread safety analysis broken by the use of template. This should be
155 // SHARED_LOCKS_REQUIRED(Locks::mutator_lock_).
156 template<bool kTransactionActive, bool kCheckTransaction = true>
157 void Set(int32_t i, T value) NO_THREAD_SAFETY_ANALYSIS {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100158 if (LIKELY(CheckIsValidIndex(i))) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100159 SetWithoutChecks<kTransactionActive, kCheckTransaction>(i, value);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100160 } else {
161 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800162 }
163 }
164
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100165 // TODO fix thread safety analysis broken by the use of template. This should be
166 // SHARED_LOCKS_REQUIRED(Locks::mutator_lock_).
167 template<bool kTransactionActive, bool kCheckTransaction = true>
168 void SetWithoutChecks(int32_t i, T value) NO_THREAD_SAFETY_ANALYSIS {
169 if (kCheckTransaction) {
170 DCHECK_EQ(kTransactionActive, Runtime::Current()->IsActiveTransaction());
171 }
172 if (kTransactionActive) {
173 Runtime::Current()->RecordWriteArray(this, i, GetWithoutChecks(i));
174 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100175 DCHECK(CheckIsValidIndex(i));
176 GetData()[i] = value;
177 }
178
Ian Rogersef7d42f2014-01-06 12:55:46 -0800179 /*
180 * Works like memmove(), except we guarantee not to allow tearing of array values (ie using
181 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
182 * and the arrays non-null.
183 */
184 void Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
185 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
186
187 /*
188 * Works like memcpy(), except we guarantee not to allow tearing of array values (ie using
189 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
190 * and the arrays non-null.
191 */
192 void Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
193 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
194
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800195 static void SetArrayClass(Class* array_class) {
196 CHECK(array_class_ == NULL);
197 CHECK(array_class != NULL);
198 array_class_ = array_class;
199 }
200
201 static void ResetArrayClass() {
202 CHECK(array_class_ != NULL);
203 array_class_ = NULL;
204 }
205
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800206 static void VisitRoots(RootCallback* callback, void* arg)
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800207 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
208
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800209 private:
210 static Class* array_class_;
211
212 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
213};
214
215} // namespace mirror
216} // namespace art
217
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700218#endif // ART_RUNTIME_MIRROR_ARRAY_H_