blob: b44104e2990cc82a61873ccea9202d3b9ecce578 [file] [log] [blame]
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -07001/*
2 * Copyright (C) 2015 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 "class_table.h"
18
19#include "mirror/class-inl.h"
20
21namespace art {
22
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070023ClassTable::ClassTable() : lock_("Class loader classes", kClassLoaderClassesLock) {
Mathieu Chartier32cc9ee2015-10-15 09:19:15 -070024 Runtime* const runtime = Runtime::Current();
25 classes_.push_back(ClassSet(runtime->GetHashTableMinLoadFactor(),
26 runtime->GetHashTableMaxLoadFactor()));
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070027}
28
29void ClassTable::FreezeSnapshot() {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070030 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070031 classes_.push_back(ClassSet());
32}
33
Mathieu Chartier28357fa2016-10-18 16:27:40 -070034bool ClassTable::Contains(ObjPtr<mirror::Class> klass) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070035 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070036 for (ClassSet& class_set : classes_) {
37 auto it = class_set.Find(GcRoot<mirror::Class>(klass));
38 if (it != class_set.end()) {
39 return it->Read() == klass;
40 }
41 }
42 return false;
43}
44
Mathieu Chartier28357fa2016-10-18 16:27:40 -070045mirror::Class* ClassTable::LookupByDescriptor(ObjPtr<mirror::Class> klass) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070046 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartierfbc31082016-01-24 11:59:56 -080047 for (ClassSet& class_set : classes_) {
48 auto it = class_set.Find(GcRoot<mirror::Class>(klass));
49 if (it != class_set.end()) {
50 return it->Read();
51 }
52 }
53 return nullptr;
54}
55
Pirama Arumuga Nainar813b9c42016-08-25 23:42:50 -070056// Bug: http://b/31104323 Ignore -Wunreachable-code from the for loop below
57#pragma clang diagnostic push
58#pragma clang diagnostic ignored "-Wunreachable-code"
59
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070060mirror::Class* ClassTable::UpdateClass(const char* descriptor, mirror::Class* klass, size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070061 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070062 // Should only be updating latest table.
63 auto existing_it = classes_.back().FindWithHash(descriptor, hash);
64 if (kIsDebugBuild && existing_it == classes_.back().end()) {
65 for (const ClassSet& class_set : classes_) {
66 if (class_set.FindWithHash(descriptor, hash) != class_set.end()) {
67 LOG(FATAL) << "Updating class found in frozen table " << descriptor;
68 }
69 }
70 LOG(FATAL) << "Updating class not found " << descriptor;
71 }
72 mirror::Class* const existing = existing_it->Read();
73 CHECK_NE(existing, klass) << descriptor;
74 CHECK(!existing->IsResolved()) << descriptor;
75 CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusResolving) << descriptor;
76 CHECK(!klass->IsTemp()) << descriptor;
77 VerifyObject(klass);
78 // Update the element in the hash set with the new class. This is safe to do since the descriptor
79 // doesn't change.
80 *existing_it = GcRoot<mirror::Class>(klass);
81 return existing;
82}
83
Pirama Arumuga Nainar813b9c42016-08-25 23:42:50 -070084#pragma clang diagnostic pop // http://b/31104323
85
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070086size_t ClassTable::NumZygoteClasses() const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070087 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070088 size_t sum = 0;
89 for (size_t i = 0; i < classes_.size() - 1; ++i) {
90 sum += classes_[i].Size();
91 }
92 return sum;
93}
94
95size_t ClassTable::NumNonZygoteClasses() const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070096 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070097 return classes_.back().Size();
98}
99
100mirror::Class* ClassTable::Lookup(const char* descriptor, size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700101 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700102 for (ClassSet& class_set : classes_) {
103 auto it = class_set.FindWithHash(descriptor, hash);
104 if (it != class_set.end()) {
105 return it->Read();
106 }
107 }
108 return nullptr;
109}
110
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700111void ClassTable::Insert(ObjPtr<mirror::Class> klass) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700112 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700113 classes_.back().Insert(GcRoot<mirror::Class>(klass));
114}
115
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700116void ClassTable::InsertWithoutLocks(ObjPtr<mirror::Class> klass) {
Mathieu Chartier496577f2016-09-20 15:33:31 -0700117 classes_.back().Insert(GcRoot<mirror::Class>(klass));
118}
119
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700120void ClassTable::InsertWithHash(ObjPtr<mirror::Class> klass, size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700121 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700122 classes_.back().InsertWithHash(GcRoot<mirror::Class>(klass), hash);
123}
124
125bool ClassTable::Remove(const char* descriptor) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700126 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700127 for (ClassSet& class_set : classes_) {
128 auto it = class_set.Find(descriptor);
129 if (it != class_set.end()) {
130 class_set.Erase(it);
131 return true;
132 }
133 }
134 return false;
135}
136
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800137uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& root)
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700138 const {
139 std::string temp;
140 return ComputeModifiedUtf8Hash(root.Read()->GetDescriptor(&temp));
141}
142
143bool ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a,
144 const GcRoot<mirror::Class>& b) const {
145 DCHECK_EQ(a.Read()->GetClassLoader(), b.Read()->GetClassLoader());
146 std::string temp;
147 return a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp));
148}
149
150bool ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a,
151 const char* descriptor) const {
152 return a.Read()->DescriptorEquals(descriptor);
153}
154
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800155uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const char* descriptor) const {
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700156 return ComputeModifiedUtf8Hash(descriptor);
157}
158
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700159bool ClassTable::InsertStrongRoot(ObjPtr<mirror::Object> obj) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700160 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700161 DCHECK(obj != nullptr);
162 for (GcRoot<mirror::Object>& root : strong_roots_) {
163 if (root.Read() == obj) {
Mathieu Chartier00310e02015-10-17 12:46:42 -0700164 return false;
165 }
166 }
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700167 strong_roots_.push_back(GcRoot<mirror::Object>(obj));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000168 // If `obj` is a dex cache associated with a new oat file with GC roots, add it to oat_files_.
169 if (obj->IsDexCache()) {
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700170 const DexFile* dex_file = ObjPtr<mirror::DexCache>::DownCast(obj)->GetDexFile();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000171 if (dex_file != nullptr && dex_file->GetOatDexFile() != nullptr) {
172 const OatFile* oat_file = dex_file->GetOatDexFile()->GetOatFile();
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000173 if (!oat_file->GetBssGcRoots().empty()) {
174 InsertOatFileLocked(oat_file); // Ignore return value.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000175 }
176 }
177 }
Mathieu Chartier00310e02015-10-17 12:46:42 -0700178 return true;
179}
180
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000181bool ClassTable::InsertOatFile(const OatFile* oat_file) {
182 WriterMutexLock mu(Thread::Current(), lock_);
183 return InsertOatFileLocked(oat_file);
184}
185
186bool ClassTable::InsertOatFileLocked(const OatFile* oat_file) {
187 if (ContainsElement(oat_files_, oat_file)) {
188 return false;
189 }
190 oat_files_.push_back(oat_file);
191 return true;
192}
193
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800194size_t ClassTable::WriteToMemory(uint8_t* ptr) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700195 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800196 ClassSet combined;
197 // Combine all the class sets in case there are multiple, also adjusts load factor back to
198 // default in case classes were pruned.
199 for (const ClassSet& class_set : classes_) {
200 for (const GcRoot<mirror::Class>& root : class_set) {
201 combined.Insert(root);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800202 }
203 }
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800204 const size_t ret = combined.WriteToMemory(ptr);
205 // Sanity check.
206 if (kIsDebugBuild && ptr != nullptr) {
207 size_t read_count;
208 ClassSet class_set(ptr, /*make copy*/false, &read_count);
209 class_set.Verify();
210 }
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800211 return ret;
212}
213
214size_t ClassTable::ReadFromMemory(uint8_t* ptr) {
215 size_t read_count = 0;
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800216 AddClassSet(ClassSet(ptr, /*make copy*/false, &read_count));
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800217 return read_count;
218}
219
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800220void ClassTable::AddClassSet(ClassSet&& set) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700221 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800222 classes_.insert(classes_.begin(), std::move(set));
223}
224
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700225void ClassTable::ClearStrongRoots() {
226 WriterMutexLock mu(Thread::Current(), lock_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000227 oat_files_.clear();
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700228 strong_roots_.clear();
229}
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700230} // namespace art