blob: 478f4ec210de8a4612febf85d6a5ad27cdac1caa [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 Rogers2dd0e2c2013-01-24 12:42:14 -080028
29namespace art {
30namespace mirror {
31
32template<class T>
33inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self, Class* object_array_class, int32_t length) {
Mathieu Chartier590fee92013-09-13 13:46:47 -070034 Array* array = Array::Alloc<kMovingCollector, true>(self, object_array_class, length, sizeof(Object*));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035 if (UNLIKELY(array == NULL)) {
36 return NULL;
37 } else {
38 return array->AsObjectArray<T>();
39 }
40}
41
42template<class T>
43inline T* ObjectArray<T>::Get(int32_t i) const {
44 if (UNLIKELY(!IsValidIndex(i))) {
45 return NULL;
46 }
47 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
48 return GetFieldObject<T*>(data_offset, false);
49}
50
51template<class T>
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020052inline bool ObjectArray<T>::CheckAssignable(T* object) {
53 if (object != NULL) {
54 Class* element_class = GetClass()->GetComponentType();
55 if (UNLIKELY(!object->InstanceOf(element_class))) {
56 ThrowArrayStoreException(object);
57 return false;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058 }
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020059 }
60 return true;
61}
62
63template<class T>
64inline void ObjectArray<T>::Set(int32_t i, T* object) {
65 if (LIKELY(IsValidIndex(i) && CheckAssignable(object))) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
67 SetFieldObject(data_offset, object, false);
Sebastien Hertz6bdd8f42013-05-17 14:44:01 +020068 } else {
69 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080070 }
71}
72
73template<class T>
74inline void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
75 DCHECK(IsValidIndex(i));
76 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
77 SetFieldObject(data_offset, object, false);
78}
79
80template<class T>
81inline void ObjectArray<T>::SetPtrWithoutChecks(int32_t i, T* object) {
82 DCHECK(IsValidIndex(i));
83 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
84 SetFieldPtr(data_offset, object, false);
85}
86
87template<class T>
88inline T* ObjectArray<T>::GetWithoutChecks(int32_t i) const {
89 DCHECK(IsValidIndex(i));
90 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
91 return GetFieldObject<T*>(data_offset, false);
92}
93
94template<class T>
95inline void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos,
96 ObjectArray<T>* dst, int dst_pos,
97 size_t length) {
98 if (src->IsValidIndex(src_pos) &&
99 src->IsValidIndex(src_pos+length-1) &&
100 dst->IsValidIndex(dst_pos) &&
101 dst->IsValidIndex(dst_pos+length-1)) {
102 MemberOffset src_offset(DataOffset(sizeof(Object*)).Int32Value() + src_pos * sizeof(Object*));
103 MemberOffset dst_offset(DataOffset(sizeof(Object*)).Int32Value() + dst_pos * sizeof(Object*));
104 Class* array_class = dst->GetClass();
Ian Rogers1d54e732013-05-02 21:10:01 -0700105 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106 if (array_class == src->GetClass()) {
107 // No need for array store checks if arrays are of the same type
108 for (size_t i = 0; i < length; i++) {
109 Object* object = src->GetFieldObject<Object*>(src_offset, false);
110 heap->VerifyObject(object);
111 // directly set field, we do a bulk write barrier at the end
112 dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true);
113 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
114 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
115 }
116 } else {
117 Class* element_class = array_class->GetComponentType();
118 CHECK(!element_class->IsPrimitive());
119 for (size_t i = 0; i < length; i++) {
120 Object* object = src->GetFieldObject<Object*>(src_offset, false);
121 if (object != NULL && !object->InstanceOf(element_class)) {
122 dst->ThrowArrayStoreException(object);
123 return;
124 }
125 heap->VerifyObject(object);
126 // directly set field, we do a bulk write barrier at the end
127 dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true);
128 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
129 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
130 }
131 }
132 heap->WriteBarrierArray(dst, dst_pos, length);
133 }
134}
135
136template<class T>
137inline ObjectArray<T>* ObjectArray<T>::CopyOf(Thread* self, int32_t new_length) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700138 // We may get copied by a compacting GC.
139 SirtRef<ObjectArray<T> > sirt_this(self, this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140 ObjectArray<T>* new_array = Alloc(self, GetClass(), new_length);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700141 if (LIKELY(new_array != nullptr)) {
142 Copy(sirt_this.get(), 0, new_array, 0, std::min(sirt_this->GetLength(), new_length));
Ian Rogersa436fde2013-08-27 23:34:06 -0700143 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800144 return new_array;
145}
146
147} // namespace mirror
148} // namespace art
149
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700150#endif // ART_RUNTIME_MIRROR_OBJECT_ARRAY_INL_H_