blob: 7555975c411a44361b4d0f75475baa65b38db4f3 [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
Ian Rogersef7d42f2014-01-06 12:55:46 -080056 size_t SizeOf() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057
Ian Rogersef7d42f2014-01-06 12:55:46 -080058 int32_t GetLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
60 }
61
Ian Rogersef7d42f2014-01-06 12:55:46 -080062 void SetLength(int32_t length) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063 CHECK_GE(length, 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010064 // We use non transactional version since we can't undo this write. We also disable checking
65 // since it would fail during a transaction.
66 SetField32<false, false>(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false, false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080067 }
68
69 static MemberOffset LengthOffset() {
70 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
71 }
72
73 static MemberOffset DataOffset(size_t component_size) {
74 if (component_size != sizeof(int64_t)) {
75 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
76 } else {
77 // Align longs and doubles.
78 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_) + 4);
79 }
80 }
81
Ian Rogersef7d42f2014-01-06 12:55:46 -080082 void* GetRawData(size_t component_size, int32_t index)
83 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
84 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
85 + (index * component_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086 return reinterpret_cast<void*>(data);
87 }
88
Ian Rogersef7d42f2014-01-06 12:55:46 -080089 const void* GetRawData(size_t component_size, int32_t index) const {
90 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
91 + (index * component_size);
92 return reinterpret_cast<void*>(data);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080093 }
94
Sebastien Hertzabff6432014-01-27 18:01:39 +010095 // Returns true if the index is valid. If not, throws an ArrayIndexOutOfBoundsException and
96 // returns false.
Ian Rogersef7d42f2014-01-06 12:55:46 -080097 bool CheckIsValidIndex(int32_t index) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz66d1aee2013-07-23 11:59:01 +020098 if (UNLIKELY(static_cast<uint32_t>(index) >= static_cast<uint32_t>(GetLength()))) {
Sebastien Hertz9897be92013-06-27 18:24:46 +020099 ThrowArrayIndexOutOfBoundsException(index);
100 return false;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800101 }
102 return true;
103 }
104
105 protected:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800106 void ThrowArrayStoreException(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107
108 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800109 void ThrowArrayIndexOutOfBoundsException(int32_t index)
Sebastien Hertzabff6432014-01-27 18:01:39 +0100110 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
111
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800112 // The number of array elements.
113 int32_t length_;
114 // Marker for the data (used by generated code)
115 uint32_t first_element_[0];
116
117 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
118};
119
120template<class T>
121class MANAGED PrimitiveArray : public Array {
122 public:
123 typedef T ElementType;
124
125 static PrimitiveArray<T>* Alloc(Thread* self, size_t length)
126 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
127
Ian Rogersef7d42f2014-01-06 12:55:46 -0800128 const T* GetData() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
129 return reinterpret_cast<const T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800130 }
131
Ian Rogersef7d42f2014-01-06 12:55:46 -0800132 T* GetData() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
133 return reinterpret_cast<T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800134 }
135
Ian Rogersef7d42f2014-01-06 12:55:46 -0800136 T Get(int32_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100137 if (UNLIKELY(!CheckIsValidIndex(i))) {
138 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139 return T(0);
140 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100141 return GetWithoutChecks(i);
142 }
143
Ian Rogersef7d42f2014-01-06 12:55:46 -0800144 T GetWithoutChecks(int32_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100145 DCHECK(CheckIsValidIndex(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800146 return GetData()[i];
147 }
148
149 void Set(int32_t i, T value) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100150 if (Runtime::Current()->IsActiveTransaction()) {
151 Set<true>(i, value);
152 } else {
153 Set<false>(i, value);
154 }
155 }
156
157 // TODO fix thread safety analysis broken by the use of template. This should be
158 // SHARED_LOCKS_REQUIRED(Locks::mutator_lock_).
159 template<bool kTransactionActive, bool kCheckTransaction = true>
160 void Set(int32_t i, T value) NO_THREAD_SAFETY_ANALYSIS {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100161 if (LIKELY(CheckIsValidIndex(i))) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100162 SetWithoutChecks<kTransactionActive, kCheckTransaction>(i, value);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100163 } else {
164 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800165 }
166 }
167
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100168 // TODO fix thread safety analysis broken by the use of template. This should be
169 // SHARED_LOCKS_REQUIRED(Locks::mutator_lock_).
170 template<bool kTransactionActive, bool kCheckTransaction = true>
171 void SetWithoutChecks(int32_t i, T value) NO_THREAD_SAFETY_ANALYSIS {
172 if (kCheckTransaction) {
173 DCHECK_EQ(kTransactionActive, Runtime::Current()->IsActiveTransaction());
174 }
175 if (kTransactionActive) {
176 Runtime::Current()->RecordWriteArray(this, i, GetWithoutChecks(i));
177 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100178 DCHECK(CheckIsValidIndex(i));
179 GetData()[i] = value;
180 }
181
Ian Rogersef7d42f2014-01-06 12:55:46 -0800182 /*
183 * Works like memmove(), except we guarantee not to allow tearing of array values (ie using
184 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
185 * and the arrays non-null.
186 */
187 void Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
188 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
189
190 /*
191 * Works like memcpy(), except we guarantee not to allow tearing of array values (ie using
192 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
193 * and the arrays non-null.
194 */
195 void Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
196 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
197
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800198 static void SetArrayClass(Class* array_class) {
199 CHECK(array_class_ == NULL);
200 CHECK(array_class != NULL);
201 array_class_ = array_class;
202 }
203
204 static void ResetArrayClass() {
205 CHECK(array_class_ != NULL);
206 array_class_ = NULL;
207 }
208
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800209 static void VisitRoots(RootCallback* callback, void* arg)
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800210 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
211
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800212 private:
213 static Class* array_class_;
214
215 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
216};
217
218} // namespace mirror
219} // namespace art
220
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700221#endif // ART_RUNTIME_MIRROR_ARRAY_H_