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 | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | namespace mirror { |
| 31 | |
| 32 | template<class T> |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 33 | inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self, Class* object_array_class, |
| 34 | int32_t length, gc::AllocatorType allocator_type) { |
| 35 | Array* array = Array::Alloc<true>(self, object_array_class, length, sizeof(Object*), |
| 36 | allocator_type); |
| 37 | if (UNLIKELY(array == nullptr)) { |
| 38 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 39 | } else { |
| 40 | return array->AsObjectArray<T>(); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | template<class T> |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 45 | inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self, Class* object_array_class, |
| 46 | int32_t length) { |
| 47 | return Alloc(self, object_array_class, length, |
| 48 | Runtime::Current()->GetHeap()->GetCurrentAllocator()); |
| 49 | } |
| 50 | |
| 51 | template<class T> |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 52 | inline T* ObjectArray<T>::Get(int32_t i) const { |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame^] | 53 | if (UNLIKELY(!CheckIsValidIndex(i))) { |
| 54 | DCHECK(Thread::Current()->IsExceptionPending()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 55 | return NULL; |
| 56 | } |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame^] | 57 | return GetWithoutChecks(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | template<class T> |
Sebastien Hertz | 6bdd8f4 | 2013-05-17 14:44:01 +0200 | [diff] [blame] | 61 | inline bool ObjectArray<T>::CheckAssignable(T* object) { |
| 62 | if (object != NULL) { |
| 63 | Class* element_class = GetClass()->GetComponentType(); |
| 64 | if (UNLIKELY(!object->InstanceOf(element_class))) { |
| 65 | ThrowArrayStoreException(object); |
| 66 | return false; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 67 | } |
Sebastien Hertz | 6bdd8f4 | 2013-05-17 14:44:01 +0200 | [diff] [blame] | 68 | } |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | template<class T> |
| 73 | inline void ObjectArray<T>::Set(int32_t i, T* object) { |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame^] | 74 | if (LIKELY(CheckIsValidIndex(i) && CheckAssignable(object))) { |
| 75 | SetWithoutChecks(i, object); |
Sebastien Hertz | 6bdd8f4 | 2013-05-17 14:44:01 +0200 | [diff] [blame] | 76 | } else { |
| 77 | DCHECK(Thread::Current()->IsExceptionPending()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | template<class T> |
| 82 | inline void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) { |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame^] | 83 | DCHECK(CheckIsValidIndex(i)); |
| 84 | DCHECK(CheckAssignable(object)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 85 | MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*)); |
| 86 | SetFieldObject(data_offset, object, false); |
| 87 | } |
| 88 | |
| 89 | template<class T> |
| 90 | inline void ObjectArray<T>::SetPtrWithoutChecks(int32_t i, T* object) { |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame^] | 91 | DCHECK(CheckIsValidIndex(i)); |
| 92 | // TODO enable this check. It fails when writing the image in ImageWriter::FixupObjectArray. |
| 93 | // DCHECK(CheckAssignable(object)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 94 | MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*)); |
| 95 | SetFieldPtr(data_offset, object, false); |
| 96 | } |
| 97 | |
| 98 | template<class T> |
| 99 | inline T* ObjectArray<T>::GetWithoutChecks(int32_t i) const { |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame^] | 100 | DCHECK(CheckIsValidIndex(i)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 101 | MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*)); |
| 102 | return GetFieldObject<T*>(data_offset, false); |
| 103 | } |
| 104 | |
| 105 | template<class T> |
| 106 | inline void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos, |
| 107 | ObjectArray<T>* dst, int dst_pos, |
| 108 | size_t length) { |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame^] | 109 | if (src->CheckIsValidIndex(src_pos) && |
| 110 | src->CheckIsValidIndex(src_pos + length - 1) && |
| 111 | dst->CheckIsValidIndex(dst_pos) && |
| 112 | dst->CheckIsValidIndex(dst_pos + length - 1)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 113 | MemberOffset src_offset(DataOffset(sizeof(Object*)).Int32Value() + src_pos * sizeof(Object*)); |
| 114 | MemberOffset dst_offset(DataOffset(sizeof(Object*)).Int32Value() + dst_pos * sizeof(Object*)); |
| 115 | Class* array_class = dst->GetClass(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 116 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 117 | if (array_class == src->GetClass()) { |
| 118 | // No need for array store checks if arrays are of the same type |
| 119 | for (size_t i = 0; i < length; i++) { |
| 120 | Object* object = src->GetFieldObject<Object*>(src_offset, false); |
| 121 | heap->VerifyObject(object); |
| 122 | // directly set field, we do a bulk write barrier at the end |
| 123 | dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true); |
| 124 | src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*)); |
| 125 | dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*)); |
| 126 | } |
| 127 | } else { |
| 128 | Class* element_class = array_class->GetComponentType(); |
| 129 | CHECK(!element_class->IsPrimitive()); |
| 130 | for (size_t i = 0; i < length; i++) { |
| 131 | Object* object = src->GetFieldObject<Object*>(src_offset, false); |
| 132 | if (object != NULL && !object->InstanceOf(element_class)) { |
| 133 | dst->ThrowArrayStoreException(object); |
| 134 | return; |
| 135 | } |
| 136 | heap->VerifyObject(object); |
| 137 | // directly set field, we do a bulk write barrier at the end |
| 138 | dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true); |
| 139 | src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*)); |
| 140 | dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*)); |
| 141 | } |
| 142 | } |
| 143 | heap->WriteBarrierArray(dst, dst_pos, length); |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame^] | 144 | } else { |
| 145 | DCHECK(Thread::Current()->IsExceptionPending()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | template<class T> |
| 150 | inline ObjectArray<T>* ObjectArray<T>::CopyOf(Thread* self, int32_t new_length) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 151 | // We may get copied by a compacting GC. |
| 152 | SirtRef<ObjectArray<T> > sirt_this(self, this); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 153 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 154 | gc::AllocatorType allocator_type = heap->IsMovableObject(this) ? heap->GetCurrentAllocator() : |
| 155 | heap->GetCurrentNonMovingAllocator(); |
| 156 | ObjectArray<T>* new_array = Alloc(self, GetClass(), new_length, allocator_type); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 157 | if (LIKELY(new_array != nullptr)) { |
| 158 | Copy(sirt_this.get(), 0, new_array, 0, std::min(sirt_this->GetLength(), new_length)); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 159 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 160 | return new_array; |
| 161 | } |
| 162 | |
| 163 | } // namespace mirror |
| 164 | } // namespace art |
| 165 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 166 | #endif // ART_RUNTIME_MIRROR_OBJECT_ARRAY_INL_H_ |