blob: 2e123ef215a7ab1ec9134645f93215ac4437332a [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 Chartier83c8ee02014-01-28 14:50:23 -080021#include "object_callbacks.h"
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080022#include "gc/heap.h"
Sebastien Hertzabff6432014-01-27 18:01:39 +010023#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024
25namespace art {
26namespace mirror {
27
28class MANAGED Array : public Object {
29 public:
Mathieu Chartier590fee92013-09-13 13:46:47 -070030 // A convenience for code that doesn't know the component size, and doesn't want to have to work
31 // it out itself.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080032 template <bool kIsInstrumented>
33 static Array* Alloc(Thread* self, Class* array_class, int32_t component_count,
34 gc::AllocatorType allocator_type)
35 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
36
37 template <bool kIsInstrumented>
38 static Array* Alloc(Thread* self, Class* array_class, int32_t component_count,
39 size_t component_size, gc::AllocatorType allocator_type)
40 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
41
42 template <bool kIsInstrumented>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043 static Array* Alloc(Thread* self, Class* array_class, int32_t component_count)
44 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
45
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080046 template <bool kIsInstrumented>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047 static Array* Alloc(Thread* self, Class* array_class, int32_t component_count,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080048 size_t component_size)
49 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050
Mathieu Chartier5bb99032014-02-08 16:20:58 -080051 static Array* CreateMultiArray(Thread* self, const SirtRef<Class>& element_class,
52 const SirtRef<IntArray>& dimensions)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
54
Ian Rogersef7d42f2014-01-06 12:55:46 -080055 size_t SizeOf() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056
Ian Rogersef7d42f2014-01-06 12:55:46 -080057 int32_t GetLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
59 }
60
Ian Rogersef7d42f2014-01-06 12:55:46 -080061 void SetLength(int32_t length) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080062 CHECK_GE(length, 0);
Ian Rogersef7d42f2014-01-06 12:55:46 -080063 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false, false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064 }
65
66 static MemberOffset LengthOffset() {
67 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
68 }
69
70 static MemberOffset DataOffset(size_t component_size) {
71 if (component_size != sizeof(int64_t)) {
72 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
73 } else {
74 // Align longs and doubles.
75 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_) + 4);
76 }
77 }
78
Ian Rogersef7d42f2014-01-06 12:55:46 -080079 void* GetRawData(size_t component_size, int32_t index)
80 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
81 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
82 + (index * component_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080083 return reinterpret_cast<void*>(data);
84 }
85
Ian Rogersef7d42f2014-01-06 12:55:46 -080086 const void* GetRawData(size_t component_size, int32_t index) const {
87 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
88 + (index * component_size);
89 return reinterpret_cast<void*>(data);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080090 }
91
Sebastien Hertzabff6432014-01-27 18:01:39 +010092 // Returns true if the index is valid. If not, throws an ArrayIndexOutOfBoundsException and
93 // returns false.
Ian Rogersef7d42f2014-01-06 12:55:46 -080094 bool CheckIsValidIndex(int32_t index) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz66d1aee2013-07-23 11:59:01 +020095 if (UNLIKELY(static_cast<uint32_t>(index) >= static_cast<uint32_t>(GetLength()))) {
Sebastien Hertz9897be92013-06-27 18:24:46 +020096 ThrowArrayIndexOutOfBoundsException(index);
97 return false;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080098 }
99 return true;
100 }
101
102 protected:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800103 void ThrowArrayStoreException(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104
105 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800106 void ThrowArrayIndexOutOfBoundsException(int32_t index)
Sebastien Hertzabff6432014-01-27 18:01:39 +0100107 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
108
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109 // The number of array elements.
110 int32_t length_;
111 // Marker for the data (used by generated code)
112 uint32_t first_element_[0];
113
114 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
115};
116
117template<class T>
118class MANAGED PrimitiveArray : public Array {
119 public:
120 typedef T ElementType;
121
122 static PrimitiveArray<T>* Alloc(Thread* self, size_t length)
123 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
124
Ian Rogersef7d42f2014-01-06 12:55:46 -0800125 const T* GetData() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
126 return reinterpret_cast<const T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800127 }
128
Ian Rogersef7d42f2014-01-06 12:55:46 -0800129 T* GetData() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
130 return reinterpret_cast<T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131 }
132
Ian Rogersef7d42f2014-01-06 12:55:46 -0800133 T Get(int32_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100134 if (UNLIKELY(!CheckIsValidIndex(i))) {
135 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800136 return T(0);
137 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100138 return GetWithoutChecks(i);
139 }
140
Ian Rogersef7d42f2014-01-06 12:55:46 -0800141 T GetWithoutChecks(int32_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100142 DCHECK(CheckIsValidIndex(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800143 return GetData()[i];
144 }
145
146 void Set(int32_t i, T value) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100147 if (LIKELY(CheckIsValidIndex(i))) {
148 SetWithoutChecks(i, value);
149 } else {
150 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800151 }
152 }
153
Sebastien Hertzabff6432014-01-27 18:01:39 +0100154 void SetWithoutChecks(int32_t i, T value) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
155 DCHECK(CheckIsValidIndex(i));
156 GetData()[i] = value;
157 }
158
Ian Rogersef7d42f2014-01-06 12:55:46 -0800159 /*
160 * Works like memmove(), except we guarantee not to allow tearing of array values (ie using
161 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
162 * and the arrays non-null.
163 */
164 void Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
165 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
166
167 /*
168 * Works like memcpy(), except we guarantee not to allow tearing of array values (ie using
169 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
170 * and the arrays non-null.
171 */
172 void Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
173 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
174
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800175 static void SetArrayClass(Class* array_class) {
176 CHECK(array_class_ == NULL);
177 CHECK(array_class != NULL);
178 array_class_ = array_class;
179 }
180
181 static void ResetArrayClass() {
182 CHECK(array_class_ != NULL);
183 array_class_ = NULL;
184 }
185
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800186 static void VisitRoots(RootCallback* callback, void* arg)
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800187 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
188
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800189 private:
190 static Class* array_class_;
191
192 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
193};
194
195} // namespace mirror
196} // namespace art
197
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700198#endif // ART_RUNTIME_MIRROR_ARRAY_H_