blob: 87d02c9469bbc11ff9da92e19bcf16c481429cb4 [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) {
Mathieu Chartier0732d592013-11-06 11:02:50 -080043 mirror::Class* c = GetClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044 DCHECK(!c->IsClassClass());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045 // Object::SizeOf gets the right size even if we're an array.
46 // Using c->AllocObject() here would be wrong.
47 size_t num_bytes = SizeOf();
Ian Rogers1d54e732013-05-02 21:10:01 -070048 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier0732d592013-11-06 11:02:50 -080049 SirtRef<mirror::Object> sirt_this(self, this);
50 Object* copy = heap->AllocObject(self, c, num_bytes);
51 if (UNLIKELY(copy == nullptr)) {
52 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054 // Copy instance data. We assume memcpy copies by words.
55 // TODO: expose and use move32.
Mathieu Chartier0732d592013-11-06 11:02:50 -080056 byte* src_bytes = reinterpret_cast<byte*>(sirt_this.get());
57 byte* dst_bytes = reinterpret_cast<byte*>(copy);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058 size_t offset = sizeof(Object);
59 memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060 // Perform write barriers on copied object references.
Mathieu Chartier0732d592013-11-06 11:02:50 -080061 c = copy->GetClass(); // Re-read Class in case it moved.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080062 if (c->IsArrayClass()) {
63 if (!c->GetComponentType()->IsPrimitive()) {
64 const ObjectArray<Object>* array = copy->AsObjectArray<Object>();
Mathieu Chartier0732d592013-11-06 11:02:50 -080065 heap->WriteBarrierArray(copy, 0, array->GetLength());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066 }
67 } else {
Mathieu Chartier0732d592013-11-06 11:02:50 -080068 heap->WriteBarrierEveryFieldOf(copy);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080070 if (c->IsFinalizable()) {
Mathieu Chartier0732d592013-11-06 11:02:50 -080071 SirtRef<mirror::Object> sirt_copy(self, copy);
72 heap->AddFinalizerReference(self, copy);
73 return sirt_copy.get();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074 }
Mathieu Chartier0732d592013-11-06 11:02:50 -080075 return copy;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076}
77
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -070078int32_t Object::GenerateIdentityHashCode() {
Mathieu Chartierad2541a2013-10-25 10:05:23 -070079 static AtomicInteger seed(987654321 + std::time(nullptr));
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -070080 int32_t expected_value, new_value;
Mathieu Chartierad2541a2013-10-25 10:05:23 -070081 do {
82 expected_value = static_cast<uint32_t>(seed.load());
83 new_value = expected_value * 1103515245 + 12345;
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -070084 } while ((expected_value & LockWord::kHashMask) == 0 ||
85 !seed.compare_and_swap(expected_value, new_value));
Mathieu Chartierad2541a2013-10-25 10:05:23 -070086 return expected_value & LockWord::kHashMask;
87}
88
89int32_t Object::IdentityHashCode() const {
90 while (true) {
91 LockWord lw = GetLockWord();
92 switch (lw.GetState()) {
93 case LockWord::kUnlocked: {
94 // Try to compare and swap in a new hash, if we succeed we will return the hash on the next
95 // loop iteration.
96 LockWord hash_word(LockWord::FromHashCode(GenerateIdentityHashCode()));
97 DCHECK_EQ(hash_word.GetState(), LockWord::kHashCode);
98 if (const_cast<Object*>(this)->CasLockWord(lw, hash_word)) {
99 return hash_word.GetHashCode();
100 }
101 break;
102 }
103 case LockWord::kThinLocked: {
104 // Inflate the thin lock to a monitor and stick the hash code inside of the monitor.
105 Thread* self = Thread::Current();
106 Monitor::InflateThinLocked(self, const_cast<Object*>(this), lw, GenerateIdentityHashCode());
107 break;
108 }
109 case LockWord::kFatLocked: {
110 // Already inflated, return the has stored in the monitor.
111 Monitor* monitor = lw.FatLockMonitor();
112 DCHECK(monitor != nullptr);
113 return monitor->GetHashCode();
114 }
115 case LockWord::kHashCode: {
116 return lw.GetHashCode();
117 }
118 }
119 }
120 LOG(FATAL) << "Unreachable";
121 return 0;
122}
123
Ian Rogers04d7aa92013-03-16 14:29:17 -0700124void Object::CheckFieldAssignmentImpl(MemberOffset field_offset, const Object* new_value) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800125 const Class* c = GetClass();
126 if (Runtime::Current()->GetClassLinker() == NULL ||
127 !Runtime::Current()->GetHeap()->IsObjectValidationEnabled() ||
128 !c->IsResolved()) {
129 return;
130 }
131 for (const Class* cur = c; cur != NULL; cur = cur->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700132 ObjectArray<ArtField>* fields = cur->GetIFields();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800133 if (fields != NULL) {
134 size_t num_ref_ifields = cur->NumReferenceInstanceFields();
135 for (size_t i = 0; i < num_ref_ifields; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700136 ArtField* field = fields->Get(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800137 if (field->GetOffset().Int32Value() == field_offset.Int32Value()) {
138 FieldHelper fh(field);
139 CHECK(fh.GetType()->IsAssignableFrom(new_value->GetClass()));
140 return;
141 }
142 }
143 }
144 }
145 if (c->IsArrayClass()) {
146 // Bounds and assign-ability done in the array setter.
147 return;
148 }
149 if (IsClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700150 ObjectArray<ArtField>* fields = AsClass()->GetSFields();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800151 if (fields != NULL) {
152 size_t num_ref_sfields = AsClass()->NumReferenceStaticFields();
153 for (size_t i = 0; i < num_ref_sfields; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700154 ArtField* field = fields->Get(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155 if (field->GetOffset().Int32Value() == field_offset.Int32Value()) {
156 FieldHelper fh(field);
157 CHECK(fh.GetType()->IsAssignableFrom(new_value->GetClass()));
158 return;
159 }
160 }
161 }
162 }
163 LOG(FATAL) << "Failed to find field for assignment to " << reinterpret_cast<void*>(this)
164 << " of type " << PrettyDescriptor(c) << " at offset " << field_offset;
165}
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800166
167} // namespace mirror
168} // namespace art