blob: e0c14c3ea0813b15f2c17c63d84322db0b10a1de [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_OBJECT_ARRAY_INL_H_
18#define ART_RUNTIME_MIRROR_OBJECT_ARRAY_INL_H_
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019
20#include "object_array.h"
21
Ian Rogers1d54e732013-05-02 21:10:01 -070022#include "gc/heap.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070023#include "mirror/art_field.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "mirror/class.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070026#include "sirt_ref.h"
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020027#include "thread.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080028#include <string>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029
30namespace art {
31namespace mirror {
32
33template<class T>
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080034inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self, Class* object_array_class,
35 int32_t length, gc::AllocatorType allocator_type) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080036 Array* array = Array::Alloc<true>(self, object_array_class, length,
37 sizeof(HeapReference<Object>), allocator_type);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080038 if (UNLIKELY(array == nullptr)) {
39 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040 } else {
41 return array->AsObjectArray<T>();
42 }
43}
44
45template<class T>
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080046inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self, Class* object_array_class,
47 int32_t length) {
48 return Alloc(self, object_array_class, length,
49 Runtime::Current()->GetHeap()->GetCurrentAllocator());
50}
51
52template<class T>
Ian Rogersef7d42f2014-01-06 12:55:46 -080053inline T* ObjectArray<T>::Get(int32_t i) {
Sebastien Hertzabff6432014-01-27 18:01:39 +010054 if (UNLIKELY(!CheckIsValidIndex(i))) {
55 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056 return NULL;
57 }
Ian Rogersef7d42f2014-01-06 12:55:46 -080058 return GetFieldObject<T>(OffsetOfElement(i), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059}
60
Mathieu Chartier4e305412014-02-19 10:54:44 -080061template<class T> template<VerifyObjectFlags kVerifyFlags>
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020062inline bool ObjectArray<T>::CheckAssignable(T* object) {
63 if (object != NULL) {
Mathieu Chartier4e305412014-02-19 10:54:44 -080064 Class* element_class = GetClass<kVerifyFlags>()->GetComponentType();
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020065 if (UNLIKELY(!object->InstanceOf(element_class))) {
66 ThrowArrayStoreException(object);
67 return false;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080068 }
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020069 }
70 return true;
71}
72
73template<class T>
74inline void ObjectArray<T>::Set(int32_t i, T* object) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010075 if (Runtime::Current()->IsActiveTransaction()) {
76 Set<true>(i, object);
77 } else {
78 Set<false>(i, object);
79 }
80}
81
82template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -080083template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010084inline void ObjectArray<T>::Set(int32_t i, T* object) {
Mathieu Chartier4e305412014-02-19 10:54:44 -080085 if (LIKELY(CheckIsValidIndex(i) && CheckAssignable<kVerifyFlags>(object))) {
86 SetFieldObject<kTransactionActive, kCheckTransaction, kVerifyFlags>(OffsetOfElement(i), object,
87 false);
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020088 } else {
89 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080090 }
91}
92
93template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -080094template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080095inline void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
Mathieu Chartier4e305412014-02-19 10:54:44 -080096 DCHECK(CheckIsValidIndex<kVerifyFlags>(i));
97 DCHECK(CheckAssignable<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>(object));
98 SetFieldObject<kTransactionActive, kCheckTransaction, kVerifyFlags>(OffsetOfElement(i), object,
99 false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100}
101
102template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -0800103template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800104inline void ObjectArray<T>::SetWithoutChecksAndWriteBarrier(int32_t i, T* object) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800105 DCHECK(CheckIsValidIndex<kVerifyFlags>(i));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800106 // TODO: enable this check. It fails when writing the image in ImageWriter::FixupObjectArray.
Sebastien Hertzabff6432014-01-27 18:01:39 +0100107 // DCHECK(CheckAssignable(object));
Mathieu Chartier4e305412014-02-19 10:54:44 -0800108 SetFieldObjectWithoutWriteBarrier<kTransactionActive, kCheckTransaction, kVerifyFlags>(
109 OffsetOfElement(i), object, false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800110}
111
112template<class T>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800113inline T* ObjectArray<T>::GetWithoutChecks(int32_t i) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100114 DCHECK(CheckIsValidIndex(i));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800115 return GetFieldObject<T>(OffsetOfElement(i), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800116}
117
118template<class T>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800119inline void ObjectArray<T>::AssignableMemmove(int32_t dst_pos, ObjectArray<T>* src,
120 int32_t src_pos, int32_t count) {
121 if (kIsDebugBuild) {
122 for (int i = 0; i < count; ++i) {
123 // The Get will perform the VerifyObject.
124 src->GetWithoutChecks(src_pos + i);
125 }
126 }
127 // Perform the memmove using int memmove then perform the write barrier.
128 CHECK_EQ(sizeof(HeapReference<T>), sizeof(uint32_t));
129 IntArray* dstAsIntArray = reinterpret_cast<IntArray*>(this);
130 IntArray* srcAsIntArray = reinterpret_cast<IntArray*>(src);
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700131 if (kUseBakerOrBrooksReadBarrier) {
132 // TODO: Optimize this later?
133 const bool copy_forward = (src != this) || (dst_pos < src_pos) || (dst_pos - src_pos >= count);
134 if (copy_forward) {
135 // Forward copy.
136 for (int i = 0; i < count; ++i) {
137 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
138 Object* obj = src->GetWithoutChecks(src_pos + i);
139 SetWithoutChecks<false>(dst_pos + i, obj);
140 }
141 } else {
142 // Backward copy.
143 for (int i = count - 1; i >= 0; --i) {
144 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
145 Object* obj = src->GetWithoutChecks(src_pos + i);
146 SetWithoutChecks<false>(dst_pos + i, obj);
147 }
148 }
149 } else {
150 dstAsIntArray->Memmove(dst_pos, srcAsIntArray, src_pos, count);
151 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800152 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
153 if (kIsDebugBuild) {
154 for (int i = 0; i < count; ++i) {
155 // The Get will perform the VerifyObject.
156 GetWithoutChecks(dst_pos + i);
157 }
158 }
159}
160
161template<class T>
162inline void ObjectArray<T>::AssignableMemcpy(int32_t dst_pos, ObjectArray<T>* src,
163 int32_t src_pos, int32_t count) {
164 if (kIsDebugBuild) {
165 for (int i = 0; i < count; ++i) {
166 // The Get will perform the VerifyObject.
167 src->GetWithoutChecks(src_pos + i);
168 }
169 }
170 // Perform the memmove using int memcpy then perform the write barrier.
171 CHECK_EQ(sizeof(HeapReference<T>), sizeof(uint32_t));
172 IntArray* dstAsIntArray = reinterpret_cast<IntArray*>(this);
173 IntArray* srcAsIntArray = reinterpret_cast<IntArray*>(src);
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700174 if (kUseBakerOrBrooksReadBarrier) {
175 // TODO: Optimize this later?
176 for (int i = 0; i < count; ++i) {
177 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
178 T* obj = src->GetWithoutChecks(src_pos + i);
179 SetWithoutChecks<false>(dst_pos + i, obj);
180 }
181 } else {
182 dstAsIntArray->Memcpy(dst_pos, srcAsIntArray, src_pos, count);
183 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800184 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
185 if (kIsDebugBuild) {
186 for (int i = 0; i < count; ++i) {
187 // The Get will perform the VerifyObject.
188 GetWithoutChecks(dst_pos + i);
189 }
190 }
191}
192
193template<class T>
194inline void ObjectArray<T>::AssignableCheckingMemcpy(int32_t dst_pos, ObjectArray<T>* src,
195 int32_t src_pos, int32_t count,
196 bool throw_exception) {
197 DCHECK_NE(this, src)
198 << "This case should be handled with memmove that handles overlaps correctly";
199 // We want to avoid redundant IsAssignableFrom checks where possible, so we cache a class that
200 // we know is assignable to the destination array's component type.
201 Class* dst_class = GetClass()->GetComponentType();
202 Class* lastAssignableElementClass = dst_class;
203
204 Object* o = nullptr;
205 int i = 0;
206 for (; i < count; ++i) {
207 // The follow get operations force the objects to be verified.
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700208 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800209 o = src->GetWithoutChecks(src_pos + i);
210 if (o == nullptr) {
211 // Null is always assignable.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100212 SetWithoutChecks<false>(dst_pos + i, nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800213 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800214 // TODO: use the underlying class reference to avoid uncompression when not necessary.
215 Class* o_class = o->GetClass();
216 if (LIKELY(lastAssignableElementClass == o_class)) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100217 SetWithoutChecks<false>(dst_pos + i, o);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800218 } else if (LIKELY(dst_class->IsAssignableFrom(o_class))) {
219 lastAssignableElementClass = o_class;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100220 SetWithoutChecks<false>(dst_pos + i, o);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800221 } else {
222 // Can't put this element into the array, break to perform write-barrier and throw
223 // exception.
224 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800225 }
226 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800227 }
228 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
229 if (UNLIKELY(i != count)) {
230 std::string actualSrcType(PrettyTypeOf(o));
231 std::string dstType(PrettyTypeOf(this));
232 Thread* self = Thread::Current();
233 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
234 if (throw_exception) {
235 self->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayStoreException;",
236 "source[%d] of type %s cannot be stored in destination array of type %s",
237 src_pos + i, actualSrcType.c_str(), dstType.c_str());
238 } else {
239 LOG(FATAL) << StringPrintf("source[%d] of type %s cannot be stored in destination array of type %s",
240 src_pos + i, actualSrcType.c_str(), dstType.c_str());
241 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800242 }
243}
244
245template<class T>
246inline ObjectArray<T>* ObjectArray<T>::CopyOf(Thread* self, int32_t new_length) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800247 DCHECK_GE(new_length, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700248 // We may get copied by a compacting GC.
249 SirtRef<ObjectArray<T> > sirt_this(self, this);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800250 gc::Heap* heap = Runtime::Current()->GetHeap();
251 gc::AllocatorType allocator_type = heap->IsMovableObject(this) ? heap->GetCurrentAllocator() :
252 heap->GetCurrentNonMovingAllocator();
253 ObjectArray<T>* new_array = Alloc(self, GetClass(), new_length, allocator_type);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700254 if (LIKELY(new_array != nullptr)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800255 new_array->AssignableMemcpy(0, sirt_this.get(), 0, std::min(sirt_this->GetLength(), new_length));
Ian Rogersa436fde2013-08-27 23:34:06 -0700256 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800257 return new_array;
258}
259
Ian Rogersef7d42f2014-01-06 12:55:46 -0800260template<class T>
261inline MemberOffset ObjectArray<T>::OffsetOfElement(int32_t i) {
262 return MemberOffset(DataOffset(sizeof(HeapReference<Object>)).Int32Value() +
263 (i * sizeof(HeapReference<Object>)));
264}
265
Mathieu Chartier407f7022014-02-18 14:37:05 -0800266template<class T> template<const bool kVisitClass, typename Visitor>
267void ObjectArray<T>::VisitReferences(const Visitor& visitor) {
268 if (kVisitClass) {
269 visitor(this, ClassOffset(), false);
270 }
271 const size_t length = static_cast<size_t>(GetLength());
272 for (size_t i = 0; i < length; ++i) {
273 visitor(this, OffsetOfElement(i), false);
274 }
275}
276
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800277} // namespace mirror
278} // namespace art
279
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700280#endif // ART_RUNTIME_MIRROR_OBJECT_ARRAY_INL_H_