blob: ad18d8a2ceaa4dbe7bed1528eee5e481cadb3a8d [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
24mirror::Object* const ObjectRegistry::kInvalidObject = reinterpret_cast<mirror::Object*>(1);
25
26std::ostream& operator<<(std::ostream& os, const ObjectRegistryEntry& rhs) {
27 os << "ObjectRegistryEntry[" << rhs.jni_reference_type
28 << ",reference=" << rhs.jni_reference
29 << ",count=" << rhs.reference_count
30 << ",id=" << rhs.id << "]";
31 return os;
32}
33
Elliott Hughes0f827162013-02-26 12:12:58 -080034ObjectRegistry::ObjectRegistry()
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070035 : lock_("ObjectRegistry lock", kJdwpObjectRegistryLock), next_id_(1) {
Elliott Hughes64f574f2013-02-20 14:57:12 -080036}
37
38JDWP::RefTypeId ObjectRegistry::AddRefType(mirror::Class* c) {
39 return InternalAdd(c);
40}
41
42JDWP::ObjectId ObjectRegistry::Add(mirror::Object* o) {
43 return InternalAdd(o);
44}
45
46JDWP::ObjectId ObjectRegistry::InternalAdd(mirror::Object* o) {
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070047 if (o == nullptr) {
Elliott Hughes64f574f2013-02-20 14:57:12 -080048 return 0;
49 }
50
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070051 // Call IdentityHashCode here to avoid a lock level violation between lock_ and monitor_lock.
52 int32_t identity_hash_code = o->IdentityHashCode();
Elliott Hughes64f574f2013-02-20 14:57:12 -080053 ScopedObjectAccessUnchecked soa(Thread::Current());
54 MutexLock mu(soa.Self(), lock_);
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070055 ObjectRegistryEntry* entry = nullptr;
56 if (ContainsLocked(soa.Self(), o, identity_hash_code, &entry)) {
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080057 // This object was already in our map.
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080058 ++entry->reference_count;
59 } else {
60 entry = new ObjectRegistryEntry;
61 entry->jni_reference_type = JNIWeakGlobalRefType;
62 entry->jni_reference = nullptr;
63 entry->reference_count = 0;
64 entry->id = 0;
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070065 entry->identity_hash_code = identity_hash_code;
66 object_to_entry_.insert(std::make_pair(identity_hash_code, entry));
Elliott Hughes64f574f2013-02-20 14:57:12 -080067
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080068 // This object isn't in the registry yet, so add it.
69 JNIEnv* env = soa.Env();
Elliott Hughes64f574f2013-02-20 14:57:12 -080070
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080071 jobject local_reference = soa.AddLocalReference<jobject>(o);
Elliott Hughes64f574f2013-02-20 14:57:12 -080072
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080073 entry->jni_reference_type = JNIWeakGlobalRefType;
74 entry->jni_reference = env->NewWeakGlobalRef(local_reference);
75 entry->reference_count = 1;
76 entry->id = next_id_++;
Elliott Hughes64f574f2013-02-20 14:57:12 -080077
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080078 id_to_entry_.Put(entry->id, entry);
Elliott Hughes64f574f2013-02-20 14:57:12 -080079
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080080 env->DeleteLocalRef(local_reference);
81 }
82 return entry->id;
Elliott Hughes64f574f2013-02-20 14:57:12 -080083}
84
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070085bool ObjectRegistry::Contains(mirror::Object* o, ObjectRegistryEntry** out_entry) {
86 if (o == nullptr) {
87 return false;
88 }
89 // Call IdentityHashCode here to avoid a lock level violation between lock_ and monitor_lock.
90 int32_t identity_hash_code = o->IdentityHashCode();
91 Thread* self = Thread::Current();
92 MutexLock mu(self, lock_);
93 return ContainsLocked(self, o, identity_hash_code, out_entry);
94}
95
96bool ObjectRegistry::ContainsLocked(Thread* self, mirror::Object* o, int32_t identity_hash_code,
97 ObjectRegistryEntry** out_entry) {
98 DCHECK(o != nullptr);
99 for (auto it = object_to_entry_.lower_bound(identity_hash_code), end = object_to_entry_.end();
100 it != end && it->first == identity_hash_code; ++it) {
101 ObjectRegistryEntry* entry = it->second;
102 if (o == self->DecodeJObject(entry->jni_reference)) {
103 if (out_entry != nullptr) {
104 *out_entry = entry;
105 }
106 return true;
107 }
108 }
109 return false;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800110}
111
112void ObjectRegistry::Clear() {
113 Thread* self = Thread::Current();
114 MutexLock mu(self, lock_);
115 VLOG(jdwp) << "Object registry contained " << object_to_entry_.size() << " entries";
Elliott Hughes64f574f2013-02-20 14:57:12 -0800116 // Delete all the JNI references.
117 JNIEnv* env = self->GetJniEnv();
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800118 for (const auto& pair : object_to_entry_) {
Sebastien Hertza0328702014-06-25 22:06:12 +0200119 const ObjectRegistryEntry* entry = pair.second;
120 if (entry->jni_reference_type == JNIWeakGlobalRefType) {
121 env->DeleteWeakGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800122 } else {
Sebastien Hertza0328702014-06-25 22:06:12 +0200123 env->DeleteGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800124 }
Sebastien Hertza0328702014-06-25 22:06:12 +0200125 delete entry;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800126 }
Elliott Hughes64f574f2013-02-20 14:57:12 -0800127 // Clear the maps.
128 object_to_entry_.clear();
129 id_to_entry_.clear();
130}
131
132mirror::Object* ObjectRegistry::InternalGet(JDWP::ObjectId id) {
133 Thread* self = Thread::Current();
134 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800135 auto it = id_to_entry_.find(id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800136 if (it == id_to_entry_.end()) {
137 return kInvalidObject;
138 }
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800139 ObjectRegistryEntry& entry = *it->second;
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