blob: 35aaf0ab8f66f93a9965da5beb9ee69a93e982f6 [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
Ian Rogerse63db272014-07-15 15:36:11 -070019#include "mirror/class.h"
Elliott Hughes64f574f2013-02-20 14:57:12 -080020#include "scoped_thread_state_change.h"
21
22namespace art {
23
Elliott Hughes64f574f2013-02-20 14:57:12 -080024std::ostream& operator<<(std::ostream& os, const ObjectRegistryEntry& rhs) {
25 os << "ObjectRegistryEntry[" << rhs.jni_reference_type
26 << ",reference=" << rhs.jni_reference
27 << ",count=" << rhs.reference_count
28 << ",id=" << rhs.id << "]";
29 return os;
30}
31
Elliott Hughes0f827162013-02-26 12:12:58 -080032ObjectRegistry::ObjectRegistry()
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070033 : lock_("ObjectRegistry lock", kJdwpObjectRegistryLock), next_id_(1) {
Elliott Hughes64f574f2013-02-20 14:57:12 -080034}
35
36JDWP::RefTypeId ObjectRegistry::AddRefType(mirror::Class* c) {
37 return InternalAdd(c);
38}
39
40JDWP::ObjectId ObjectRegistry::Add(mirror::Object* o) {
41 return InternalAdd(o);
42}
43
44JDWP::ObjectId ObjectRegistry::InternalAdd(mirror::Object* o) {
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070045 if (o == nullptr) {
Elliott Hughes64f574f2013-02-20 14:57:12 -080046 return 0;
47 }
48
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070049 // Call IdentityHashCode here to avoid a lock level violation between lock_ and monitor_lock.
50 int32_t identity_hash_code = o->IdentityHashCode();
Elliott Hughes64f574f2013-02-20 14:57:12 -080051 ScopedObjectAccessUnchecked soa(Thread::Current());
52 MutexLock mu(soa.Self(), lock_);
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070053 ObjectRegistryEntry* entry = nullptr;
54 if (ContainsLocked(soa.Self(), o, identity_hash_code, &entry)) {
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080055 // This object was already in our map.
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080056 ++entry->reference_count;
57 } else {
58 entry = new ObjectRegistryEntry;
59 entry->jni_reference_type = JNIWeakGlobalRefType;
60 entry->jni_reference = nullptr;
61 entry->reference_count = 0;
62 entry->id = 0;
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070063 entry->identity_hash_code = identity_hash_code;
64 object_to_entry_.insert(std::make_pair(identity_hash_code, entry));
Elliott Hughes64f574f2013-02-20 14:57:12 -080065
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080066 // This object isn't in the registry yet, so add it.
67 JNIEnv* env = soa.Env();
Elliott Hughes64f574f2013-02-20 14:57:12 -080068
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080069 jobject local_reference = soa.AddLocalReference<jobject>(o);
Elliott Hughes64f574f2013-02-20 14:57:12 -080070
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080071 entry->jni_reference_type = JNIWeakGlobalRefType;
72 entry->jni_reference = env->NewWeakGlobalRef(local_reference);
73 entry->reference_count = 1;
74 entry->id = next_id_++;
Elliott Hughes64f574f2013-02-20 14:57:12 -080075
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080076 id_to_entry_.Put(entry->id, entry);
Elliott Hughes64f574f2013-02-20 14:57:12 -080077
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080078 env->DeleteLocalRef(local_reference);
79 }
80 return entry->id;
Elliott Hughes64f574f2013-02-20 14:57:12 -080081}
82
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070083bool ObjectRegistry::Contains(mirror::Object* o, ObjectRegistryEntry** out_entry) {
84 if (o == nullptr) {
85 return false;
86 }
87 // Call IdentityHashCode here to avoid a lock level violation between lock_ and monitor_lock.
88 int32_t identity_hash_code = o->IdentityHashCode();
89 Thread* self = Thread::Current();
90 MutexLock mu(self, lock_);
91 return ContainsLocked(self, o, identity_hash_code, out_entry);
92}
93
94bool ObjectRegistry::ContainsLocked(Thread* self, mirror::Object* o, int32_t identity_hash_code,
95 ObjectRegistryEntry** out_entry) {
96 DCHECK(o != nullptr);
97 for (auto it = object_to_entry_.lower_bound(identity_hash_code), end = object_to_entry_.end();
98 it != end && it->first == identity_hash_code; ++it) {
99 ObjectRegistryEntry* entry = it->second;
100 if (o == self->DecodeJObject(entry->jni_reference)) {
101 if (out_entry != nullptr) {
102 *out_entry = entry;
103 }
104 return true;
105 }
106 }
107 return false;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800108}
109
110void ObjectRegistry::Clear() {
111 Thread* self = Thread::Current();
112 MutexLock mu(self, lock_);
113 VLOG(jdwp) << "Object registry contained " << object_to_entry_.size() << " entries";
Elliott Hughes64f574f2013-02-20 14:57:12 -0800114 // Delete all the JNI references.
115 JNIEnv* env = self->GetJniEnv();
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800116 for (const auto& pair : object_to_entry_) {
Sebastien Hertza0328702014-06-25 22:06:12 +0200117 const ObjectRegistryEntry* entry = pair.second;
118 if (entry->jni_reference_type == JNIWeakGlobalRefType) {
119 env->DeleteWeakGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800120 } else {
Sebastien Hertza0328702014-06-25 22:06:12 +0200121 env->DeleteGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800122 }
Sebastien Hertza0328702014-06-25 22:06:12 +0200123 delete entry;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800124 }
Elliott Hughes64f574f2013-02-20 14:57:12 -0800125 // Clear the maps.
126 object_to_entry_.clear();
127 id_to_entry_.clear();
128}
129
Ian Rogersc0542af2014-09-03 16:16:56 -0700130mirror::Object* ObjectRegistry::InternalGet(JDWP::ObjectId id, JDWP::JdwpError* error) {
Elliott Hughes64f574f2013-02-20 14:57:12 -0800131 Thread* self = Thread::Current();
132 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800133 auto it = id_to_entry_.find(id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800134 if (it == id_to_entry_.end()) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700135 *error = JDWP::ERR_INVALID_OBJECT;
136 return nullptr;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800137 }
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800138 ObjectRegistryEntry& entry = *it->second;
Ian Rogersc0542af2014-09-03 16:16:56 -0700139 *error = JDWP::ERR_NONE;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800140 return self->DecodeJObject(entry.jni_reference);
141}
142
Jeff Hao449db332013-04-12 18:30:52 -0700143jobject ObjectRegistry::GetJObject(JDWP::ObjectId id) {
Sebastien Hertz0630ab52013-11-28 18:53:35 +0100144 if (id == 0) {
145 return NULL;
146 }
Jeff Hao449db332013-04-12 18:30:52 -0700147 Thread* self = Thread::Current();
148 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800149 auto it = id_to_entry_.find(id);
Jeff Hao449db332013-04-12 18:30:52 -0700150 CHECK(it != id_to_entry_.end()) << id;
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800151 ObjectRegistryEntry& entry = *it->second;
Jeff Hao449db332013-04-12 18:30:52 -0700152 return entry.jni_reference;
153}
154
Elliott Hughes64f574f2013-02-20 14:57:12 -0800155void ObjectRegistry::DisableCollection(JDWP::ObjectId id) {
156 Thread* self = Thread::Current();
157 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800158 auto it = id_to_entry_.find(id);
Sebastien Hertze96060a2013-12-11 12:06:28 +0100159 CHECK(it != id_to_entry_.end());
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800160 Promote(*it->second);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800161}
162
163void ObjectRegistry::EnableCollection(JDWP::ObjectId id) {
164 Thread* self = Thread::Current();
165 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800166 auto it = id_to_entry_.find(id);
Sebastien Hertze96060a2013-12-11 12:06:28 +0100167 CHECK(it != id_to_entry_.end());
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800168 Demote(*it->second);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800169}
170
171void ObjectRegistry::Demote(ObjectRegistryEntry& entry) {
172 if (entry.jni_reference_type == JNIGlobalRefType) {
173 Thread* self = Thread::Current();
174 JNIEnv* env = self->GetJniEnv();
175 jobject global = entry.jni_reference;
176 entry.jni_reference = env->NewWeakGlobalRef(entry.jni_reference);
177 entry.jni_reference_type = JNIWeakGlobalRefType;
178 env->DeleteGlobalRef(global);
179 }
180}
181
182void ObjectRegistry::Promote(ObjectRegistryEntry& entry) {
183 if (entry.jni_reference_type == JNIWeakGlobalRefType) {
184 Thread* self = Thread::Current();
185 JNIEnv* env = self->GetJniEnv();
186 jobject weak = entry.jni_reference;
187 entry.jni_reference = env->NewGlobalRef(entry.jni_reference);
188 entry.jni_reference_type = JNIGlobalRefType;
189 env->DeleteWeakGlobalRef(weak);
190 }
191}
192
193bool ObjectRegistry::IsCollected(JDWP::ObjectId id) {
194 Thread* self = Thread::Current();
195 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800196 auto it = id_to_entry_.find(id);
Sebastien Hertze96060a2013-12-11 12:06:28 +0100197 CHECK(it != id_to_entry_.end());
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800198 ObjectRegistryEntry& entry = *it->second;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800199 if (entry.jni_reference_type == JNIWeakGlobalRefType) {
200 JNIEnv* env = self->GetJniEnv();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700201 return env->IsSameObject(entry.jni_reference, NULL); // Has the jweak been collected?
Elliott Hughes64f574f2013-02-20 14:57:12 -0800202 } else {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700203 return false; // We hold a strong reference, so we know this is live.
Elliott Hughes64f574f2013-02-20 14:57:12 -0800204 }
205}
206
207void ObjectRegistry::DisposeObject(JDWP::ObjectId id, uint32_t reference_count) {
208 Thread* self = Thread::Current();
209 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800210 auto it = id_to_entry_.find(id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800211 if (it == id_to_entry_.end()) {
212 return;
213 }
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800214 ObjectRegistryEntry* entry = it->second;
215 entry->reference_count -= reference_count;
216 if (entry->reference_count <= 0) {
Elliott Hughes64f574f2013-02-20 14:57:12 -0800217 JNIEnv* env = self->GetJniEnv();
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700218 // Erase the object from the maps. Note object may be null if it's
219 // a weak ref and the GC has cleared it.
220 int32_t hash_code = entry->identity_hash_code;
221 for (auto it = object_to_entry_.lower_bound(hash_code), end = object_to_entry_.end();
222 it != end && it->first == hash_code; ++it) {
223 if (entry == it->second) {
224 object_to_entry_.erase(it);
225 break;
226 }
227 }
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800228 if (entry->jni_reference_type == JNIWeakGlobalRefType) {
229 env->DeleteWeakGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800230 } else {
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800231 env->DeleteGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800232 }
Elliott Hughes64f574f2013-02-20 14:57:12 -0800233 id_to_entry_.erase(id);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800234 delete entry;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800235 }
236}
237
Elliott Hughes64f574f2013-02-20 14:57:12 -0800238} // namespace art