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" |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 21 | #include "oat_file.h" |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | template<class Visitor> |
| 26 | void ClassTable::VisitRoots(Visitor& visitor) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 27 | ReaderMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 28 | for (ClassSet& class_set : classes_) { |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame^] | 29 | for (TableSlot& table_slot : class_set) { |
| 30 | table_slot.VisitRoot(visitor); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 31 | } |
| 32 | } |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 33 | for (GcRoot<mirror::Object>& root : strong_roots_) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 34 | visitor.VisitRoot(root.AddressWithoutBarrier()); |
| 35 | } |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 36 | for (const OatFile* oat_file : oat_files_) { |
| 37 | for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) { |
| 38 | visitor.VisitRootIfNonNull(root.AddressWithoutBarrier()); |
| 39 | } |
| 40 | } |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | template<class Visitor> |
| 44 | void ClassTable::VisitRoots(const Visitor& visitor) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 45 | ReaderMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 46 | for (ClassSet& class_set : classes_) { |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame^] | 47 | for (TableSlot& table_slot : class_set) { |
| 48 | table_slot.VisitRoot(visitor); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 49 | } |
| 50 | } |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 51 | for (GcRoot<mirror::Object>& root : strong_roots_) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 52 | visitor.VisitRoot(root.AddressWithoutBarrier()); |
| 53 | } |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 54 | for (const OatFile* oat_file : oat_files_) { |
| 55 | for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) { |
| 56 | visitor.VisitRootIfNonNull(root.AddressWithoutBarrier()); |
| 57 | } |
| 58 | } |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 61 | template <typename Visitor> |
| 62 | bool ClassTable::Visit(Visitor& visitor) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 63 | ReaderMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 64 | for (ClassSet& class_set : classes_) { |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame^] | 65 | for (TableSlot& table_slot : class_set) { |
| 66 | if (!visitor(table_slot.Read())) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 67 | return false; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | return true; |
| 72 | } |
| 73 | |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame^] | 74 | template<ReadBarrierOption kReadBarrierOption> |
| 75 | inline mirror::Class* ClassTable::TableSlot::Read() const { |
| 76 | const uint32_t before = data_.LoadRelaxed(); |
| 77 | ObjPtr<mirror::Class> const before_ptr(ExtractPtr(before)); |
| 78 | ObjPtr<mirror::Class> const after_ptr( |
| 79 | GcRoot<mirror::Class>(before_ptr).Read<kReadBarrierOption>()); |
| 80 | if (kReadBarrierOption != kWithoutReadBarrier && before_ptr != after_ptr) { |
| 81 | // If another thread raced and updated the reference, do not store the read barrier updated |
| 82 | // one. |
| 83 | data_.CompareExchangeStrongRelaxed(before, Encode(after_ptr, MaskHash(before))); |
| 84 | } |
| 85 | return after_ptr.Ptr(); |
| 86 | } |
| 87 | |
| 88 | template<typename Visitor> |
| 89 | inline void ClassTable::TableSlot::VisitRoot(const Visitor& visitor) const { |
| 90 | const uint32_t before = data_.LoadRelaxed(); |
| 91 | ObjPtr<mirror::Class> before_ptr(ExtractPtr(before)); |
| 92 | GcRoot<mirror::Class> root(before_ptr); |
| 93 | visitor.VisitRoot(root.AddressWithoutBarrier()); |
| 94 | ObjPtr<mirror::Class> after_ptr(root.Read<kWithoutReadBarrier>()); |
| 95 | if (before_ptr != after_ptr) { |
| 96 | // If another thread raced and updated the reference, do not store the read barrier updated |
| 97 | // one. |
| 98 | data_.CompareExchangeStrongRelaxed(before, Encode(after_ptr, MaskHash(before))); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | inline ObjPtr<mirror::Class> ClassTable::TableSlot::ExtractPtr(uint32_t data) { |
| 103 | return reinterpret_cast<mirror::Class*>(data & ~kHashMask); |
| 104 | } |
| 105 | |
| 106 | inline uint32_t ClassTable::TableSlot::Encode(ObjPtr<mirror::Class> klass, uint32_t hash_bits) { |
| 107 | DCHECK_LE(hash_bits, kHashMask); |
| 108 | return reinterpret_cast<uintptr_t>(klass.Ptr()) | hash_bits; |
| 109 | } |
| 110 | |
| 111 | inline ClassTable::TableSlot::TableSlot(ObjPtr<mirror::Class> klass, uint32_t descriptor_hash) |
| 112 | : data_(Encode(klass, MaskHash(descriptor_hash))) { |
| 113 | if (kIsDebugBuild) { |
| 114 | std::string temp; |
| 115 | const uint32_t hash = ComputeModifiedUtf8Hash(klass->GetDescriptor(&temp)); |
| 116 | CHECK_EQ(descriptor_hash, hash); |
| 117 | } |
| 118 | } |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 119 | |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 120 | } // namespace art |
| 121 | |
| 122 | #endif // ART_RUNTIME_CLASS_TABLE_INL_H_ |