blob: 49bad4ccb8ea0a789104bd015909116c56e6947f [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
Mathieu Chartierad2541a2013-10-25 10:05:23 -070017#include <ctime>
18
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019#include "object.h"
20
Brian Carlstromea46f952013-07-30 01:26:50 -070021#include "art_field.h"
22#include "art_field-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "array-inl.h"
24#include "class.h"
25#include "class-inl.h"
Ian Rogers04d7aa92013-03-16 14:29:17 -070026#include "class_linker-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070027#include "gc/accounting/card_table-inl.h"
28#include "gc/heap.h"
Ian Rogers04d7aa92013-03-16 14:29:17 -070029#include "iftable-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "monitor.h"
31#include "object-inl.h"
Ian Rogers04d7aa92013-03-16 14:29:17 -070032#include "object_array-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "object_utils.h"
34#include "runtime.h"
35#include "sirt_ref.h"
36#include "throwable.h"
37#include "well_known_classes.h"
38
39namespace art {
40namespace mirror {
41
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042Object* Object::Clone(Thread* self) {
43 Class* c = GetClass();
44 DCHECK(!c->IsClassClass());
45
46 // Object::SizeOf gets the right size even if we're an array.
47 // Using c->AllocObject() here would be wrong.
48 size_t num_bytes = SizeOf();
Ian Rogers1d54e732013-05-02 21:10:01 -070049 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050 SirtRef<Object> copy(self, heap->AllocObject(self, c, num_bytes));
51 if (copy.get() == NULL) {
52 return NULL;
53 }
54
55 // Copy instance data. We assume memcpy copies by words.
56 // TODO: expose and use move32.
57 byte* src_bytes = reinterpret_cast<byte*>(this);
58 byte* dst_bytes = reinterpret_cast<byte*>(copy.get());
59 size_t offset = sizeof(Object);
60 memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset);
61
62 // Perform write barriers on copied object references.
63 if (c->IsArrayClass()) {
64 if (!c->GetComponentType()->IsPrimitive()) {
65 const ObjectArray<Object>* array = copy->AsObjectArray<Object>();
66 heap->WriteBarrierArray(copy.get(), 0, array->GetLength());
67 }
68 } else {
69 for (const Class* klass = c; klass != NULL; klass = klass->GetSuperClass()) {
70 size_t num_reference_fields = klass->NumReferenceInstanceFields();
71 for (size_t i = 0; i < num_reference_fields; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -070072 ArtField* field = klass->GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073 MemberOffset field_offset = field->GetOffset();
74 const Object* ref = copy->GetFieldObject<const Object*>(field_offset, false);
75 heap->WriteBarrierField(copy.get(), field_offset, ref);
76 }
77 }
78 }
79
80 if (c->IsFinalizable()) {
81 heap->AddFinalizerReference(Thread::Current(), copy.get());
82 }
83
84 return copy.get();
85}
86
Mathieu Chartierad2541a2013-10-25 10:05:23 -070087uint32_t Object::GenerateIdentityHashCode() {
88 static AtomicInteger seed(987654321 + std::time(nullptr));
89 uint32_t expected_value, new_value;
90 do {
91 expected_value = static_cast<uint32_t>(seed.load());
92 new_value = expected_value * 1103515245 + 12345;
93 } while (!seed.compare_and_swap(static_cast<int32_t>(expected_value),
94 static_cast<int32_t>(new_value)));
95 return expected_value & LockWord::kHashMask;
96}
97
98int32_t Object::IdentityHashCode() const {
99 while (true) {
100 LockWord lw = GetLockWord();
101 switch (lw.GetState()) {
102 case LockWord::kUnlocked: {
103 // Try to compare and swap in a new hash, if we succeed we will return the hash on the next
104 // loop iteration.
105 LockWord hash_word(LockWord::FromHashCode(GenerateIdentityHashCode()));
106 DCHECK_EQ(hash_word.GetState(), LockWord::kHashCode);
107 if (const_cast<Object*>(this)->CasLockWord(lw, hash_word)) {
108 return hash_word.GetHashCode();
109 }
110 break;
111 }
112 case LockWord::kThinLocked: {
113 // Inflate the thin lock to a monitor and stick the hash code inside of the monitor.
114 Thread* self = Thread::Current();
115 Monitor::InflateThinLocked(self, const_cast<Object*>(this), lw, GenerateIdentityHashCode());
116 break;
117 }
118 case LockWord::kFatLocked: {
119 // Already inflated, return the has stored in the monitor.
120 Monitor* monitor = lw.FatLockMonitor();
121 DCHECK(monitor != nullptr);
122 return monitor->GetHashCode();
123 }
124 case LockWord::kHashCode: {
125 return lw.GetHashCode();
126 }
127 }
128 }
129 LOG(FATAL) << "Unreachable";
130 return 0;
131}
132
Ian Rogers04d7aa92013-03-16 14:29:17 -0700133void Object::CheckFieldAssignmentImpl(MemberOffset field_offset, const Object* new_value) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800134 const Class* c = GetClass();
135 if (Runtime::Current()->GetClassLinker() == NULL ||
136 !Runtime::Current()->GetHeap()->IsObjectValidationEnabled() ||
137 !c->IsResolved()) {
138 return;
139 }
140 for (const Class* cur = c; cur != NULL; cur = cur->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700141 ObjectArray<ArtField>* fields = cur->GetIFields();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800142 if (fields != NULL) {
143 size_t num_ref_ifields = cur->NumReferenceInstanceFields();
144 for (size_t i = 0; i < num_ref_ifields; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700145 ArtField* field = fields->Get(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800146 if (field->GetOffset().Int32Value() == field_offset.Int32Value()) {
147 FieldHelper fh(field);
148 CHECK(fh.GetType()->IsAssignableFrom(new_value->GetClass()));
149 return;
150 }
151 }
152 }
153 }
154 if (c->IsArrayClass()) {
155 // Bounds and assign-ability done in the array setter.
156 return;
157 }
158 if (IsClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700159 ObjectArray<ArtField>* fields = AsClass()->GetSFields();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800160 if (fields != NULL) {
161 size_t num_ref_sfields = AsClass()->NumReferenceStaticFields();
162 for (size_t i = 0; i < num_ref_sfields; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700163 ArtField* field = fields->Get(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800164 if (field->GetOffset().Int32Value() == field_offset.Int32Value()) {
165 FieldHelper fh(field);
166 CHECK(fh.GetType()->IsAssignableFrom(new_value->GetClass()));
167 return;
168 }
169 }
170 }
171 }
172 LOG(FATAL) << "Failed to find field for assignment to " << reinterpret_cast<void*>(this)
173 << " of type " << PrettyDescriptor(c) << " at offset " << field_offset;
174}
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800175
176} // namespace mirror
177} // namespace art