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 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 38 | Object* Object::Clone(Thread* self) { |
| 39 | Class* c = GetClass(); |
| 40 | DCHECK(!c->IsClassClass()); |
| 41 | |
| 42 | // Object::SizeOf gets the right size even if we're an array. |
| 43 | // Using c->AllocObject() here would be wrong. |
| 44 | size_t num_bytes = SizeOf(); |
| 45 | Heap* heap = Runtime::Current()->GetHeap(); |
| 46 | SirtRef<Object> copy(self, heap->AllocObject(self, c, num_bytes)); |
| 47 | if (copy.get() == NULL) { |
| 48 | return NULL; |
| 49 | } |
| 50 | |
| 51 | // Copy instance data. We assume memcpy copies by words. |
| 52 | // TODO: expose and use move32. |
| 53 | byte* src_bytes = reinterpret_cast<byte*>(this); |
| 54 | byte* dst_bytes = reinterpret_cast<byte*>(copy.get()); |
| 55 | size_t offset = sizeof(Object); |
| 56 | memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset); |
| 57 | |
| 58 | // Perform write barriers on copied object references. |
| 59 | if (c->IsArrayClass()) { |
| 60 | if (!c->GetComponentType()->IsPrimitive()) { |
| 61 | const ObjectArray<Object>* array = copy->AsObjectArray<Object>(); |
| 62 | heap->WriteBarrierArray(copy.get(), 0, array->GetLength()); |
| 63 | } |
| 64 | } else { |
| 65 | for (const Class* klass = c; klass != NULL; klass = klass->GetSuperClass()) { |
| 66 | size_t num_reference_fields = klass->NumReferenceInstanceFields(); |
| 67 | for (size_t i = 0; i < num_reference_fields; ++i) { |
| 68 | Field* field = klass->GetInstanceField(i); |
| 69 | MemberOffset field_offset = field->GetOffset(); |
| 70 | const Object* ref = copy->GetFieldObject<const Object*>(field_offset, false); |
| 71 | heap->WriteBarrierField(copy.get(), field_offset, ref); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if (c->IsFinalizable()) { |
| 77 | heap->AddFinalizerReference(Thread::Current(), copy.get()); |
| 78 | } |
| 79 | |
| 80 | return copy.get(); |
| 81 | } |
| 82 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 83 | #if VERIFY_OBJECT_ENABLED |
| 84 | void Object::CheckFieldAssignment(MemberOffset field_offset, const Object* new_value) { |
| 85 | const Class* c = GetClass(); |
| 86 | if (Runtime::Current()->GetClassLinker() == NULL || |
| 87 | !Runtime::Current()->GetHeap()->IsObjectValidationEnabled() || |
| 88 | !c->IsResolved()) { |
| 89 | return; |
| 90 | } |
| 91 | for (const Class* cur = c; cur != NULL; cur = cur->GetSuperClass()) { |
| 92 | ObjectArray<Field>* fields = cur->GetIFields(); |
| 93 | if (fields != NULL) { |
| 94 | size_t num_ref_ifields = cur->NumReferenceInstanceFields(); |
| 95 | for (size_t i = 0; i < num_ref_ifields; ++i) { |
| 96 | Field* field = fields->Get(i); |
| 97 | if (field->GetOffset().Int32Value() == field_offset.Int32Value()) { |
| 98 | FieldHelper fh(field); |
| 99 | CHECK(fh.GetType()->IsAssignableFrom(new_value->GetClass())); |
| 100 | return; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | if (c->IsArrayClass()) { |
| 106 | // Bounds and assign-ability done in the array setter. |
| 107 | return; |
| 108 | } |
| 109 | if (IsClass()) { |
| 110 | ObjectArray<Field>* fields = AsClass()->GetSFields(); |
| 111 | if (fields != NULL) { |
| 112 | size_t num_ref_sfields = AsClass()->NumReferenceStaticFields(); |
| 113 | for (size_t i = 0; i < num_ref_sfields; ++i) { |
| 114 | Field* field = fields->Get(i); |
| 115 | if (field->GetOffset().Int32Value() == field_offset.Int32Value()) { |
| 116 | FieldHelper fh(field); |
| 117 | CHECK(fh.GetType()->IsAssignableFrom(new_value->GetClass())); |
| 118 | return; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | LOG(FATAL) << "Failed to find field for assignment to " << reinterpret_cast<void*>(this) |
| 124 | << " of type " << PrettyDescriptor(c) << " at offset " << field_offset; |
| 125 | } |
| 126 | #endif |
| 127 | |
| 128 | } // namespace mirror |
| 129 | } // namespace art |