blob: 40ba3e37be8c3e90d1b608cae1e471b5dfd68c73 [file] [log] [blame]
Elliott Hughes64f574f2013-02-20 14:57:12 -08001/*
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
21namespace art {
22
23mirror::Object* const ObjectRegistry::kInvalidObject = reinterpret_cast<mirror::Object*>(1);
24
25std::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 Hughes0f827162013-02-26 12:12:58 -080033ObjectRegistry::ObjectRegistry()
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080034 : lock_("ObjectRegistry lock", kJdwpObjectRegistryLock), allow_new_objects_(true),
35 condition_("object registry condition", lock_), next_id_(1) {
Elliott Hughes64f574f2013-02-20 14:57:12 -080036}
37
38JDWP::RefTypeId ObjectRegistry::AddRefType(mirror::Class* c) {
39 return InternalAdd(c);
40}
41
42JDWP::ObjectId ObjectRegistry::Add(mirror::Object* o) {
43 return InternalAdd(o);
44}
45
46JDWP::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 Chartier412c7fc2014-02-07 12:18:39 -080053 while (UNLIKELY(!allow_new_objects_)) {
54 condition_.WaitHoldingLocks(soa.Self());
Elliott Hughes64f574f2013-02-20 14:57:12 -080055 }
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080056 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 Hughes64f574f2013-02-20 14:57:12 -080069
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080070 // This object isn't in the registry yet, so add it.
71 JNIEnv* env = soa.Env();
Elliott Hughes64f574f2013-02-20 14:57:12 -080072
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080073 jobject local_reference = soa.AddLocalReference<jobject>(o);
Elliott Hughes64f574f2013-02-20 14:57:12 -080074
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080075 entry->jni_reference_type = JNIWeakGlobalRefType;
76 entry->jni_reference = env->NewWeakGlobalRef(local_reference);
77 entry->reference_count = 1;
78 entry->id = next_id_++;
Elliott Hughes64f574f2013-02-20 14:57:12 -080079
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080080 id_to_entry_.Put(entry->id, entry);
Elliott Hughes64f574f2013-02-20 14:57:12 -080081
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080082 env->DeleteLocalRef(local_reference);
83 }
84 return entry->id;
Elliott Hughes64f574f2013-02-20 14:57:12 -080085}
86
87bool ObjectRegistry::Contains(mirror::Object* o) {
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080088 MutexLock mu(Thread::Current(), lock_);
89 return object_to_entry_.find(o) != object_to_entry_.end();
Elliott Hughes64f574f2013-02-20 14:57:12 -080090}
91
92void ObjectRegistry::Clear() {
93 Thread* self = Thread::Current();
94 MutexLock mu(self, lock_);
95 VLOG(jdwp) << "Object registry contained " << object_to_entry_.size() << " entries";
Elliott Hughes64f574f2013-02-20 14:57:12 -080096 // Delete all the JNI references.
97 JNIEnv* env = self->GetJniEnv();
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080098 for (const auto& pair : object_to_entry_) {
99 const ObjectRegistryEntry& entry = *pair.second;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800100 if (entry.jni_reference_type == JNIWeakGlobalRefType) {
101 env->DeleteWeakGlobalRef(entry.jni_reference);
102 } else {
103 env->DeleteGlobalRef(entry.jni_reference);
104 }
105 }
Elliott Hughes64f574f2013-02-20 14:57:12 -0800106 // Clear the maps.
107 object_to_entry_.clear();
108 id_to_entry_.clear();
109}
110
111mirror::Object* ObjectRegistry::InternalGet(JDWP::ObjectId id) {
112 Thread* self = Thread::Current();
113 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800114 auto it = id_to_entry_.find(id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800115 if (it == id_to_entry_.end()) {
116 return kInvalidObject;
117 }
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800118 ObjectRegistryEntry& entry = *it->second;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800119 return self->DecodeJObject(entry.jni_reference);
120}
121
Jeff Hao449db332013-04-12 18:30:52 -0700122jobject ObjectRegistry::GetJObject(JDWP::ObjectId id) {
Sebastien Hertz0630ab52013-11-28 18:53:35 +0100123 if (id == 0) {
124 return NULL;
125 }
Jeff Hao449db332013-04-12 18:30:52 -0700126 Thread* self = Thread::Current();
127 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800128 auto it = id_to_entry_.find(id);
Jeff Hao449db332013-04-12 18:30:52 -0700129 CHECK(it != id_to_entry_.end()) << id;
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800130 ObjectRegistryEntry& entry = *it->second;
Jeff Hao449db332013-04-12 18:30:52 -0700131 return entry.jni_reference;
132}
133
Elliott Hughes64f574f2013-02-20 14:57:12 -0800134void ObjectRegistry::DisableCollection(JDWP::ObjectId id) {
135 Thread* self = Thread::Current();
136 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800137 auto it = id_to_entry_.find(id);
Sebastien Hertze96060a2013-12-11 12:06:28 +0100138 CHECK(it != id_to_entry_.end());
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800139 Promote(*it->second);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800140}
141
142void ObjectRegistry::EnableCollection(JDWP::ObjectId id) {
143 Thread* self = Thread::Current();
144 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800145 auto it = id_to_entry_.find(id);
Sebastien Hertze96060a2013-12-11 12:06:28 +0100146 CHECK(it != id_to_entry_.end());
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800147 Demote(*it->second);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800148}
149
150void 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
161void 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
172bool ObjectRegistry::IsCollected(JDWP::ObjectId id) {
173 Thread* self = Thread::Current();
174 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800175 auto it = id_to_entry_.find(id);
Sebastien Hertze96060a2013-12-11 12:06:28 +0100176 CHECK(it != id_to_entry_.end());
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800177 ObjectRegistryEntry& entry = *it->second;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800178 if (entry.jni_reference_type == JNIWeakGlobalRefType) {
179 JNIEnv* env = self->GetJniEnv();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700180 return env->IsSameObject(entry.jni_reference, NULL); // Has the jweak been collected?
Elliott Hughes64f574f2013-02-20 14:57:12 -0800181 } else {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700182 return false; // We hold a strong reference, so we know this is live.
Elliott Hughes64f574f2013-02-20 14:57:12 -0800183 }
184}
185
186void ObjectRegistry::DisposeObject(JDWP::ObjectId id, uint32_t reference_count) {
187 Thread* self = Thread::Current();
188 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800189 auto it = id_to_entry_.find(id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800190 if (it == id_to_entry_.end()) {
191 return;
192 }
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800193 ObjectRegistryEntry* entry = it->second;
194 entry->reference_count -= reference_count;
195 if (entry->reference_count <= 0) {
Elliott Hughes64f574f2013-02-20 14:57:12 -0800196 JNIEnv* env = self->GetJniEnv();
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800197 mirror::Object* object = self->DecodeJObject(entry->jni_reference);
198 if (entry->jni_reference_type == JNIWeakGlobalRefType) {
199 env->DeleteWeakGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800200 } else {
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800201 env->DeleteGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800202 }
203 object_to_entry_.erase(object);
204 id_to_entry_.erase(id);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800205 delete entry;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800206 }
207}
208
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800209void 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
227void ObjectRegistry::AllowNewObjects() {
228 Thread* self = Thread::Current();
229 MutexLock mu(self, lock_);
230 allow_new_objects_ = true;
231 condition_.Broadcast(self);
232}
233
234void ObjectRegistry::DisallowNewObjects() {
235 Thread* self = Thread::Current();
236 MutexLock mu(self, lock_);
237 allow_new_objects_ = false;
238}
239
Elliott Hughes64f574f2013-02-20 14:57:12 -0800240} // namespace art