Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 21 | namespace art { |
| 22 | |
| 23 | mirror::Object* const ObjectRegistry::kInvalidObject = reinterpret_cast<mirror::Object*>(1); |
| 24 | |
| 25 | std::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 Hughes | 0f82716 | 2013-02-26 12:12:58 -0800 | [diff] [blame] | 33 | ObjectRegistry::ObjectRegistry() |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 34 | : lock_("ObjectRegistry lock", kJdwpObjectRegistryLock), next_id_(1) { |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | JDWP::RefTypeId ObjectRegistry::AddRefType(mirror::Class* c) { |
| 38 | return InternalAdd(c); |
| 39 | } |
| 40 | |
| 41 | JDWP::ObjectId ObjectRegistry::Add(mirror::Object* o) { |
| 42 | return InternalAdd(o); |
| 43 | } |
| 44 | |
| 45 | JDWP::ObjectId ObjectRegistry::InternalAdd(mirror::Object* o) { |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 46 | if (o == nullptr) { |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 47 | return 0; |
| 48 | } |
| 49 | |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 50 | // Call IdentityHashCode here to avoid a lock level violation between lock_ and monitor_lock. |
| 51 | int32_t identity_hash_code = o->IdentityHashCode(); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 52 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 53 | MutexLock mu(soa.Self(), lock_); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 54 | ObjectRegistryEntry* entry = nullptr; |
| 55 | if (ContainsLocked(soa.Self(), o, identity_hash_code, &entry)) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 56 | // This object was already in our map. |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 57 | ++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 Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 64 | entry->identity_hash_code = identity_hash_code; |
| 65 | object_to_entry_.insert(std::make_pair(identity_hash_code, entry)); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 66 | |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 67 | // This object isn't in the registry yet, so add it. |
| 68 | JNIEnv* env = soa.Env(); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 69 | |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 70 | jobject local_reference = soa.AddLocalReference<jobject>(o); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 71 | |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 72 | entry->jni_reference_type = JNIWeakGlobalRefType; |
| 73 | entry->jni_reference = env->NewWeakGlobalRef(local_reference); |
| 74 | entry->reference_count = 1; |
| 75 | entry->id = next_id_++; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 76 | |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 77 | id_to_entry_.Put(entry->id, entry); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 78 | |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 79 | env->DeleteLocalRef(local_reference); |
| 80 | } |
| 81 | return entry->id; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 84 | bool 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 | |
| 95 | bool 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 Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void ObjectRegistry::Clear() { |
| 112 | Thread* self = Thread::Current(); |
| 113 | MutexLock mu(self, lock_); |
| 114 | VLOG(jdwp) << "Object registry contained " << object_to_entry_.size() << " entries"; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 115 | // Delete all the JNI references. |
| 116 | JNIEnv* env = self->GetJniEnv(); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 117 | for (const auto& pair : object_to_entry_) { |
| 118 | const ObjectRegistryEntry& entry = *pair.second; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 119 | if (entry.jni_reference_type == JNIWeakGlobalRefType) { |
| 120 | env->DeleteWeakGlobalRef(entry.jni_reference); |
| 121 | } else { |
| 122 | env->DeleteGlobalRef(entry.jni_reference); |
| 123 | } |
| 124 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 125 | // Clear the maps. |
| 126 | object_to_entry_.clear(); |
| 127 | id_to_entry_.clear(); |
| 128 | } |
| 129 | |
| 130 | mirror::Object* ObjectRegistry::InternalGet(JDWP::ObjectId id) { |
| 131 | Thread* self = Thread::Current(); |
| 132 | MutexLock mu(self, lock_); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 133 | auto it = id_to_entry_.find(id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 134 | if (it == id_to_entry_.end()) { |
| 135 | return kInvalidObject; |
| 136 | } |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 137 | ObjectRegistryEntry& entry = *it->second; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 138 | return self->DecodeJObject(entry.jni_reference); |
| 139 | } |
| 140 | |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 141 | jobject ObjectRegistry::GetJObject(JDWP::ObjectId id) { |
Sebastien Hertz | 0630ab5 | 2013-11-28 18:53:35 +0100 | [diff] [blame] | 142 | if (id == 0) { |
| 143 | return NULL; |
| 144 | } |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 145 | Thread* self = Thread::Current(); |
| 146 | MutexLock mu(self, lock_); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 147 | auto it = id_to_entry_.find(id); |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 148 | CHECK(it != id_to_entry_.end()) << id; |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 149 | ObjectRegistryEntry& entry = *it->second; |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 150 | return entry.jni_reference; |
| 151 | } |
| 152 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 153 | void ObjectRegistry::DisableCollection(JDWP::ObjectId id) { |
| 154 | Thread* self = Thread::Current(); |
| 155 | MutexLock mu(self, lock_); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 156 | auto it = id_to_entry_.find(id); |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 157 | CHECK(it != id_to_entry_.end()); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 158 | Promote(*it->second); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void ObjectRegistry::EnableCollection(JDWP::ObjectId id) { |
| 162 | Thread* self = Thread::Current(); |
| 163 | MutexLock mu(self, lock_); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 164 | auto it = id_to_entry_.find(id); |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 165 | CHECK(it != id_to_entry_.end()); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 166 | Demote(*it->second); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void ObjectRegistry::Demote(ObjectRegistryEntry& entry) { |
| 170 | if (entry.jni_reference_type == JNIGlobalRefType) { |
| 171 | Thread* self = Thread::Current(); |
| 172 | JNIEnv* env = self->GetJniEnv(); |
| 173 | jobject global = entry.jni_reference; |
| 174 | entry.jni_reference = env->NewWeakGlobalRef(entry.jni_reference); |
| 175 | entry.jni_reference_type = JNIWeakGlobalRefType; |
| 176 | env->DeleteGlobalRef(global); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void ObjectRegistry::Promote(ObjectRegistryEntry& entry) { |
| 181 | if (entry.jni_reference_type == JNIWeakGlobalRefType) { |
| 182 | Thread* self = Thread::Current(); |
| 183 | JNIEnv* env = self->GetJniEnv(); |
| 184 | jobject weak = entry.jni_reference; |
| 185 | entry.jni_reference = env->NewGlobalRef(entry.jni_reference); |
| 186 | entry.jni_reference_type = JNIGlobalRefType; |
| 187 | env->DeleteWeakGlobalRef(weak); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | bool ObjectRegistry::IsCollected(JDWP::ObjectId id) { |
| 192 | Thread* self = Thread::Current(); |
| 193 | MutexLock mu(self, lock_); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 194 | auto it = id_to_entry_.find(id); |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 195 | CHECK(it != id_to_entry_.end()); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 196 | ObjectRegistryEntry& entry = *it->second; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 197 | if (entry.jni_reference_type == JNIWeakGlobalRefType) { |
| 198 | JNIEnv* env = self->GetJniEnv(); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 199 | return env->IsSameObject(entry.jni_reference, NULL); // Has the jweak been collected? |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 200 | } else { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 201 | return false; // We hold a strong reference, so we know this is live. |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| 205 | void ObjectRegistry::DisposeObject(JDWP::ObjectId id, uint32_t reference_count) { |
| 206 | Thread* self = Thread::Current(); |
| 207 | MutexLock mu(self, lock_); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 208 | auto it = id_to_entry_.find(id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 209 | if (it == id_to_entry_.end()) { |
| 210 | return; |
| 211 | } |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 212 | ObjectRegistryEntry* entry = it->second; |
| 213 | entry->reference_count -= reference_count; |
| 214 | if (entry->reference_count <= 0) { |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 215 | JNIEnv* env = self->GetJniEnv(); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 216 | // Erase the object from the maps. Note object may be null if it's |
| 217 | // a weak ref and the GC has cleared it. |
| 218 | int32_t hash_code = entry->identity_hash_code; |
| 219 | for (auto it = object_to_entry_.lower_bound(hash_code), end = object_to_entry_.end(); |
| 220 | it != end && it->first == hash_code; ++it) { |
| 221 | if (entry == it->second) { |
| 222 | object_to_entry_.erase(it); |
| 223 | break; |
| 224 | } |
| 225 | } |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 226 | if (entry->jni_reference_type == JNIWeakGlobalRefType) { |
| 227 | env->DeleteWeakGlobalRef(entry->jni_reference); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 228 | } else { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 229 | env->DeleteGlobalRef(entry->jni_reference); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 230 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 231 | id_to_entry_.erase(id); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 232 | delete entry; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 236 | } // namespace art |