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 | #include "object.h" |
| 18 | |
| 19 | #include "array-inl.h" |
| 20 | #include "class.h" |
| 21 | #include "class-inl.h" |
| 22 | #include "field.h" |
| 23 | #include "field-inl.h" |
| 24 | #include "gc/card_table-inl.h" |
| 25 | #include "heap.h" |
| 26 | #include "monitor.h" |
| 27 | #include "object-inl.h" |
| 28 | #include "object_array.h" |
| 29 | #include "object_utils.h" |
| 30 | #include "runtime.h" |
| 31 | #include "sirt_ref.h" |
| 32 | #include "throwable.h" |
| 33 | #include "well_known_classes.h" |
| 34 | |
| 35 | namespace art { |
| 36 | namespace mirror { |
| 37 | |
| 38 | Array* Object::AsArray() { |
| 39 | DCHECK(IsArrayInstance()); |
| 40 | return down_cast<Array*>(this); |
| 41 | } |
| 42 | |
| 43 | const Array* Object::AsArray() const { |
| 44 | DCHECK(IsArrayInstance()); |
| 45 | return down_cast<const Array*>(this); |
| 46 | } |
| 47 | |
| 48 | BooleanArray* Object::AsBooleanArray() { |
| 49 | DCHECK(GetClass()->IsArrayClass()); |
| 50 | DCHECK(GetClass()->GetComponentType()->IsPrimitiveBoolean()); |
| 51 | return down_cast<BooleanArray*>(this); |
| 52 | } |
| 53 | |
| 54 | ByteArray* Object::AsByteArray() { |
| 55 | DCHECK(GetClass()->IsArrayClass()); |
| 56 | DCHECK(GetClass()->GetComponentType()->IsPrimitiveByte()); |
| 57 | return down_cast<ByteArray*>(this); |
| 58 | } |
| 59 | |
| 60 | CharArray* Object::AsCharArray() { |
| 61 | DCHECK(GetClass()->IsArrayClass()); |
| 62 | DCHECK(GetClass()->GetComponentType()->IsPrimitiveChar()); |
| 63 | return down_cast<CharArray*>(this); |
| 64 | } |
| 65 | |
| 66 | ShortArray* Object::AsShortArray() { |
| 67 | DCHECK(GetClass()->IsArrayClass()); |
| 68 | DCHECK(GetClass()->GetComponentType()->IsPrimitiveShort()); |
| 69 | return down_cast<ShortArray*>(this); |
| 70 | } |
| 71 | |
| 72 | IntArray* Object::AsIntArray() { |
| 73 | DCHECK(GetClass()->IsArrayClass()); |
| 74 | DCHECK(GetClass()->GetComponentType()->IsPrimitiveInt() || |
| 75 | GetClass()->GetComponentType()->IsPrimitiveFloat()); |
| 76 | return down_cast<IntArray*>(this); |
| 77 | } |
| 78 | |
| 79 | LongArray* Object::AsLongArray() { |
| 80 | DCHECK(GetClass()->IsArrayClass()); |
| 81 | DCHECK(GetClass()->GetComponentType()->IsPrimitiveLong() || |
| 82 | GetClass()->GetComponentType()->IsPrimitiveDouble()); |
| 83 | return down_cast<LongArray*>(this); |
| 84 | } |
| 85 | |
| 86 | String* Object::AsString() { |
| 87 | DCHECK(GetClass()->IsStringClass()); |
| 88 | return down_cast<String*>(this); |
| 89 | } |
| 90 | |
| 91 | Throwable* Object::AsThrowable() { |
| 92 | DCHECK(GetClass()->IsThrowableClass()); |
| 93 | return down_cast<Throwable*>(this); |
| 94 | } |
| 95 | |
| 96 | Object* Object::Clone(Thread* self) { |
| 97 | Class* c = GetClass(); |
| 98 | DCHECK(!c->IsClassClass()); |
| 99 | |
| 100 | // Object::SizeOf gets the right size even if we're an array. |
| 101 | // Using c->AllocObject() here would be wrong. |
| 102 | size_t num_bytes = SizeOf(); |
| 103 | Heap* heap = Runtime::Current()->GetHeap(); |
| 104 | SirtRef<Object> copy(self, heap->AllocObject(self, c, num_bytes)); |
| 105 | if (copy.get() == NULL) { |
| 106 | return NULL; |
| 107 | } |
| 108 | |
| 109 | // Copy instance data. We assume memcpy copies by words. |
| 110 | // TODO: expose and use move32. |
| 111 | byte* src_bytes = reinterpret_cast<byte*>(this); |
| 112 | byte* dst_bytes = reinterpret_cast<byte*>(copy.get()); |
| 113 | size_t offset = sizeof(Object); |
| 114 | memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset); |
| 115 | |
| 116 | // Perform write barriers on copied object references. |
| 117 | if (c->IsArrayClass()) { |
| 118 | if (!c->GetComponentType()->IsPrimitive()) { |
| 119 | const ObjectArray<Object>* array = copy->AsObjectArray<Object>(); |
| 120 | heap->WriteBarrierArray(copy.get(), 0, array->GetLength()); |
| 121 | } |
| 122 | } else { |
| 123 | for (const Class* klass = c; klass != NULL; klass = klass->GetSuperClass()) { |
| 124 | size_t num_reference_fields = klass->NumReferenceInstanceFields(); |
| 125 | for (size_t i = 0; i < num_reference_fields; ++i) { |
| 126 | Field* field = klass->GetInstanceField(i); |
| 127 | MemberOffset field_offset = field->GetOffset(); |
| 128 | const Object* ref = copy->GetFieldObject<const Object*>(field_offset, false); |
| 129 | heap->WriteBarrierField(copy.get(), field_offset, ref); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if (c->IsFinalizable()) { |
| 135 | heap->AddFinalizerReference(Thread::Current(), copy.get()); |
| 136 | } |
| 137 | |
| 138 | return copy.get(); |
| 139 | } |
| 140 | |
| 141 | uint32_t Object::GetThinLockId() { |
| 142 | return Monitor::GetThinLockId(monitor_); |
| 143 | } |
| 144 | |
| 145 | void Object::MonitorEnter(Thread* thread) { |
| 146 | Monitor::MonitorEnter(thread, this); |
| 147 | } |
| 148 | |
| 149 | bool Object::MonitorExit(Thread* thread) { |
| 150 | return Monitor::MonitorExit(thread, this); |
| 151 | } |
| 152 | |
| 153 | void Object::Notify() { |
| 154 | Monitor::Notify(Thread::Current(), this); |
| 155 | } |
| 156 | |
| 157 | void Object::NotifyAll() { |
| 158 | Monitor::NotifyAll(Thread::Current(), this); |
| 159 | } |
| 160 | |
| 161 | void Object::Wait() { |
| 162 | Monitor::Wait(Thread::Current(), this, 0, 0, true, kWaiting); |
| 163 | } |
| 164 | |
| 165 | void Object::Wait(int64_t ms, int32_t ns) { |
| 166 | Monitor::Wait(Thread::Current(), this, ms, ns, true, kTimedWaiting); |
| 167 | } |
| 168 | |
| 169 | #if VERIFY_OBJECT_ENABLED |
| 170 | void Object::CheckFieldAssignment(MemberOffset field_offset, const Object* new_value) { |
| 171 | const Class* c = GetClass(); |
| 172 | if (Runtime::Current()->GetClassLinker() == NULL || |
| 173 | !Runtime::Current()->GetHeap()->IsObjectValidationEnabled() || |
| 174 | !c->IsResolved()) { |
| 175 | return; |
| 176 | } |
| 177 | for (const Class* cur = c; cur != NULL; cur = cur->GetSuperClass()) { |
| 178 | ObjectArray<Field>* fields = cur->GetIFields(); |
| 179 | if (fields != NULL) { |
| 180 | size_t num_ref_ifields = cur->NumReferenceInstanceFields(); |
| 181 | for (size_t i = 0; i < num_ref_ifields; ++i) { |
| 182 | Field* field = fields->Get(i); |
| 183 | if (field->GetOffset().Int32Value() == field_offset.Int32Value()) { |
| 184 | FieldHelper fh(field); |
| 185 | CHECK(fh.GetType()->IsAssignableFrom(new_value->GetClass())); |
| 186 | return; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | if (c->IsArrayClass()) { |
| 192 | // Bounds and assign-ability done in the array setter. |
| 193 | return; |
| 194 | } |
| 195 | if (IsClass()) { |
| 196 | ObjectArray<Field>* fields = AsClass()->GetSFields(); |
| 197 | if (fields != NULL) { |
| 198 | size_t num_ref_sfields = AsClass()->NumReferenceStaticFields(); |
| 199 | for (size_t i = 0; i < num_ref_sfields; ++i) { |
| 200 | Field* field = fields->Get(i); |
| 201 | if (field->GetOffset().Int32Value() == field_offset.Int32Value()) { |
| 202 | FieldHelper fh(field); |
| 203 | CHECK(fh.GetType()->IsAssignableFrom(new_value->GetClass())); |
| 204 | return; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | LOG(FATAL) << "Failed to find field for assignment to " << reinterpret_cast<void*>(this) |
| 210 | << " of type " << PrettyDescriptor(c) << " at offset " << field_offset; |
| 211 | } |
| 212 | #endif |
| 213 | |
| 214 | } // namespace mirror |
| 215 | } // namespace art |