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 | |
| 33 | ObjectRegistry::ObjectRegistry() : lock_("ObjectRegistry lock"), next_id_(1) { |
| 34 | } |
| 35 | |
| 36 | JDWP::RefTypeId ObjectRegistry::AddRefType(mirror::Class* c) { |
| 37 | return InternalAdd(c); |
| 38 | } |
| 39 | |
| 40 | JDWP::ObjectId ObjectRegistry::Add(mirror::Object* o) { |
| 41 | return InternalAdd(o); |
| 42 | } |
| 43 | |
| 44 | JDWP::ObjectId ObjectRegistry::InternalAdd(mirror::Object* o) { |
| 45 | if (o == NULL) { |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 50 | MutexLock mu(soa.Self(), lock_); |
| 51 | ObjectRegistryEntry dummy; |
| 52 | std::pair<object_iterator, bool> result = object_to_entry_.insert(std::make_pair(o, dummy)); |
| 53 | ObjectRegistryEntry& entry = result.first->second; |
| 54 | if (!result.second) { |
| 55 | // This object was already in our map. |
| 56 | entry.reference_count += 1; |
| 57 | return entry.id; |
| 58 | } |
| 59 | |
| 60 | // This object isn't in the registry yet, so add it. |
| 61 | JNIEnv* env = soa.Env(); |
| 62 | |
| 63 | jobject local_reference = soa.AddLocalReference<jobject>(o); |
| 64 | |
| 65 | entry.jni_reference_type = JNIWeakGlobalRefType; |
| 66 | entry.jni_reference = env->NewWeakGlobalRef(local_reference); |
| 67 | entry.reference_count = 1; |
| 68 | entry.id = next_id_++; |
| 69 | |
| 70 | id_to_entry_.Put(entry.id, &entry); |
| 71 | |
| 72 | env->DeleteLocalRef(local_reference); |
| 73 | |
| 74 | return entry.id; |
| 75 | } |
| 76 | |
| 77 | bool ObjectRegistry::Contains(mirror::Object* o) { |
| 78 | Thread* self = Thread::Current(); |
| 79 | MutexLock mu(self, lock_); |
| 80 | return (object_to_entry_.find(o) != object_to_entry_.end()); |
| 81 | } |
| 82 | |
| 83 | void ObjectRegistry::Clear() { |
| 84 | Thread* self = Thread::Current(); |
| 85 | MutexLock mu(self, lock_); |
| 86 | VLOG(jdwp) << "Object registry contained " << object_to_entry_.size() << " entries"; |
| 87 | |
| 88 | // Delete all the JNI references. |
| 89 | JNIEnv* env = self->GetJniEnv(); |
| 90 | for (object_iterator it = object_to_entry_.begin(); it != object_to_entry_.end(); ++it) { |
| 91 | ObjectRegistryEntry& entry = (it->second); |
| 92 | if (entry.jni_reference_type == JNIWeakGlobalRefType) { |
| 93 | env->DeleteWeakGlobalRef(entry.jni_reference); |
| 94 | } else { |
| 95 | env->DeleteGlobalRef(entry.jni_reference); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Clear the maps. |
| 100 | object_to_entry_.clear(); |
| 101 | id_to_entry_.clear(); |
| 102 | } |
| 103 | |
| 104 | mirror::Object* ObjectRegistry::InternalGet(JDWP::ObjectId id) { |
| 105 | Thread* self = Thread::Current(); |
| 106 | MutexLock mu(self, lock_); |
| 107 | id_iterator it = id_to_entry_.find(id); |
| 108 | if (it == id_to_entry_.end()) { |
| 109 | return kInvalidObject; |
| 110 | } |
| 111 | ObjectRegistryEntry& entry = *(it->second); |
| 112 | return self->DecodeJObject(entry.jni_reference); |
| 113 | } |
| 114 | |
| 115 | void ObjectRegistry::DisableCollection(JDWP::ObjectId id) { |
| 116 | Thread* self = Thread::Current(); |
| 117 | MutexLock mu(self, lock_); |
| 118 | id_iterator it = id_to_entry_.find(id); |
| 119 | if (it == id_to_entry_.end()) { |
| 120 | return; |
| 121 | } |
| 122 | Promote(*(it->second)); |
| 123 | } |
| 124 | |
| 125 | void ObjectRegistry::EnableCollection(JDWP::ObjectId id) { |
| 126 | Thread* self = Thread::Current(); |
| 127 | MutexLock mu(self, lock_); |
| 128 | id_iterator it = id_to_entry_.find(id); |
| 129 | if (it == id_to_entry_.end()) { |
| 130 | return; |
| 131 | } |
| 132 | Demote(*(it->second)); |
| 133 | } |
| 134 | |
| 135 | void ObjectRegistry::Demote(ObjectRegistryEntry& entry) { |
| 136 | if (entry.jni_reference_type == JNIGlobalRefType) { |
| 137 | Thread* self = Thread::Current(); |
| 138 | JNIEnv* env = self->GetJniEnv(); |
| 139 | jobject global = entry.jni_reference; |
| 140 | entry.jni_reference = env->NewWeakGlobalRef(entry.jni_reference); |
| 141 | entry.jni_reference_type = JNIWeakGlobalRefType; |
| 142 | env->DeleteGlobalRef(global); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void ObjectRegistry::Promote(ObjectRegistryEntry& entry) { |
| 147 | if (entry.jni_reference_type == JNIWeakGlobalRefType) { |
| 148 | Thread* self = Thread::Current(); |
| 149 | JNIEnv* env = self->GetJniEnv(); |
| 150 | jobject weak = entry.jni_reference; |
| 151 | entry.jni_reference = env->NewGlobalRef(entry.jni_reference); |
| 152 | entry.jni_reference_type = JNIGlobalRefType; |
| 153 | env->DeleteWeakGlobalRef(weak); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | bool ObjectRegistry::IsCollected(JDWP::ObjectId id) { |
| 158 | Thread* self = Thread::Current(); |
| 159 | MutexLock mu(self, lock_); |
| 160 | id_iterator it = id_to_entry_.find(id); |
| 161 | if (it == id_to_entry_.end()) { |
| 162 | return true; // TODO: can we report that this was an invalid id? |
| 163 | } |
| 164 | |
| 165 | ObjectRegistryEntry& entry = *(it->second); |
| 166 | if (entry.jni_reference_type == JNIWeakGlobalRefType) { |
| 167 | JNIEnv* env = self->GetJniEnv(); |
| 168 | return env->IsSameObject(entry.jni_reference, NULL); // Has the jweak been collected? |
| 169 | } else { |
| 170 | return false; // We hold a strong reference, so we know this is live. |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void ObjectRegistry::DisposeObject(JDWP::ObjectId id, uint32_t reference_count) { |
| 175 | Thread* self = Thread::Current(); |
| 176 | MutexLock mu(self, lock_); |
| 177 | id_iterator it = id_to_entry_.find(id); |
| 178 | if (it == id_to_entry_.end()) { |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | ObjectRegistryEntry& entry = *(it->second); |
| 183 | entry.reference_count -= reference_count; |
| 184 | if (entry.reference_count <= 0) { |
| 185 | JNIEnv* env = self->GetJniEnv(); |
| 186 | mirror::Object* object = self->DecodeJObject(entry.jni_reference); |
| 187 | if (entry.jni_reference_type == JNIWeakGlobalRefType) { |
| 188 | env->DeleteWeakGlobalRef(entry.jni_reference); |
| 189 | } else { |
| 190 | env->DeleteGlobalRef(entry.jni_reference); |
| 191 | } |
| 192 | object_to_entry_.erase(object); |
| 193 | id_to_entry_.erase(id); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | } // namespace art |