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" |
Andreas Gampe | 508fdf3 | 2017-06-05 16:42:13 -0700 | [diff] [blame] | 23 | #include "gc_root-inl.h" |
Vladimir Marko | 6834d34 | 2018-05-25 13:12:09 +0100 | [diff] [blame] | 24 | #include "mirror/class.h" |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 25 | #include "oat_file.h" |
Vladimir Marko | 1fe5839 | 2019-04-10 16:14:56 +0100 | [diff] [blame^] | 26 | #include "obj_ptr-inl.h" |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
| 30 | template<class Visitor> |
| 31 | void ClassTable::VisitRoots(Visitor& visitor) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 32 | ReaderMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 33 | for (ClassSet& class_set : classes_) { |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 34 | for (TableSlot& table_slot : class_set) { |
| 35 | table_slot.VisitRoot(visitor); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 36 | } |
| 37 | } |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 38 | for (GcRoot<mirror::Object>& root : strong_roots_) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 39 | visitor.VisitRoot(root.AddressWithoutBarrier()); |
| 40 | } |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 41 | for (const OatFile* oat_file : oat_files_) { |
| 42 | for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) { |
| 43 | visitor.VisitRootIfNonNull(root.AddressWithoutBarrier()); |
| 44 | } |
| 45 | } |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | template<class Visitor> |
| 49 | void ClassTable::VisitRoots(const Visitor& visitor) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 50 | ReaderMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 51 | for (ClassSet& class_set : classes_) { |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 52 | for (TableSlot& table_slot : class_set) { |
| 53 | table_slot.VisitRoot(visitor); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 54 | } |
| 55 | } |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 56 | for (GcRoot<mirror::Object>& root : strong_roots_) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 57 | visitor.VisitRoot(root.AddressWithoutBarrier()); |
| 58 | } |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 59 | for (const OatFile* oat_file : oat_files_) { |
| 60 | for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) { |
| 61 | visitor.VisitRootIfNonNull(root.AddressWithoutBarrier()); |
| 62 | } |
| 63 | } |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 66 | template <typename Visitor, ReadBarrierOption kReadBarrierOption> |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 67 | bool ClassTable::Visit(Visitor& visitor) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 68 | ReaderMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 69 | for (ClassSet& class_set : classes_) { |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 70 | for (TableSlot& table_slot : class_set) { |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 71 | if (!visitor(table_slot.Read<kReadBarrierOption>())) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 72 | return false; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | return true; |
| 77 | } |
| 78 | |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 79 | template <typename Visitor, ReadBarrierOption kReadBarrierOption> |
Mathieu Chartier | db70ce5 | 2016-12-12 11:06:59 -0800 | [diff] [blame] | 80 | bool ClassTable::Visit(const Visitor& visitor) { |
| 81 | ReaderMutexLock mu(Thread::Current(), lock_); |
| 82 | for (ClassSet& class_set : classes_) { |
| 83 | for (TableSlot& table_slot : class_set) { |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 84 | if (!visitor(table_slot.Read<kReadBarrierOption>())) { |
Mathieu Chartier | db70ce5 | 2016-12-12 11:06:59 -0800 | [diff] [blame] | 85 | return false; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | return true; |
| 90 | } |
| 91 | |
Vladimir Marko | c6934e3 | 2019-04-10 11:40:01 +0100 | [diff] [blame] | 92 | inline bool ClassTable::TableSlot::IsNull() const { |
| 93 | return Read<kWithoutReadBarrier>() == nullptr; |
| 94 | } |
| 95 | |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 96 | template<ReadBarrierOption kReadBarrierOption> |
Vladimir Marko | 1fe5839 | 2019-04-10 16:14:56 +0100 | [diff] [blame^] | 97 | inline ObjPtr<mirror::Class> ClassTable::TableSlot::Read() const { |
Orion Hodson | 88591fe | 2018-03-06 13:35:43 +0000 | [diff] [blame] | 98 | const uint32_t before = data_.load(std::memory_order_relaxed); |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 99 | const ObjPtr<mirror::Class> before_ptr(ExtractPtr(before)); |
| 100 | const ObjPtr<mirror::Class> after_ptr( |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 101 | GcRoot<mirror::Class>(before_ptr).Read<kReadBarrierOption>()); |
| 102 | if (kReadBarrierOption != kWithoutReadBarrier && before_ptr != after_ptr) { |
| 103 | // If another thread raced and updated the reference, do not store the read barrier updated |
| 104 | // one. |
Orion Hodson | 4557b38 | 2018-01-03 11:47:54 +0000 | [diff] [blame] | 105 | data_.CompareAndSetStrongRelease(before, Encode(after_ptr, MaskHash(before))); |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 106 | } |
Vladimir Marko | 1fe5839 | 2019-04-10 16:14:56 +0100 | [diff] [blame^] | 107 | return after_ptr; |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | template<typename Visitor> |
| 111 | inline void ClassTable::TableSlot::VisitRoot(const Visitor& visitor) const { |
Orion Hodson | 88591fe | 2018-03-06 13:35:43 +0000 | [diff] [blame] | 112 | const uint32_t before = data_.load(std::memory_order_relaxed); |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 113 | ObjPtr<mirror::Class> before_ptr(ExtractPtr(before)); |
| 114 | GcRoot<mirror::Class> root(before_ptr); |
| 115 | visitor.VisitRoot(root.AddressWithoutBarrier()); |
| 116 | ObjPtr<mirror::Class> after_ptr(root.Read<kWithoutReadBarrier>()); |
| 117 | if (before_ptr != after_ptr) { |
| 118 | // If another thread raced and updated the reference, do not store the read barrier updated |
| 119 | // one. |
Orion Hodson | 4557b38 | 2018-01-03 11:47:54 +0000 | [diff] [blame] | 120 | data_.CompareAndSetStrongRelease(before, Encode(after_ptr, MaskHash(before))); |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | inline ObjPtr<mirror::Class> ClassTable::TableSlot::ExtractPtr(uint32_t data) { |
| 125 | return reinterpret_cast<mirror::Class*>(data & ~kHashMask); |
| 126 | } |
| 127 | |
| 128 | inline uint32_t ClassTable::TableSlot::Encode(ObjPtr<mirror::Class> klass, uint32_t hash_bits) { |
| 129 | DCHECK_LE(hash_bits, kHashMask); |
| 130 | return reinterpret_cast<uintptr_t>(klass.Ptr()) | hash_bits; |
| 131 | } |
| 132 | |
| 133 | inline ClassTable::TableSlot::TableSlot(ObjPtr<mirror::Class> klass, uint32_t descriptor_hash) |
| 134 | : data_(Encode(klass, MaskHash(descriptor_hash))) { |
Vladimir Marko | c6934e3 | 2019-04-10 11:40:01 +0100 | [diff] [blame] | 135 | DCHECK_EQ(descriptor_hash, HashDescriptor(klass)); |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 136 | } |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 137 | |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 138 | template <typename Filter> |
| 139 | inline void ClassTable::RemoveStrongRoots(const Filter& filter) { |
| 140 | WriterMutexLock mu(Thread::Current(), lock_); |
| 141 | strong_roots_.erase(std::remove_if(strong_roots_.begin(), strong_roots_.end(), filter), |
| 142 | strong_roots_.end()); |
| 143 | } |
| 144 | |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 145 | } // namespace art |
| 146 | |
| 147 | #endif // ART_RUNTIME_CLASS_TABLE_INL_H_ |