blob: 6e366a08f55620bd3a608cf89afc2a94cbd1cc34 [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
50 static Array* CreateMultiArray(Thread* self, Class* element_class, IntArray* dimensions)
51 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
52
Ian Rogersef7d42f2014-01-06 12:55:46 -080053 size_t SizeOf() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054
Ian Rogersef7d42f2014-01-06 12:55:46 -080055 int32_t GetLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
57 }
58
Ian Rogersef7d42f2014-01-06 12:55:46 -080059 void SetLength(int32_t length) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060 CHECK_GE(length, 0);
Ian Rogersef7d42f2014-01-06 12:55:46 -080061 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false, false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080062 }
63
64 static MemberOffset LengthOffset() {
65 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
66 }
67
68 static MemberOffset DataOffset(size_t component_size) {
69 if (component_size != sizeof(int64_t)) {
70 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
71 } else {
72 // Align longs and doubles.
73 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_) + 4);
74 }
75 }
76
Ian Rogersef7d42f2014-01-06 12:55:46 -080077 void* GetRawData(size_t component_size, int32_t index)
78 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
79 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
80 + (index * component_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081 return reinterpret_cast<void*>(data);
82 }
83
Ian Rogersef7d42f2014-01-06 12:55:46 -080084 const void* GetRawData(size_t component_size, int32_t index) const {
85 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
86 + (index * component_size);
87 return reinterpret_cast<void*>(data);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080088 }
89
Sebastien Hertzabff6432014-01-27 18:01:39 +010090 // Returns true if the index is valid. If not, throws an ArrayIndexOutOfBoundsException and
91 // returns false.
Ian Rogersef7d42f2014-01-06 12:55:46 -080092 bool CheckIsValidIndex(int32_t index) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz66d1aee2013-07-23 11:59:01 +020093 if (UNLIKELY(static_cast<uint32_t>(index) >= static_cast<uint32_t>(GetLength()))) {
Sebastien Hertz9897be92013-06-27 18:24:46 +020094 ThrowArrayIndexOutOfBoundsException(index);
95 return false;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080096 }
97 return true;
98 }
99
100 protected:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800101 void ThrowArrayStoreException(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800102
103 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800104 void ThrowArrayIndexOutOfBoundsException(int32_t index)
Sebastien Hertzabff6432014-01-27 18:01:39 +0100105 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
106
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107 // The number of array elements.
108 int32_t length_;
109 // Marker for the data (used by generated code)
110 uint32_t first_element_[0];
111
112 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
113};
114
115template<class T>
116class MANAGED PrimitiveArray : public Array {
117 public:
118 typedef T ElementType;
119
120 static PrimitiveArray<T>* Alloc(Thread* self, size_t length)
121 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
122
Ian Rogersef7d42f2014-01-06 12:55:46 -0800123 const T* GetData() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
124 return reinterpret_cast<const T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800125 }
126
Ian Rogersef7d42f2014-01-06 12:55:46 -0800127 T* GetData() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
128 return reinterpret_cast<T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800129 }
130
Ian Rogersef7d42f2014-01-06 12:55:46 -0800131 T Get(int32_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100132 if (UNLIKELY(!CheckIsValidIndex(i))) {
133 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800134 return T(0);
135 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100136 return GetWithoutChecks(i);
137 }
138
Ian Rogersef7d42f2014-01-06 12:55:46 -0800139 T GetWithoutChecks(int32_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100140 DCHECK(CheckIsValidIndex(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800141 return GetData()[i];
142 }
143
144 void Set(int32_t i, T value) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100145 if (LIKELY(CheckIsValidIndex(i))) {
146 SetWithoutChecks(i, value);
147 } else {
148 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800149 }
150 }
151
Sebastien Hertzabff6432014-01-27 18:01:39 +0100152 void SetWithoutChecks(int32_t i, T value) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
153 DCHECK(CheckIsValidIndex(i));
154 GetData()[i] = value;
155 }
156
Ian Rogersef7d42f2014-01-06 12:55:46 -0800157 /*
158 * Works like memmove(), except we guarantee not to allow tearing of array values (ie using
159 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
160 * and the arrays non-null.
161 */
162 void Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
163 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
164
165 /*
166 * Works like memcpy(), except we guarantee not to allow tearing of array values (ie using
167 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
168 * and the arrays non-null.
169 */
170 void Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
171 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
172
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173 static void SetArrayClass(Class* array_class) {
174 CHECK(array_class_ == NULL);
175 CHECK(array_class != NULL);
176 array_class_ = array_class;
177 }
178
179 static void ResetArrayClass() {
180 CHECK(array_class_ != NULL);
181 array_class_ = NULL;
182 }
183
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800184 static void VisitRoots(RootVisitor* visitor, void* arg)
185 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
186
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800187 private:
188 static Class* array_class_;
189
190 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
191};
192
193} // namespace mirror
194} // namespace art
195
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700196#endif // ART_RUNTIME_MIRROR_ARRAY_H_