blob: c7540dcb7ae5ffe13307c0e0f67126e294dc79c6 [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,
38 sizeof(HeapReference<Object>), allocator_type);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080039 if (UNLIKELY(array == nullptr)) {
40 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041 } else {
42 return array->AsObjectArray<T>();
43 }
44}
45
46template<class T>
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080047inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self, Class* object_array_class,
48 int32_t length) {
49 return Alloc(self, object_array_class, length,
50 Runtime::Current()->GetHeap()->GetCurrentAllocator());
51}
52
53template<class T>
Ian Rogersef7d42f2014-01-06 12:55:46 -080054inline T* ObjectArray<T>::Get(int32_t i) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070055 if (!CheckIsValidIndex(i)) {
Sebastien Hertzabff6432014-01-27 18:01:39 +010056 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057 return NULL;
58 }
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070059 return GetFieldObject<T>(OffsetOfElement(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060}
61
Mathieu Chartier4e305412014-02-19 10:54:44 -080062template<class T> template<VerifyObjectFlags kVerifyFlags>
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020063inline bool ObjectArray<T>::CheckAssignable(T* object) {
64 if (object != NULL) {
Mathieu Chartier4e305412014-02-19 10:54:44 -080065 Class* element_class = GetClass<kVerifyFlags>()->GetComponentType();
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020066 if (UNLIKELY(!object->InstanceOf(element_class))) {
67 ThrowArrayStoreException(object);
68 return false;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069 }
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020070 }
71 return true;
72}
73
74template<class T>
75inline void ObjectArray<T>::Set(int32_t i, T* object) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010076 if (Runtime::Current()->IsActiveTransaction()) {
77 Set<true>(i, object);
78 } else {
79 Set<false>(i, object);
80 }
81}
82
83template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -080084template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010085inline void ObjectArray<T>::Set(int32_t i, T* object) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070086 if (CheckIsValidIndex(i) && CheckAssignable<kVerifyFlags>(object)) {
87 SetFieldObject<kTransactionActive, kCheckTransaction, kVerifyFlags>(OffsetOfElement(i), object);
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));
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070098 SetFieldObject<kTransactionActive, kCheckTransaction, kVerifyFlags>(OffsetOfElement(i), object);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099}
100
101template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -0800102template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800103inline void ObjectArray<T>::SetWithoutChecksAndWriteBarrier(int32_t i, T* object) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800104 DCHECK(CheckIsValidIndex<kVerifyFlags>(i));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800105 // TODO: enable this check. It fails when writing the image in ImageWriter::FixupObjectArray.
Sebastien Hertzabff6432014-01-27 18:01:39 +0100106 // DCHECK(CheckAssignable(object));
Mathieu Chartier4e305412014-02-19 10:54:44 -0800107 SetFieldObjectWithoutWriteBarrier<kTransactionActive, kCheckTransaction, kVerifyFlags>(
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700108 OffsetOfElement(i), object);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109}
110
111template<class T>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800112inline T* ObjectArray<T>::GetWithoutChecks(int32_t i) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100113 DCHECK(CheckIsValidIndex(i));
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700114 return GetFieldObject<T>(OffsetOfElement(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115}
116
117template<class T>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800118inline void ObjectArray<T>::AssignableMemmove(int32_t dst_pos, ObjectArray<T>* src,
119 int32_t src_pos, int32_t count) {
120 if (kIsDebugBuild) {
121 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700122 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800123 src->GetWithoutChecks(src_pos + i);
124 }
125 }
126 // Perform the memmove using int memmove then perform the write barrier.
127 CHECK_EQ(sizeof(HeapReference<T>), sizeof(uint32_t));
128 IntArray* dstAsIntArray = reinterpret_cast<IntArray*>(this);
129 IntArray* srcAsIntArray = reinterpret_cast<IntArray*>(src);
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700130 if (kUseBakerOrBrooksReadBarrier) {
131 // TODO: Optimize this later?
132 const bool copy_forward = (src != this) || (dst_pos < src_pos) || (dst_pos - src_pos >= count);
133 if (copy_forward) {
134 // Forward copy.
135 for (int i = 0; i < count; ++i) {
136 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
137 Object* obj = src->GetWithoutChecks(src_pos + i);
138 SetWithoutChecks<false>(dst_pos + i, obj);
139 }
140 } else {
141 // Backward copy.
142 for (int i = count - 1; i >= 0; --i) {
143 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
144 Object* obj = src->GetWithoutChecks(src_pos + i);
145 SetWithoutChecks<false>(dst_pos + i, obj);
146 }
147 }
148 } else {
149 dstAsIntArray->Memmove(dst_pos, srcAsIntArray, src_pos, count);
150 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800151 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
152 if (kIsDebugBuild) {
153 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700154 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800155 GetWithoutChecks(dst_pos + i);
156 }
157 }
158}
159
160template<class T>
161inline void ObjectArray<T>::AssignableMemcpy(int32_t dst_pos, ObjectArray<T>* src,
162 int32_t src_pos, int32_t count) {
163 if (kIsDebugBuild) {
164 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700165 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800166 src->GetWithoutChecks(src_pos + i);
167 }
168 }
169 // Perform the memmove using int memcpy then perform the write barrier.
170 CHECK_EQ(sizeof(HeapReference<T>), sizeof(uint32_t));
171 IntArray* dstAsIntArray = reinterpret_cast<IntArray*>(this);
172 IntArray* srcAsIntArray = reinterpret_cast<IntArray*>(src);
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700173 if (kUseBakerOrBrooksReadBarrier) {
174 // TODO: Optimize this later?
175 for (int i = 0; i < count; ++i) {
176 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
177 T* obj = src->GetWithoutChecks(src_pos + i);
178 SetWithoutChecks<false>(dst_pos + i, obj);
179 }
180 } else {
181 dstAsIntArray->Memcpy(dst_pos, srcAsIntArray, src_pos, count);
182 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800183 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
184 if (kIsDebugBuild) {
185 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700186 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800187 GetWithoutChecks(dst_pos + i);
188 }
189 }
190}
191
192template<class T>
193inline void ObjectArray<T>::AssignableCheckingMemcpy(int32_t dst_pos, ObjectArray<T>* src,
194 int32_t src_pos, int32_t count,
195 bool throw_exception) {
196 DCHECK_NE(this, src)
197 << "This case should be handled with memmove that handles overlaps correctly";
198 // We want to avoid redundant IsAssignableFrom checks where possible, so we cache a class that
199 // we know is assignable to the destination array's component type.
200 Class* dst_class = GetClass()->GetComponentType();
201 Class* lastAssignableElementClass = dst_class;
202
203 Object* o = nullptr;
204 int i = 0;
205 for (; i < count; ++i) {
206 // The follow get operations force the objects to be verified.
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700207 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800208 o = src->GetWithoutChecks(src_pos + i);
209 if (o == nullptr) {
210 // Null is always assignable.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100211 SetWithoutChecks<false>(dst_pos + i, nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800212 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800213 // TODO: use the underlying class reference to avoid uncompression when not necessary.
214 Class* o_class = o->GetClass();
215 if (LIKELY(lastAssignableElementClass == o_class)) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100216 SetWithoutChecks<false>(dst_pos + i, o);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800217 } else if (LIKELY(dst_class->IsAssignableFrom(o_class))) {
218 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 {
221 // Can't put this element into the array, break to perform write-barrier and throw
222 // exception.
223 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800224 }
225 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800226 }
227 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
228 if (UNLIKELY(i != count)) {
229 std::string actualSrcType(PrettyTypeOf(o));
230 std::string dstType(PrettyTypeOf(this));
231 Thread* self = Thread::Current();
232 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
233 if (throw_exception) {
234 self->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayStoreException;",
235 "source[%d] of type %s cannot be stored in destination array of type %s",
236 src_pos + i, actualSrcType.c_str(), dstType.c_str());
237 } else {
238 LOG(FATAL) << StringPrintf("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 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800241 }
242}
243
244template<class T>
245inline ObjectArray<T>* ObjectArray<T>::CopyOf(Thread* self, int32_t new_length) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800246 DCHECK_GE(new_length, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700247 // We may get copied by a compacting GC.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700248 StackHandleScope<1> hs(self);
Ian Rogers700a4022014-05-19 16:49:03 -0700249 Handle<ObjectArray<T>> h_this(hs.NewHandle(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)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700255 new_array->AssignableMemcpy(0, h_this.Get(), 0, std::min(h_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_