blob: 04f03c3a55c096c8121bd4719bab6731272f6614 [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
20#include "object.h"
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080021#include "gc/heap.h"
Sebastien Hertzabff6432014-01-27 18:01:39 +010022#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023
24namespace art {
25namespace mirror {
26
27class MANAGED Array : public Object {
28 public:
Mathieu Chartier590fee92013-09-13 13:46:47 -070029 // A convenience for code that doesn't know the component size, and doesn't want to have to work
30 // it out itself.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080031 template <bool kIsInstrumented>
32 static Array* Alloc(Thread* self, Class* array_class, int32_t component_count,
33 gc::AllocatorType allocator_type)
34 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
35
36 template <bool kIsInstrumented>
37 static Array* Alloc(Thread* self, Class* array_class, int32_t component_count,
38 size_t component_size, gc::AllocatorType allocator_type)
39 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
40
41 template <bool kIsInstrumented>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042 static Array* Alloc(Thread* self, Class* array_class, int32_t component_count)
43 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
44
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080045 template <bool kIsInstrumented>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046 static Array* Alloc(Thread* self, Class* array_class, int32_t component_count,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080047 size_t component_size)
48 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049
Mathieu Chartier5bb99032014-02-08 16:20:58 -080050 static Array* CreateMultiArray(Thread* self, const SirtRef<Class>& element_class,
51 const SirtRef<IntArray>& dimensions)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
53
Ian Rogersef7d42f2014-01-06 12:55:46 -080054 size_t SizeOf() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055
Ian Rogersef7d42f2014-01-06 12:55:46 -080056 int32_t GetLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
58 }
59
Ian Rogersef7d42f2014-01-06 12:55:46 -080060 void SetLength(int32_t length) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061 CHECK_GE(length, 0);
Ian Rogersef7d42f2014-01-06 12:55:46 -080062 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false, false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063 }
64
65 static MemberOffset LengthOffset() {
66 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
67 }
68
69 static MemberOffset DataOffset(size_t component_size) {
70 if (component_size != sizeof(int64_t)) {
71 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
72 } else {
73 // Align longs and doubles.
74 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_) + 4);
75 }
76 }
77
Ian Rogersef7d42f2014-01-06 12:55:46 -080078 void* GetRawData(size_t component_size, int32_t index)
79 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
80 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
81 + (index * component_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080082 return reinterpret_cast<void*>(data);
83 }
84
Ian Rogersef7d42f2014-01-06 12:55:46 -080085 const void* GetRawData(size_t component_size, int32_t index) const {
86 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
87 + (index * component_size);
88 return reinterpret_cast<void*>(data);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080089 }
90
Sebastien Hertzabff6432014-01-27 18:01:39 +010091 // Returns true if the index is valid. If not, throws an ArrayIndexOutOfBoundsException and
92 // returns false.
Ian Rogersef7d42f2014-01-06 12:55:46 -080093 bool CheckIsValidIndex(int32_t index) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz66d1aee2013-07-23 11:59:01 +020094 if (UNLIKELY(static_cast<uint32_t>(index) >= static_cast<uint32_t>(GetLength()))) {
Sebastien Hertz9897be92013-06-27 18:24:46 +020095 ThrowArrayIndexOutOfBoundsException(index);
96 return false;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097 }
98 return true;
99 }
100
101 protected:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800102 void ThrowArrayStoreException(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103
104 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800105 void ThrowArrayIndexOutOfBoundsException(int32_t index)
Sebastien Hertzabff6432014-01-27 18:01:39 +0100106 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
107
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800108 // The number of array elements.
109 int32_t length_;
110 // Marker for the data (used by generated code)
111 uint32_t first_element_[0];
112
113 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
114};
115
116template<class T>
117class MANAGED PrimitiveArray : public Array {
118 public:
119 typedef T ElementType;
120
121 static PrimitiveArray<T>* Alloc(Thread* self, size_t length)
122 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
123
Ian Rogersef7d42f2014-01-06 12:55:46 -0800124 const T* GetData() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
125 return reinterpret_cast<const T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126 }
127
Ian Rogersef7d42f2014-01-06 12:55:46 -0800128 T* GetData() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
129 return reinterpret_cast<T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800130 }
131
Ian Rogersef7d42f2014-01-06 12:55:46 -0800132 T Get(int32_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100133 if (UNLIKELY(!CheckIsValidIndex(i))) {
134 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800135 return T(0);
136 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100137 return GetWithoutChecks(i);
138 }
139
Ian Rogersef7d42f2014-01-06 12:55:46 -0800140 T GetWithoutChecks(int32_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100141 DCHECK(CheckIsValidIndex(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800142 return GetData()[i];
143 }
144
145 void Set(int32_t i, T value) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100146 if (LIKELY(CheckIsValidIndex(i))) {
147 SetWithoutChecks(i, value);
148 } else {
149 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800150 }
151 }
152
Sebastien Hertzabff6432014-01-27 18:01:39 +0100153 void SetWithoutChecks(int32_t i, T value) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
154 DCHECK(CheckIsValidIndex(i));
155 GetData()[i] = value;
156 }
157
Ian Rogersef7d42f2014-01-06 12:55:46 -0800158 /*
159 * Works like memmove(), except we guarantee not to allow tearing of array values (ie using
160 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
161 * and the arrays non-null.
162 */
163 void Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
164 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
165
166 /*
167 * Works like memcpy(), except we guarantee not to allow tearing of array values (ie using
168 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
169 * and the arrays non-null.
170 */
171 void Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
172 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
173
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800174 static void SetArrayClass(Class* array_class) {
175 CHECK(array_class_ == NULL);
176 CHECK(array_class != NULL);
177 array_class_ = array_class;
178 }
179
180 static void ResetArrayClass() {
181 CHECK(array_class_ != NULL);
182 array_class_ = NULL;
183 }
184
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800185 static void VisitRoots(RootVisitor* visitor, void* arg)
186 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
187
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800188 private:
189 static Class* array_class_;
190
191 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
192};
193
194} // namespace mirror
195} // namespace art
196
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700197#endif // ART_RUNTIME_MIRROR_ARRAY_H_