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 | |
| 17 | #ifndef ART_SRC_MIRROR_OBJECT_ARRAY_INL_H_ |
| 18 | #define ART_SRC_MIRROR_OBJECT_ARRAY_INL_H_ |
| 19 | |
| 20 | #include "object_array.h" |
| 21 | |
| 22 | #include "heap.h" |
| 23 | #include "mirror/class.h" |
| 24 | #include "mirror/field.h" |
| 25 | #include "runtime.h" |
| 26 | |
| 27 | namespace art { |
| 28 | namespace mirror { |
| 29 | |
| 30 | template<class T> |
| 31 | inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self, Class* object_array_class, int32_t length) { |
| 32 | Array* array = Array::Alloc(self, object_array_class, length, sizeof(Object*)); |
| 33 | if (UNLIKELY(array == NULL)) { |
| 34 | return NULL; |
| 35 | } else { |
| 36 | return array->AsObjectArray<T>(); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | template<class T> |
| 41 | inline T* ObjectArray<T>::Get(int32_t i) const { |
| 42 | if (UNLIKELY(!IsValidIndex(i))) { |
| 43 | return NULL; |
| 44 | } |
| 45 | MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*)); |
| 46 | return GetFieldObject<T*>(data_offset, false); |
| 47 | } |
| 48 | |
| 49 | template<class T> |
| 50 | inline void ObjectArray<T>::Set(int32_t i, T* object) { |
| 51 | if (LIKELY(IsValidIndex(i))) { |
| 52 | if (object != NULL) { |
| 53 | Class* element_class = GetClass()->GetComponentType(); |
| 54 | if (UNLIKELY(!object->InstanceOf(element_class))) { |
| 55 | ThrowArrayStoreException(object); |
| 56 | return; |
| 57 | } |
| 58 | } |
| 59 | MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*)); |
| 60 | SetFieldObject(data_offset, object, false); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | template<class T> |
| 65 | inline void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) { |
| 66 | DCHECK(IsValidIndex(i)); |
| 67 | MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*)); |
| 68 | SetFieldObject(data_offset, object, false); |
| 69 | } |
| 70 | |
| 71 | template<class T> |
| 72 | inline void ObjectArray<T>::SetPtrWithoutChecks(int32_t i, T* object) { |
| 73 | DCHECK(IsValidIndex(i)); |
| 74 | MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*)); |
| 75 | SetFieldPtr(data_offset, object, false); |
| 76 | } |
| 77 | |
| 78 | template<class T> |
| 79 | inline T* ObjectArray<T>::GetWithoutChecks(int32_t i) const { |
| 80 | DCHECK(IsValidIndex(i)); |
| 81 | MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*)); |
| 82 | return GetFieldObject<T*>(data_offset, false); |
| 83 | } |
| 84 | |
| 85 | template<class T> |
| 86 | inline void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos, |
| 87 | ObjectArray<T>* dst, int dst_pos, |
| 88 | size_t length) { |
| 89 | if (src->IsValidIndex(src_pos) && |
| 90 | src->IsValidIndex(src_pos+length-1) && |
| 91 | dst->IsValidIndex(dst_pos) && |
| 92 | dst->IsValidIndex(dst_pos+length-1)) { |
| 93 | MemberOffset src_offset(DataOffset(sizeof(Object*)).Int32Value() + src_pos * sizeof(Object*)); |
| 94 | MemberOffset dst_offset(DataOffset(sizeof(Object*)).Int32Value() + dst_pos * sizeof(Object*)); |
| 95 | Class* array_class = dst->GetClass(); |
| 96 | Heap* heap = Runtime::Current()->GetHeap(); |
| 97 | if (array_class == src->GetClass()) { |
| 98 | // No need for array store checks if arrays are of the same type |
| 99 | for (size_t i = 0; i < length; i++) { |
| 100 | Object* object = src->GetFieldObject<Object*>(src_offset, false); |
| 101 | heap->VerifyObject(object); |
| 102 | // directly set field, we do a bulk write barrier at the end |
| 103 | dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true); |
| 104 | src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*)); |
| 105 | dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*)); |
| 106 | } |
| 107 | } else { |
| 108 | Class* element_class = array_class->GetComponentType(); |
| 109 | CHECK(!element_class->IsPrimitive()); |
| 110 | for (size_t i = 0; i < length; i++) { |
| 111 | Object* object = src->GetFieldObject<Object*>(src_offset, false); |
| 112 | if (object != NULL && !object->InstanceOf(element_class)) { |
| 113 | dst->ThrowArrayStoreException(object); |
| 114 | return; |
| 115 | } |
| 116 | heap->VerifyObject(object); |
| 117 | // directly set field, we do a bulk write barrier at the end |
| 118 | dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true); |
| 119 | src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*)); |
| 120 | dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*)); |
| 121 | } |
| 122 | } |
| 123 | heap->WriteBarrierArray(dst, dst_pos, length); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | template<class T> |
| 128 | inline ObjectArray<T>* ObjectArray<T>::CopyOf(Thread* self, int32_t new_length) { |
| 129 | ObjectArray<T>* new_array = Alloc(self, GetClass(), new_length); |
| 130 | Copy(this, 0, new_array, 0, std::min(GetLength(), new_length)); |
| 131 | return new_array; |
| 132 | } |
| 133 | |
| 134 | } // namespace mirror |
| 135 | } // namespace art |
| 136 | |
| 137 | #endif // ART_SRC_MIRROR_OBJET_ARRAY_INL_H_ |