blob: e9154cb400603e2c2de4156876efee5b03c4733b [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 Chartierb8aa1e42016-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 Chartierb8aa1e42016-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 Chartierb8aa1e42016-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 Chartierb8aa1e42016-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
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070056mirror::Class* ClassTable::UpdateClass(const char* descriptor, mirror::Class* klass, size_t hash) {
Mathieu Chartierb8aa1e42016-04-05 14:36:57 -070057 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070058 // Should only be updating latest table.
59 auto existing_it = classes_.back().FindWithHash(descriptor, hash);
60 if (kIsDebugBuild && existing_it == classes_.back().end()) {
61 for (const ClassSet& class_set : classes_) {
62 if (class_set.FindWithHash(descriptor, hash) != class_set.end()) {
63 LOG(FATAL) << "Updating class found in frozen table " << descriptor;
64 }
65 }
66 LOG(FATAL) << "Updating class not found " << descriptor;
67 }
68 mirror::Class* const existing = existing_it->Read();
69 CHECK_NE(existing, klass) << descriptor;
70 CHECK(!existing->IsResolved()) << descriptor;
71 CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusResolving) << descriptor;
72 CHECK(!klass->IsTemp()) << descriptor;
73 VerifyObject(klass);
74 // Update the element in the hash set with the new class. This is safe to do since the descriptor
75 // doesn't change.
76 *existing_it = GcRoot<mirror::Class>(klass);
77 return existing;
78}
79
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070080size_t ClassTable::NumZygoteClasses() const {
Mathieu Chartierb8aa1e42016-04-05 14:36:57 -070081 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070082 size_t sum = 0;
83 for (size_t i = 0; i < classes_.size() - 1; ++i) {
84 sum += classes_[i].Size();
85 }
86 return sum;
87}
88
89size_t ClassTable::NumNonZygoteClasses() const {
Mathieu Chartierb8aa1e42016-04-05 14:36:57 -070090 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070091 return classes_.back().Size();
92}
93
94mirror::Class* ClassTable::Lookup(const char* descriptor, size_t hash) {
Mathieu Chartierb8aa1e42016-04-05 14:36:57 -070095 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070096 for (ClassSet& class_set : classes_) {
97 auto it = class_set.FindWithHash(descriptor, hash);
98 if (it != class_set.end()) {
99 return it->Read();
100 }
101 }
102 return nullptr;
103}
104
105void ClassTable::Insert(mirror::Class* klass) {
Mathieu Chartierb8aa1e42016-04-05 14:36:57 -0700106 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700107 classes_.back().Insert(GcRoot<mirror::Class>(klass));
108}
109
110void ClassTable::InsertWithHash(mirror::Class* klass, size_t hash) {
Mathieu Chartierb8aa1e42016-04-05 14:36:57 -0700111 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700112 classes_.back().InsertWithHash(GcRoot<mirror::Class>(klass), hash);
113}
114
115bool ClassTable::Remove(const char* descriptor) {
Mathieu Chartierb8aa1e42016-04-05 14:36:57 -0700116 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700117 for (ClassSet& class_set : classes_) {
118 auto it = class_set.Find(descriptor);
119 if (it != class_set.end()) {
120 class_set.Erase(it);
121 return true;
122 }
123 }
124 return false;
125}
126
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800127uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& root)
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700128 const {
129 std::string temp;
130 return ComputeModifiedUtf8Hash(root.Read()->GetDescriptor(&temp));
131}
132
133bool ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a,
134 const GcRoot<mirror::Class>& b) const {
135 DCHECK_EQ(a.Read()->GetClassLoader(), b.Read()->GetClassLoader());
136 std::string temp;
137 return a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp));
138}
139
140bool ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a,
141 const char* descriptor) const {
142 return a.Read()->DescriptorEquals(descriptor);
143}
144
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800145uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const char* descriptor) const {
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700146 return ComputeModifiedUtf8Hash(descriptor);
147}
148
Mathieu Chartier696632e2016-06-03 17:47:32 -0700149bool ClassTable::InsertStrongRoot(mirror::Object* obj) {
Mathieu Chartierb8aa1e42016-04-05 14:36:57 -0700150 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier696632e2016-06-03 17:47:32 -0700151 DCHECK(obj != nullptr);
152 for (GcRoot<mirror::Object>& root : strong_roots_) {
153 if (root.Read() == obj) {
Mathieu Chartier00310e02015-10-17 12:46:42 -0700154 return false;
155 }
156 }
Mathieu Chartier696632e2016-06-03 17:47:32 -0700157 strong_roots_.push_back(GcRoot<mirror::Object>(obj));
Mathieu Chartier00310e02015-10-17 12:46:42 -0700158 return true;
159}
160
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800161size_t ClassTable::WriteToMemory(uint8_t* ptr) const {
Mathieu Chartierb8aa1e42016-04-05 14:36:57 -0700162 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800163 ClassSet combined;
164 // Combine all the class sets in case there are multiple, also adjusts load factor back to
165 // default in case classes were pruned.
166 for (const ClassSet& class_set : classes_) {
167 for (const GcRoot<mirror::Class>& root : class_set) {
168 combined.Insert(root);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800169 }
170 }
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800171 const size_t ret = combined.WriteToMemory(ptr);
172 // Sanity check.
173 if (kIsDebugBuild && ptr != nullptr) {
174 size_t read_count;
175 ClassSet class_set(ptr, /*make copy*/false, &read_count);
176 class_set.Verify();
177 }
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800178 return ret;
179}
180
181size_t ClassTable::ReadFromMemory(uint8_t* ptr) {
182 size_t read_count = 0;
Mathieu Chartier69731002016-03-02 16:08:31 -0800183 AddClassSet(ClassSet(ptr, /*make copy*/false, &read_count));
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800184 return read_count;
185}
186
Mathieu Chartier69731002016-03-02 16:08:31 -0800187void ClassTable::AddClassSet(ClassSet&& set) {
Mathieu Chartierb8aa1e42016-04-05 14:36:57 -0700188 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier69731002016-03-02 16:08:31 -0800189 classes_.insert(classes_.begin(), std::move(set));
190}
191
Mathieu Chartier696632e2016-06-03 17:47:32 -0700192void ClassTable::ClearStrongRoots() {
193 WriterMutexLock mu(Thread::Current(), lock_);
194 strong_roots_.clear();
195}
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700196} // namespace art