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() |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame^] | 34 | : lock_("ObjectRegistry lock", kJdwpObjectRegistryLock), allow_new_objects_(true), |
| 35 | condition_("object registry condition", lock_), next_id_(1) { |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | JDWP::RefTypeId ObjectRegistry::AddRefType(mirror::Class* c) { |
| 39 | return InternalAdd(c); |
| 40 | } |
| 41 | |
| 42 | JDWP::ObjectId ObjectRegistry::Add(mirror::Object* o) { |
| 43 | return InternalAdd(o); |
| 44 | } |
| 45 | |
| 46 | JDWP::ObjectId ObjectRegistry::InternalAdd(mirror::Object* o) { |
| 47 | if (o == NULL) { |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 52 | MutexLock mu(soa.Self(), lock_); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame^] | 53 | while (UNLIKELY(!allow_new_objects_)) { |
| 54 | condition_.WaitHoldingLocks(soa.Self()); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 55 | } |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame^] | 56 | ObjectRegistryEntry* entry; |
| 57 | auto it = object_to_entry_.find(o); |
| 58 | if (it != object_to_entry_.end()) { |
| 59 | // This object was already in our map. |
| 60 | entry = it->second; |
| 61 | ++entry->reference_count; |
| 62 | } else { |
| 63 | entry = new ObjectRegistryEntry; |
| 64 | entry->jni_reference_type = JNIWeakGlobalRefType; |
| 65 | entry->jni_reference = nullptr; |
| 66 | entry->reference_count = 0; |
| 67 | entry->id = 0; |
| 68 | object_to_entry_.insert(std::make_pair(o, entry)); |
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 | // This object isn't in the registry yet, so add it. |
| 71 | JNIEnv* env = soa.Env(); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 72 | |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame^] | 73 | jobject local_reference = soa.AddLocalReference<jobject>(o); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 74 | |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame^] | 75 | entry->jni_reference_type = JNIWeakGlobalRefType; |
| 76 | entry->jni_reference = env->NewWeakGlobalRef(local_reference); |
| 77 | entry->reference_count = 1; |
| 78 | entry->id = next_id_++; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 79 | |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame^] | 80 | id_to_entry_.Put(entry->id, entry); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 81 | |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame^] | 82 | env->DeleteLocalRef(local_reference); |
| 83 | } |
| 84 | return entry->id; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | bool ObjectRegistry::Contains(mirror::Object* o) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame^] | 88 | MutexLock mu(Thread::Current(), lock_); |
| 89 | return object_to_entry_.find(o) != object_to_entry_.end(); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void ObjectRegistry::Clear() { |
| 93 | Thread* self = Thread::Current(); |
| 94 | MutexLock mu(self, lock_); |
| 95 | VLOG(jdwp) << "Object registry contained " << object_to_entry_.size() << " entries"; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 96 | // Delete all the JNI references. |
| 97 | JNIEnv* env = self->GetJniEnv(); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame^] | 98 | for (const auto& pair : object_to_entry_) { |
| 99 | const ObjectRegistryEntry& entry = *pair.second; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 100 | if (entry.jni_reference_type == JNIWeakGlobalRefType) { |
| 101 | env->DeleteWeakGlobalRef(entry.jni_reference); |
| 102 | } else { |
| 103 | env->DeleteGlobalRef(entry.jni_reference); |
| 104 | } |
| 105 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 106 | // Clear the maps. |
| 107 | object_to_entry_.clear(); |
| 108 | id_to_entry_.clear(); |
| 109 | } |
| 110 | |
| 111 | mirror::Object* ObjectRegistry::InternalGet(JDWP::ObjectId id) { |
| 112 | Thread* self = Thread::Current(); |
| 113 | MutexLock mu(self, lock_); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame^] | 114 | auto it = id_to_entry_.find(id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 115 | if (it == id_to_entry_.end()) { |
| 116 | return kInvalidObject; |
| 117 | } |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame^] | 118 | ObjectRegistryEntry& entry = *it->second; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 119 | return self->DecodeJObject(entry.jni_reference); |
| 120 | } |
| 121 | |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 122 | jobject ObjectRegistry::GetJObject(JDWP::ObjectId id) { |
Sebastien Hertz | 0630ab5 | 2013-11-28 18:53:35 +0100 | [diff] [blame] | 123 | if (id == 0) { |
| 124 | return NULL; |
| 125 | } |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 126 | Thread* self = Thread::Current(); |
| 127 | MutexLock mu(self, lock_); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame^] | 128 | auto it = id_to_entry_.find(id); |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 129 | CHECK(it != id_to_entry_.end()) << id; |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame^] | 130 | ObjectRegistryEntry& entry = *it->second; |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 131 | return entry.jni_reference; |
| 132 | } |
| 133 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 134 | void ObjectRegistry::DisableCollection(JDWP::ObjectId id) { |
| 135 | Thread* self = Thread::Current(); |
| 136 | MutexLock mu(self, lock_); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame^] | 137 | auto it = id_to_entry_.find(id); |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 138 | CHECK(it != id_to_entry_.end()); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame^] | 139 | Promote(*it->second); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void ObjectRegistry::EnableCollection(JDWP::ObjectId id) { |
| 143 | Thread* self = Thread::Current(); |
| 144 | MutexLock mu(self, lock_); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame^] | 145 | auto it = id_to_entry_.find(id); |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 146 | CHECK(it != id_to_entry_.end()); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame^] | 147 | Demote(*it->second); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void ObjectRegistry::Demote(ObjectRegistryEntry& entry) { |
| 151 | if (entry.jni_reference_type == JNIGlobalRefType) { |
| 152 | Thread* self = Thread::Current(); |
| 153 | JNIEnv* env = self->GetJniEnv(); |
| 154 | jobject global = entry.jni_reference; |
| 155 | entry.jni_reference = env->NewWeakGlobalRef(entry.jni_reference); |
| 156 | entry.jni_reference_type = JNIWeakGlobalRefType; |
| 157 | env->DeleteGlobalRef(global); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void ObjectRegistry::Promote(ObjectRegistryEntry& entry) { |
| 162 | if (entry.jni_reference_type == JNIWeakGlobalRefType) { |
| 163 | Thread* self = Thread::Current(); |
| 164 | JNIEnv* env = self->GetJniEnv(); |
| 165 | jobject weak = entry.jni_reference; |
| 166 | entry.jni_reference = env->NewGlobalRef(entry.jni_reference); |
| 167 | entry.jni_reference_type = JNIGlobalRefType; |
| 168 | env->DeleteWeakGlobalRef(weak); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | bool ObjectRegistry::IsCollected(JDWP::ObjectId id) { |
| 173 | Thread* self = Thread::Current(); |
| 174 | MutexLock mu(self, lock_); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame^] | 175 | auto it = id_to_entry_.find(id); |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 176 | CHECK(it != id_to_entry_.end()); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame^] | 177 | ObjectRegistryEntry& entry = *it->second; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 178 | if (entry.jni_reference_type == JNIWeakGlobalRefType) { |
| 179 | JNIEnv* env = self->GetJniEnv(); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 180 | return env->IsSameObject(entry.jni_reference, NULL); // Has the jweak been collected? |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 181 | } else { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 182 | 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] | 183 | } |
| 184 | } |
| 185 | |
| 186 | void ObjectRegistry::DisposeObject(JDWP::ObjectId id, uint32_t reference_count) { |
| 187 | Thread* self = Thread::Current(); |
| 188 | MutexLock mu(self, lock_); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame^] | 189 | auto it = id_to_entry_.find(id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 190 | if (it == id_to_entry_.end()) { |
| 191 | return; |
| 192 | } |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame^] | 193 | ObjectRegistryEntry* entry = it->second; |
| 194 | entry->reference_count -= reference_count; |
| 195 | if (entry->reference_count <= 0) { |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 196 | JNIEnv* env = self->GetJniEnv(); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame^] | 197 | mirror::Object* object = self->DecodeJObject(entry->jni_reference); |
| 198 | if (entry->jni_reference_type == JNIWeakGlobalRefType) { |
| 199 | env->DeleteWeakGlobalRef(entry->jni_reference); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 200 | } else { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame^] | 201 | env->DeleteGlobalRef(entry->jni_reference); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 202 | } |
| 203 | object_to_entry_.erase(object); |
| 204 | id_to_entry_.erase(id); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame^] | 205 | delete entry; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame^] | 209 | void ObjectRegistry::UpdateObjectPointers(RootVisitor visitor, void* arg) { |
| 210 | MutexLock mu(Thread::Current(), lock_); |
| 211 | if (object_to_entry_.empty()) { |
| 212 | return; |
| 213 | } |
| 214 | std::map<mirror::Object*, ObjectRegistryEntry*> new_object_to_entry; |
| 215 | for (auto& pair : object_to_entry_) { |
| 216 | mirror::Object* new_obj; |
| 217 | if (pair.first != nullptr) { |
| 218 | new_obj = visitor(pair.first, arg); |
| 219 | if (new_obj != nullptr) { |
| 220 | new_object_to_entry.insert(std::make_pair(new_obj, pair.second)); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | object_to_entry_ = new_object_to_entry; |
| 225 | } |
| 226 | |
| 227 | void ObjectRegistry::AllowNewObjects() { |
| 228 | Thread* self = Thread::Current(); |
| 229 | MutexLock mu(self, lock_); |
| 230 | allow_new_objects_ = true; |
| 231 | condition_.Broadcast(self); |
| 232 | } |
| 233 | |
| 234 | void ObjectRegistry::DisallowNewObjects() { |
| 235 | Thread* self = Thread::Current(); |
| 236 | MutexLock mu(self, lock_); |
| 237 | allow_new_objects_ = false; |
| 238 | } |
| 239 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 240 | } // namespace art |