blob: afb0556e1e5f6f3facf954f412ebdbe9f3907fb8 [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 Chartiercc5ebdf2015-07-27 11:19:43 -070076size_t ClassTable::NumZygoteClasses() const {
77 size_t sum = 0;
78 for (size_t i = 0; i < classes_.size() - 1; ++i) {
79 sum += classes_[i].Size();
80 }
81 return sum;
82}
83
84size_t ClassTable::NumNonZygoteClasses() const {
85 return classes_.back().Size();
86}
87
88mirror::Class* ClassTable::Lookup(const char* descriptor, size_t hash) {
89 for (ClassSet& class_set : classes_) {
90 auto it = class_set.FindWithHash(descriptor, hash);
91 if (it != class_set.end()) {
92 return it->Read();
93 }
94 }
95 return nullptr;
96}
97
98void ClassTable::Insert(mirror::Class* klass) {
99 classes_.back().Insert(GcRoot<mirror::Class>(klass));
100}
101
102void ClassTable::InsertWithHash(mirror::Class* klass, size_t hash) {
103 classes_.back().InsertWithHash(GcRoot<mirror::Class>(klass), hash);
104}
105
106bool ClassTable::Remove(const char* descriptor) {
107 for (ClassSet& class_set : classes_) {
108 auto it = class_set.Find(descriptor);
109 if (it != class_set.end()) {
110 class_set.Erase(it);
111 return true;
112 }
113 }
114 return false;
115}
116
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800117uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& root)
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700118 const {
119 std::string temp;
120 return ComputeModifiedUtf8Hash(root.Read()->GetDescriptor(&temp));
121}
122
123bool ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a,
124 const GcRoot<mirror::Class>& b) const {
125 DCHECK_EQ(a.Read()->GetClassLoader(), b.Read()->GetClassLoader());
126 std::string temp;
127 return a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp));
128}
129
130bool ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a,
131 const char* descriptor) const {
132 return a.Read()->DescriptorEquals(descriptor);
133}
134
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800135uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const char* descriptor) const {
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700136 return ComputeModifiedUtf8Hash(descriptor);
137}
138
Mathieu Chartier00310e02015-10-17 12:46:42 -0700139bool ClassTable::InsertDexFile(mirror::Object* dex_file) {
140 DCHECK(dex_file != nullptr);
141 for (GcRoot<mirror::Object>& root : dex_files_) {
142 if (root.Read() == dex_file) {
143 return false;
144 }
145 }
146 dex_files_.push_back(GcRoot<mirror::Object>(dex_file));
147 return true;
148}
149
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800150size_t ClassTable::WriteToMemory(uint8_t* ptr) const {
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800151 ClassSet combined;
152 // Combine all the class sets in case there are multiple, also adjusts load factor back to
153 // default in case classes were pruned.
154 for (const ClassSet& class_set : classes_) {
155 for (const GcRoot<mirror::Class>& root : class_set) {
156 combined.Insert(root);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800157 }
158 }
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800159 const size_t ret = combined.WriteToMemory(ptr);
160 // Sanity check.
161 if (kIsDebugBuild && ptr != nullptr) {
162 size_t read_count;
163 ClassSet class_set(ptr, /*make copy*/false, &read_count);
164 class_set.Verify();
165 }
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800166 return ret;
167}
168
169size_t ClassTable::ReadFromMemory(uint8_t* ptr) {
170 size_t read_count = 0;
171 classes_.insert(classes_.begin(), ClassSet(ptr, /*make copy*/false, &read_count));
172 return read_count;
173}
174
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700175} // namespace art