blob: 92f0e67b1748da1f37e399ac291b0c18af247f29 [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
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070020#include "gc/allocator_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "object.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080022#include "object_callbacks.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023
24namespace art {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070025
26template<class T> class SirtRef;
27
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028namespace mirror {
29
30class MANAGED Array : public Object {
31 public:
Ian Rogers6fac4472014-02-25 17:01:10 -080032 // Allocates an array with the given properties, if fill_usable is true the array will be of at
33 // least component_count size, however, if there's usable space at the end of the allocation the
34 // array will fill it.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080035 template <bool kIsInstrumented>
36 static Array* Alloc(Thread* self, Class* array_class, int32_t component_count,
Ian Rogers6fac4472014-02-25 17:01:10 -080037 size_t component_size, gc::AllocatorType allocator_type,
38 bool fill_usable = false)
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080039 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040
Mathieu Chartier5bb99032014-02-08 16:20:58 -080041 static Array* CreateMultiArray(Thread* self, const SirtRef<Class>& element_class,
42 const SirtRef<IntArray>& dimensions)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
44
Hiroshi Yamauchi9103c862014-04-22 13:51:07 -070045 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kDoReadBarrier = true>
Ian Rogersef7d42f2014-01-06 12:55:46 -080046 size_t SizeOf() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier4e305412014-02-19 10:54:44 -080047 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -080048 int32_t GetLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070049 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Array, length_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050 }
51
Ian Rogersef7d42f2014-01-06 12:55:46 -080052 void SetLength(int32_t length) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053 CHECK_GE(length, 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010054 // We use non transactional version since we can't undo this write. We also disable checking
55 // since it would fail during a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070056 SetField32<false, false, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Array, length_), length);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057 }
58
59 static MemberOffset LengthOffset() {
60 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
61 }
62
63 static MemberOffset DataOffset(size_t component_size) {
64 if (component_size != sizeof(int64_t)) {
65 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
66 } else {
67 // Align longs and doubles.
68 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_) + 4);
69 }
70 }
71
Ian Rogersef7d42f2014-01-06 12:55:46 -080072 void* GetRawData(size_t component_size, int32_t index)
73 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
74 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
75 + (index * component_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076 return reinterpret_cast<void*>(data);
77 }
78
Ian Rogersef7d42f2014-01-06 12:55:46 -080079 const void* GetRawData(size_t component_size, int32_t index) const {
80 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
81 + (index * component_size);
82 return reinterpret_cast<void*>(data);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080083 }
84
Sebastien Hertzabff6432014-01-27 18:01:39 +010085 // Returns true if the index is valid. If not, throws an ArrayIndexOutOfBoundsException and
86 // returns false.
Mathieu Chartier4e305412014-02-19 10:54:44 -080087 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070088 bool CheckIsValidIndex(int32_t index) ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080089
90 protected:
Ian Rogersef7d42f2014-01-06 12:55:46 -080091 void ThrowArrayStoreException(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092
93 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -080094 void ThrowArrayIndexOutOfBoundsException(int32_t index)
Sebastien Hertzabff6432014-01-27 18:01:39 +010095 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
96
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097 // The number of array elements.
98 int32_t length_;
99 // Marker for the data (used by generated code)
100 uint32_t first_element_[0];
101
102 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
103};
104
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700105template<typename T>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106class MANAGED PrimitiveArray : public Array {
107 public:
108 typedef T ElementType;
109
110 static PrimitiveArray<T>* Alloc(Thread* self, size_t length)
111 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
112
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700113 const T* GetData() const ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800114 return reinterpret_cast<const T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115 }
116
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700117 T* GetData() ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800118 return reinterpret_cast<T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800119 }
120
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700121 T Get(int32_t i) ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100122
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700123 T GetWithoutChecks(int32_t i) ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100124 DCHECK(CheckIsValidIndex(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800125 return GetData()[i];
126 }
127
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700128 void Set(int32_t i, T value) ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100129
130 // TODO fix thread safety analysis broken by the use of template. This should be
131 // SHARED_LOCKS_REQUIRED(Locks::mutator_lock_).
132 template<bool kTransactionActive, bool kCheckTransaction = true>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700133 void Set(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800134
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100135 // TODO fix thread safety analysis broken by the use of template. This should be
136 // SHARED_LOCKS_REQUIRED(Locks::mutator_lock_).
137 template<bool kTransactionActive, bool kCheckTransaction = true>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700138 void SetWithoutChecks(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Sebastien Hertzabff6432014-01-27 18:01:39 +0100139
Ian Rogersef7d42f2014-01-06 12:55:46 -0800140 /*
141 * Works like memmove(), except we guarantee not to allow tearing of array values (ie using
142 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
143 * and the arrays non-null.
144 */
145 void Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
146 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
147
148 /*
149 * Works like memcpy(), except we guarantee not to allow tearing of array values (ie using
150 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
151 * and the arrays non-null.
152 */
153 void Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
154 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
155
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800156 static void SetArrayClass(Class* array_class) {
157 CHECK(array_class_ == NULL);
158 CHECK(array_class != NULL);
159 array_class_ = array_class;
160 }
161
162 static void ResetArrayClass() {
163 CHECK(array_class_ != NULL);
164 array_class_ = NULL;
165 }
166
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800167 static void VisitRoots(RootCallback* callback, void* arg)
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800168 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
169
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800170 private:
171 static Class* array_class_;
172
173 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
174};
175
176} // namespace mirror
177} // namespace art
178
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700179#endif // ART_RUNTIME_MIRROR_ARRAY_H_