• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
21 
22 #include "base/mutex-inl.h"
23 #include "gc_root-inl.h"
24 #include "mirror/class.h"
25 #include "oat_file.h"
26 
27 namespace art {
28 
29 template<class Visitor>
VisitRoots(Visitor & visitor)30 void ClassTable::VisitRoots(Visitor& visitor) {
31   ReaderMutexLock mu(Thread::Current(), lock_);
32   for (ClassSet& class_set : classes_) {
33     for (TableSlot& table_slot : class_set) {
34       table_slot.VisitRoot(visitor);
35     }
36   }
37   for (GcRoot<mirror::Object>& root : strong_roots_) {
38     visitor.VisitRoot(root.AddressWithoutBarrier());
39   }
40   for (const OatFile* oat_file : oat_files_) {
41     for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) {
42       visitor.VisitRootIfNonNull(root.AddressWithoutBarrier());
43     }
44   }
45 }
46 
47 template<class Visitor>
VisitRoots(const Visitor & visitor)48 void ClassTable::VisitRoots(const Visitor& visitor) {
49   ReaderMutexLock mu(Thread::Current(), lock_);
50   for (ClassSet& class_set : classes_) {
51     for (TableSlot& table_slot : class_set) {
52       table_slot.VisitRoot(visitor);
53     }
54   }
55   for (GcRoot<mirror::Object>& root : strong_roots_) {
56     visitor.VisitRoot(root.AddressWithoutBarrier());
57   }
58   for (const OatFile* oat_file : oat_files_) {
59     for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) {
60       visitor.VisitRootIfNonNull(root.AddressWithoutBarrier());
61     }
62   }
63 }
64 
65 template <typename Visitor, ReadBarrierOption kReadBarrierOption>
Visit(Visitor & visitor)66 bool ClassTable::Visit(Visitor& visitor) {
67   ReaderMutexLock mu(Thread::Current(), lock_);
68   for (ClassSet& class_set : classes_) {
69     for (TableSlot& table_slot : class_set) {
70       if (!visitor(table_slot.Read<kReadBarrierOption>())) {
71         return false;
72       }
73     }
74   }
75   return true;
76 }
77 
78 template <typename Visitor, ReadBarrierOption kReadBarrierOption>
Visit(const Visitor & visitor)79 bool ClassTable::Visit(const Visitor& visitor) {
80   ReaderMutexLock mu(Thread::Current(), lock_);
81   for (ClassSet& class_set : classes_) {
82     for (TableSlot& table_slot : class_set) {
83       if (!visitor(table_slot.Read<kReadBarrierOption>())) {
84         return false;
85       }
86     }
87   }
88   return true;
89 }
90 
91 template<ReadBarrierOption kReadBarrierOption>
Read()92 inline mirror::Class* ClassTable::TableSlot::Read() const {
93   const uint32_t before = data_.load(std::memory_order_relaxed);
94   const ObjPtr<mirror::Class> before_ptr(ExtractPtr(before));
95   const ObjPtr<mirror::Class> after_ptr(
96       GcRoot<mirror::Class>(before_ptr).Read<kReadBarrierOption>());
97   if (kReadBarrierOption != kWithoutReadBarrier && before_ptr != after_ptr) {
98     // If another thread raced and updated the reference, do not store the read barrier updated
99     // one.
100     data_.CompareAndSetStrongRelease(before, Encode(after_ptr, MaskHash(before)));
101   }
102   return after_ptr.Ptr();
103 }
104 
105 template<typename Visitor>
VisitRoot(const Visitor & visitor)106 inline void ClassTable::TableSlot::VisitRoot(const Visitor& visitor) const {
107   const uint32_t before = data_.load(std::memory_order_relaxed);
108   ObjPtr<mirror::Class> before_ptr(ExtractPtr(before));
109   GcRoot<mirror::Class> root(before_ptr);
110   visitor.VisitRoot(root.AddressWithoutBarrier());
111   ObjPtr<mirror::Class> after_ptr(root.Read<kWithoutReadBarrier>());
112   if (before_ptr != after_ptr) {
113     // If another thread raced and updated the reference, do not store the read barrier updated
114     // one.
115     data_.CompareAndSetStrongRelease(before, Encode(after_ptr, MaskHash(before)));
116   }
117 }
118 
ExtractPtr(uint32_t data)119 inline ObjPtr<mirror::Class> ClassTable::TableSlot::ExtractPtr(uint32_t data) {
120   return reinterpret_cast<mirror::Class*>(data & ~kHashMask);
121 }
122 
Encode(ObjPtr<mirror::Class> klass,uint32_t hash_bits)123 inline uint32_t ClassTable::TableSlot::Encode(ObjPtr<mirror::Class> klass, uint32_t hash_bits) {
124   DCHECK_LE(hash_bits, kHashMask);
125   return reinterpret_cast<uintptr_t>(klass.Ptr()) | hash_bits;
126 }
127 
TableSlot(ObjPtr<mirror::Class> klass,uint32_t descriptor_hash)128 inline ClassTable::TableSlot::TableSlot(ObjPtr<mirror::Class> klass, uint32_t descriptor_hash)
129     : data_(Encode(klass, MaskHash(descriptor_hash))) {
130   if (kIsDebugBuild) {
131     std::string temp;
132     const uint32_t hash = ComputeModifiedUtf8Hash(klass->GetDescriptor(&temp));
133     CHECK_EQ(descriptor_hash, hash);
134   }
135 }
136 
137 template <typename Filter>
RemoveStrongRoots(const Filter & filter)138 inline void ClassTable::RemoveStrongRoots(const Filter& filter) {
139   WriterMutexLock mu(Thread::Current(), lock_);
140   strong_roots_.erase(std::remove_if(strong_roots_.begin(), strong_roots_.end(), filter),
141                       strong_roots_.end());
142 }
143 
144 }  // namespace art
145 
146 #endif  // ART_RUNTIME_CLASS_TABLE_INL_H_
147