object_registry.cc revision 277ccbd200ea43590dfc06a93ae184a765327ad0
164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes/* 264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * Copyright (C) 2013 The Android Open Source Project 364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * 464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * you may not use this file except in compliance with the License. 664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * You may obtain a copy of the License at 764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * 864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * 1064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * Unless required by applicable law or agreed to in writing, software 1164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 1264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * See the License for the specific language governing permissions and 1464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * limitations under the License. 1564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes */ 1664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 1764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes#include "object_registry.h" 1864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 19e2d628b5b0a1b9c29c173f3cbad3ef6cb6c24d2dSebastien Hertz#include "handle_scope-inl.h" 206a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers#include "jni_internal.h" 21e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "mirror/class.h" 2264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes#include "scoped_thread_state_change.h" 2364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 2464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesnamespace art { 2564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 2664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesstd::ostream& operator<<(std::ostream& os, const ObjectRegistryEntry& rhs) { 2764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes os << "ObjectRegistryEntry[" << rhs.jni_reference_type 2864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes << ",reference=" << rhs.jni_reference 2964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes << ",count=" << rhs.reference_count 3064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes << ",id=" << rhs.id << "]"; 3164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes return os; 3264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes} 3364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 340f827169742aad6209d830db773a101849c32a83Elliott HughesObjectRegistry::ObjectRegistry() 35b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi : lock_("ObjectRegistry lock", kJdwpObjectRegistryLock), next_id_(1) { 3664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes} 3764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 3864f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::RefTypeId ObjectRegistry::AddRefType(mirror::Class* c) { 3964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes return InternalAdd(c); 4064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes} 4164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 4264f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::ObjectId ObjectRegistry::Add(mirror::Object* o) { 4364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes return InternalAdd(o); 4464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes} 4564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 4664f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::ObjectId ObjectRegistry::InternalAdd(mirror::Object* o) { 47b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi if (o == nullptr) { 4864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes return 0; 4964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes } 5064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 51e2d628b5b0a1b9c29c173f3cbad3ef6cb6c24d2dSebastien Hertz Thread* const self = Thread::Current(); 52e2d628b5b0a1b9c29c173f3cbad3ef6cb6c24d2dSebastien Hertz StackHandleScope<1> hs(self); 53e2d628b5b0a1b9c29c173f3cbad3ef6cb6c24d2dSebastien Hertz Handle<mirror::Object> obj_h(hs.NewHandle(o)); 54e2d628b5b0a1b9c29c173f3cbad3ef6cb6c24d2dSebastien Hertz 55b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi // Call IdentityHashCode here to avoid a lock level violation between lock_ and monitor_lock. 56e2d628b5b0a1b9c29c173f3cbad3ef6cb6c24d2dSebastien Hertz int32_t identity_hash_code = obj_h->IdentityHashCode(); 57e2d628b5b0a1b9c29c173f3cbad3ef6cb6c24d2dSebastien Hertz 58e2d628b5b0a1b9c29c173f3cbad3ef6cb6c24d2dSebastien Hertz ScopedObjectAccessUnchecked soa(self); 5964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes MutexLock mu(soa.Self(), lock_); 60b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi ObjectRegistryEntry* entry = nullptr; 61e2d628b5b0a1b9c29c173f3cbad3ef6cb6c24d2dSebastien Hertz if (ContainsLocked(soa.Self(), obj_h.Get(), identity_hash_code, &entry)) { 62412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier // This object was already in our map. 63412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier ++entry->reference_count; 64412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier } else { 65412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier entry = new ObjectRegistryEntry; 66412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier entry->jni_reference_type = JNIWeakGlobalRefType; 67412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier entry->jni_reference = nullptr; 68412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier entry->reference_count = 0; 69412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier entry->id = 0; 70b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi entry->identity_hash_code = identity_hash_code; 71b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi object_to_entry_.insert(std::make_pair(identity_hash_code, entry)); 7264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 73412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier // This object isn't in the registry yet, so add it. 74412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier JNIEnv* env = soa.Env(); 7564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 76e2d628b5b0a1b9c29c173f3cbad3ef6cb6c24d2dSebastien Hertz jobject local_reference = soa.AddLocalReference<jobject>(obj_h.Get()); 7764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 78412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier entry->jni_reference_type = JNIWeakGlobalRefType; 79412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier entry->jni_reference = env->NewWeakGlobalRef(local_reference); 80412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier entry->reference_count = 1; 81412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier entry->id = next_id_++; 8264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 83412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier id_to_entry_.Put(entry->id, entry); 8464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 85412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier env->DeleteLocalRef(local_reference); 86412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier } 87412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier return entry->id; 8864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes} 8964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 90b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchibool ObjectRegistry::ContainsLocked(Thread* self, mirror::Object* o, int32_t identity_hash_code, 91b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi ObjectRegistryEntry** out_entry) { 92b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi DCHECK(o != nullptr); 93b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi for (auto it = object_to_entry_.lower_bound(identity_hash_code), end = object_to_entry_.end(); 94b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi it != end && it->first == identity_hash_code; ++it) { 95b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi ObjectRegistryEntry* entry = it->second; 96b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi if (o == self->DecodeJObject(entry->jni_reference)) { 97b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi if (out_entry != nullptr) { 98b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi *out_entry = entry; 99b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi } 100b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi return true; 101b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi } 102b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi } 103b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi return false; 10464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes} 10564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 10664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesvoid ObjectRegistry::Clear() { 10764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes Thread* self = Thread::Current(); 10864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes MutexLock mu(self, lock_); 10964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes VLOG(jdwp) << "Object registry contained " << object_to_entry_.size() << " entries"; 11064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes // Delete all the JNI references. 11164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes JNIEnv* env = self->GetJniEnv(); 112412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier for (const auto& pair : object_to_entry_) { 113a032870e19e4a6c8d62b7dab806261006fa711eeSebastien Hertz const ObjectRegistryEntry* entry = pair.second; 114a032870e19e4a6c8d62b7dab806261006fa711eeSebastien Hertz if (entry->jni_reference_type == JNIWeakGlobalRefType) { 115a032870e19e4a6c8d62b7dab806261006fa711eeSebastien Hertz env->DeleteWeakGlobalRef(entry->jni_reference); 11664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes } else { 117a032870e19e4a6c8d62b7dab806261006fa711eeSebastien Hertz env->DeleteGlobalRef(entry->jni_reference); 11864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes } 119a032870e19e4a6c8d62b7dab806261006fa711eeSebastien Hertz delete entry; 12064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes } 12164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes // Clear the maps. 12264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes object_to_entry_.clear(); 12364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes id_to_entry_.clear(); 12464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes} 12564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 126c0542af3e2170143ba40d89136e284997e16bf64Ian Rogersmirror::Object* ObjectRegistry::InternalGet(JDWP::ObjectId id, JDWP::JdwpError* error) { 12764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes Thread* self = Thread::Current(); 12864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes MutexLock mu(self, lock_); 129412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier auto it = id_to_entry_.find(id); 13064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes if (it == id_to_entry_.end()) { 131c0542af3e2170143ba40d89136e284997e16bf64Ian Rogers *error = JDWP::ERR_INVALID_OBJECT; 132c0542af3e2170143ba40d89136e284997e16bf64Ian Rogers return nullptr; 13364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes } 134412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier ObjectRegistryEntry& entry = *it->second; 135c0542af3e2170143ba40d89136e284997e16bf64Ian Rogers *error = JDWP::ERR_NONE; 13664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes return self->DecodeJObject(entry.jni_reference); 13764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes} 13864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 139449db33fafa29578df60e8a323f78d5eb6247e76Jeff Haojobject ObjectRegistry::GetJObject(JDWP::ObjectId id) { 1400630ab5239a7d7be24dedbc3f66c822332446fc3Sebastien Hertz if (id == 0) { 1410630ab5239a7d7be24dedbc3f66c822332446fc3Sebastien Hertz return NULL; 1420630ab5239a7d7be24dedbc3f66c822332446fc3Sebastien Hertz } 143449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao Thread* self = Thread::Current(); 144449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao MutexLock mu(self, lock_); 145412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier auto it = id_to_entry_.find(id); 146449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao CHECK(it != id_to_entry_.end()) << id; 147412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier ObjectRegistryEntry& entry = *it->second; 148449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao return entry.jni_reference; 149449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao} 150449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao 15164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesvoid ObjectRegistry::DisableCollection(JDWP::ObjectId id) { 15264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes Thread* self = Thread::Current(); 15364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes MutexLock mu(self, lock_); 154412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier auto it = id_to_entry_.find(id); 155e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz CHECK(it != id_to_entry_.end()); 156412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier Promote(*it->second); 15764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes} 15864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 15964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesvoid ObjectRegistry::EnableCollection(JDWP::ObjectId id) { 16064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes Thread* self = Thread::Current(); 16164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes MutexLock mu(self, lock_); 162412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier auto it = id_to_entry_.find(id); 163e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz CHECK(it != id_to_entry_.end()); 164412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier Demote(*it->second); 16564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes} 16664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 16764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesvoid ObjectRegistry::Demote(ObjectRegistryEntry& entry) { 16864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes if (entry.jni_reference_type == JNIGlobalRefType) { 16964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes Thread* self = Thread::Current(); 17064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes JNIEnv* env = self->GetJniEnv(); 17164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes jobject global = entry.jni_reference; 17264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes entry.jni_reference = env->NewWeakGlobalRef(entry.jni_reference); 17364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes entry.jni_reference_type = JNIWeakGlobalRefType; 17464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes env->DeleteGlobalRef(global); 17564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes } 17664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes} 17764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 17864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesvoid ObjectRegistry::Promote(ObjectRegistryEntry& entry) { 17964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes if (entry.jni_reference_type == JNIWeakGlobalRefType) { 18064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes Thread* self = Thread::Current(); 18164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes JNIEnv* env = self->GetJniEnv(); 18264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes jobject weak = entry.jni_reference; 18364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes entry.jni_reference = env->NewGlobalRef(entry.jni_reference); 18464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes entry.jni_reference_type = JNIGlobalRefType; 18564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes env->DeleteWeakGlobalRef(weak); 18664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes } 18764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes} 18864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 18964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesbool ObjectRegistry::IsCollected(JDWP::ObjectId id) { 19064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes Thread* self = Thread::Current(); 19164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes MutexLock mu(self, lock_); 192412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier auto it = id_to_entry_.find(id); 193e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz CHECK(it != id_to_entry_.end()); 194412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier ObjectRegistryEntry& entry = *it->second; 19564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes if (entry.jni_reference_type == JNIWeakGlobalRefType) { 19664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes JNIEnv* env = self->GetJniEnv(); 1977934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom return env->IsSameObject(entry.jni_reference, NULL); // Has the jweak been collected? 19864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes } else { 1997934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom return false; // We hold a strong reference, so we know this is live. 20064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes } 20164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes} 20264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 20364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesvoid ObjectRegistry::DisposeObject(JDWP::ObjectId id, uint32_t reference_count) { 20464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes Thread* self = Thread::Current(); 20564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes MutexLock mu(self, lock_); 206412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier auto it = id_to_entry_.find(id); 20764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes if (it == id_to_entry_.end()) { 20864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes return; 20964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes } 210412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier ObjectRegistryEntry* entry = it->second; 211412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier entry->reference_count -= reference_count; 212412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier if (entry->reference_count <= 0) { 21364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes JNIEnv* env = self->GetJniEnv(); 214b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi // Erase the object from the maps. Note object may be null if it's 215b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi // a weak ref and the GC has cleared it. 216b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi int32_t hash_code = entry->identity_hash_code; 217277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe for (auto inner_it = object_to_entry_.lower_bound(hash_code), end = object_to_entry_.end(); 218277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe inner_it != end && inner_it->first == hash_code; ++inner_it) { 219277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe if (entry == inner_it->second) { 220277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe object_to_entry_.erase(inner_it); 221b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi break; 222b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi } 223b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi } 224412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier if (entry->jni_reference_type == JNIWeakGlobalRefType) { 225412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier env->DeleteWeakGlobalRef(entry->jni_reference); 22664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes } else { 227412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier env->DeleteGlobalRef(entry->jni_reference); 22864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes } 22964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes id_to_entry_.erase(id); 230412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier delete entry; 23164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes } 23264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes} 23364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 23464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes} // namespace art 235