blob: c245d4e780dcbc1b51d385aed02a65107a7d5f16 [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() {
24 classes_.push_back(ClassSet());
25}
26
27void ClassTable::FreezeSnapshot() {
28 classes_.push_back(ClassSet());
29}
30
31bool ClassTable::Contains(mirror::Class* klass) {
32 for (ClassSet& class_set : classes_) {
33 auto it = class_set.Find(GcRoot<mirror::Class>(klass));
34 if (it != class_set.end()) {
35 return it->Read() == klass;
36 }
37 }
38 return false;
39}
40
41mirror::Class* ClassTable::UpdateClass(const char* descriptor, mirror::Class* klass, size_t hash) {
42 // Should only be updating latest table.
43 auto existing_it = classes_.back().FindWithHash(descriptor, hash);
44 if (kIsDebugBuild && existing_it == classes_.back().end()) {
45 for (const ClassSet& class_set : classes_) {
46 if (class_set.FindWithHash(descriptor, hash) != class_set.end()) {
47 LOG(FATAL) << "Updating class found in frozen table " << descriptor;
48 }
49 }
50 LOG(FATAL) << "Updating class not found " << descriptor;
51 }
52 mirror::Class* const existing = existing_it->Read();
53 CHECK_NE(existing, klass) << descriptor;
54 CHECK(!existing->IsResolved()) << descriptor;
55 CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusResolving) << descriptor;
56 CHECK(!klass->IsTemp()) << descriptor;
57 VerifyObject(klass);
58 // Update the element in the hash set with the new class. This is safe to do since the descriptor
59 // doesn't change.
60 *existing_it = GcRoot<mirror::Class>(klass);
61 return existing;
62}
63
64void ClassTable::VisitRoots(RootVisitor* visitor, VisitRootFlags flags ATTRIBUTE_UNUSED) {
65 BufferedRootVisitor<kDefaultBufferedRootCount> buffered_visitor(
66 visitor, RootInfo(kRootStickyClass));
67 for (ClassSet& class_set : classes_) {
68 for (GcRoot<mirror::Class>& root : class_set) {
69 buffered_visitor.VisitRoot(root);
70 }
71 }
72}
73
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070074bool ClassTable::Visit(ClassVisitor* visitor) {
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070075 for (ClassSet& class_set : classes_) {
76 for (GcRoot<mirror::Class>& root : class_set) {
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070077 if (!visitor->Visit(root.Read())) {
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070078 return false;
79 }
80 }
81 }
82 return true;
83}
84
85size_t ClassTable::NumZygoteClasses() const {
86 size_t sum = 0;
87 for (size_t i = 0; i < classes_.size() - 1; ++i) {
88 sum += classes_[i].Size();
89 }
90 return sum;
91}
92
93size_t ClassTable::NumNonZygoteClasses() const {
94 return classes_.back().Size();
95}
96
97mirror::Class* ClassTable::Lookup(const char* descriptor, size_t hash) {
98 for (ClassSet& class_set : classes_) {
99 auto it = class_set.FindWithHash(descriptor, hash);
100 if (it != class_set.end()) {
101 return it->Read();
102 }
103 }
104 return nullptr;
105}
106
107void ClassTable::Insert(mirror::Class* klass) {
108 classes_.back().Insert(GcRoot<mirror::Class>(klass));
109}
110
111void ClassTable::InsertWithHash(mirror::Class* klass, size_t hash) {
112 classes_.back().InsertWithHash(GcRoot<mirror::Class>(klass), hash);
113}
114
115bool ClassTable::Remove(const char* descriptor) {
116 for (ClassSet& class_set : classes_) {
117 auto it = class_set.Find(descriptor);
118 if (it != class_set.end()) {
119 class_set.Erase(it);
120 return true;
121 }
122 }
123 return false;
124}
125
126std::size_t ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& root)
127 const {
128 std::string temp;
129 return ComputeModifiedUtf8Hash(root.Read()->GetDescriptor(&temp));
130}
131
132bool ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a,
133 const GcRoot<mirror::Class>& b) const {
134 DCHECK_EQ(a.Read()->GetClassLoader(), b.Read()->GetClassLoader());
135 std::string temp;
136 return a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp));
137}
138
139bool ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a,
140 const char* descriptor) const {
141 return a.Read()->DescriptorEquals(descriptor);
142}
143
144std::size_t ClassTable::ClassDescriptorHashEquals::operator()(const char* descriptor) const {
145 return ComputeModifiedUtf8Hash(descriptor);
146}
147
148} // namespace art