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 | |
Andreas Gampe | 2ff3b97 | 2017-06-05 18:14:53 -0700 | [diff] [blame] | 17 | #include "class_table-inl.h" |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 18 | |
Andreas Gampe | 5678db5 | 2017-06-08 14:11:18 -0700 | [diff] [blame] | 19 | #include "base/stl_util.h" |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 20 | #include "mirror/class-inl.h" |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 21 | #include "oat_file.h" |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 25 | ClassTable::ClassTable() : lock_("Class loader classes", kClassLoaderClassesLock) { |
Mathieu Chartier | 32cc9ee | 2015-10-15 09:19:15 -0700 | [diff] [blame] | 26 | Runtime* const runtime = Runtime::Current(); |
| 27 | classes_.push_back(ClassSet(runtime->GetHashTableMinLoadFactor(), |
| 28 | runtime->GetHashTableMaxLoadFactor())); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | void ClassTable::FreezeSnapshot() { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 32 | WriterMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 33 | classes_.push_back(ClassSet()); |
| 34 | } |
| 35 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 36 | bool ClassTable::Contains(ObjPtr<mirror::Class> klass) { |
Vladimir Marko | c6934e3 | 2019-04-10 11:40:01 +0100 | [diff] [blame] | 37 | return LookupByDescriptor(klass) == klass; |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Vladimir Marko | 1fe5839 | 2019-04-10 16:14:56 +0100 | [diff] [blame] | 40 | ObjPtr<mirror::Class> ClassTable::LookupByDescriptor(ObjPtr<mirror::Class> klass) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 41 | ReaderMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | db70ce5 | 2016-12-12 11:06:59 -0800 | [diff] [blame] | 42 | TableSlot slot(klass); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 43 | for (ClassSet& class_set : classes_) { |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 44 | auto it = class_set.find(slot); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 45 | if (it != class_set.end()) { |
| 46 | return it->Read(); |
| 47 | } |
| 48 | } |
| 49 | return nullptr; |
| 50 | } |
| 51 | |
Vladimir Marko | 1fe5839 | 2019-04-10 16:14:56 +0100 | [diff] [blame] | 52 | ObjPtr<mirror::Class> ClassTable::UpdateClass(const char* descriptor, |
| 53 | ObjPtr<mirror::Class> klass, |
| 54 | size_t hash) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 55 | WriterMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 56 | // Should only be updating latest table. |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 57 | DescriptorHashPair pair(descriptor, hash); |
| 58 | auto existing_it = classes_.back().FindWithHash(pair, hash); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 59 | if (kIsDebugBuild && existing_it == classes_.back().end()) { |
| 60 | for (const ClassSet& class_set : classes_) { |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 61 | if (class_set.FindWithHash(pair, hash) != class_set.end()) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 62 | LOG(FATAL) << "Updating class found in frozen table " << descriptor; |
| 63 | } |
| 64 | } |
| 65 | LOG(FATAL) << "Updating class not found " << descriptor; |
| 66 | } |
Vladimir Marko | 1fe5839 | 2019-04-10 16:14:56 +0100 | [diff] [blame] | 67 | const ObjPtr<mirror::Class> existing = existing_it->Read(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 68 | CHECK_NE(existing, klass) << descriptor; |
| 69 | CHECK(!existing->IsResolved()) << descriptor; |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 70 | CHECK_EQ(klass->GetStatus(), ClassStatus::kResolving) << descriptor; |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 71 | CHECK(!klass->IsTemp()) << descriptor; |
| 72 | VerifyObject(klass); |
| 73 | // Update the element in the hash set with the new class. This is safe to do since the descriptor |
| 74 | // doesn't change. |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 75 | *existing_it = TableSlot(klass, hash); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 76 | return existing; |
| 77 | } |
| 78 | |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 79 | size_t ClassTable::CountDefiningLoaderClasses(ObjPtr<mirror::ClassLoader> defining_loader, |
| 80 | const ClassSet& set) const { |
| 81 | size_t count = 0; |
| 82 | for (const TableSlot& root : set) { |
| 83 | if (root.Read()->GetClassLoader() == defining_loader) { |
| 84 | ++count; |
| 85 | } |
| 86 | } |
| 87 | return count; |
| 88 | } |
| 89 | |
| 90 | size_t ClassTable::NumZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 91 | ReaderMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 92 | size_t sum = 0; |
| 93 | for (size_t i = 0; i < classes_.size() - 1; ++i) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 94 | sum += CountDefiningLoaderClasses(defining_loader, classes_[i]); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 95 | } |
| 96 | return sum; |
| 97 | } |
| 98 | |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 99 | size_t ClassTable::NumNonZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 100 | ReaderMutexLock mu(Thread::Current(), lock_); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 101 | return CountDefiningLoaderClasses(defining_loader, classes_.back()); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 104 | size_t ClassTable::NumReferencedZygoteClasses() const { |
| 105 | ReaderMutexLock mu(Thread::Current(), lock_); |
| 106 | size_t sum = 0; |
| 107 | for (size_t i = 0; i < classes_.size() - 1; ++i) { |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 108 | sum += classes_[i].size(); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 109 | } |
| 110 | return sum; |
| 111 | } |
| 112 | |
| 113 | size_t ClassTable::NumReferencedNonZygoteClasses() const { |
| 114 | ReaderMutexLock mu(Thread::Current(), lock_); |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 115 | return classes_.back().size(); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Vladimir Marko | 1fe5839 | 2019-04-10 16:14:56 +0100 | [diff] [blame] | 118 | ObjPtr<mirror::Class> ClassTable::Lookup(const char* descriptor, size_t hash) { |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 119 | DescriptorHashPair pair(descriptor, hash); |
Mathieu Chartier | db70ce5 | 2016-12-12 11:06:59 -0800 | [diff] [blame] | 120 | ReaderMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 121 | for (ClassSet& class_set : classes_) { |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 122 | auto it = class_set.FindWithHash(pair, hash); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 123 | if (it != class_set.end()) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 124 | return it->Read(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | return nullptr; |
| 128 | } |
| 129 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 130 | ObjPtr<mirror::Class> ClassTable::TryInsert(ObjPtr<mirror::Class> klass) { |
| 131 | TableSlot slot(klass); |
| 132 | WriterMutexLock mu(Thread::Current(), lock_); |
| 133 | for (ClassSet& class_set : classes_) { |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 134 | auto it = class_set.find(slot); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 135 | if (it != class_set.end()) { |
| 136 | return it->Read(); |
| 137 | } |
| 138 | } |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 139 | classes_.back().insert(slot); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 140 | return klass; |
| 141 | } |
| 142 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 143 | void ClassTable::Insert(ObjPtr<mirror::Class> klass) { |
Mathieu Chartier | db70ce5 | 2016-12-12 11:06:59 -0800 | [diff] [blame] | 144 | const uint32_t hash = TableSlot::HashDescriptor(klass); |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 145 | WriterMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | db70ce5 | 2016-12-12 11:06:59 -0800 | [diff] [blame] | 146 | classes_.back().InsertWithHash(TableSlot(klass, hash), hash); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 149 | void ClassTable::CopyWithoutLocks(const ClassTable& source_table) { |
| 150 | if (kIsDebugBuild) { |
| 151 | for (ClassSet& class_set : classes_) { |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 152 | CHECK(class_set.empty()); |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | for (const ClassSet& class_set : source_table.classes_) { |
| 156 | for (const TableSlot& slot : class_set) { |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 157 | classes_.back().insert(slot); |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 162 | void ClassTable::InsertWithoutLocks(ObjPtr<mirror::Class> klass) { |
Mathieu Chartier | db70ce5 | 2016-12-12 11:06:59 -0800 | [diff] [blame] | 163 | const uint32_t hash = TableSlot::HashDescriptor(klass); |
| 164 | classes_.back().InsertWithHash(TableSlot(klass, hash), hash); |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 167 | void ClassTable::InsertWithHash(ObjPtr<mirror::Class> klass, size_t hash) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 168 | WriterMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 169 | classes_.back().InsertWithHash(TableSlot(klass, hash), hash); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | bool ClassTable::Remove(const char* descriptor) { |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 173 | DescriptorHashPair pair(descriptor, ComputeModifiedUtf8Hash(descriptor)); |
Mathieu Chartier | db70ce5 | 2016-12-12 11:06:59 -0800 | [diff] [blame] | 174 | WriterMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 175 | for (ClassSet& class_set : classes_) { |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 176 | auto it = class_set.find(pair); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 177 | if (it != class_set.end()) { |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 178 | class_set.erase(it); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 179 | return true; |
| 180 | } |
| 181 | } |
| 182 | return false; |
| 183 | } |
| 184 | |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 185 | uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& slot) |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 186 | const { |
| 187 | std::string temp; |
Vladimir Marko | c6934e3 | 2019-04-10 11:40:01 +0100 | [diff] [blame] | 188 | // No read barrier needed, we're reading a chain of constant references for comparison |
| 189 | // with null and retrieval of constant primitive data. See ReadBarrierOption. |
| 190 | return ComputeModifiedUtf8Hash(slot.Read<kWithoutReadBarrier>()->GetDescriptor(&temp)); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 193 | bool ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& a, |
| 194 | const TableSlot& b) const { |
Vladimir Marko | c6934e3 | 2019-04-10 11:40:01 +0100 | [diff] [blame] | 195 | // No read barrier needed, we're reading a chain of constant references for comparison |
| 196 | // with null and retrieval of constant primitive data. See ReadBarrierOption. |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 197 | if (a.Hash() != b.Hash()) { |
| 198 | std::string temp; |
Vladimir Marko | c6934e3 | 2019-04-10 11:40:01 +0100 | [diff] [blame] | 199 | DCHECK(!a.Read<kWithoutReadBarrier>()->DescriptorEquals( |
| 200 | b.Read<kWithoutReadBarrier>()->GetDescriptor(&temp))); |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 201 | return false; |
| 202 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 203 | std::string temp; |
Vladimir Marko | c6934e3 | 2019-04-10 11:40:01 +0100 | [diff] [blame] | 204 | return a.Read<kWithoutReadBarrier>()->DescriptorEquals( |
| 205 | b.Read<kWithoutReadBarrier>()->GetDescriptor(&temp)); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 208 | bool ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& a, |
| 209 | const DescriptorHashPair& b) const { |
Vladimir Marko | c6934e3 | 2019-04-10 11:40:01 +0100 | [diff] [blame] | 210 | // No read barrier needed, we're reading a chain of constant references for comparison |
| 211 | // with null and retrieval of constant primitive data. See ReadBarrierOption. |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 212 | if (!a.MaskedHashEquals(b.second)) { |
Vladimir Marko | c6934e3 | 2019-04-10 11:40:01 +0100 | [diff] [blame] | 213 | DCHECK(!a.Read<kWithoutReadBarrier>()->DescriptorEquals(b.first)); |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 214 | return false; |
| 215 | } |
Vladimir Marko | c6934e3 | 2019-04-10 11:40:01 +0100 | [diff] [blame] | 216 | return a.Read<kWithoutReadBarrier>()->DescriptorEquals(b.first); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 219 | uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const DescriptorHashPair& pair) const { |
| 220 | return ComputeModifiedUtf8Hash(pair.first); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 223 | bool ClassTable::InsertStrongRoot(ObjPtr<mirror::Object> obj) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 224 | WriterMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 225 | DCHECK(obj != nullptr); |
| 226 | for (GcRoot<mirror::Object>& root : strong_roots_) { |
| 227 | if (root.Read() == obj) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 228 | return false; |
| 229 | } |
| 230 | } |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 231 | strong_roots_.push_back(GcRoot<mirror::Object>(obj)); |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 232 | // If `obj` is a dex cache associated with a new oat file with GC roots, add it to oat_files_. |
| 233 | if (obj->IsDexCache()) { |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 234 | const DexFile* dex_file = ObjPtr<mirror::DexCache>::DownCast(obj)->GetDexFile(); |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 235 | if (dex_file != nullptr && dex_file->GetOatDexFile() != nullptr) { |
| 236 | const OatFile* oat_file = dex_file->GetOatDexFile()->GetOatFile(); |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 237 | if (oat_file != nullptr && !oat_file->GetBssGcRoots().empty()) { |
Vladimir Marko | 1bc4b17 | 2016-10-24 16:53:39 +0000 | [diff] [blame] | 238 | InsertOatFileLocked(oat_file); // Ignore return value. |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | } |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 242 | return true; |
| 243 | } |
| 244 | |
Vladimir Marko | 1bc4b17 | 2016-10-24 16:53:39 +0000 | [diff] [blame] | 245 | bool ClassTable::InsertOatFile(const OatFile* oat_file) { |
| 246 | WriterMutexLock mu(Thread::Current(), lock_); |
| 247 | return InsertOatFileLocked(oat_file); |
| 248 | } |
| 249 | |
| 250 | bool ClassTable::InsertOatFileLocked(const OatFile* oat_file) { |
| 251 | if (ContainsElement(oat_files_, oat_file)) { |
| 252 | return false; |
| 253 | } |
| 254 | oat_files_.push_back(oat_file); |
| 255 | return true; |
| 256 | } |
| 257 | |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 258 | size_t ClassTable::WriteToMemory(uint8_t* ptr) const { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 259 | ReaderMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | 41dc8ce | 2015-12-04 15:07:48 -0800 | [diff] [blame] | 260 | ClassSet combined; |
| 261 | // Combine all the class sets in case there are multiple, also adjusts load factor back to |
| 262 | // default in case classes were pruned. |
| 263 | for (const ClassSet& class_set : classes_) { |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 264 | for (const TableSlot& root : class_set) { |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 265 | combined.insert(root); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 266 | } |
| 267 | } |
Mathieu Chartier | 41dc8ce | 2015-12-04 15:07:48 -0800 | [diff] [blame] | 268 | const size_t ret = combined.WriteToMemory(ptr); |
| 269 | // Sanity check. |
| 270 | if (kIsDebugBuild && ptr != nullptr) { |
| 271 | size_t read_count; |
| 272 | ClassSet class_set(ptr, /*make copy*/false, &read_count); |
| 273 | class_set.Verify(); |
| 274 | } |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | size_t ClassTable::ReadFromMemory(uint8_t* ptr) { |
| 279 | size_t read_count = 0; |
Mathieu Chartier | 88027bd | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 280 | AddClassSet(ClassSet(ptr, /*make copy*/false, &read_count)); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 281 | return read_count; |
| 282 | } |
| 283 | |
Mathieu Chartier | 88027bd | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 284 | void ClassTable::AddClassSet(ClassSet&& set) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 285 | WriterMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | 88027bd | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 286 | classes_.insert(classes_.begin(), std::move(set)); |
| 287 | } |
| 288 | |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 289 | void ClassTable::ClearStrongRoots() { |
| 290 | WriterMutexLock mu(Thread::Current(), lock_); |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 291 | oat_files_.clear(); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 292 | strong_roots_.clear(); |
| 293 | } |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 294 | |
Mathieu Chartier | db70ce5 | 2016-12-12 11:06:59 -0800 | [diff] [blame] | 295 | ClassTable::TableSlot::TableSlot(ObjPtr<mirror::Class> klass) |
| 296 | : TableSlot(klass, HashDescriptor(klass)) {} |
| 297 | |
| 298 | uint32_t ClassTable::TableSlot::HashDescriptor(ObjPtr<mirror::Class> klass) { |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 299 | std::string temp; |
Mathieu Chartier | db70ce5 | 2016-12-12 11:06:59 -0800 | [diff] [blame] | 300 | return ComputeModifiedUtf8Hash(klass->GetDescriptor(&temp)); |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 301 | } |
| 302 | |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 303 | } // namespace art |