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 | |
Sebastien Hertz | e2d628b | 2014-10-23 15:39:33 +0200 | [diff] [blame] | 19 | #include "handle_scope-inl.h" |
Vladimir Marko | 3e61dc0 | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 20 | #include "jni/jni_internal.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 21 | #include "mirror/class.h" |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 22 | #include "mirror/throwable.h" |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 23 | #include "obj_ptr-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 24 | #include "scoped_thread_state_change-inl.h" |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 28 | std::ostream& operator<<(std::ostream& os, const ObjectRegistryEntry& rhs) { |
| 29 | os << "ObjectRegistryEntry[" << rhs.jni_reference_type |
| 30 | << ",reference=" << rhs.jni_reference |
| 31 | << ",count=" << rhs.reference_count |
| 32 | << ",id=" << rhs.id << "]"; |
| 33 | return os; |
| 34 | } |
| 35 | |
Elliott Hughes | 0f82716 | 2013-02-26 12:12:58 -0800 | [diff] [blame] | 36 | ObjectRegistry::ObjectRegistry() |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 37 | : lock_("ObjectRegistry lock", kJdwpObjectRegistryLock), next_id_(1) { |
Hiroshi Yamauchi | 8a43324 | 2017-03-07 14:39:22 -0800 | [diff] [blame] | 38 | Locks::AddToExpectedMutexesOnWeakRefAccess(&lock_); |
| 39 | } |
| 40 | |
| 41 | ObjectRegistry::~ObjectRegistry() { |
| 42 | Locks::RemoveFromExpectedMutexesOnWeakRefAccess(&lock_); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 45 | JDWP::RefTypeId ObjectRegistry::AddRefType(ObjPtr<mirror::Class> c) { |
Sebastien Hertz | 261bc04 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 46 | return Add(c); |
| 47 | } |
| 48 | |
| 49 | JDWP::RefTypeId ObjectRegistry::AddRefType(Handle<mirror::Class> c_h) { |
| 50 | return Add(c_h); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 53 | JDWP::ObjectId ObjectRegistry::Add(ObjPtr<mirror::Object> o) { |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 54 | if (o == nullptr) { |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 55 | return 0; |
| 56 | } |
Sebastien Hertz | 261bc04 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 57 | Thread* const self = Thread::Current(); |
| 58 | StackHandleScope<1> hs(self); |
| 59 | return InternalAdd(hs.NewHandle(o)); |
| 60 | } |
| 61 | |
| 62 | // Template instantiations must be declared below. |
| 63 | template<class T> |
| 64 | JDWP::ObjectId ObjectRegistry::Add(Handle<T> obj_h) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 65 | if (obj_h == nullptr) { |
Sebastien Hertz | 261bc04 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 66 | return 0; |
| 67 | } |
| 68 | return InternalAdd(obj_h); |
| 69 | } |
| 70 | |
| 71 | // Explicit template instantiation. |
| 72 | template |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 73 | REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 74 | REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_) |
Sebastien Hertz | 261bc04 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 75 | JDWP::ObjectId ObjectRegistry::Add(Handle<mirror::Object> obj_h); |
| 76 | |
| 77 | template |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 78 | REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 79 | REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_) |
Sebastien Hertz | 261bc04 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 80 | JDWP::ObjectId ObjectRegistry::Add(Handle<mirror::Throwable> obj_h); |
| 81 | |
| 82 | template<class T> |
| 83 | JDWP::ObjectId ObjectRegistry::InternalAdd(Handle<T> obj_h) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 84 | CHECK(obj_h != nullptr); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 85 | |
Sebastien Hertz | e2d628b | 2014-10-23 15:39:33 +0200 | [diff] [blame] | 86 | Thread* const self = Thread::Current(); |
Sebastien Hertz | e4266c5 | 2014-10-29 12:06:51 +0100 | [diff] [blame] | 87 | self->AssertNoPendingException(); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 88 | // Object::IdentityHashCode may cause these locks to be held so check we do not already |
| 89 | // hold them. |
| 90 | Locks::thread_list_lock_->AssertNotHeld(self); |
| 91 | Locks::thread_suspend_count_lock_->AssertNotHeld(self); |
Sebastien Hertz | e4266c5 | 2014-10-29 12:06:51 +0100 | [diff] [blame] | 92 | |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 93 | // Call IdentityHashCode here to avoid a lock level violation between lock_ and monitor_lock. |
Sebastien Hertz | e2d628b | 2014-10-23 15:39:33 +0200 | [diff] [blame] | 94 | int32_t identity_hash_code = obj_h->IdentityHashCode(); |
| 95 | |
| 96 | ScopedObjectAccessUnchecked soa(self); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 97 | MutexLock mu(soa.Self(), lock_); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 98 | ObjectRegistryEntry* entry = nullptr; |
Sebastien Hertz | e2d628b | 2014-10-23 15:39:33 +0200 | [diff] [blame] | 99 | if (ContainsLocked(soa.Self(), obj_h.Get(), identity_hash_code, &entry)) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 100 | // This object was already in our map. |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 101 | ++entry->reference_count; |
| 102 | } else { |
| 103 | entry = new ObjectRegistryEntry; |
| 104 | entry->jni_reference_type = JNIWeakGlobalRefType; |
| 105 | entry->jni_reference = nullptr; |
| 106 | entry->reference_count = 0; |
| 107 | entry->id = 0; |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 108 | entry->identity_hash_code = identity_hash_code; |
| 109 | object_to_entry_.insert(std::make_pair(identity_hash_code, entry)); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 110 | |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 111 | // This object isn't in the registry yet, so add it. |
| 112 | JNIEnv* env = soa.Env(); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 113 | |
Sebastien Hertz | e2d628b | 2014-10-23 15:39:33 +0200 | [diff] [blame] | 114 | jobject local_reference = soa.AddLocalReference<jobject>(obj_h.Get()); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 115 | |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 116 | entry->jni_reference_type = JNIWeakGlobalRefType; |
| 117 | entry->jni_reference = env->NewWeakGlobalRef(local_reference); |
| 118 | entry->reference_count = 1; |
| 119 | entry->id = next_id_++; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 120 | |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 121 | id_to_entry_.Put(entry->id, entry); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 122 | |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 123 | env->DeleteLocalRef(local_reference); |
| 124 | } |
| 125 | return entry->id; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 128 | bool ObjectRegistry::ContainsLocked(Thread* self, |
| 129 | ObjPtr<mirror::Object> o, |
| 130 | int32_t identity_hash_code, |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 131 | ObjectRegistryEntry** out_entry) { |
| 132 | DCHECK(o != nullptr); |
| 133 | for (auto it = object_to_entry_.lower_bound(identity_hash_code), end = object_to_entry_.end(); |
| 134 | it != end && it->first == identity_hash_code; ++it) { |
| 135 | ObjectRegistryEntry* entry = it->second; |
| 136 | if (o == self->DecodeJObject(entry->jni_reference)) { |
| 137 | if (out_entry != nullptr) { |
| 138 | *out_entry = entry; |
| 139 | } |
| 140 | return true; |
| 141 | } |
| 142 | } |
| 143 | return false; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void ObjectRegistry::Clear() { |
Sebastien Hertz | 55f6534 | 2015-01-13 22:48:34 +0100 | [diff] [blame] | 147 | Thread* const self = Thread::Current(); |
| 148 | |
| 149 | // We must not hold the mutator lock exclusively if we want to delete weak global |
| 150 | // references. Otherwise this can lead to a deadlock with a running GC: |
| 151 | // 1. GC thread disables access to weak global references, then releases |
| 152 | // mutator lock. |
| 153 | // 2. JDWP thread takes mutator lock exclusively after suspending all |
| 154 | // threads. |
| 155 | // 3. GC thread waits for shared mutator lock which is held by JDWP |
| 156 | // thread. |
| 157 | // 4. JDWP thread clears weak global references but need to wait for GC |
| 158 | // thread to re-enable access to them. |
| 159 | Locks::mutator_lock_->AssertNotExclusiveHeld(self); |
| 160 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 161 | MutexLock mu(self, lock_); |
| 162 | VLOG(jdwp) << "Object registry contained " << object_to_entry_.size() << " entries"; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 163 | // Delete all the JNI references. |
| 164 | JNIEnv* env = self->GetJniEnv(); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 165 | for (const auto& pair : object_to_entry_) { |
Sebastien Hertz | a032870 | 2014-06-25 22:06:12 +0200 | [diff] [blame] | 166 | const ObjectRegistryEntry* entry = pair.second; |
| 167 | if (entry->jni_reference_type == JNIWeakGlobalRefType) { |
| 168 | env->DeleteWeakGlobalRef(entry->jni_reference); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 169 | } else { |
Sebastien Hertz | a032870 | 2014-06-25 22:06:12 +0200 | [diff] [blame] | 170 | env->DeleteGlobalRef(entry->jni_reference); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 171 | } |
Sebastien Hertz | a032870 | 2014-06-25 22:06:12 +0200 | [diff] [blame] | 172 | delete entry; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 173 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 174 | // Clear the maps. |
| 175 | object_to_entry_.clear(); |
| 176 | id_to_entry_.clear(); |
| 177 | } |
| 178 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 179 | mirror::Object* ObjectRegistry::InternalGet(JDWP::ObjectId id, JDWP::JdwpError* error) { |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 180 | Thread* self = Thread::Current(); |
| 181 | MutexLock mu(self, lock_); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 182 | auto it = id_to_entry_.find(id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 183 | if (it == id_to_entry_.end()) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 184 | *error = JDWP::ERR_INVALID_OBJECT; |
| 185 | return nullptr; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 186 | } |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 187 | ObjectRegistryEntry& entry = *it->second; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 188 | *error = JDWP::ERR_NONE; |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 189 | return self->DecodeJObject(entry.jni_reference).Ptr(); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 192 | jobject ObjectRegistry::GetJObject(JDWP::ObjectId id) { |
Sebastien Hertz | 0630ab5 | 2013-11-28 18:53:35 +0100 | [diff] [blame] | 193 | if (id == 0) { |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 194 | return nullptr; |
Sebastien Hertz | 0630ab5 | 2013-11-28 18:53:35 +0100 | [diff] [blame] | 195 | } |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 196 | Thread* self = Thread::Current(); |
| 197 | MutexLock mu(self, lock_); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 198 | auto it = id_to_entry_.find(id); |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 199 | CHECK(it != id_to_entry_.end()) << id; |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 200 | ObjectRegistryEntry& entry = *it->second; |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 201 | return entry.jni_reference; |
| 202 | } |
| 203 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 204 | void ObjectRegistry::DisableCollection(JDWP::ObjectId id) { |
| 205 | Thread* self = Thread::Current(); |
| 206 | MutexLock mu(self, lock_); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 207 | auto it = id_to_entry_.find(id); |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 208 | CHECK(it != id_to_entry_.end()); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 209 | Promote(*it->second); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void ObjectRegistry::EnableCollection(JDWP::ObjectId id) { |
| 213 | Thread* self = Thread::Current(); |
| 214 | MutexLock mu(self, lock_); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 215 | auto it = id_to_entry_.find(id); |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 216 | CHECK(it != id_to_entry_.end()); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 217 | Demote(*it->second); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | void ObjectRegistry::Demote(ObjectRegistryEntry& entry) { |
| 221 | if (entry.jni_reference_type == JNIGlobalRefType) { |
| 222 | Thread* self = Thread::Current(); |
| 223 | JNIEnv* env = self->GetJniEnv(); |
| 224 | jobject global = entry.jni_reference; |
| 225 | entry.jni_reference = env->NewWeakGlobalRef(entry.jni_reference); |
| 226 | entry.jni_reference_type = JNIWeakGlobalRefType; |
| 227 | env->DeleteGlobalRef(global); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | void ObjectRegistry::Promote(ObjectRegistryEntry& entry) { |
| 232 | if (entry.jni_reference_type == JNIWeakGlobalRefType) { |
| 233 | Thread* self = Thread::Current(); |
| 234 | JNIEnv* env = self->GetJniEnv(); |
| 235 | jobject weak = entry.jni_reference; |
| 236 | entry.jni_reference = env->NewGlobalRef(entry.jni_reference); |
| 237 | entry.jni_reference_type = JNIGlobalRefType; |
| 238 | env->DeleteWeakGlobalRef(weak); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | bool ObjectRegistry::IsCollected(JDWP::ObjectId id) { |
| 243 | Thread* self = Thread::Current(); |
| 244 | MutexLock mu(self, lock_); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 245 | auto it = id_to_entry_.find(id); |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 246 | CHECK(it != id_to_entry_.end()); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 247 | ObjectRegistryEntry& entry = *it->second; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 248 | if (entry.jni_reference_type == JNIWeakGlobalRefType) { |
| 249 | JNIEnv* env = self->GetJniEnv(); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 250 | return env->IsSameObject(entry.jni_reference, nullptr); // Has the jweak been collected? |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 251 | } else { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 252 | 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] | 253 | } |
| 254 | } |
| 255 | |
| 256 | void ObjectRegistry::DisposeObject(JDWP::ObjectId id, uint32_t reference_count) { |
| 257 | Thread* self = Thread::Current(); |
| 258 | MutexLock mu(self, lock_); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 259 | auto it = id_to_entry_.find(id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 260 | if (it == id_to_entry_.end()) { |
| 261 | return; |
| 262 | } |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 263 | ObjectRegistryEntry* entry = it->second; |
| 264 | entry->reference_count -= reference_count; |
| 265 | if (entry->reference_count <= 0) { |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 266 | JNIEnv* env = self->GetJniEnv(); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 267 | // Erase the object from the maps. Note object may be null if it's |
| 268 | // a weak ref and the GC has cleared it. |
| 269 | int32_t hash_code = entry->identity_hash_code; |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 270 | for (auto inner_it = object_to_entry_.lower_bound(hash_code), end = object_to_entry_.end(); |
| 271 | inner_it != end && inner_it->first == hash_code; ++inner_it) { |
| 272 | if (entry == inner_it->second) { |
| 273 | object_to_entry_.erase(inner_it); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 274 | break; |
| 275 | } |
| 276 | } |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 277 | if (entry->jni_reference_type == JNIWeakGlobalRefType) { |
| 278 | env->DeleteWeakGlobalRef(entry->jni_reference); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 279 | } else { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 280 | env->DeleteGlobalRef(entry->jni_reference); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 281 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 282 | id_to_entry_.erase(id); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 283 | delete entry; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 287 | } // namespace art |