Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_MIRROR_OBJECT_ARRAY_INL_H_ |
| 18 | #define ART_RUNTIME_MIRROR_OBJECT_ARRAY_INL_H_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | |
| 20 | #include "object_array.h" |
| 21 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 22 | #include "gc/heap.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 23 | #include "mirror/art_field.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 24 | #include "mirror/class.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 25 | #include "runtime.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 26 | #include "sirt_ref.h" |
Sebastien Hertz | 6bdd8f4 | 2013-05-17 14:44:01 +0200 | [diff] [blame] | 27 | #include "thread.h" |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 28 | #include <string> |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | namespace mirror { |
| 32 | |
| 33 | template<class T> |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 34 | inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self, Class* object_array_class, |
| 35 | int32_t length, gc::AllocatorType allocator_type) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 36 | Array* array = Array::Alloc<true>(self, object_array_class, length, |
| 37 | sizeof(HeapReference<Object>), allocator_type); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 38 | if (UNLIKELY(array == nullptr)) { |
| 39 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 40 | } else { |
| 41 | return array->AsObjectArray<T>(); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | template<class T> |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 46 | inline 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 | |
| 52 | template<class T> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 53 | inline T* ObjectArray<T>::Get(int32_t i) { |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 54 | if (UNLIKELY(!CheckIsValidIndex(i))) { |
| 55 | DCHECK(Thread::Current()->IsExceptionPending()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 56 | return NULL; |
| 57 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 58 | return GetFieldObject<T>(OffsetOfElement(i), false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 61 | template<class T> template<VerifyObjectFlags kVerifyFlags> |
Sebastien Hertz | 6bdd8f4 | 2013-05-17 14:44:01 +0200 | [diff] [blame] | 62 | inline bool ObjectArray<T>::CheckAssignable(T* object) { |
| 63 | if (object != NULL) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 64 | Class* element_class = GetClass<kVerifyFlags>()->GetComponentType(); |
Sebastien Hertz | 6bdd8f4 | 2013-05-17 14:44:01 +0200 | [diff] [blame] | 65 | if (UNLIKELY(!object->InstanceOf(element_class))) { |
| 66 | ThrowArrayStoreException(object); |
| 67 | return false; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 68 | } |
Sebastien Hertz | 6bdd8f4 | 2013-05-17 14:44:01 +0200 | [diff] [blame] | 69 | } |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | template<class T> |
| 74 | inline void ObjectArray<T>::Set(int32_t i, T* object) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 75 | if (Runtime::Current()->IsActiveTransaction()) { |
| 76 | Set<true>(i, object); |
| 77 | } else { |
| 78 | Set<false>(i, object); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | template<class T> |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 83 | template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags> |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 84 | inline void ObjectArray<T>::Set(int32_t i, T* object) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 85 | if (LIKELY(CheckIsValidIndex(i) && CheckAssignable<kVerifyFlags>(object))) { |
| 86 | SetFieldObject<kTransactionActive, kCheckTransaction, kVerifyFlags>(OffsetOfElement(i), object, |
| 87 | false); |
Sebastien Hertz | 6bdd8f4 | 2013-05-17 14:44:01 +0200 | [diff] [blame] | 88 | } else { |
| 89 | DCHECK(Thread::Current()->IsExceptionPending()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | template<class T> |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 94 | template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags> |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 95 | inline void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 96 | DCHECK(CheckIsValidIndex<kVerifyFlags>(i)); |
| 97 | DCHECK(CheckAssignable<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>(object)); |
| 98 | SetFieldObject<kTransactionActive, kCheckTransaction, kVerifyFlags>(OffsetOfElement(i), object, |
| 99 | false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | template<class T> |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 103 | template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 104 | inline void ObjectArray<T>::SetWithoutChecksAndWriteBarrier(int32_t i, T* object) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 105 | DCHECK(CheckIsValidIndex<kVerifyFlags>(i)); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 106 | // TODO: enable this check. It fails when writing the image in ImageWriter::FixupObjectArray. |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 107 | // DCHECK(CheckAssignable(object)); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 108 | SetFieldObjectWithoutWriteBarrier<kTransactionActive, kCheckTransaction, kVerifyFlags>( |
| 109 | OffsetOfElement(i), object, false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | template<class T> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 113 | inline T* ObjectArray<T>::GetWithoutChecks(int32_t i) { |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 114 | DCHECK(CheckIsValidIndex(i)); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 115 | return GetFieldObject<T>(OffsetOfElement(i), false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | template<class T> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 119 | inline void ObjectArray<T>::AssignableMemmove(int32_t dst_pos, ObjectArray<T>* src, |
| 120 | int32_t src_pos, int32_t count) { |
| 121 | if (kIsDebugBuild) { |
| 122 | for (int i = 0; i < count; ++i) { |
| 123 | // The Get will perform the VerifyObject. |
| 124 | src->GetWithoutChecks(src_pos + i); |
| 125 | } |
| 126 | } |
| 127 | // Perform the memmove using int memmove then perform the write barrier. |
| 128 | CHECK_EQ(sizeof(HeapReference<T>), sizeof(uint32_t)); |
| 129 | IntArray* dstAsIntArray = reinterpret_cast<IntArray*>(this); |
| 130 | IntArray* srcAsIntArray = reinterpret_cast<IntArray*>(src); |
| 131 | dstAsIntArray->Memmove(dst_pos, srcAsIntArray, src_pos, count); |
| 132 | Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count); |
| 133 | if (kIsDebugBuild) { |
| 134 | for (int i = 0; i < count; ++i) { |
| 135 | // The Get will perform the VerifyObject. |
| 136 | GetWithoutChecks(dst_pos + i); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | template<class T> |
| 142 | inline void ObjectArray<T>::AssignableMemcpy(int32_t dst_pos, ObjectArray<T>* src, |
| 143 | int32_t src_pos, int32_t count) { |
| 144 | if (kIsDebugBuild) { |
| 145 | for (int i = 0; i < count; ++i) { |
| 146 | // The Get will perform the VerifyObject. |
| 147 | src->GetWithoutChecks(src_pos + i); |
| 148 | } |
| 149 | } |
| 150 | // Perform the memmove using int memcpy then perform the write barrier. |
| 151 | CHECK_EQ(sizeof(HeapReference<T>), sizeof(uint32_t)); |
| 152 | IntArray* dstAsIntArray = reinterpret_cast<IntArray*>(this); |
| 153 | IntArray* srcAsIntArray = reinterpret_cast<IntArray*>(src); |
| 154 | dstAsIntArray->Memcpy(dst_pos, srcAsIntArray, src_pos, count); |
| 155 | Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count); |
| 156 | if (kIsDebugBuild) { |
| 157 | for (int i = 0; i < count; ++i) { |
| 158 | // The Get will perform the VerifyObject. |
| 159 | GetWithoutChecks(dst_pos + i); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | template<class T> |
| 165 | inline void ObjectArray<T>::AssignableCheckingMemcpy(int32_t dst_pos, ObjectArray<T>* src, |
| 166 | int32_t src_pos, int32_t count, |
| 167 | bool throw_exception) { |
| 168 | DCHECK_NE(this, src) |
| 169 | << "This case should be handled with memmove that handles overlaps correctly"; |
| 170 | // We want to avoid redundant IsAssignableFrom checks where possible, so we cache a class that |
| 171 | // we know is assignable to the destination array's component type. |
| 172 | Class* dst_class = GetClass()->GetComponentType(); |
| 173 | Class* lastAssignableElementClass = dst_class; |
| 174 | |
| 175 | Object* o = nullptr; |
| 176 | int i = 0; |
| 177 | for (; i < count; ++i) { |
| 178 | // The follow get operations force the objects to be verified. |
| 179 | o = src->GetWithoutChecks(src_pos + i); |
| 180 | if (o == nullptr) { |
| 181 | // Null is always assignable. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 182 | SetWithoutChecks<false>(dst_pos + i, nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 183 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 184 | // TODO: use the underlying class reference to avoid uncompression when not necessary. |
| 185 | Class* o_class = o->GetClass(); |
| 186 | if (LIKELY(lastAssignableElementClass == o_class)) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 187 | SetWithoutChecks<false>(dst_pos + i, o); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 188 | } else if (LIKELY(dst_class->IsAssignableFrom(o_class))) { |
| 189 | lastAssignableElementClass = o_class; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 190 | SetWithoutChecks<false>(dst_pos + i, o); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 191 | } else { |
| 192 | // Can't put this element into the array, break to perform write-barrier and throw |
| 193 | // exception. |
| 194 | break; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 195 | } |
| 196 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 197 | } |
| 198 | Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count); |
| 199 | if (UNLIKELY(i != count)) { |
| 200 | std::string actualSrcType(PrettyTypeOf(o)); |
| 201 | std::string dstType(PrettyTypeOf(this)); |
| 202 | Thread* self = Thread::Current(); |
| 203 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 204 | if (throw_exception) { |
| 205 | self->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayStoreException;", |
| 206 | "source[%d] of type %s cannot be stored in destination array of type %s", |
| 207 | src_pos + i, actualSrcType.c_str(), dstType.c_str()); |
| 208 | } else { |
| 209 | LOG(FATAL) << StringPrintf("source[%d] of type %s cannot be stored in destination array of type %s", |
| 210 | src_pos + i, actualSrcType.c_str(), dstType.c_str()); |
| 211 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | template<class T> |
| 216 | inline ObjectArray<T>* ObjectArray<T>::CopyOf(Thread* self, int32_t new_length) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 217 | DCHECK_GE(new_length, 0); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 218 | // We may get copied by a compacting GC. |
| 219 | SirtRef<ObjectArray<T> > sirt_this(self, this); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 220 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 221 | gc::AllocatorType allocator_type = heap->IsMovableObject(this) ? heap->GetCurrentAllocator() : |
| 222 | heap->GetCurrentNonMovingAllocator(); |
| 223 | ObjectArray<T>* new_array = Alloc(self, GetClass(), new_length, allocator_type); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 224 | if (LIKELY(new_array != nullptr)) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 225 | new_array->AssignableMemcpy(0, sirt_this.get(), 0, std::min(sirt_this->GetLength(), new_length)); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 226 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 227 | return new_array; |
| 228 | } |
| 229 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 230 | template<class T> |
| 231 | inline MemberOffset ObjectArray<T>::OffsetOfElement(int32_t i) { |
| 232 | return MemberOffset(DataOffset(sizeof(HeapReference<Object>)).Int32Value() + |
| 233 | (i * sizeof(HeapReference<Object>))); |
| 234 | } |
| 235 | |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame^] | 236 | template<class T> template<const bool kVisitClass, typename Visitor> |
| 237 | void ObjectArray<T>::VisitReferences(const Visitor& visitor) { |
| 238 | if (kVisitClass) { |
| 239 | visitor(this, ClassOffset(), false); |
| 240 | } |
| 241 | const size_t length = static_cast<size_t>(GetLength()); |
| 242 | for (size_t i = 0; i < length; ++i) { |
| 243 | visitor(this, OffsetOfElement(i), false); |
| 244 | } |
| 245 | } |
| 246 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 247 | } // namespace mirror |
| 248 | } // namespace art |
| 249 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 250 | #endif // ART_RUNTIME_MIRROR_OBJECT_ARRAY_INL_H_ |