blob: 2a4f0e01af66931b661c631b5fc51fa8e8aa3d03 [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
23ClassTable::ClassTable() {
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() {
30 classes_.push_back(ClassSet());
31}
32
33bool ClassTable::Contains(mirror::Class* klass) {
34 for (ClassSet& class_set : classes_) {
35 auto it = class_set.Find(GcRoot<mirror::Class>(klass));
36 if (it != class_set.end()) {
37 return it->Read() == klass;
38 }
39 }
40 return false;
41}
42
Mathieu Chartierfbc31082016-01-24 11:59:56 -080043mirror::Class* ClassTable::LookupByDescriptor(mirror::Class* klass) {
44 for (ClassSet& class_set : classes_) {
45 auto it = class_set.Find(GcRoot<mirror::Class>(klass));
46 if (it != class_set.end()) {
47 return it->Read();
48 }
49 }
50 return nullptr;
51}
52
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070053mirror::Class* ClassTable::UpdateClass(const char* descriptor, mirror::Class* klass, size_t hash) {
54 // Should only be updating latest table.
55 auto existing_it = classes_.back().FindWithHash(descriptor, hash);
56 if (kIsDebugBuild && existing_it == classes_.back().end()) {
57 for (const ClassSet& class_set : classes_) {
58 if (class_set.FindWithHash(descriptor, hash) != class_set.end()) {
59 LOG(FATAL) << "Updating class found in frozen table " << descriptor;
60 }
61 }
62 LOG(FATAL) << "Updating class not found " << descriptor;
63 }
64 mirror::Class* const existing = existing_it->Read();
65 CHECK_NE(existing, klass) << descriptor;
66 CHECK(!existing->IsResolved()) << descriptor;
67 CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusResolving) << descriptor;
68 CHECK(!klass->IsTemp()) << descriptor;
69 VerifyObject(klass);
70 // Update the element in the hash set with the new class. This is safe to do since the descriptor
71 // doesn't change.
72 *existing_it = GcRoot<mirror::Class>(klass);
73 return existing;
74}
75
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070076bool ClassTable::Visit(ClassVisitor* visitor) {
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070077 for (ClassSet& class_set : classes_) {
78 for (GcRoot<mirror::Class>& root : class_set) {
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070079 if (!visitor->Visit(root.Read())) {
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070080 return false;
81 }
82 }
83 }
84 return true;
85}
86
87size_t ClassTable::NumZygoteClasses() const {
88 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 {
96 return classes_.back().Size();
97}
98
99mirror::Class* ClassTable::Lookup(const char* descriptor, size_t hash) {
100 for (ClassSet& class_set : classes_) {
101 auto it = class_set.FindWithHash(descriptor, hash);
102 if (it != class_set.end()) {
103 return it->Read();
104 }
105 }
106 return nullptr;
107}
108
109void ClassTable::Insert(mirror::Class* klass) {
110 classes_.back().Insert(GcRoot<mirror::Class>(klass));
111}
112
113void ClassTable::InsertWithHash(mirror::Class* klass, size_t hash) {
114 classes_.back().InsertWithHash(GcRoot<mirror::Class>(klass), hash);
115}
116
117bool ClassTable::Remove(const char* descriptor) {
118 for (ClassSet& class_set : classes_) {
119 auto it = class_set.Find(descriptor);
120 if (it != class_set.end()) {
121 class_set.Erase(it);
122 return true;
123 }
124 }
125 return false;
126}
127
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800128uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& root)
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700129 const {
130 std::string temp;
131 return ComputeModifiedUtf8Hash(root.Read()->GetDescriptor(&temp));
132}
133
134bool ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a,
135 const GcRoot<mirror::Class>& b) const {
136 DCHECK_EQ(a.Read()->GetClassLoader(), b.Read()->GetClassLoader());
137 std::string temp;
138 return a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp));
139}
140
141bool ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a,
142 const char* descriptor) const {
143 return a.Read()->DescriptorEquals(descriptor);
144}
145
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800146uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const char* descriptor) const {
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700147 return ComputeModifiedUtf8Hash(descriptor);
148}
149
Mathieu Chartier00310e02015-10-17 12:46:42 -0700150bool ClassTable::InsertDexFile(mirror::Object* dex_file) {
151 DCHECK(dex_file != nullptr);
152 for (GcRoot<mirror::Object>& root : dex_files_) {
153 if (root.Read() == dex_file) {
154 return false;
155 }
156 }
157 dex_files_.push_back(GcRoot<mirror::Object>(dex_file));
158 return true;
159}
160
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800161size_t ClassTable::WriteToMemory(uint8_t* ptr) const {
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800162 ClassSet combined;
163 // Combine all the class sets in case there are multiple, also adjusts load factor back to
164 // default in case classes were pruned.
165 for (const ClassSet& class_set : classes_) {
166 for (const GcRoot<mirror::Class>& root : class_set) {
167 combined.Insert(root);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800168 }
169 }
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800170 const size_t ret = combined.WriteToMemory(ptr);
171 // Sanity check.
172 if (kIsDebugBuild && ptr != nullptr) {
173 size_t read_count;
174 ClassSet class_set(ptr, /*make copy*/false, &read_count);
175 class_set.Verify();
176 }
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800177 return ret;
178}
179
180size_t ClassTable::ReadFromMemory(uint8_t* ptr) {
181 size_t read_count = 0;
182 classes_.insert(classes_.begin(), ClassSet(ptr, /*make copy*/false, &read_count));
183 return read_count;
184}
185
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800186void ClassTable::SetClassLoader(mirror::ClassLoader* class_loader) {
187 for (const ClassSet& class_set : classes_) {
188 for (const GcRoot<mirror::Class>& root : class_set) {
189 root.Read()->SetClassLoader(class_loader);
190 }
191 }
192}
193
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700194} // namespace art