blob: 81cdce57a894f5225c70621af545b26dfbc0c44d [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
Vladimir Markoe47172b2016-11-25 11:47:20 +000086size_t ClassTable::CountDefiningLoaderClasses(ObjPtr<mirror::ClassLoader> defining_loader,
87 const ClassSet& set) const {
88 size_t count = 0;
89 for (const GcRoot<mirror::Class>& klass : set) {
90 if (klass.Read()->GetClassLoader() == defining_loader) {
91 ++count;
92 }
93 }
94 return count;
95}
96
97size_t ClassTable::NumZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070098 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070099 size_t sum = 0;
100 for (size_t i = 0; i < classes_.size() - 1; ++i) {
Vladimir Markoe47172b2016-11-25 11:47:20 +0000101 sum += CountDefiningLoaderClasses(defining_loader, classes_[i]);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700102 }
103 return sum;
104}
105
Vladimir Markoe47172b2016-11-25 11:47:20 +0000106size_t ClassTable::NumNonZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700107 ReaderMutexLock mu(Thread::Current(), lock_);
Vladimir Markoe47172b2016-11-25 11:47:20 +0000108 return CountDefiningLoaderClasses(defining_loader, classes_.back());
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700109}
110
111mirror::Class* ClassTable::Lookup(const char* descriptor, size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700112 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700113 for (ClassSet& class_set : classes_) {
114 auto it = class_set.FindWithHash(descriptor, hash);
115 if (it != class_set.end()) {
Vladimir Markoe47172b2016-11-25 11:47:20 +0000116 return it->Read();
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700117 }
118 }
119 return nullptr;
120}
121
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700122void ClassTable::Insert(ObjPtr<mirror::Class> klass) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700123 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700124 classes_.back().Insert(GcRoot<mirror::Class>(klass));
125}
126
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700127void ClassTable::InsertWithoutLocks(ObjPtr<mirror::Class> klass) {
Mathieu Chartier496577f2016-09-20 15:33:31 -0700128 classes_.back().Insert(GcRoot<mirror::Class>(klass));
129}
130
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700131void ClassTable::InsertWithHash(ObjPtr<mirror::Class> klass, size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700132 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700133 classes_.back().InsertWithHash(GcRoot<mirror::Class>(klass), hash);
134}
135
136bool ClassTable::Remove(const char* descriptor) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700137 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700138 for (ClassSet& class_set : classes_) {
139 auto it = class_set.Find(descriptor);
140 if (it != class_set.end()) {
141 class_set.Erase(it);
142 return true;
143 }
144 }
145 return false;
146}
147
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800148uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& root)
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700149 const {
150 std::string temp;
151 return ComputeModifiedUtf8Hash(root.Read()->GetDescriptor(&temp));
152}
153
154bool ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a,
155 const GcRoot<mirror::Class>& b) const {
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700156 std::string temp;
157 return a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp));
158}
159
160bool ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a,
161 const char* descriptor) const {
162 return a.Read()->DescriptorEquals(descriptor);
163}
164
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800165uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const char* descriptor) const {
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700166 return ComputeModifiedUtf8Hash(descriptor);
167}
168
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700169bool ClassTable::InsertStrongRoot(ObjPtr<mirror::Object> obj) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700170 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700171 DCHECK(obj != nullptr);
172 for (GcRoot<mirror::Object>& root : strong_roots_) {
173 if (root.Read() == obj) {
Mathieu Chartier00310e02015-10-17 12:46:42 -0700174 return false;
175 }
176 }
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700177 strong_roots_.push_back(GcRoot<mirror::Object>(obj));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000178 // If `obj` is a dex cache associated with a new oat file with GC roots, add it to oat_files_.
179 if (obj->IsDexCache()) {
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700180 const DexFile* dex_file = ObjPtr<mirror::DexCache>::DownCast(obj)->GetDexFile();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000181 if (dex_file != nullptr && dex_file->GetOatDexFile() != nullptr) {
182 const OatFile* oat_file = dex_file->GetOatDexFile()->GetOatFile();
Mathieu Chartier1b868492016-11-16 16:22:37 -0800183 if (oat_file != nullptr && !oat_file->GetBssGcRoots().empty()) {
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000184 InsertOatFileLocked(oat_file); // Ignore return value.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000185 }
186 }
187 }
Mathieu Chartier00310e02015-10-17 12:46:42 -0700188 return true;
189}
190
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000191bool ClassTable::InsertOatFile(const OatFile* oat_file) {
192 WriterMutexLock mu(Thread::Current(), lock_);
193 return InsertOatFileLocked(oat_file);
194}
195
196bool ClassTable::InsertOatFileLocked(const OatFile* oat_file) {
197 if (ContainsElement(oat_files_, oat_file)) {
198 return false;
199 }
200 oat_files_.push_back(oat_file);
201 return true;
202}
203
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800204size_t ClassTable::WriteToMemory(uint8_t* ptr) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700205 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800206 ClassSet combined;
207 // Combine all the class sets in case there are multiple, also adjusts load factor back to
208 // default in case classes were pruned.
209 for (const ClassSet& class_set : classes_) {
210 for (const GcRoot<mirror::Class>& root : class_set) {
211 combined.Insert(root);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800212 }
213 }
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800214 const size_t ret = combined.WriteToMemory(ptr);
215 // Sanity check.
216 if (kIsDebugBuild && ptr != nullptr) {
217 size_t read_count;
218 ClassSet class_set(ptr, /*make copy*/false, &read_count);
219 class_set.Verify();
220 }
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800221 return ret;
222}
223
224size_t ClassTable::ReadFromMemory(uint8_t* ptr) {
225 size_t read_count = 0;
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800226 AddClassSet(ClassSet(ptr, /*make copy*/false, &read_count));
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800227 return read_count;
228}
229
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800230void ClassTable::AddClassSet(ClassSet&& set) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700231 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800232 classes_.insert(classes_.begin(), std::move(set));
233}
234
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700235void ClassTable::ClearStrongRoots() {
236 WriterMutexLock mu(Thread::Current(), lock_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000237 oat_files_.clear();
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700238 strong_roots_.clear();
239}
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700240} // namespace art