blob: fbc4f4a6758c785d0388e3b5cc27b92ad119ee9c [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 Rogers7e70b002014-10-08 11:47:24 -070022#include "array-inl.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070023#include "base/stringprintf.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070024#include "gc/heap.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070025#include "mirror/art_field.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "mirror/class.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "runtime.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070028#include "handle_scope-inl.h"
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020029#include "thread.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080030#include <string>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031
32namespace art {
33namespace mirror {
34
35template<class T>
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080036inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self, Class* object_array_class,
37 int32_t length, gc::AllocatorType allocator_type) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080038 Array* array = Array::Alloc<true>(self, object_array_class, length,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070039 ComponentSizeShiftWidth<sizeof(HeapReference<Object>)>(),
40 allocator_type);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080041 if (UNLIKELY(array == nullptr)) {
42 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043 } else {
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070044 DCHECK_EQ(array->GetClass()->GetComponentSizeShift(),
45 ComponentSizeShiftWidth<sizeof(HeapReference<Object>)>());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046 return array->AsObjectArray<T>();
47 }
48}
49
50template<class T>
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080051inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self, Class* object_array_class,
52 int32_t length) {
53 return Alloc(self, object_array_class, length,
54 Runtime::Current()->GetHeap()->GetCurrentAllocator());
55}
56
57template<class T>
Ian Rogersef7d42f2014-01-06 12:55:46 -080058inline T* ObjectArray<T>::Get(int32_t i) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070059 if (!CheckIsValidIndex(i)) {
Sebastien Hertzabff6432014-01-27 18:01:39 +010060 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061 return NULL;
62 }
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070063 return GetFieldObject<T>(OffsetOfElement(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064}
65
Mathieu Chartier4e305412014-02-19 10:54:44 -080066template<class T> template<VerifyObjectFlags kVerifyFlags>
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020067inline bool ObjectArray<T>::CheckAssignable(T* object) {
68 if (object != NULL) {
Mathieu Chartier4e305412014-02-19 10:54:44 -080069 Class* element_class = GetClass<kVerifyFlags>()->GetComponentType();
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020070 if (UNLIKELY(!object->InstanceOf(element_class))) {
71 ThrowArrayStoreException(object);
72 return false;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073 }
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020074 }
75 return true;
76}
77
78template<class T>
79inline void ObjectArray<T>::Set(int32_t i, T* object) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010080 if (Runtime::Current()->IsActiveTransaction()) {
81 Set<true>(i, object);
82 } else {
83 Set<false>(i, object);
84 }
85}
86
87template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -080088template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010089inline void ObjectArray<T>::Set(int32_t i, T* object) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070090 if (CheckIsValidIndex(i) && CheckAssignable<kVerifyFlags>(object)) {
91 SetFieldObject<kTransactionActive, kCheckTransaction, kVerifyFlags>(OffsetOfElement(i), object);
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020092 } else {
93 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094 }
95}
96
97template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -080098template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099inline void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800100 DCHECK(CheckIsValidIndex<kVerifyFlags>(i));
101 DCHECK(CheckAssignable<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>(object));
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700102 SetFieldObject<kTransactionActive, kCheckTransaction, kVerifyFlags>(OffsetOfElement(i), object);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103}
104
105template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -0800106template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800107inline void ObjectArray<T>::SetWithoutChecksAndWriteBarrier(int32_t i, T* object) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800108 DCHECK(CheckIsValidIndex<kVerifyFlags>(i));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800109 // TODO: enable this check. It fails when writing the image in ImageWriter::FixupObjectArray.
Sebastien Hertzabff6432014-01-27 18:01:39 +0100110 // DCHECK(CheckAssignable(object));
Mathieu Chartier4e305412014-02-19 10:54:44 -0800111 SetFieldObjectWithoutWriteBarrier<kTransactionActive, kCheckTransaction, kVerifyFlags>(
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700112 OffsetOfElement(i), object);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800113}
114
115template<class T>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800116inline T* ObjectArray<T>::GetWithoutChecks(int32_t i) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100117 DCHECK(CheckIsValidIndex(i));
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700118 return GetFieldObject<T>(OffsetOfElement(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800119}
120
121template<class T>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800122inline void ObjectArray<T>::AssignableMemmove(int32_t dst_pos, ObjectArray<T>* src,
123 int32_t src_pos, int32_t count) {
124 if (kIsDebugBuild) {
125 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700126 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800127 src->GetWithoutChecks(src_pos + i);
128 }
129 }
130 // Perform the memmove using int memmove then perform the write barrier.
131 CHECK_EQ(sizeof(HeapReference<T>), sizeof(uint32_t));
132 IntArray* dstAsIntArray = reinterpret_cast<IntArray*>(this);
133 IntArray* srcAsIntArray = reinterpret_cast<IntArray*>(src);
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700134 if (kUseBakerOrBrooksReadBarrier) {
135 // TODO: Optimize this later?
136 const bool copy_forward = (src != this) || (dst_pos < src_pos) || (dst_pos - src_pos >= count);
137 if (copy_forward) {
138 // Forward copy.
139 for (int i = 0; i < count; ++i) {
140 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
141 Object* obj = src->GetWithoutChecks(src_pos + i);
142 SetWithoutChecks<false>(dst_pos + i, obj);
143 }
144 } else {
145 // Backward copy.
146 for (int i = count - 1; i >= 0; --i) {
147 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
148 Object* obj = src->GetWithoutChecks(src_pos + i);
149 SetWithoutChecks<false>(dst_pos + i, obj);
150 }
151 }
152 } else {
153 dstAsIntArray->Memmove(dst_pos, srcAsIntArray, src_pos, count);
154 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800155 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
156 if (kIsDebugBuild) {
157 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700158 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800159 GetWithoutChecks(dst_pos + i);
160 }
161 }
162}
163
164template<class T>
165inline void ObjectArray<T>::AssignableMemcpy(int32_t dst_pos, ObjectArray<T>* src,
166 int32_t src_pos, int32_t count) {
167 if (kIsDebugBuild) {
168 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700169 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800170 src->GetWithoutChecks(src_pos + i);
171 }
172 }
173 // Perform the memmove using int memcpy then perform the write barrier.
174 CHECK_EQ(sizeof(HeapReference<T>), sizeof(uint32_t));
175 IntArray* dstAsIntArray = reinterpret_cast<IntArray*>(this);
176 IntArray* srcAsIntArray = reinterpret_cast<IntArray*>(src);
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700177 if (kUseBakerOrBrooksReadBarrier) {
178 // TODO: Optimize this later?
179 for (int i = 0; i < count; ++i) {
180 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
181 T* obj = src->GetWithoutChecks(src_pos + i);
182 SetWithoutChecks<false>(dst_pos + i, obj);
183 }
184 } else {
185 dstAsIntArray->Memcpy(dst_pos, srcAsIntArray, src_pos, count);
186 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800187 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
188 if (kIsDebugBuild) {
189 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700190 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800191 GetWithoutChecks(dst_pos + i);
192 }
193 }
194}
195
196template<class T>
197inline void ObjectArray<T>::AssignableCheckingMemcpy(int32_t dst_pos, ObjectArray<T>* src,
198 int32_t src_pos, int32_t count,
199 bool throw_exception) {
200 DCHECK_NE(this, src)
201 << "This case should be handled with memmove that handles overlaps correctly";
202 // We want to avoid redundant IsAssignableFrom checks where possible, so we cache a class that
203 // we know is assignable to the destination array's component type.
204 Class* dst_class = GetClass()->GetComponentType();
205 Class* lastAssignableElementClass = dst_class;
206
207 Object* o = nullptr;
208 int i = 0;
209 for (; i < count; ++i) {
210 // The follow get operations force the objects to be verified.
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700211 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800212 o = src->GetWithoutChecks(src_pos + i);
213 if (o == nullptr) {
214 // Null is always assignable.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100215 SetWithoutChecks<false>(dst_pos + i, nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800216 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800217 // TODO: use the underlying class reference to avoid uncompression when not necessary.
218 Class* o_class = o->GetClass();
219 if (LIKELY(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 if (LIKELY(dst_class->IsAssignableFrom(o_class))) {
222 lastAssignableElementClass = o_class;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100223 SetWithoutChecks<false>(dst_pos + i, o);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800224 } else {
225 // Can't put this element into the array, break to perform write-barrier and throw
226 // exception.
227 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800228 }
229 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800230 }
231 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
232 if (UNLIKELY(i != count)) {
233 std::string actualSrcType(PrettyTypeOf(o));
234 std::string dstType(PrettyTypeOf(this));
235 Thread* self = Thread::Current();
236 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
237 if (throw_exception) {
238 self->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayStoreException;",
239 "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 } else {
242 LOG(FATAL) << StringPrintf("source[%d] of type %s cannot be stored in destination array of type %s",
243 src_pos + i, actualSrcType.c_str(), dstType.c_str());
244 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800245 }
246}
247
248template<class T>
249inline ObjectArray<T>* ObjectArray<T>::CopyOf(Thread* self, int32_t new_length) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800250 DCHECK_GE(new_length, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700251 // We may get copied by a compacting GC.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700252 StackHandleScope<1> hs(self);
Ian Rogers700a4022014-05-19 16:49:03 -0700253 Handle<ObjectArray<T>> h_this(hs.NewHandle(this));
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800254 gc::Heap* heap = Runtime::Current()->GetHeap();
255 gc::AllocatorType allocator_type = heap->IsMovableObject(this) ? heap->GetCurrentAllocator() :
256 heap->GetCurrentNonMovingAllocator();
257 ObjectArray<T>* new_array = Alloc(self, GetClass(), new_length, allocator_type);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700258 if (LIKELY(new_array != nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700259 new_array->AssignableMemcpy(0, h_this.Get(), 0, std::min(h_this->GetLength(), new_length));
Ian Rogersa436fde2013-08-27 23:34:06 -0700260 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800261 return new_array;
262}
263
Ian Rogersef7d42f2014-01-06 12:55:46 -0800264template<class T>
265inline MemberOffset ObjectArray<T>::OffsetOfElement(int32_t i) {
266 return MemberOffset(DataOffset(sizeof(HeapReference<Object>)).Int32Value() +
267 (i * sizeof(HeapReference<Object>)));
268}
269
Mathieu Chartier407f7022014-02-18 14:37:05 -0800270template<class T> template<const bool kVisitClass, typename Visitor>
271void ObjectArray<T>::VisitReferences(const Visitor& visitor) {
272 if (kVisitClass) {
273 visitor(this, ClassOffset(), false);
274 }
275 const size_t length = static_cast<size_t>(GetLength());
276 for (size_t i = 0; i < length; ++i) {
277 visitor(this, OffsetOfElement(i), false);
278 }
279}
280
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800281} // namespace mirror
282} // namespace art
283
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700284#endif // ART_RUNTIME_MIRROR_OBJECT_ARRAY_INL_H_