blob: c59e2e881ddbd9a1f23ddb1d9cb7cc8c9590f28a [file] [log] [blame]
Mathieu Chartiere4275c02015-08-06 15:34:15 -07001/*
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 Gampe508fdf32017-06-05 16:42:13 -070021
22#include "gc_root-inl.h"
Vladimir Markoaad75c62016-10-03 08:46:48 +000023#include "oat_file.h"
Mathieu Chartiere4275c02015-08-06 15:34:15 -070024
25namespace art {
26
27template<class Visitor>
28void ClassTable::VisitRoots(Visitor& visitor) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070029 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiere4275c02015-08-06 15:34:15 -070030 for (ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080031 for (TableSlot& table_slot : class_set) {
32 table_slot.VisitRoot(visitor);
Mathieu Chartiere4275c02015-08-06 15:34:15 -070033 }
34 }
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -070035 for (GcRoot<mirror::Object>& root : strong_roots_) {
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -080036 visitor.VisitRoot(root.AddressWithoutBarrier());
37 }
Vladimir Markoaad75c62016-10-03 08:46:48 +000038 for (const OatFile* oat_file : oat_files_) {
39 for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) {
40 visitor.VisitRootIfNonNull(root.AddressWithoutBarrier());
41 }
42 }
Mathieu Chartiere4275c02015-08-06 15:34:15 -070043}
44
45template<class Visitor>
46void ClassTable::VisitRoots(const Visitor& visitor) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070047 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiere4275c02015-08-06 15:34:15 -070048 for (ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080049 for (TableSlot& table_slot : class_set) {
50 table_slot.VisitRoot(visitor);
Mathieu Chartiere4275c02015-08-06 15:34:15 -070051 }
52 }
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -070053 for (GcRoot<mirror::Object>& root : strong_roots_) {
Mathieu Chartier00310e02015-10-17 12:46:42 -070054 visitor.VisitRoot(root.AddressWithoutBarrier());
55 }
Vladimir Markoaad75c62016-10-03 08:46:48 +000056 for (const OatFile* oat_file : oat_files_) {
57 for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) {
58 visitor.VisitRootIfNonNull(root.AddressWithoutBarrier());
59 }
60 }
Mathieu Chartiere4275c02015-08-06 15:34:15 -070061}
62
Alexey Grebenkinbe4c2bd2018-02-01 19:09:59 +030063template <typename Visitor, ReadBarrierOption kReadBarrierOption>
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -080064bool ClassTable::Visit(Visitor& visitor) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070065 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -080066 for (ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080067 for (TableSlot& table_slot : class_set) {
Alexey Grebenkinbe4c2bd2018-02-01 19:09:59 +030068 if (!visitor(table_slot.Read<kReadBarrierOption>())) {
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -080069 return false;
70 }
71 }
72 }
73 return true;
74}
75
Alexey Grebenkinbe4c2bd2018-02-01 19:09:59 +030076template <typename Visitor, ReadBarrierOption kReadBarrierOption>
Mathieu Chartierdb70ce52016-12-12 11:06:59 -080077bool ClassTable::Visit(const Visitor& visitor) {
78 ReaderMutexLock mu(Thread::Current(), lock_);
79 for (ClassSet& class_set : classes_) {
80 for (TableSlot& table_slot : class_set) {
Alexey Grebenkinbe4c2bd2018-02-01 19:09:59 +030081 if (!visitor(table_slot.Read<kReadBarrierOption>())) {
Mathieu Chartierdb70ce52016-12-12 11:06:59 -080082 return false;
83 }
84 }
85 }
86 return true;
87}
88
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080089template<ReadBarrierOption kReadBarrierOption>
90inline mirror::Class* ClassTable::TableSlot::Read() const {
91 const uint32_t before = data_.LoadRelaxed();
92 ObjPtr<mirror::Class> const before_ptr(ExtractPtr(before));
93 ObjPtr<mirror::Class> const after_ptr(
94 GcRoot<mirror::Class>(before_ptr).Read<kReadBarrierOption>());
95 if (kReadBarrierOption != kWithoutReadBarrier && before_ptr != after_ptr) {
96 // If another thread raced and updated the reference, do not store the read barrier updated
97 // one.
Orion Hodson4557b382018-01-03 11:47:54 +000098 data_.CompareAndSetStrongRelease(before, Encode(after_ptr, MaskHash(before)));
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080099 }
100 return after_ptr.Ptr();
101}
102
103template<typename Visitor>
104inline void ClassTable::TableSlot::VisitRoot(const Visitor& visitor) const {
105 const uint32_t before = data_.LoadRelaxed();
106 ObjPtr<mirror::Class> before_ptr(ExtractPtr(before));
107 GcRoot<mirror::Class> root(before_ptr);
108 visitor.VisitRoot(root.AddressWithoutBarrier());
109 ObjPtr<mirror::Class> after_ptr(root.Read<kWithoutReadBarrier>());
110 if (before_ptr != after_ptr) {
111 // If another thread raced and updated the reference, do not store the read barrier updated
112 // one.
Orion Hodson4557b382018-01-03 11:47:54 +0000113 data_.CompareAndSetStrongRelease(before, Encode(after_ptr, MaskHash(before)));
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800114 }
115}
116
117inline ObjPtr<mirror::Class> ClassTable::TableSlot::ExtractPtr(uint32_t data) {
118 return reinterpret_cast<mirror::Class*>(data & ~kHashMask);
119}
120
121inline uint32_t ClassTable::TableSlot::Encode(ObjPtr<mirror::Class> klass, uint32_t hash_bits) {
122 DCHECK_LE(hash_bits, kHashMask);
123 return reinterpret_cast<uintptr_t>(klass.Ptr()) | hash_bits;
124}
125
126inline ClassTable::TableSlot::TableSlot(ObjPtr<mirror::Class> klass, uint32_t descriptor_hash)
127 : data_(Encode(klass, MaskHash(descriptor_hash))) {
128 if (kIsDebugBuild) {
129 std::string temp;
130 const uint32_t hash = ComputeModifiedUtf8Hash(klass->GetDescriptor(&temp));
131 CHECK_EQ(descriptor_hash, hash);
132 }
133}
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -0800134
Mathieu Chartier72041a02017-07-14 18:23:25 -0700135template <typename Filter>
136inline void ClassTable::RemoveStrongRoots(const Filter& filter) {
137 WriterMutexLock mu(Thread::Current(), lock_);
138 strong_roots_.erase(std::remove_if(strong_roots_.begin(), strong_roots_.end(), filter),
139 strong_roots_.end());
140}
141
Mathieu Chartiere4275c02015-08-06 15:34:15 -0700142} // namespace art
143
144#endif // ART_RUNTIME_CLASS_TABLE_INL_H_