blob: 5fb945958db1c073965237eff46fdf0582c0809e [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
Vladimir Marko80afd022015-05-19 18:08:00 +010020#include <string>
21
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "object_array.h"
23
Ian Rogers7e70b002014-10-08 11:47:24 -070024#include "array-inl.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070025#include "base/stringprintf.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070026#include "gc/heap.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/class.h"
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070028#include "obj_ptr-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "runtime.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070030#include "handle_scope-inl.h"
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020031#include "thread.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010032#include "utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033
34namespace art {
35namespace mirror {
36
37template<class T>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070038inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self,
39 ObjPtr<Class> object_array_class,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080040 int32_t length, gc::AllocatorType allocator_type) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070041 Array* array = Array::Alloc<true>(self,
42 object_array_class.Ptr(),
43 length,
44 ComponentSizeShiftWidth(kHeapReferenceSize),
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070045 allocator_type);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080046 if (UNLIKELY(array == nullptr)) {
47 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048 }
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070049 DCHECK_EQ(array->GetClass()->GetComponentSizeShift(),
50 ComponentSizeShiftWidth(kHeapReferenceSize));
51 return array->AsObjectArray<T>();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052}
53
54template<class T>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070055inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self,
56 ObjPtr<Class> object_array_class,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080057 int32_t length) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070058 return Alloc(self,
59 object_array_class,
60 length,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080061 Runtime::Current()->GetHeap()->GetCurrentAllocator());
62}
63
Mathieu Chartierfbc31082016-01-24 11:59:56 -080064template<class T> template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
Ian Rogersef7d42f2014-01-06 12:55:46 -080065inline T* ObjectArray<T>::Get(int32_t i) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070066 if (!CheckIsValidIndex(i)) {
Sebastien Hertzabff6432014-01-27 18:01:39 +010067 DCHECK(Thread::Current()->IsExceptionPending());
Mathieu Chartier2cebb242015-04-21 16:50:40 -070068 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -080070 return GetFieldObject<T, kVerifyFlags, kReadBarrierOption>(OffsetOfElement(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071}
72
Mathieu Chartier4e305412014-02-19 10:54:44 -080073template<class T> template<VerifyObjectFlags kVerifyFlags>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070074inline bool ObjectArray<T>::CheckAssignable(ObjPtr<T> object) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070075 if (object != nullptr) {
Mathieu Chartier4e305412014-02-19 10:54:44 -080076 Class* element_class = GetClass<kVerifyFlags>()->GetComponentType();
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020077 if (UNLIKELY(!object->InstanceOf(element_class))) {
78 ThrowArrayStoreException(object);
79 return false;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080 }
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020081 }
82 return true;
83}
84
85template<class T>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070086inline void ObjectArray<T>::Set(int32_t i, ObjPtr<T> object) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010087 if (Runtime::Current()->IsActiveTransaction()) {
88 Set<true>(i, object);
89 } else {
90 Set<false>(i, object);
91 }
92}
93
94template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -080095template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070096inline void ObjectArray<T>::Set(int32_t i, ObjPtr<T> object) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070097 if (CheckIsValidIndex(i) && CheckAssignable<kVerifyFlags>(object)) {
98 SetFieldObject<kTransactionActive, kCheckTransaction, kVerifyFlags>(OffsetOfElement(i), object);
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020099 } else {
100 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800101 }
102}
103
104template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -0800105template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700106inline void ObjectArray<T>::SetWithoutChecks(int32_t i, ObjPtr<T> object) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800107 DCHECK(CheckIsValidIndex<kVerifyFlags>(i));
108 DCHECK(CheckAssignable<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>(object));
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700109 SetFieldObject<kTransactionActive, kCheckTransaction, kVerifyFlags>(OffsetOfElement(i), object);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800110}
111
112template<class T>
Mathieu Chartier4e305412014-02-19 10:54:44 -0800113template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700114inline void ObjectArray<T>::SetWithoutChecksAndWriteBarrier(int32_t i, ObjPtr<T> object) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800115 DCHECK(CheckIsValidIndex<kVerifyFlags>(i));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800116 // TODO: enable this check. It fails when writing the image in ImageWriter::FixupObjectArray.
Sebastien Hertzabff6432014-01-27 18:01:39 +0100117 // DCHECK(CheckAssignable(object));
Mathieu Chartier4e305412014-02-19 10:54:44 -0800118 SetFieldObjectWithoutWriteBarrier<kTransactionActive, kCheckTransaction, kVerifyFlags>(
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700119 OffsetOfElement(i), object);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800120}
121
122template<class T>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800123inline T* ObjectArray<T>::GetWithoutChecks(int32_t i) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100124 DCHECK(CheckIsValidIndex(i));
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700125 return GetFieldObject<T>(OffsetOfElement(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126}
127
128template<class T>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700129inline void ObjectArray<T>::AssignableMemmove(int32_t dst_pos,
130 ObjPtr<ObjectArray<T>> src,
131 int32_t src_pos,
132 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800133 if (kIsDebugBuild) {
134 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700135 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800136 src->GetWithoutChecks(src_pos + i);
137 }
138 }
139 // Perform the memmove using int memmove then perform the write barrier.
Roland Levillain33d69032015-06-18 18:20:59 +0100140 static_assert(sizeof(HeapReference<T>) == sizeof(uint32_t),
141 "art::mirror::HeapReference<T> and uint32_t have different sizes.");
Mathieu Chartierfec13d42016-10-07 12:59:33 -0700142 // TODO: Optimize this later?
143 // We can't use memmove since it does not handle read barriers and may do by per byte copying.
144 // See b/32012820.
145 const bool copy_forward = (src != this) || (dst_pos < src_pos) || (dst_pos - src_pos >= count);
146 if (copy_forward) {
147 // Forward copy.
148 for (int i = 0; i < count; ++i) {
149 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
150 Object* obj = src->GetWithoutChecks(src_pos + i);
151 SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj);
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700152 }
153 } else {
Mathieu Chartierfec13d42016-10-07 12:59:33 -0700154 // Backward copy.
155 for (int i = count - 1; i >= 0; --i) {
156 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
157 Object* obj = src->GetWithoutChecks(src_pos + i);
158 SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj);
159 }
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700160 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800161 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
162 if (kIsDebugBuild) {
163 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700164 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800165 GetWithoutChecks(dst_pos + i);
166 }
167 }
168}
169
170template<class T>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700171inline void ObjectArray<T>::AssignableMemcpy(int32_t dst_pos,
172 ObjPtr<ObjectArray<T>> src,
173 int32_t src_pos,
174 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800175 if (kIsDebugBuild) {
176 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700177 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800178 src->GetWithoutChecks(src_pos + i);
179 }
180 }
181 // Perform the memmove using int memcpy then perform the write barrier.
Roland Levillain33d69032015-06-18 18:20:59 +0100182 static_assert(sizeof(HeapReference<T>) == sizeof(uint32_t),
183 "art::mirror::HeapReference<T> and uint32_t have different sizes.");
Mathieu Chartierfec13d42016-10-07 12:59:33 -0700184 // TODO: Optimize this later?
185 // We can't use memmove since it does not handle read barriers and may do by per byte copying.
186 // See b/32012820.
187 for (int i = 0; i < count; ++i) {
188 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
189 T* obj = src->GetWithoutChecks(src_pos + i);
190 SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj);
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700191 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800192 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
193 if (kIsDebugBuild) {
194 for (int i = 0; i < count; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700195 // The get will perform the VerifyObject.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800196 GetWithoutChecks(dst_pos + i);
197 }
198 }
199}
200
201template<class T>
Andreas Gampe85a098a2016-03-31 13:30:53 -0700202template<bool kTransactionActive>
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700203inline void ObjectArray<T>::AssignableCheckingMemcpy(int32_t dst_pos,
204 ObjPtr<ObjectArray<T>> src,
205 int32_t src_pos,
206 int32_t count,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800207 bool throw_exception) {
208 DCHECK_NE(this, src)
209 << "This case should be handled with memmove that handles overlaps correctly";
210 // We want to avoid redundant IsAssignableFrom checks where possible, so we cache a class that
211 // we know is assignable to the destination array's component type.
212 Class* dst_class = GetClass()->GetComponentType();
213 Class* lastAssignableElementClass = dst_class;
214
215 Object* o = nullptr;
216 int i = 0;
217 for (; i < count; ++i) {
218 // The follow get operations force the objects to be verified.
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700219 // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800220 o = src->GetWithoutChecks(src_pos + i);
221 if (o == nullptr) {
222 // Null is always assignable.
Andreas Gampe85a098a2016-03-31 13:30:53 -0700223 SetWithoutChecks<kTransactionActive>(dst_pos + i, nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800224 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800225 // TODO: use the underlying class reference to avoid uncompression when not necessary.
226 Class* o_class = o->GetClass();
227 if (LIKELY(lastAssignableElementClass == o_class)) {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700228 SetWithoutChecks<kTransactionActive>(dst_pos + i, o);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800229 } else if (LIKELY(dst_class->IsAssignableFrom(o_class))) {
230 lastAssignableElementClass = o_class;
Andreas Gampe85a098a2016-03-31 13:30:53 -0700231 SetWithoutChecks<kTransactionActive>(dst_pos + i, o);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800232 } else {
233 // Can't put this element into the array, break to perform write-barrier and throw
234 // exception.
235 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800236 }
237 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800238 }
239 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
240 if (UNLIKELY(i != count)) {
David Sehr709b0702016-10-13 09:12:37 -0700241 std::string actualSrcType(mirror::Object::PrettyTypeOf(o));
242 std::string dstType(PrettyTypeOf());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800243 Thread* self = Thread::Current();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800244 if (throw_exception) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000245 self->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Ian Rogersef7d42f2014-01-06 12:55:46 -0800246 "source[%d] of type %s cannot be stored in destination array of type %s",
247 src_pos + i, actualSrcType.c_str(), dstType.c_str());
248 } else {
249 LOG(FATAL) << StringPrintf("source[%d] of type %s cannot be stored in destination array of type %s",
250 src_pos + i, actualSrcType.c_str(), dstType.c_str());
251 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800252 }
253}
254
255template<class T>
256inline ObjectArray<T>* ObjectArray<T>::CopyOf(Thread* self, int32_t new_length) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800257 DCHECK_GE(new_length, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700258 // We may get copied by a compacting GC.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700259 StackHandleScope<1> hs(self);
Ian Rogers700a4022014-05-19 16:49:03 -0700260 Handle<ObjectArray<T>> h_this(hs.NewHandle(this));
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800261 gc::Heap* heap = Runtime::Current()->GetHeap();
262 gc::AllocatorType allocator_type = heap->IsMovableObject(this) ? heap->GetCurrentAllocator() :
263 heap->GetCurrentNonMovingAllocator();
264 ObjectArray<T>* new_array = Alloc(self, GetClass(), new_length, allocator_type);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700265 if (LIKELY(new_array != nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700266 new_array->AssignableMemcpy(0, h_this.Get(), 0, std::min(h_this->GetLength(), new_length));
Ian Rogersa436fde2013-08-27 23:34:06 -0700267 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800268 return new_array;
269}
270
Ian Rogersef7d42f2014-01-06 12:55:46 -0800271template<class T>
272inline MemberOffset ObjectArray<T>::OffsetOfElement(int32_t i) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700273 return MemberOffset(DataOffset(kHeapReferenceSize).Int32Value() + (i * kHeapReferenceSize));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800274}
275
Mathieu Chartier059ef3d2015-08-18 13:54:21 -0700276template<class T> template<typename Visitor>
Hiroshi Yamauchi723e6ce2015-10-28 20:59:47 -0700277inline void ObjectArray<T>::VisitReferences(const Visitor& visitor) {
Mathieu Chartier407f7022014-02-18 14:37:05 -0800278 const size_t length = static_cast<size_t>(GetLength());
279 for (size_t i = 0; i < length; ++i) {
280 visitor(this, OffsetOfElement(i), false);
281 }
282}
283
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800284} // namespace mirror
285} // namespace art
286
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700287#endif // ART_RUNTIME_MIRROR_OBJECT_ARRAY_INL_H_