blob: 2ae7e8cc30728368eefc9eb035c9c37cc20b9299 [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
34bool ClassTable::Contains(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 Chartierfbc31082016-01-24 11:59:56 -080045mirror::Class* ClassTable::LookupByDescriptor(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
111void ClassTable::Insert(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 Chartier496577f2016-09-20 15:33:31 -0700116void ClassTable::InsertWithoutLocks(mirror::Class* klass) {
117 classes_.back().Insert(GcRoot<mirror::Class>(klass));
118}
119
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700120void ClassTable::InsertWithHash(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 Chartierc9dbb1d2016-06-03 17:47:32 -0700159bool ClassTable::InsertStrongRoot(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()) {
170 const DexFile* dex_file = down_cast<mirror::DexCache*>(obj)->GetDexFile();
171 if (dex_file != nullptr && dex_file->GetOatDexFile() != nullptr) {
172 const OatFile* oat_file = dex_file->GetOatDexFile()->GetOatFile();
173 if (!oat_file->GetBssGcRoots().empty() && !ContainsElement(oat_files_, oat_file)) {
174 oat_files_.push_back(oat_file);
175 }
176 }
177 }
Mathieu Chartier00310e02015-10-17 12:46:42 -0700178 return true;
179}
180
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800181size_t ClassTable::WriteToMemory(uint8_t* ptr) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700182 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800183 ClassSet combined;
184 // Combine all the class sets in case there are multiple, also adjusts load factor back to
185 // default in case classes were pruned.
186 for (const ClassSet& class_set : classes_) {
187 for (const GcRoot<mirror::Class>& root : class_set) {
188 combined.Insert(root);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800189 }
190 }
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800191 const size_t ret = combined.WriteToMemory(ptr);
192 // Sanity check.
193 if (kIsDebugBuild && ptr != nullptr) {
194 size_t read_count;
195 ClassSet class_set(ptr, /*make copy*/false, &read_count);
196 class_set.Verify();
197 }
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800198 return ret;
199}
200
201size_t ClassTable::ReadFromMemory(uint8_t* ptr) {
202 size_t read_count = 0;
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800203 AddClassSet(ClassSet(ptr, /*make copy*/false, &read_count));
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800204 return read_count;
205}
206
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800207void ClassTable::AddClassSet(ClassSet&& set) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700208 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800209 classes_.insert(classes_.begin(), std::move(set));
210}
211
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700212void ClassTable::ClearStrongRoots() {
213 WriterMutexLock mu(Thread::Current(), lock_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000214 oat_files_.clear();
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700215 strong_roots_.clear();
216}
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700217} // namespace art