blob: 08c12e97488a3abc27620b79726ea93dcc3f74ac [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 Chartier90443472015-07-16 20:32:27 -070042 SHARED_REQUIRES(Locks::mutator_lock_);
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 Chartier90443472015-07-16 20:32:27 -070046 SHARED_REQUIRES(Locks::mutator_lock_);
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 Chartier90443472015-07-16 20:32:27 -070087 Array* CopyOf(Thread* self, int32_t new_length) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -070088
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080089 protected:
Mathieu Chartier90443472015-07-16 20:32:27 -070090 void ThrowArrayStoreException(Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091
92 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -080093 void ThrowArrayIndexOutOfBoundsException(int32_t index)
Mathieu Chartier90443472015-07-16 20:32:27 -070094 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertzabff6432014-01-27 18:01:39 +010095
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080096 // The number of array elements.
97 int32_t length_;
98 // Marker for the data (used by generated code)
99 uint32_t first_element_[0];
100
101 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
102};
103
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700104template<typename T>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800105class MANAGED PrimitiveArray : public Array {
106 public:
107 typedef T ElementType;
108
109 static PrimitiveArray<T>* Alloc(Thread* self, size_t length)
Mathieu Chartier90443472015-07-16 20:32:27 -0700110 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111
Mathieu Chartier90443472015-07-16 20:32:27 -0700112 const T* GetData() const ALWAYS_INLINE SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800113 return reinterpret_cast<const T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800114 }
115
Mathieu Chartier90443472015-07-16 20:32:27 -0700116 T* GetData() ALWAYS_INLINE SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800117 return reinterpret_cast<T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800118 }
119
Mathieu Chartier90443472015-07-16 20:32:27 -0700120 T Get(int32_t i) ALWAYS_INLINE SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100121
Mathieu Chartier90443472015-07-16 20:32:27 -0700122 T GetWithoutChecks(int32_t i) ALWAYS_INLINE SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100123 DCHECK(CheckIsValidIndex(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800124 return GetData()[i];
125 }
126
Mathieu Chartier90443472015-07-16 20:32:27 -0700127 void Set(int32_t i, T value) ALWAYS_INLINE SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100128
129 // TODO fix thread safety analysis broken by the use of template. This should be
Mathieu Chartier90443472015-07-16 20:32:27 -0700130 // SHARED_REQUIRES(Locks::mutator_lock_).
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100131 template<bool kTransactionActive, bool kCheckTransaction = true>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700132 void Set(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800133
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100134 // TODO fix thread safety analysis broken by the use of template. This should be
Mathieu Chartier90443472015-07-16 20:32:27 -0700135 // SHARED_REQUIRES(Locks::mutator_lock_).
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700136 template<bool kTransactionActive,
137 bool kCheckTransaction = true,
138 VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700139 void SetWithoutChecks(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Sebastien Hertzabff6432014-01-27 18:01:39 +0100140
Ian Rogersef7d42f2014-01-06 12:55:46 -0800141 /*
142 * Works like memmove(), except we guarantee not to allow tearing of array values (ie using
143 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
144 * and the arrays non-null.
145 */
146 void Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
Mathieu Chartier90443472015-07-16 20:32:27 -0700147 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800148
149 /*
150 * Works like memcpy(), except we guarantee not to allow tearing of array values (ie using
151 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
152 * and the arrays non-null.
153 */
154 void Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
Mathieu Chartier90443472015-07-16 20:32:27 -0700155 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800156
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800157 static void SetArrayClass(Class* array_class) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700158 CHECK(array_class_.IsNull());
Ian Rogers2d10b202014-05-12 19:15:18 -0700159 CHECK(array_class != nullptr);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700160 array_class_ = GcRoot<Class>(array_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161 }
162
Mathieu Chartier90443472015-07-16 20:32:27 -0700163 static Class* GetArrayClass() SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700164 DCHECK(!array_class_.IsNull());
165 return array_class_.Read();
Ian Rogers2d10b202014-05-12 19:15:18 -0700166 }
167
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800168 static void ResetArrayClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700169 CHECK(!array_class_.IsNull());
170 array_class_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171 }
172
Mathieu Chartier90443472015-07-16 20:32:27 -0700173 static void VisitRoots(RootVisitor* visitor) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800174
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800175 private:
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700176 static GcRoot<Class> array_class_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800177
178 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
179};
180
Mathieu Chartiere401d142015-04-22 13:56:20 -0700181// Either an IntArray or a LongArray.
182class PointerArray : public Array {
183 public:
184 template<typename T>
185 T GetElementPtrSize(uint32_t idx, size_t ptr_size)
Mathieu Chartier90443472015-07-16 20:32:27 -0700186 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700187
188 template<bool kTransactionActive = false, bool kUnchecked = false, typename T>
189 void SetElementPtrSize(uint32_t idx, T element, size_t ptr_size)
Mathieu Chartier90443472015-07-16 20:32:27 -0700190 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700191};
192
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800193} // namespace mirror
194} // namespace art
195
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700196#endif // ART_RUNTIME_MIRROR_ARRAY_H_