blob: bdb3250d57ded16342a4916576f55d3db11c2677 [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
Mathieu Chartier590fee92013-09-13 13:46:47 -070042static Object* CopyObject(Thread* self, mirror::Object* dest, mirror::Object* src, size_t num_bytes)
43 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044 // Copy instance data. We assume memcpy copies by words.
45 // TODO: expose and use move32.
Mathieu Chartier590fee92013-09-13 13:46:47 -070046 byte* src_bytes = reinterpret_cast<byte*>(src);
47 byte* dst_bytes = reinterpret_cast<byte*>(dest);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048 size_t offset = sizeof(Object);
49 memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset);
Mathieu Chartier590fee92013-09-13 13:46:47 -070050 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051 // Perform write barriers on copied object references.
Mathieu Chartier590fee92013-09-13 13:46:47 -070052 Class* c = src->GetClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053 if (c->IsArrayClass()) {
54 if (!c->GetComponentType()->IsPrimitive()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -070055 const ObjectArray<Object>* array = dest->AsObjectArray<Object>();
56 heap->WriteBarrierArray(dest, 0, array->GetLength());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057 }
58 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -070059 heap->WriteBarrierEveryFieldOf(dest);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061 if (c->IsFinalizable()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -070062 SirtRef<Object> sirt_dest(self, dest);
63 heap->AddFinalizerReference(self, dest);
64 return sirt_dest.get();
65 }
66 return dest;
67}
68
69Object* Object::Clone(Thread* self) {
70 CHECK(!IsClass()) << "Can't clone classes.";
71 // Object::SizeOf gets the right size even if we're an array. Using c->AllocObject() here would
72 // be wrong.
73 gc::Heap* heap = Runtime::Current()->GetHeap();
74 size_t num_bytes = SizeOf();
75 SirtRef<Object> this_object(self, this);
76 Object* copy;
77 if (heap->IsMovableObject(this)) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080078 copy = heap->AllocObject<true>(self, GetClass(), num_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -070079 } else {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080080 copy = heap->AllocNonMovableObject<true>(self, GetClass(), num_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -070081 }
82 if (LIKELY(copy != nullptr)) {
83 return CopyObject(self, copy, this_object.get(), num_bytes);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084 }
Mathieu Chartier0732d592013-11-06 11:02:50 -080085 return copy;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086}
87
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -070088int32_t Object::GenerateIdentityHashCode() {
Mathieu Chartierad2541a2013-10-25 10:05:23 -070089 static AtomicInteger seed(987654321 + std::time(nullptr));
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -070090 int32_t expected_value, new_value;
Mathieu Chartierad2541a2013-10-25 10:05:23 -070091 do {
Ian Rogersb122a4b2013-11-19 18:00:50 -080092 expected_value = static_cast<uint32_t>(seed.Load());
Mathieu Chartierad2541a2013-10-25 10:05:23 -070093 new_value = expected_value * 1103515245 + 12345;
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -070094 } while ((expected_value & LockWord::kHashMask) == 0 ||
Ian Rogersb122a4b2013-11-19 18:00:50 -080095 !seed.CompareAndSwap(expected_value, new_value));
Mathieu Chartierad2541a2013-10-25 10:05:23 -070096 return expected_value & LockWord::kHashMask;
97}
98
99int32_t Object::IdentityHashCode() const {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700100 mirror::Object* current_this = const_cast<mirror::Object*>(this);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700101 while (true) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700102 LockWord lw = current_this->GetLockWord();
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700103 switch (lw.GetState()) {
104 case LockWord::kUnlocked: {
105 // Try to compare and swap in a new hash, if we succeed we will return the hash on the next
106 // loop iteration.
107 LockWord hash_word(LockWord::FromHashCode(GenerateIdentityHashCode()));
108 DCHECK_EQ(hash_word.GetState(), LockWord::kHashCode);
109 if (const_cast<Object*>(this)->CasLockWord(lw, hash_word)) {
110 return hash_word.GetHashCode();
111 }
112 break;
113 }
114 case LockWord::kThinLocked: {
115 // Inflate the thin lock to a monitor and stick the hash code inside of the monitor.
116 Thread* self = Thread::Current();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700117 SirtRef<mirror::Object> sirt_this(self, current_this);
118 Monitor::InflateThinLocked(self, sirt_this, lw, GenerateIdentityHashCode());
119 // A GC may have occurred when we switched to kBlocked.
120 current_this = sirt_this.get();
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700121 break;
122 }
123 case LockWord::kFatLocked: {
124 // Already inflated, return the has stored in the monitor.
125 Monitor* monitor = lw.FatLockMonitor();
126 DCHECK(monitor != nullptr);
127 return monitor->GetHashCode();
128 }
129 case LockWord::kHashCode: {
130 return lw.GetHashCode();
131 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700132 default: {
133 LOG(FATAL) << "Invalid state during hashcode " << lw.GetState();
134 break;
135 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700136 }
137 }
138 LOG(FATAL) << "Unreachable";
139 return 0;
140}
141
Ian Rogers04d7aa92013-03-16 14:29:17 -0700142void Object::CheckFieldAssignmentImpl(MemberOffset field_offset, const Object* new_value) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800143 const Class* c = GetClass();
144 if (Runtime::Current()->GetClassLinker() == NULL ||
145 !Runtime::Current()->GetHeap()->IsObjectValidationEnabled() ||
146 !c->IsResolved()) {
147 return;
148 }
149 for (const Class* cur = c; cur != NULL; cur = cur->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700150 ObjectArray<ArtField>* fields = cur->GetIFields();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800151 if (fields != NULL) {
152 size_t num_ref_ifields = cur->NumReferenceInstanceFields();
153 for (size_t i = 0; i < num_ref_ifields; ++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 if (c->IsArrayClass()) {
164 // Bounds and assign-ability done in the array setter.
165 return;
166 }
167 if (IsClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700168 ObjectArray<ArtField>* fields = AsClass()->GetSFields();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 if (fields != NULL) {
170 size_t num_ref_sfields = AsClass()->NumReferenceStaticFields();
171 for (size_t i = 0; i < num_ref_sfields; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700172 ArtField* field = fields->Get(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173 if (field->GetOffset().Int32Value() == field_offset.Int32Value()) {
174 FieldHelper fh(field);
175 CHECK(fh.GetType()->IsAssignableFrom(new_value->GetClass()));
176 return;
177 }
178 }
179 }
180 }
181 LOG(FATAL) << "Failed to find field for assignment to " << reinterpret_cast<void*>(this)
182 << " of type " << PrettyDescriptor(c) << " at offset " << field_offset;
183}
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800184
185} // namespace mirror
186} // namespace art