blob: c3424793e934535da7c4d3b9a4e8bf3d2ddf0cf7 [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
61template<class T>
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020062inline bool ObjectArray<T>::CheckAssignable(T* object) {
63 if (object != NULL) {
64 Class* element_class = GetClass()->GetComponentType();
65 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 Hertzabff6432014-01-27 18:01:39 +010075 if (LIKELY(CheckIsValidIndex(i) && CheckAssignable(object))) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080076 SetFieldObject(OffsetOfElement(i), object, false);
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020077 } else {
78 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079 }
80}
81
82template<class T>
83inline void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
Sebastien Hertzabff6432014-01-27 18:01:39 +010084 DCHECK(CheckIsValidIndex(i));
85 DCHECK(CheckAssignable(object));
Ian Rogersef7d42f2014-01-06 12:55:46 -080086 SetFieldObject(OffsetOfElement(i), object, false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087}
88
89template<class T>
Ian Rogersef7d42f2014-01-06 12:55:46 -080090inline void ObjectArray<T>::SetWithoutChecksAndWriteBarrier(int32_t i, T* object) {
Sebastien Hertzabff6432014-01-27 18:01:39 +010091 DCHECK(CheckIsValidIndex(i));
Ian Rogersef7d42f2014-01-06 12:55:46 -080092 // TODO: enable this check. It fails when writing the image in ImageWriter::FixupObjectArray.
Sebastien Hertzabff6432014-01-27 18:01:39 +010093 // DCHECK(CheckAssignable(object));
Ian Rogersef7d42f2014-01-06 12:55:46 -080094 SetFieldObjectWithoutWriteBarrier(OffsetOfElement(i), object, false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080095}
96
97template<class T>
Ian Rogersef7d42f2014-01-06 12:55:46 -080098inline T* ObjectArray<T>::GetWithoutChecks(int32_t i) {
Sebastien Hertzabff6432014-01-27 18:01:39 +010099 DCHECK(CheckIsValidIndex(i));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800100 return GetFieldObject<T>(OffsetOfElement(i), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800101}
102
103template<class T>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800104inline void ObjectArray<T>::AssignableMemmove(int32_t dst_pos, ObjectArray<T>* src,
105 int32_t src_pos, int32_t count) {
106 if (kIsDebugBuild) {
107 for (int i = 0; i < count; ++i) {
108 // The Get will perform the VerifyObject.
109 src->GetWithoutChecks(src_pos + i);
110 }
111 }
112 // Perform the memmove using int memmove then perform the write barrier.
113 CHECK_EQ(sizeof(HeapReference<T>), sizeof(uint32_t));
114 IntArray* dstAsIntArray = reinterpret_cast<IntArray*>(this);
115 IntArray* srcAsIntArray = reinterpret_cast<IntArray*>(src);
116 dstAsIntArray->Memmove(dst_pos, srcAsIntArray, src_pos, count);
117 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
118 if (kIsDebugBuild) {
119 for (int i = 0; i < count; ++i) {
120 // The Get will perform the VerifyObject.
121 GetWithoutChecks(dst_pos + i);
122 }
123 }
124}
125
126template<class T>
127inline void ObjectArray<T>::AssignableMemcpy(int32_t dst_pos, ObjectArray<T>* src,
128 int32_t src_pos, int32_t count) {
129 if (kIsDebugBuild) {
130 for (int i = 0; i < count; ++i) {
131 // The Get will perform the VerifyObject.
132 src->GetWithoutChecks(src_pos + i);
133 }
134 }
135 // Perform the memmove using int memcpy then perform the write barrier.
136 CHECK_EQ(sizeof(HeapReference<T>), sizeof(uint32_t));
137 IntArray* dstAsIntArray = reinterpret_cast<IntArray*>(this);
138 IntArray* srcAsIntArray = reinterpret_cast<IntArray*>(src);
139 dstAsIntArray->Memcpy(dst_pos, srcAsIntArray, src_pos, count);
140 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
141 if (kIsDebugBuild) {
142 for (int i = 0; i < count; ++i) {
143 // The Get will perform the VerifyObject.
144 GetWithoutChecks(dst_pos + i);
145 }
146 }
147}
148
149template<class T>
150inline void ObjectArray<T>::AssignableCheckingMemcpy(int32_t dst_pos, ObjectArray<T>* src,
151 int32_t src_pos, int32_t count,
152 bool throw_exception) {
153 DCHECK_NE(this, src)
154 << "This case should be handled with memmove that handles overlaps correctly";
155 // We want to avoid redundant IsAssignableFrom checks where possible, so we cache a class that
156 // we know is assignable to the destination array's component type.
157 Class* dst_class = GetClass()->GetComponentType();
158 Class* lastAssignableElementClass = dst_class;
159
160 Object* o = nullptr;
161 int i = 0;
162 for (; i < count; ++i) {
163 // The follow get operations force the objects to be verified.
164 o = src->GetWithoutChecks(src_pos + i);
165 if (o == nullptr) {
166 // Null is always assignable.
167 SetWithoutChecks(dst_pos + i, nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800168 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800169 // TODO: use the underlying class reference to avoid uncompression when not necessary.
170 Class* o_class = o->GetClass();
171 if (LIKELY(lastAssignableElementClass == o_class)) {
172 SetWithoutChecks(dst_pos + i, o);
173 } else if (LIKELY(dst_class->IsAssignableFrom(o_class))) {
174 lastAssignableElementClass = o_class;
175 SetWithoutChecks(dst_pos + i, o);
176 } else {
177 // Can't put this element into the array, break to perform write-barrier and throw
178 // exception.
179 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800180 }
181 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800182 }
183 Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
184 if (UNLIKELY(i != count)) {
185 std::string actualSrcType(PrettyTypeOf(o));
186 std::string dstType(PrettyTypeOf(this));
187 Thread* self = Thread::Current();
188 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
189 if (throw_exception) {
190 self->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayStoreException;",
191 "source[%d] of type %s cannot be stored in destination array of type %s",
192 src_pos + i, actualSrcType.c_str(), dstType.c_str());
193 } else {
194 LOG(FATAL) << StringPrintf("source[%d] of type %s cannot be stored in destination array of type %s",
195 src_pos + i, actualSrcType.c_str(), dstType.c_str());
196 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800197 }
198}
199
200template<class T>
201inline ObjectArray<T>* ObjectArray<T>::CopyOf(Thread* self, int32_t new_length) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800202 DCHECK_GE(new_length, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700203 // We may get copied by a compacting GC.
204 SirtRef<ObjectArray<T> > sirt_this(self, this);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800205 gc::Heap* heap = Runtime::Current()->GetHeap();
206 gc::AllocatorType allocator_type = heap->IsMovableObject(this) ? heap->GetCurrentAllocator() :
207 heap->GetCurrentNonMovingAllocator();
208 ObjectArray<T>* new_array = Alloc(self, GetClass(), new_length, allocator_type);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700209 if (LIKELY(new_array != nullptr)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800210 new_array->AssignableMemcpy(0, sirt_this.get(), 0, std::min(sirt_this->GetLength(), new_length));
Ian Rogersa436fde2013-08-27 23:34:06 -0700211 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800212 return new_array;
213}
214
Ian Rogersef7d42f2014-01-06 12:55:46 -0800215template<class T>
216inline MemberOffset ObjectArray<T>::OffsetOfElement(int32_t i) {
217 return MemberOffset(DataOffset(sizeof(HeapReference<Object>)).Int32Value() +
218 (i * sizeof(HeapReference<Object>)));
219}
220
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800221} // namespace mirror
222} // namespace art
223
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700224#endif // ART_RUNTIME_MIRROR_OBJECT_ARRAY_INL_H_