blob: fc8e6c49da5a6a25a5ad2de1ad6f6acd3c97b89a [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
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070064bool ClassTable::Visit(ClassVisitor* visitor) {
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070065 for (ClassSet& class_set : classes_) {
66 for (GcRoot<mirror::Class>& root : class_set) {
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070067 if (!visitor->Visit(root.Read())) {
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070068 return false;
69 }
70 }
71 }
72 return true;
73}
74
75size_t ClassTable::NumZygoteClasses() const {
76 size_t sum = 0;
77 for (size_t i = 0; i < classes_.size() - 1; ++i) {
78 sum += classes_[i].Size();
79 }
80 return sum;
81}
82
83size_t ClassTable::NumNonZygoteClasses() const {
84 return classes_.back().Size();
85}
86
87mirror::Class* ClassTable::Lookup(const char* descriptor, size_t hash) {
88 for (ClassSet& class_set : classes_) {
89 auto it = class_set.FindWithHash(descriptor, hash);
90 if (it != class_set.end()) {
91 return it->Read();
92 }
93 }
94 return nullptr;
95}
96
97void ClassTable::Insert(mirror::Class* klass) {
98 classes_.back().Insert(GcRoot<mirror::Class>(klass));
99}
100
101void ClassTable::InsertWithHash(mirror::Class* klass, size_t hash) {
102 classes_.back().InsertWithHash(GcRoot<mirror::Class>(klass), hash);
103}
104
105bool ClassTable::Remove(const char* descriptor) {
106 for (ClassSet& class_set : classes_) {
107 auto it = class_set.Find(descriptor);
108 if (it != class_set.end()) {
109 class_set.Erase(it);
110 return true;
111 }
112 }
113 return false;
114}
115
116std::size_t ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& root)
117 const {
118 std::string temp;
119 return ComputeModifiedUtf8Hash(root.Read()->GetDescriptor(&temp));
120}
121
122bool ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a,
123 const GcRoot<mirror::Class>& b) const {
124 DCHECK_EQ(a.Read()->GetClassLoader(), b.Read()->GetClassLoader());
125 std::string temp;
126 return a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp));
127}
128
129bool ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a,
130 const char* descriptor) const {
131 return a.Read()->DescriptorEquals(descriptor);
132}
133
134std::size_t ClassTable::ClassDescriptorHashEquals::operator()(const char* descriptor) const {
135 return ComputeModifiedUtf8Hash(descriptor);
136}
137
138} // namespace art