Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 21 | namespace art { |
| 22 | |
| 23 | ClassTable::ClassTable() { |
Mathieu Chartier | 32cc9ee | 2015-10-15 09:19:15 -0700 | [diff] [blame] | 24 | Runtime* const runtime = Runtime::Current(); |
| 25 | classes_.push_back(ClassSet(runtime->GetHashTableMinLoadFactor(), |
| 26 | runtime->GetHashTableMaxLoadFactor())); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | void ClassTable::FreezeSnapshot() { |
| 30 | classes_.push_back(ClassSet()); |
| 31 | } |
| 32 | |
| 33 | bool 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 Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 43 | mirror::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 Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 53 | mirror::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 Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 76 | size_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 | |
| 84 | size_t ClassTable::NumNonZygoteClasses() const { |
| 85 | return classes_.back().Size(); |
| 86 | } |
| 87 | |
| 88 | mirror::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 | |
| 98 | void ClassTable::Insert(mirror::Class* klass) { |
| 99 | classes_.back().Insert(GcRoot<mirror::Class>(klass)); |
| 100 | } |
| 101 | |
| 102 | void ClassTable::InsertWithHash(mirror::Class* klass, size_t hash) { |
| 103 | classes_.back().InsertWithHash(GcRoot<mirror::Class>(klass), hash); |
| 104 | } |
| 105 | |
| 106 | bool 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 Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 117 | uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& root) |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 118 | const { |
| 119 | std::string temp; |
| 120 | return ComputeModifiedUtf8Hash(root.Read()->GetDescriptor(&temp)); |
| 121 | } |
| 122 | |
| 123 | bool 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 | |
| 130 | bool ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a, |
| 131 | const char* descriptor) const { |
| 132 | return a.Read()->DescriptorEquals(descriptor); |
| 133 | } |
| 134 | |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 135 | uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const char* descriptor) const { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 136 | return ComputeModifiedUtf8Hash(descriptor); |
| 137 | } |
| 138 | |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 139 | bool 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 Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 150 | size_t ClassTable::WriteToMemory(uint8_t* ptr) const { |
Mathieu Chartier | 41dc8ce | 2015-12-04 15:07:48 -0800 | [diff] [blame] | 151 | 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 Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 157 | } |
| 158 | } |
Mathieu Chartier | 41dc8ce | 2015-12-04 15:07:48 -0800 | [diff] [blame] | 159 | 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 Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | size_t ClassTable::ReadFromMemory(uint8_t* ptr) { |
| 170 | size_t read_count = 0; |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 171 | AddClassSet(ClassSet(ptr, /*make copy*/false, &read_count)); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 172 | return read_count; |
| 173 | } |
| 174 | |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 175 | void ClassTable::AddClassSet(ClassSet&& set) { |
| 176 | classes_.insert(classes_.begin(), std::move(set)); |
| 177 | } |
| 178 | |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 179 | } // namespace art |