Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -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 | #ifndef ART_RUNTIME_CLASS_TABLE_INL_H_ |
| 18 | #define ART_RUNTIME_CLASS_TABLE_INL_H_ |
| 19 | |
| 20 | #include "class_table.h" |
Andreas Gampe | 508fdf3 | 2017-06-05 16:42:13 -0700 | [diff] [blame] | 21 | |
Andreas Gampe | 88dbad3 | 2018-06-26 19:54:12 -0700 | [diff] [blame] | 22 | #include "base/mutex-inl.h" |
Vladimir Marko | 4e7b3c7 | 2021-02-23 18:13:47 +0000 | [diff] [blame] | 23 | #include "dex/utf.h" |
Andreas Gampe | 508fdf3 | 2017-06-05 16:42:13 -0700 | [diff] [blame] | 24 | #include "gc_root-inl.h" |
Vladimir Marko | 6834d34 | 2018-05-25 13:12:09 +0100 | [diff] [blame] | 25 | #include "mirror/class.h" |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 26 | #include "oat_file.h" |
Vladimir Marko | 1fe5839 | 2019-04-10 16:14:56 +0100 | [diff] [blame] | 27 | #include "obj_ptr-inl.h" |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
Vladimir Marko | 4e7b3c7 | 2021-02-23 18:13:47 +0000 | [diff] [blame] | 31 | inline uint32_t ClassTable::ClassDescriptorHash::operator()(const TableSlot& slot) const { |
| 32 | std::string temp; |
| 33 | // No read barrier needed, we're reading a chain of constant references for comparison |
| 34 | // with null and retrieval of constant primitive data. See ReadBarrierOption. |
| 35 | return ComputeModifiedUtf8Hash(slot.Read<kWithoutReadBarrier>()->GetDescriptor(&temp)); |
| 36 | } |
| 37 | |
| 38 | inline uint32_t ClassTable::ClassDescriptorHash::operator()(const DescriptorHashPair& pair) const { |
| 39 | DCHECK_EQ(ComputeModifiedUtf8Hash(pair.first), pair.second); |
| 40 | return pair.second; |
| 41 | } |
| 42 | |
| 43 | inline bool ClassTable::ClassDescriptorEquals::operator()(const TableSlot& a, |
| 44 | const TableSlot& b) const { |
| 45 | // No read barrier needed, we're reading a chain of constant references for comparison |
| 46 | // with null and retrieval of constant primitive data. See ReadBarrierOption. |
| 47 | if (a.Hash() != b.Hash()) { |
| 48 | std::string temp; |
| 49 | DCHECK(!a.Read<kWithoutReadBarrier>()->DescriptorEquals( |
| 50 | b.Read<kWithoutReadBarrier>()->GetDescriptor(&temp))); |
| 51 | return false; |
| 52 | } |
| 53 | std::string temp; |
| 54 | return a.Read<kWithoutReadBarrier>()->DescriptorEquals( |
| 55 | b.Read<kWithoutReadBarrier>()->GetDescriptor(&temp)); |
| 56 | } |
| 57 | |
| 58 | inline bool ClassTable::ClassDescriptorEquals::operator()(const TableSlot& a, |
| 59 | const DescriptorHashPair& b) const { |
| 60 | // No read barrier needed, we're reading a chain of constant references for comparison |
| 61 | // with null and retrieval of constant primitive data. See ReadBarrierOption. |
| 62 | if (!a.MaskedHashEquals(b.second)) { |
| 63 | DCHECK(!a.Read<kWithoutReadBarrier>()->DescriptorEquals(b.first)); |
| 64 | return false; |
| 65 | } |
| 66 | return a.Read<kWithoutReadBarrier>()->DescriptorEquals(b.first); |
| 67 | } |
| 68 | |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 69 | template<class Visitor> |
| 70 | void ClassTable::VisitRoots(Visitor& visitor) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 71 | ReaderMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 72 | for (ClassSet& class_set : classes_) { |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 73 | for (TableSlot& table_slot : class_set) { |
| 74 | table_slot.VisitRoot(visitor); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 77 | for (GcRoot<mirror::Object>& root : strong_roots_) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 78 | visitor.VisitRoot(root.AddressWithoutBarrier()); |
| 79 | } |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 80 | for (const OatFile* oat_file : oat_files_) { |
| 81 | for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) { |
| 82 | visitor.VisitRootIfNonNull(root.AddressWithoutBarrier()); |
| 83 | } |
| 84 | } |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | template<class Visitor> |
| 88 | void ClassTable::VisitRoots(const Visitor& visitor) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 89 | ReaderMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 90 | for (ClassSet& class_set : classes_) { |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 91 | for (TableSlot& table_slot : class_set) { |
| 92 | table_slot.VisitRoot(visitor); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 95 | for (GcRoot<mirror::Object>& root : strong_roots_) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 96 | visitor.VisitRoot(root.AddressWithoutBarrier()); |
| 97 | } |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 98 | for (const OatFile* oat_file : oat_files_) { |
| 99 | for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) { |
| 100 | visitor.VisitRootIfNonNull(root.AddressWithoutBarrier()); |
| 101 | } |
| 102 | } |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 105 | template <typename Visitor, ReadBarrierOption kReadBarrierOption> |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 106 | bool ClassTable::Visit(Visitor& visitor) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 107 | ReaderMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 108 | for (ClassSet& class_set : classes_) { |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 109 | for (TableSlot& table_slot : class_set) { |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 110 | if (!visitor(table_slot.Read<kReadBarrierOption>())) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 111 | return false; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | return true; |
| 116 | } |
| 117 | |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 118 | template <typename Visitor, ReadBarrierOption kReadBarrierOption> |
Mathieu Chartier | db70ce5 | 2016-12-12 11:06:59 -0800 | [diff] [blame] | 119 | bool ClassTable::Visit(const Visitor& visitor) { |
| 120 | ReaderMutexLock mu(Thread::Current(), lock_); |
| 121 | for (ClassSet& class_set : classes_) { |
| 122 | for (TableSlot& table_slot : class_set) { |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 123 | if (!visitor(table_slot.Read<kReadBarrierOption>())) { |
Mathieu Chartier | db70ce5 | 2016-12-12 11:06:59 -0800 | [diff] [blame] | 124 | return false; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | return true; |
| 129 | } |
| 130 | |
Vladimir Marko | c6934e3 | 2019-04-10 11:40:01 +0100 | [diff] [blame] | 131 | inline bool ClassTable::TableSlot::IsNull() const { |
| 132 | return Read<kWithoutReadBarrier>() == nullptr; |
| 133 | } |
| 134 | |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 135 | template<ReadBarrierOption kReadBarrierOption> |
Vladimir Marko | 1fe5839 | 2019-04-10 16:14:56 +0100 | [diff] [blame] | 136 | inline ObjPtr<mirror::Class> ClassTable::TableSlot::Read() const { |
Orion Hodson | 88591fe | 2018-03-06 13:35:43 +0000 | [diff] [blame] | 137 | const uint32_t before = data_.load(std::memory_order_relaxed); |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 138 | const ObjPtr<mirror::Class> before_ptr(ExtractPtr(before)); |
| 139 | const ObjPtr<mirror::Class> after_ptr( |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 140 | GcRoot<mirror::Class>(before_ptr).Read<kReadBarrierOption>()); |
| 141 | if (kReadBarrierOption != kWithoutReadBarrier && before_ptr != after_ptr) { |
| 142 | // If another thread raced and updated the reference, do not store the read barrier updated |
| 143 | // one. |
Orion Hodson | 4557b38 | 2018-01-03 11:47:54 +0000 | [diff] [blame] | 144 | data_.CompareAndSetStrongRelease(before, Encode(after_ptr, MaskHash(before))); |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 145 | } |
Vladimir Marko | 1fe5839 | 2019-04-10 16:14:56 +0100 | [diff] [blame] | 146 | return after_ptr; |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | template<typename Visitor> |
| 150 | inline void ClassTable::TableSlot::VisitRoot(const Visitor& visitor) const { |
Orion Hodson | 88591fe | 2018-03-06 13:35:43 +0000 | [diff] [blame] | 151 | const uint32_t before = data_.load(std::memory_order_relaxed); |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 152 | ObjPtr<mirror::Class> before_ptr(ExtractPtr(before)); |
| 153 | GcRoot<mirror::Class> root(before_ptr); |
| 154 | visitor.VisitRoot(root.AddressWithoutBarrier()); |
| 155 | ObjPtr<mirror::Class> after_ptr(root.Read<kWithoutReadBarrier>()); |
| 156 | if (before_ptr != after_ptr) { |
| 157 | // If another thread raced and updated the reference, do not store the read barrier updated |
| 158 | // one. |
Orion Hodson | 4557b38 | 2018-01-03 11:47:54 +0000 | [diff] [blame] | 159 | data_.CompareAndSetStrongRelease(before, Encode(after_ptr, MaskHash(before))); |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | inline ObjPtr<mirror::Class> ClassTable::TableSlot::ExtractPtr(uint32_t data) { |
| 164 | return reinterpret_cast<mirror::Class*>(data & ~kHashMask); |
| 165 | } |
| 166 | |
| 167 | inline uint32_t ClassTable::TableSlot::Encode(ObjPtr<mirror::Class> klass, uint32_t hash_bits) { |
| 168 | DCHECK_LE(hash_bits, kHashMask); |
| 169 | return reinterpret_cast<uintptr_t>(klass.Ptr()) | hash_bits; |
| 170 | } |
| 171 | |
| 172 | inline ClassTable::TableSlot::TableSlot(ObjPtr<mirror::Class> klass, uint32_t descriptor_hash) |
| 173 | : data_(Encode(klass, MaskHash(descriptor_hash))) { |
Vladimir Marko | c6934e3 | 2019-04-10 11:40:01 +0100 | [diff] [blame] | 174 | DCHECK_EQ(descriptor_hash, HashDescriptor(klass)); |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 175 | } |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 176 | |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 177 | template <typename Filter> |
| 178 | inline void ClassTable::RemoveStrongRoots(const Filter& filter) { |
| 179 | WriterMutexLock mu(Thread::Current(), lock_); |
| 180 | strong_roots_.erase(std::remove_if(strong_roots_.begin(), strong_roots_.end(), filter), |
| 181 | strong_roots_.end()); |
| 182 | } |
| 183 | |
Vladimir Marko | 1dab575 | 2021-02-25 15:41:33 +0000 | [diff] [blame] | 184 | inline ObjPtr<mirror::Class> ClassTable::LookupByDescriptor(ObjPtr<mirror::Class> klass) { |
| 185 | std::string temp; |
| 186 | const char* descriptor = klass->GetDescriptor(&temp); |
| 187 | uint32_t hash = TableSlot::HashDescriptor(klass); |
| 188 | return Lookup(descriptor, hash); |
| 189 | } |
| 190 | |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 191 | } // namespace art |
| 192 | |
| 193 | #endif // ART_RUNTIME_CLASS_TABLE_INL_H_ |