blob: c9e0cb3b17c805edb36dc4a44b55716167eb1004 [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
Andreas Gampe542451c2016-07-26 09:02:02 -070020#include "base/enums.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070021#include "gc_root.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070022#include "gc/allocator_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "object.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080024#include "object_callbacks.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025
26namespace art {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070027
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070028template<class T> class Handle;
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070029
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030namespace mirror {
31
32class MANAGED Array : public Object {
33 public:
Mingyao Yang98d1cc82014-05-15 17:02:16 -070034 // The size of a java.lang.Class representing an array.
Andreas Gampe542451c2016-07-26 09:02:02 -070035 static uint32_t ClassSize(PointerSize pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070036
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070037 // Allocates an array with the given properties, if kFillUsable is true the array will be of at
Ian Rogers6fac4472014-02-25 17:01:10 -080038 // least component_count size, however, if there's usable space at the end of the allocation the
39 // array will fill it.
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070040 template <bool kIsInstrumented, bool kFillUsable = false>
41 ALWAYS_INLINE static Array* Alloc(Thread* self, Class* array_class, int32_t component_count,
42 size_t component_size_shift, gc::AllocatorType allocator_type)
Mathieu Chartier4e2cb092015-07-22 16:17:51 -070043 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044
Mathieu Chartier0cd81352014-05-22 16:48:55 -070045 static Array* CreateMultiArray(Thread* self, Handle<Class> element_class,
46 Handle<IntArray> dimensions)
Mathieu Chartier4e2cb092015-07-22 16:17:51 -070047 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048
Hiroshi Yamauchi6e83c172014-05-01 21:25:41 -070049 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
50 ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Mathieu Chartier90443472015-07-16 20:32:27 -070051 size_t SizeOf() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier4e305412014-02-19 10:54:44 -080052 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Mathieu Chartier90443472015-07-16 20:32:27 -070053 ALWAYS_INLINE int32_t GetLength() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070054 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Array, length_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055 }
56
Mathieu Chartier90443472015-07-16 20:32:27 -070057 void SetLength(int32_t length) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -070058 DCHECK_GE(length, 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010059 // We use non transactional version since we can't undo this write. We also disable checking
60 // since it would fail during a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070061 SetField32<false, false, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Array, length_), length);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080062 }
63
64 static MemberOffset LengthOffset() {
65 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
66 }
67
Ian Rogers7e70b002014-10-08 11:47:24 -070068 static MemberOffset DataOffset(size_t component_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069
Ian Rogersef7d42f2014-01-06 12:55:46 -080070 void* GetRawData(size_t component_size, int32_t index)
Mathieu Chartier90443472015-07-16 20:32:27 -070071 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080072 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
73 + (index * component_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074 return reinterpret_cast<void*>(data);
75 }
76
Ian Rogersef7d42f2014-01-06 12:55:46 -080077 const void* GetRawData(size_t component_size, int32_t index) const {
78 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
79 + (index * component_size);
80 return reinterpret_cast<void*>(data);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081 }
82
Sebastien Hertzabff6432014-01-27 18:01:39 +010083 // Returns true if the index is valid. If not, throws an ArrayIndexOutOfBoundsException and
84 // returns false.
Mathieu Chartier4e305412014-02-19 10:54:44 -080085 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Mathieu Chartier90443472015-07-16 20:32:27 -070086 ALWAYS_INLINE bool CheckIsValidIndex(int32_t index) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087
Mathieu Chartier4e2cb092015-07-22 16:17:51 -070088 Array* CopyOf(Thread* self, int32_t new_length) SHARED_REQUIRES(Locks::mutator_lock_)
89 REQUIRES(!Roles::uninterruptible_);
Mathieu Chartiere401d142015-04-22 13:56:20 -070090
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091 protected:
Mathieu Chartiered8990a2015-07-23 14:11:16 -070092 void ThrowArrayStoreException(Object* object) SHARED_REQUIRES(Locks::mutator_lock_)
93 REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094
95 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -080096 void ThrowArrayIndexOutOfBoundsException(int32_t index)
Mathieu Chartier90443472015-07-16 20:32:27 -070097 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertzabff6432014-01-27 18:01:39 +010098
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099 // The number of array elements.
100 int32_t length_;
101 // Marker for the data (used by generated code)
102 uint32_t first_element_[0];
103
104 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
105};
106
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700107template<typename T>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800108class MANAGED PrimitiveArray : public Array {
109 public:
110 typedef T ElementType;
111
112 static PrimitiveArray<T>* Alloc(Thread* self, size_t length)
Mathieu Chartier4e2cb092015-07-22 16:17:51 -0700113 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800114
Mathieu Chartier90443472015-07-16 20:32:27 -0700115 const T* GetData() const ALWAYS_INLINE SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800116 return reinterpret_cast<const T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800117 }
118
Mathieu Chartier90443472015-07-16 20:32:27 -0700119 T* GetData() ALWAYS_INLINE SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800120 return reinterpret_cast<T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800121 }
122
Mathieu Chartier90443472015-07-16 20:32:27 -0700123 T Get(int32_t i) ALWAYS_INLINE SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100124
Mathieu Chartier90443472015-07-16 20:32:27 -0700125 T GetWithoutChecks(int32_t i) ALWAYS_INLINE SHARED_REQUIRES(Locks::mutator_lock_) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000126 DCHECK(CheckIsValidIndex(i)) << "i=" << i << " length=" << GetLength();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800127 return GetData()[i];
128 }
129
Mathieu Chartier90443472015-07-16 20:32:27 -0700130 void Set(int32_t i, T value) ALWAYS_INLINE SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100131
132 // TODO fix thread safety analysis broken by the use of template. This should be
Mathieu Chartier90443472015-07-16 20:32:27 -0700133 // SHARED_REQUIRES(Locks::mutator_lock_).
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100134 template<bool kTransactionActive, bool kCheckTransaction = true>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700135 void Set(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800136
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100137 // TODO fix thread safety analysis broken by the use of template. This should be
Mathieu Chartier90443472015-07-16 20:32:27 -0700138 // SHARED_REQUIRES(Locks::mutator_lock_).
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700139 template<bool kTransactionActive,
140 bool kCheckTransaction = true,
141 VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700142 void SetWithoutChecks(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Sebastien Hertzabff6432014-01-27 18:01:39 +0100143
Ian Rogersef7d42f2014-01-06 12:55:46 -0800144 /*
145 * Works like memmove(), except we guarantee not to allow tearing of array values (ie using
146 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
147 * and the arrays non-null.
148 */
149 void Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
Mathieu Chartier90443472015-07-16 20:32:27 -0700150 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800151
152 /*
153 * Works like memcpy(), except we guarantee not to allow tearing of array values (ie using
154 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
155 * and the arrays non-null.
156 */
157 void Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
Mathieu Chartier90443472015-07-16 20:32:27 -0700158 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800159
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800160 static void SetArrayClass(Class* array_class) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700161 CHECK(array_class_.IsNull());
Ian Rogers2d10b202014-05-12 19:15:18 -0700162 CHECK(array_class != nullptr);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700163 array_class_ = GcRoot<Class>(array_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800164 }
165
Mathieu Chartier90443472015-07-16 20:32:27 -0700166 static Class* GetArrayClass() SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700167 DCHECK(!array_class_.IsNull());
168 return array_class_.Read();
Ian Rogers2d10b202014-05-12 19:15:18 -0700169 }
170
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171 static void ResetArrayClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700172 CHECK(!array_class_.IsNull());
173 array_class_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800174 }
175
Mathieu Chartier90443472015-07-16 20:32:27 -0700176 static void VisitRoots(RootVisitor* visitor) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800177
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800178 private:
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700179 static GcRoot<Class> array_class_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800180
181 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
182};
183
Mathieu Chartiere401d142015-04-22 13:56:20 -0700184// Either an IntArray or a LongArray.
185class PointerArray : public Array {
186 public:
Mathieu Chartierdfe02f62016-02-01 20:15:11 -0800187 template<typename T,
188 VerifyObjectFlags kVerifyFlags = kVerifyNone,
189 ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Andreas Gampe542451c2016-07-26 09:02:02 -0700190 T GetElementPtrSize(uint32_t idx, PointerSize ptr_size)
Mathieu Chartier90443472015-07-16 20:32:27 -0700191 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700192
Mathieu Chartierd329a3b2016-01-27 15:30:10 -0800193 template<bool kTransactionActive = false, bool kUnchecked = false>
Andreas Gampe542451c2016-07-26 09:02:02 -0700194 void SetElementPtrSize(uint32_t idx, uint64_t element, PointerSize ptr_size)
Mathieu Chartierd329a3b2016-01-27 15:30:10 -0800195 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700196 template<bool kTransactionActive = false, bool kUnchecked = false, typename T>
Andreas Gampe542451c2016-07-26 09:02:02 -0700197 void SetElementPtrSize(uint32_t idx, T* element, PointerSize ptr_size)
Mathieu Chartier90443472015-07-16 20:32:27 -0700198 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800199
200 // Fixup the pointers in the dest arrays by passing our pointers through the visitor. Only copies
201 // to dest if visitor(source_ptr) != source_ptr.
Mathieu Chartierdfe02f62016-02-01 20:15:11 -0800202 template <VerifyObjectFlags kVerifyFlags = kVerifyNone,
203 ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
204 typename Visitor>
Andreas Gampe542451c2016-07-26 09:02:02 -0700205 void Fixup(mirror::PointerArray* dest, PointerSize pointer_size, const Visitor& visitor)
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800206 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700207};
208
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800209} // namespace mirror
210} // namespace art
211
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700212#endif // ART_RUNTIME_MIRROR_ARRAY_H_