blob: 167f824d14ab62dc9aabfba1ccf6c54d1716f79d [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.
34 static uint32_t ClassSize();
35
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 Chartiercbb2d202013-11-14 17:45:16 -080042 SHARED_LOCKS_REQUIRED(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)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
47
Hiroshi Yamauchi6e83c172014-05-01 21:25:41 -070048 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
49 ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Ian Rogersef7d42f2014-01-06 12:55:46 -080050 size_t SizeOf() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier4e305412014-02-19 10:54:44 -080051 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070052 ALWAYS_INLINE int32_t GetLength() SHARED_LOCKS_REQUIRED(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
Ian Rogersef7d42f2014-01-06 12:55:46 -080056 void SetLength(int32_t length) SHARED_LOCKS_REQUIRED(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)
70 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
71 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 Chartier2d2621a2014-10-23 16:48:06 -070085 ALWAYS_INLINE bool CheckIsValidIndex(int32_t index) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086
87 protected:
Ian Rogersef7d42f2014-01-06 12:55:46 -080088 void ThrowArrayStoreException(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080089
90 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -080091 void ThrowArrayIndexOutOfBoundsException(int32_t index)
Sebastien Hertzabff6432014-01-27 18:01:39 +010092 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
93
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094 // The number of array elements.
95 int32_t length_;
96 // Marker for the data (used by generated code)
97 uint32_t first_element_[0];
98
99 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
100};
101
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700102template<typename T>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103class MANAGED PrimitiveArray : public Array {
104 public:
105 typedef T ElementType;
106
107 static PrimitiveArray<T>* Alloc(Thread* self, size_t length)
108 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
109
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700110 const T* GetData() const ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800111 return reinterpret_cast<const T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800112 }
113
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700114 T* GetData() ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800115 return reinterpret_cast<T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800116 }
117
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700118 T Get(int32_t i) ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100119
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700120 T GetWithoutChecks(int32_t i) ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100121 DCHECK(CheckIsValidIndex(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122 return GetData()[i];
123 }
124
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700125 void Set(int32_t i, T value) ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100126
127 // TODO fix thread safety analysis broken by the use of template. This should be
128 // SHARED_LOCKS_REQUIRED(Locks::mutator_lock_).
129 template<bool kTransactionActive, bool kCheckTransaction = true>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700130 void Set(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100132 // TODO fix thread safety analysis broken by the use of template. This should be
133 // SHARED_LOCKS_REQUIRED(Locks::mutator_lock_).
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700134 template<bool kTransactionActive,
135 bool kCheckTransaction = true,
136 VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700137 void SetWithoutChecks(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Sebastien Hertzabff6432014-01-27 18:01:39 +0100138
Ian Rogersef7d42f2014-01-06 12:55:46 -0800139 /*
140 * Works like memmove(), except we guarantee not to allow tearing of array values (ie using
141 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
142 * and the arrays non-null.
143 */
144 void Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
145 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
146
147 /*
148 * Works like memcpy(), except we guarantee not to allow tearing of array values (ie using
149 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
150 * and the arrays non-null.
151 */
152 void Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
153 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
154
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155 static void SetArrayClass(Class* array_class) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700156 CHECK(array_class_.IsNull());
Ian Rogers2d10b202014-05-12 19:15:18 -0700157 CHECK(array_class != nullptr);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700158 array_class_ = GcRoot<Class>(array_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800159 }
160
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -0700161 static Class* GetArrayClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700162 DCHECK(!array_class_.IsNull());
163 return array_class_.Read();
Ian Rogers2d10b202014-05-12 19:15:18 -0700164 }
165
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800166 static void ResetArrayClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700167 CHECK(!array_class_.IsNull());
168 array_class_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 }
170
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700171 static void VisitRoots(RootVisitor* visitor) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800172
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173 private:
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700174 static GcRoot<Class> array_class_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800175
176 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
177};
178
179} // namespace mirror
180} // namespace art
181
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700182#endif // ART_RUNTIME_MIRROR_ARRAY_H_