blob: 0ca44f8a3e74a3d2909cf1b952fffec27df9b84b [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 Rogers576ca0c2014-06-06 15:58:22 -070022#include "base/stringprintf.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070023#include "gc/heap.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070024#include "mirror/art_field.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/class.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "runtime.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070027#include "handle_scope-inl.h"
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020028#include "thread.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080029#include <string>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030
31namespace art {
32namespace mirror {
33
34template<class T>
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080035inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self, Class* object_array_class,
36 int32_t length, gc::AllocatorType allocator_type) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080037 Array* array = Array::Alloc<true>(self, object_array_class, length,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070038 ComponentSizeShiftWidth<sizeof(HeapReference<Object>)>(),
39 allocator_type);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080040 if (UNLIKELY(array == nullptr)) {
41 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042 } else {
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070043 DCHECK_EQ(array->GetClass()->GetComponentSizeShift(),
44 ComponentSizeShiftWidth<sizeof(HeapReference<Object>)>());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045 return array->AsObjectArray<T>();
46 }
47}
48
49template<class T>
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080050inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self, Class* object_array_class,
51 int32_t length) {
52 return Alloc(self, object_array_class, length,
53 Runtime::Current()->GetHeap()->GetCurrentAllocator());
54}
55
56template<class T>
Ian Rogersef7d42f2014-01-06 12:55:46 -080057inline T* ObjectArray<T>::Get(int32_t i) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070058 if (!CheckIsValidIndex(i)) {
Sebastien Hertzabff6432014-01-27 18:01:39 +010059 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060 return NULL;
61 }
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070062 return GetFieldObject<T>(OffsetOfElement(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063}
64
Mathieu Chartier4e305412014-02-19 10:54:44 -080065template<class T> template<VerifyObjectFlags kVerifyFlags>
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020066inline bool ObjectArray<T>::CheckAssignable(T* object) {
67 if (object != NULL) {
Mathieu Chartier4e305412014-02-19 10:54:44 -080068 Class* element_class = GetClass<kVerifyFlags>()->GetComponentType();
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020069 if (UNLIKELY(!object->InstanceOf(element_class))) {
70 ThrowArrayStoreException(object);
71 return false;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072 }
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020073 }
74 return true;
75}
76
77template<class T>
78inline void ObjectArray<T>::Set(int32_t i, T* object) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010079 if (Runtime::Current()->IsActiveTransaction()) {
80 Set<true>(i, object);
81 } else {
82 Set<false>(i, object);
83 }
84}
85
86template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -080087template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010088inline void ObjectArray<T>::Set(int32_t i, T* object) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070089 if (CheckIsValidIndex(i) && CheckAssignable<kVerifyFlags>(object)) {
90 SetFieldObject<kTransactionActive, kCheckTransaction, kVerifyFlags>(OffsetOfElement(i), object);
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020091 } else {
92 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080093 }
94}
95
96template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -080097template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080098inline void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
Mathieu Chartier4e305412014-02-19 10:54:44 -080099 DCHECK(CheckIsValidIndex<kVerifyFlags>(i));
100 DCHECK(CheckAssignable<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>(object));
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700101 SetFieldObject<kTransactionActive, kCheckTransaction, kVerifyFlags>(OffsetOfElement(i), object);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800102}
103
104template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -0800105template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800106inline void ObjectArray<T>::SetWithoutChecksAndWriteBarrier(int32_t i, T* object) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800107 DCHECK(CheckIsValidIndex<kVerifyFlags>(i));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800108 // TODO: enable this check. It fails when writing the image in ImageWriter::FixupObjectArray.
Sebastien Hertzabff6432014-01-27 18:01:39 +0100109 // DCHECK(CheckAssignable(object));
Mathieu Chartier4e305412014-02-19 10:54:44 -0800110 SetFieldObjectWithoutWriteBarrier<kTransactionActive, kCheckTransaction, kVerifyFlags>(
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700111 OffsetOfElement(i), object);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800112}
113
114template<class T>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800115inline T* ObjectArray<T>::GetWithoutChecks(int32_t i) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100116 DCHECK(CheckIsValidIndex(i));
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700117 return GetFieldObject<T>(OffsetOfElement(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800118}
119
120template<class T>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800121inline void ObjectArray<T>::AssignableMemmove(int32_t dst_pos, ObjectArray<T>* src,
122 int32_t src_pos, int32_t count) {
123 if (kIsDebugBuild) {
124 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700125 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800126 src->GetWithoutChecks(src_pos + i);
127 }
128 }
129 // Perform the memmove using int memmove then perform the write barrier.
130 CHECK_EQ(sizeof(HeapReference<T>), sizeof(uint32_t));
131 IntArray* dstAsIntArray = reinterpret_cast<IntArray*>(this);
132 IntArray* srcAsIntArray = reinterpret_cast<IntArray*>(src);
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700133 if (kUseBakerOrBrooksReadBarrier) {
134 // TODO: Optimize this later?
135 const bool copy_forward = (src != this) || (dst_pos < src_pos) || (dst_pos - src_pos >= count);
136 if (copy_forward) {
137 // Forward copy.
138 for (int i = 0; i < count; ++i) {
139 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
140 Object* obj = src->GetWithoutChecks(src_pos + i);
141 SetWithoutChecks<false>(dst_pos + i, obj);
142 }
143 } else {
144 // Backward copy.
145 for (int i = count - 1; i >= 0; --i) {
146 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
147 Object* obj = src->GetWithoutChecks(src_pos + i);
148 SetWithoutChecks<false>(dst_pos + i, obj);
149 }
150 }
151 } else {
152 dstAsIntArray->Memmove(dst_pos, srcAsIntArray, src_pos, count);
153 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800154 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
155 if (kIsDebugBuild) {
156 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700157 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800158 GetWithoutChecks(dst_pos + i);
159 }
160 }
161}
162
163template<class T>
164inline void ObjectArray<T>::AssignableMemcpy(int32_t dst_pos, ObjectArray<T>* src,
165 int32_t src_pos, int32_t count) {
166 if (kIsDebugBuild) {
167 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700168 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800169 src->GetWithoutChecks(src_pos + i);
170 }
171 }
172 // Perform the memmove using int memcpy then perform the write barrier.
173 CHECK_EQ(sizeof(HeapReference<T>), sizeof(uint32_t));
174 IntArray* dstAsIntArray = reinterpret_cast<IntArray*>(this);
175 IntArray* srcAsIntArray = reinterpret_cast<IntArray*>(src);
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700176 if (kUseBakerOrBrooksReadBarrier) {
177 // TODO: Optimize this later?
178 for (int i = 0; i < count; ++i) {
179 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
180 T* obj = src->GetWithoutChecks(src_pos + i);
181 SetWithoutChecks<false>(dst_pos + i, obj);
182 }
183 } else {
184 dstAsIntArray->Memcpy(dst_pos, srcAsIntArray, src_pos, count);
185 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800186 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
187 if (kIsDebugBuild) {
188 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700189 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800190 GetWithoutChecks(dst_pos + i);
191 }
192 }
193}
194
195template<class T>
196inline void ObjectArray<T>::AssignableCheckingMemcpy(int32_t dst_pos, ObjectArray<T>* src,
197 int32_t src_pos, int32_t count,
198 bool throw_exception) {
199 DCHECK_NE(this, src)
200 << "This case should be handled with memmove that handles overlaps correctly";
201 // We want to avoid redundant IsAssignableFrom checks where possible, so we cache a class that
202 // we know is assignable to the destination array's component type.
203 Class* dst_class = GetClass()->GetComponentType();
204 Class* lastAssignableElementClass = dst_class;
205
206 Object* o = nullptr;
207 int i = 0;
208 for (; i < count; ++i) {
209 // The follow get operations force the objects to be verified.
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700210 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800211 o = src->GetWithoutChecks(src_pos + i);
212 if (o == nullptr) {
213 // Null is always assignable.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100214 SetWithoutChecks<false>(dst_pos + i, nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800215 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800216 // TODO: use the underlying class reference to avoid uncompression when not necessary.
217 Class* o_class = o->GetClass();
218 if (LIKELY(lastAssignableElementClass == o_class)) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100219 SetWithoutChecks<false>(dst_pos + i, o);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800220 } else if (LIKELY(dst_class->IsAssignableFrom(o_class))) {
221 lastAssignableElementClass = o_class;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100222 SetWithoutChecks<false>(dst_pos + i, o);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800223 } else {
224 // Can't put this element into the array, break to perform write-barrier and throw
225 // exception.
226 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800227 }
228 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800229 }
230 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
231 if (UNLIKELY(i != count)) {
232 std::string actualSrcType(PrettyTypeOf(o));
233 std::string dstType(PrettyTypeOf(this));
234 Thread* self = Thread::Current();
235 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
236 if (throw_exception) {
237 self->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayStoreException;",
238 "source[%d] of type %s cannot be stored in destination array of type %s",
239 src_pos + i, actualSrcType.c_str(), dstType.c_str());
240 } else {
241 LOG(FATAL) << StringPrintf("source[%d] of type %s cannot be stored in destination array of type %s",
242 src_pos + i, actualSrcType.c_str(), dstType.c_str());
243 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800244 }
245}
246
247template<class T>
248inline ObjectArray<T>* ObjectArray<T>::CopyOf(Thread* self, int32_t new_length) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800249 DCHECK_GE(new_length, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700250 // We may get copied by a compacting GC.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700251 StackHandleScope<1> hs(self);
Ian Rogers700a4022014-05-19 16:49:03 -0700252 Handle<ObjectArray<T>> h_this(hs.NewHandle(this));
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800253 gc::Heap* heap = Runtime::Current()->GetHeap();
254 gc::AllocatorType allocator_type = heap->IsMovableObject(this) ? heap->GetCurrentAllocator() :
255 heap->GetCurrentNonMovingAllocator();
256 ObjectArray<T>* new_array = Alloc(self, GetClass(), new_length, allocator_type);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700257 if (LIKELY(new_array != nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700258 new_array->AssignableMemcpy(0, h_this.Get(), 0, std::min(h_this->GetLength(), new_length));
Ian Rogersa436fde2013-08-27 23:34:06 -0700259 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800260 return new_array;
261}
262
Ian Rogersef7d42f2014-01-06 12:55:46 -0800263template<class T>
264inline MemberOffset ObjectArray<T>::OffsetOfElement(int32_t i) {
265 return MemberOffset(DataOffset(sizeof(HeapReference<Object>)).Int32Value() +
266 (i * sizeof(HeapReference<Object>)));
267}
268
Mathieu Chartier407f7022014-02-18 14:37:05 -0800269template<class T> template<const bool kVisitClass, typename Visitor>
270void ObjectArray<T>::VisitReferences(const Visitor& visitor) {
271 if (kVisitClass) {
272 visitor(this, ClassOffset(), false);
273 }
274 const size_t length = static_cast<size_t>(GetLength());
275 for (size_t i = 0; i < length; ++i) {
276 visitor(this, OffsetOfElement(i), false);
277 }
278}
279
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800280} // namespace mirror
281} // namespace art
282
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700283#endif // ART_RUNTIME_MIRROR_OBJECT_ARRAY_INL_H_