blob: d21cfae34eec975de6a703e8d6d242cc557671b0 [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
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070020#include "gc_root.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070021#include "gc/allocator_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "object.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080023#include "object_callbacks.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024
25namespace art {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070026
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070027template<class T> class Handle;
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070028
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029namespace mirror {
30
31class MANAGED Array : public Object {
32 public:
Mingyao Yang98d1cc82014-05-15 17:02:16 -070033 // The size of a java.lang.Class representing an array.
Mathieu Chartiere401d142015-04-22 13:56:20 -070034 static uint32_t ClassSize(size_t pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070035
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070036 // Allocates an array with the given properties, if kFillUsable is true the array will be of at
Ian Rogers6fac4472014-02-25 17:01:10 -080037 // least component_count size, however, if there's usable space at the end of the allocation the
38 // array will fill it.
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070039 template <bool kIsInstrumented, bool kFillUsable = false>
40 ALWAYS_INLINE static Array* Alloc(Thread* self, Class* array_class, int32_t component_count,
41 size_t component_size_shift, gc::AllocatorType allocator_type)
Mathieu Chartier4e2cb092015-07-22 16:17:51 -070042 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043
Mathieu Chartier0cd81352014-05-22 16:48:55 -070044 static Array* CreateMultiArray(Thread* self, Handle<Class> element_class,
45 Handle<IntArray> dimensions)
Mathieu Chartier4e2cb092015-07-22 16:17:51 -070046 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047
Hiroshi Yamauchi6e83c172014-05-01 21:25:41 -070048 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
49 ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Mathieu Chartier90443472015-07-16 20:32:27 -070050 size_t SizeOf() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier4e305412014-02-19 10:54:44 -080051 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Mathieu Chartier90443472015-07-16 20:32:27 -070052 ALWAYS_INLINE int32_t GetLength() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070053 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Array, length_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054 }
55
Mathieu Chartier90443472015-07-16 20:32:27 -070056 void SetLength(int32_t length) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -070057 DCHECK_GE(length, 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010058 // We use non transactional version since we can't undo this write. We also disable checking
59 // since it would fail during a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070060 SetField32<false, false, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Array, length_), length);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061 }
62
63 static MemberOffset LengthOffset() {
64 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
65 }
66
Ian Rogers7e70b002014-10-08 11:47:24 -070067 static MemberOffset DataOffset(size_t component_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080068
Ian Rogersef7d42f2014-01-06 12:55:46 -080069 void* GetRawData(size_t component_size, int32_t index)
Mathieu Chartier90443472015-07-16 20:32:27 -070070 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080071 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
72 + (index * component_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073 return reinterpret_cast<void*>(data);
74 }
75
Ian Rogersef7d42f2014-01-06 12:55:46 -080076 const void* GetRawData(size_t component_size, int32_t index) const {
77 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
78 + (index * component_size);
79 return reinterpret_cast<void*>(data);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080 }
81
Sebastien Hertzabff6432014-01-27 18:01:39 +010082 // Returns true if the index is valid. If not, throws an ArrayIndexOutOfBoundsException and
83 // returns false.
Mathieu Chartier4e305412014-02-19 10:54:44 -080084 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Mathieu Chartier90443472015-07-16 20:32:27 -070085 ALWAYS_INLINE bool CheckIsValidIndex(int32_t index) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086
Mathieu Chartier4e2cb092015-07-22 16:17:51 -070087 Array* CopyOf(Thread* self, int32_t new_length) SHARED_REQUIRES(Locks::mutator_lock_)
88 REQUIRES(!Roles::uninterruptible_);
Mathieu Chartiere401d142015-04-22 13:56:20 -070089
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080090 protected:
Mathieu Chartier90443472015-07-16 20:32:27 -070091 void ThrowArrayStoreException(Object* object) SHARED_REQUIRES(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)
Mathieu Chartier90443472015-07-16 20:32:27 -070095 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertzabff6432014-01-27 18:01:39 +010096
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)
Mathieu Chartier4e2cb092015-07-22 16:17:51 -0700111 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800112
Mathieu Chartier90443472015-07-16 20:32:27 -0700113 const T* GetData() const ALWAYS_INLINE SHARED_REQUIRES(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
Mathieu Chartier90443472015-07-16 20:32:27 -0700117 T* GetData() ALWAYS_INLINE SHARED_REQUIRES(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
Mathieu Chartier90443472015-07-16 20:32:27 -0700121 T Get(int32_t i) ALWAYS_INLINE SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100122
Mathieu Chartier90443472015-07-16 20:32:27 -0700123 T GetWithoutChecks(int32_t i) ALWAYS_INLINE SHARED_REQUIRES(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
Mathieu Chartier90443472015-07-16 20:32:27 -0700128 void Set(int32_t i, T value) ALWAYS_INLINE SHARED_REQUIRES(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
Mathieu Chartier90443472015-07-16 20:32:27 -0700131 // SHARED_REQUIRES(Locks::mutator_lock_).
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100132 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
Mathieu Chartier90443472015-07-16 20:32:27 -0700136 // SHARED_REQUIRES(Locks::mutator_lock_).
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700137 template<bool kTransactionActive,
138 bool kCheckTransaction = true,
139 VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700140 void SetWithoutChecks(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Sebastien Hertzabff6432014-01-27 18:01:39 +0100141
Ian Rogersef7d42f2014-01-06 12:55:46 -0800142 /*
143 * Works like memmove(), except we guarantee not to allow tearing of array values (ie using
144 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
145 * and the arrays non-null.
146 */
147 void Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
Mathieu Chartier90443472015-07-16 20:32:27 -0700148 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800149
150 /*
151 * Works like memcpy(), except we guarantee not to allow tearing of array values (ie using
152 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
153 * and the arrays non-null.
154 */
155 void Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
Mathieu Chartier90443472015-07-16 20:32:27 -0700156 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800157
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800158 static void SetArrayClass(Class* array_class) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700159 CHECK(array_class_.IsNull());
Ian Rogers2d10b202014-05-12 19:15:18 -0700160 CHECK(array_class != nullptr);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700161 array_class_ = GcRoot<Class>(array_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800162 }
163
Mathieu Chartier90443472015-07-16 20:32:27 -0700164 static Class* GetArrayClass() SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700165 DCHECK(!array_class_.IsNull());
166 return array_class_.Read();
Ian Rogers2d10b202014-05-12 19:15:18 -0700167 }
168
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 static void ResetArrayClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700170 CHECK(!array_class_.IsNull());
171 array_class_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800172 }
173
Mathieu Chartier90443472015-07-16 20:32:27 -0700174 static void VisitRoots(RootVisitor* visitor) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800175
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800176 private:
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700177 static GcRoot<Class> array_class_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800178
179 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
180};
181
Mathieu Chartiere401d142015-04-22 13:56:20 -0700182// Either an IntArray or a LongArray.
183class PointerArray : public Array {
184 public:
185 template<typename T>
186 T GetElementPtrSize(uint32_t idx, size_t ptr_size)
Mathieu Chartier90443472015-07-16 20:32:27 -0700187 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700188
189 template<bool kTransactionActive = false, bool kUnchecked = false, typename T>
190 void SetElementPtrSize(uint32_t idx, T element, size_t ptr_size)
Mathieu Chartier90443472015-07-16 20:32:27 -0700191 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700192};
193
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800194} // namespace mirror
195} // namespace art
196
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700197#endif // ART_RUNTIME_MIRROR_ARRAY_H_