blob: 29d3c8ab8ec1377f7c3f52a7df9562a5b11451a0 [file] [log] [blame]
Elliott Hughes64f574f2013-02-20 14:57:12 -08001/*
2 * Copyright (C) 2013 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_registry.h"
18
19#include "scoped_thread_state_change.h"
20
21namespace art {
22
23mirror::Object* const ObjectRegistry::kInvalidObject = reinterpret_cast<mirror::Object*>(1);
24
25std::ostream& operator<<(std::ostream& os, const ObjectRegistryEntry& rhs) {
26 os << "ObjectRegistryEntry[" << rhs.jni_reference_type
27 << ",reference=" << rhs.jni_reference
28 << ",count=" << rhs.reference_count
29 << ",id=" << rhs.id << "]";
30 return os;
31}
32
Elliott Hughes0f827162013-02-26 12:12:58 -080033ObjectRegistry::ObjectRegistry()
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070034 : lock_("ObjectRegistry lock", kJdwpObjectRegistryLock), next_id_(1) {
Elliott Hughes64f574f2013-02-20 14:57:12 -080035}
36
37JDWP::RefTypeId ObjectRegistry::AddRefType(mirror::Class* c) {
38 return InternalAdd(c);
39}
40
41JDWP::ObjectId ObjectRegistry::Add(mirror::Object* o) {
42 return InternalAdd(o);
43}
44
45JDWP::ObjectId ObjectRegistry::InternalAdd(mirror::Object* o) {
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070046 if (o == nullptr) {
Elliott Hughes64f574f2013-02-20 14:57:12 -080047 return 0;
48 }
49
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070050 // Call IdentityHashCode here to avoid a lock level violation between lock_ and monitor_lock.
51 int32_t identity_hash_code = o->IdentityHashCode();
Elliott Hughes64f574f2013-02-20 14:57:12 -080052 ScopedObjectAccessUnchecked soa(Thread::Current());
53 MutexLock mu(soa.Self(), lock_);
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070054 ObjectRegistryEntry* entry = nullptr;
55 if (ContainsLocked(soa.Self(), o, identity_hash_code, &entry)) {
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080056 // This object was already in our map.
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080057 ++entry->reference_count;
58 } else {
59 entry = new ObjectRegistryEntry;
60 entry->jni_reference_type = JNIWeakGlobalRefType;
61 entry->jni_reference = nullptr;
62 entry->reference_count = 0;
63 entry->id = 0;
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070064 entry->identity_hash_code = identity_hash_code;
65 object_to_entry_.insert(std::make_pair(identity_hash_code, entry));
Elliott Hughes64f574f2013-02-20 14:57:12 -080066
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080067 // This object isn't in the registry yet, so add it.
68 JNIEnv* env = soa.Env();
Elliott Hughes64f574f2013-02-20 14:57:12 -080069
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080070 jobject local_reference = soa.AddLocalReference<jobject>(o);
Elliott Hughes64f574f2013-02-20 14:57:12 -080071
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080072 entry->jni_reference_type = JNIWeakGlobalRefType;
73 entry->jni_reference = env->NewWeakGlobalRef(local_reference);
74 entry->reference_count = 1;
75 entry->id = next_id_++;
Elliott Hughes64f574f2013-02-20 14:57:12 -080076
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080077 id_to_entry_.Put(entry->id, entry);
Elliott Hughes64f574f2013-02-20 14:57:12 -080078
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080079 env->DeleteLocalRef(local_reference);
80 }
81 return entry->id;
Elliott Hughes64f574f2013-02-20 14:57:12 -080082}
83
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070084bool ObjectRegistry::Contains(mirror::Object* o, ObjectRegistryEntry** out_entry) {
85 if (o == nullptr) {
86 return false;
87 }
88 // Call IdentityHashCode here to avoid a lock level violation between lock_ and monitor_lock.
89 int32_t identity_hash_code = o->IdentityHashCode();
90 Thread* self = Thread::Current();
91 MutexLock mu(self, lock_);
92 return ContainsLocked(self, o, identity_hash_code, out_entry);
93}
94
95bool ObjectRegistry::ContainsLocked(Thread* self, mirror::Object* o, int32_t identity_hash_code,
96 ObjectRegistryEntry** out_entry) {
97 DCHECK(o != nullptr);
98 for (auto it = object_to_entry_.lower_bound(identity_hash_code), end = object_to_entry_.end();
99 it != end && it->first == identity_hash_code; ++it) {
100 ObjectRegistryEntry* entry = it->second;
101 if (o == self->DecodeJObject(entry->jni_reference)) {
102 if (out_entry != nullptr) {
103 *out_entry = entry;
104 }
105 return true;
106 }
107 }
108 return false;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800109}
110
111void ObjectRegistry::Clear() {
112 Thread* self = Thread::Current();
113 MutexLock mu(self, lock_);
114 VLOG(jdwp) << "Object registry contained " << object_to_entry_.size() << " entries";
Elliott Hughes64f574f2013-02-20 14:57:12 -0800115 // Delete all the JNI references.
116 JNIEnv* env = self->GetJniEnv();
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800117 for (const auto& pair : object_to_entry_) {
Sebastien Hertza0328702014-06-25 22:06:12 +0200118 const ObjectRegistryEntry* entry = pair.second;
119 if (entry->jni_reference_type == JNIWeakGlobalRefType) {
120 env->DeleteWeakGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800121 } else {
Sebastien Hertza0328702014-06-25 22:06:12 +0200122 env->DeleteGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800123 }
Sebastien Hertza0328702014-06-25 22:06:12 +0200124 delete entry;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800125 }
Elliott Hughes64f574f2013-02-20 14:57:12 -0800126 // Clear the maps.
127 object_to_entry_.clear();
128 id_to_entry_.clear();
129}
130
131mirror::Object* ObjectRegistry::InternalGet(JDWP::ObjectId id) {
132 Thread* self = Thread::Current();
133 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800134 auto it = id_to_entry_.find(id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800135 if (it == id_to_entry_.end()) {
136 return kInvalidObject;
137 }
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800138 ObjectRegistryEntry& entry = *it->second;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800139 return self->DecodeJObject(entry.jni_reference);
140}
141
Jeff Hao449db332013-04-12 18:30:52 -0700142jobject ObjectRegistry::GetJObject(JDWP::ObjectId id) {
Sebastien Hertz0630ab52013-11-28 18:53:35 +0100143 if (id == 0) {
144 return NULL;
145 }
Jeff Hao449db332013-04-12 18:30:52 -0700146 Thread* self = Thread::Current();
147 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800148 auto it = id_to_entry_.find(id);
Jeff Hao449db332013-04-12 18:30:52 -0700149 CHECK(it != id_to_entry_.end()) << id;
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800150 ObjectRegistryEntry& entry = *it->second;
Jeff Hao449db332013-04-12 18:30:52 -0700151 return entry.jni_reference;
152}
153
Elliott Hughes64f574f2013-02-20 14:57:12 -0800154void ObjectRegistry::DisableCollection(JDWP::ObjectId id) {
155 Thread* self = Thread::Current();
156 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800157 auto it = id_to_entry_.find(id);
Sebastien Hertze96060a2013-12-11 12:06:28 +0100158 CHECK(it != id_to_entry_.end());
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800159 Promote(*it->second);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800160}
161
162void ObjectRegistry::EnableCollection(JDWP::ObjectId id) {
163 Thread* self = Thread::Current();
164 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800165 auto it = id_to_entry_.find(id);
Sebastien Hertze96060a2013-12-11 12:06:28 +0100166 CHECK(it != id_to_entry_.end());
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800167 Demote(*it->second);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800168}
169
170void ObjectRegistry::Demote(ObjectRegistryEntry& entry) {
171 if (entry.jni_reference_type == JNIGlobalRefType) {
172 Thread* self = Thread::Current();
173 JNIEnv* env = self->GetJniEnv();
174 jobject global = entry.jni_reference;
175 entry.jni_reference = env->NewWeakGlobalRef(entry.jni_reference);
176 entry.jni_reference_type = JNIWeakGlobalRefType;
177 env->DeleteGlobalRef(global);
178 }
179}
180
181void ObjectRegistry::Promote(ObjectRegistryEntry& entry) {
182 if (entry.jni_reference_type == JNIWeakGlobalRefType) {
183 Thread* self = Thread::Current();
184 JNIEnv* env = self->GetJniEnv();
185 jobject weak = entry.jni_reference;
186 entry.jni_reference = env->NewGlobalRef(entry.jni_reference);
187 entry.jni_reference_type = JNIGlobalRefType;
188 env->DeleteWeakGlobalRef(weak);
189 }
190}
191
192bool ObjectRegistry::IsCollected(JDWP::ObjectId id) {
193 Thread* self = Thread::Current();
194 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800195 auto it = id_to_entry_.find(id);
Sebastien Hertze96060a2013-12-11 12:06:28 +0100196 CHECK(it != id_to_entry_.end());
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800197 ObjectRegistryEntry& entry = *it->second;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800198 if (entry.jni_reference_type == JNIWeakGlobalRefType) {
199 JNIEnv* env = self->GetJniEnv();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700200 return env->IsSameObject(entry.jni_reference, NULL); // Has the jweak been collected?
Elliott Hughes64f574f2013-02-20 14:57:12 -0800201 } else {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700202 return false; // We hold a strong reference, so we know this is live.
Elliott Hughes64f574f2013-02-20 14:57:12 -0800203 }
204}
205
206void ObjectRegistry::DisposeObject(JDWP::ObjectId id, uint32_t reference_count) {
207 Thread* self = Thread::Current();
208 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800209 auto it = id_to_entry_.find(id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800210 if (it == id_to_entry_.end()) {
211 return;
212 }
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800213 ObjectRegistryEntry* entry = it->second;
214 entry->reference_count -= reference_count;
215 if (entry->reference_count <= 0) {
Elliott Hughes64f574f2013-02-20 14:57:12 -0800216 JNIEnv* env = self->GetJniEnv();
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700217 // Erase the object from the maps. Note object may be null if it's
218 // a weak ref and the GC has cleared it.
219 int32_t hash_code = entry->identity_hash_code;
220 for (auto it = object_to_entry_.lower_bound(hash_code), end = object_to_entry_.end();
221 it != end && it->first == hash_code; ++it) {
222 if (entry == it->second) {
223 object_to_entry_.erase(it);
224 break;
225 }
226 }
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800227 if (entry->jni_reference_type == JNIWeakGlobalRefType) {
228 env->DeleteWeakGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800229 } else {
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800230 env->DeleteGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800231 }
Elliott Hughes64f574f2013-02-20 14:57:12 -0800232 id_to_entry_.erase(id);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800233 delete entry;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800234 }
235}
236
Elliott Hughes64f574f2013-02-20 14:57:12 -0800237} // namespace art