blob: 8e44ee362051d5e981fe8e9a329b4bc6fb790b96 [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
Andreas Gampe88dbad32018-06-26 19:54:12 -070022#include "base/mutex-inl.h"
Vladimir Marko4e7b3c72021-02-23 18:13:47 +000023#include "dex/utf.h"
Andreas Gampe508fdf32017-06-05 16:42:13 -070024#include "gc_root-inl.h"
Vladimir Marko6834d342018-05-25 13:12:09 +010025#include "mirror/class.h"
Vladimir Markoaad75c62016-10-03 08:46:48 +000026#include "oat_file.h"
Vladimir Marko1fe58392019-04-10 16:14:56 +010027#include "obj_ptr-inl.h"
Mathieu Chartiere4275c02015-08-06 15:34:15 -070028
29namespace art {
30
Vladimir Marko4e7b3c72021-02-23 18:13:47 +000031inline 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
38inline uint32_t ClassTable::ClassDescriptorHash::operator()(const DescriptorHashPair& pair) const {
39 DCHECK_EQ(ComputeModifiedUtf8Hash(pair.first), pair.second);
40 return pair.second;
41}
42
43inline 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
58inline 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 Chartiere4275c02015-08-06 15:34:15 -070069template<class Visitor>
70void ClassTable::VisitRoots(Visitor& visitor) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070071 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiere4275c02015-08-06 15:34:15 -070072 for (ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080073 for (TableSlot& table_slot : class_set) {
74 table_slot.VisitRoot(visitor);
Mathieu Chartiere4275c02015-08-06 15:34:15 -070075 }
76 }
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -070077 for (GcRoot<mirror::Object>& root : strong_roots_) {
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -080078 visitor.VisitRoot(root.AddressWithoutBarrier());
79 }
Vladimir Markoaad75c62016-10-03 08:46:48 +000080 for (const OatFile* oat_file : oat_files_) {
81 for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) {
82 visitor.VisitRootIfNonNull(root.AddressWithoutBarrier());
83 }
84 }
Mathieu Chartiere4275c02015-08-06 15:34:15 -070085}
86
87template<class Visitor>
88void ClassTable::VisitRoots(const Visitor& visitor) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070089 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiere4275c02015-08-06 15:34:15 -070090 for (ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080091 for (TableSlot& table_slot : class_set) {
92 table_slot.VisitRoot(visitor);
Mathieu Chartiere4275c02015-08-06 15:34:15 -070093 }
94 }
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -070095 for (GcRoot<mirror::Object>& root : strong_roots_) {
Mathieu Chartier00310e02015-10-17 12:46:42 -070096 visitor.VisitRoot(root.AddressWithoutBarrier());
97 }
Vladimir Markoaad75c62016-10-03 08:46:48 +000098 for (const OatFile* oat_file : oat_files_) {
99 for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) {
100 visitor.VisitRootIfNonNull(root.AddressWithoutBarrier());
101 }
102 }
Mathieu Chartiere4275c02015-08-06 15:34:15 -0700103}
104
Alexey Grebenkinbe4c2bd2018-02-01 19:09:59 +0300105template <typename Visitor, ReadBarrierOption kReadBarrierOption>
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -0800106bool ClassTable::Visit(Visitor& visitor) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700107 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -0800108 for (ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800109 for (TableSlot& table_slot : class_set) {
Alexey Grebenkinbe4c2bd2018-02-01 19:09:59 +0300110 if (!visitor(table_slot.Read<kReadBarrierOption>())) {
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -0800111 return false;
112 }
113 }
114 }
115 return true;
116}
117
Alexey Grebenkinbe4c2bd2018-02-01 19:09:59 +0300118template <typename Visitor, ReadBarrierOption kReadBarrierOption>
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800119bool 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 Grebenkinbe4c2bd2018-02-01 19:09:59 +0300123 if (!visitor(table_slot.Read<kReadBarrierOption>())) {
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800124 return false;
125 }
126 }
127 }
128 return true;
129}
130
Vladimir Markoc6934e32019-04-10 11:40:01 +0100131inline bool ClassTable::TableSlot::IsNull() const {
132 return Read<kWithoutReadBarrier>() == nullptr;
133}
134
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800135template<ReadBarrierOption kReadBarrierOption>
Vladimir Marko1fe58392019-04-10 16:14:56 +0100136inline ObjPtr<mirror::Class> ClassTable::TableSlot::Read() const {
Orion Hodson88591fe2018-03-06 13:35:43 +0000137 const uint32_t before = data_.load(std::memory_order_relaxed);
Vladimir Marko0984e482019-03-27 16:41:41 +0000138 const ObjPtr<mirror::Class> before_ptr(ExtractPtr(before));
139 const ObjPtr<mirror::Class> after_ptr(
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800140 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 Hodson4557b382018-01-03 11:47:54 +0000144 data_.CompareAndSetStrongRelease(before, Encode(after_ptr, MaskHash(before)));
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800145 }
Vladimir Marko1fe58392019-04-10 16:14:56 +0100146 return after_ptr;
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800147}
148
149template<typename Visitor>
150inline void ClassTable::TableSlot::VisitRoot(const Visitor& visitor) const {
Orion Hodson88591fe2018-03-06 13:35:43 +0000151 const uint32_t before = data_.load(std::memory_order_relaxed);
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800152 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 Hodson4557b382018-01-03 11:47:54 +0000159 data_.CompareAndSetStrongRelease(before, Encode(after_ptr, MaskHash(before)));
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800160 }
161}
162
163inline ObjPtr<mirror::Class> ClassTable::TableSlot::ExtractPtr(uint32_t data) {
164 return reinterpret_cast<mirror::Class*>(data & ~kHashMask);
165}
166
167inline 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
172inline ClassTable::TableSlot::TableSlot(ObjPtr<mirror::Class> klass, uint32_t descriptor_hash)
173 : data_(Encode(klass, MaskHash(descriptor_hash))) {
Vladimir Markoc6934e32019-04-10 11:40:01 +0100174 DCHECK_EQ(descriptor_hash, HashDescriptor(klass));
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800175}
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -0800176
Mathieu Chartier72041a02017-07-14 18:23:25 -0700177template <typename Filter>
178inline 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 Marko1dab5752021-02-25 15:41:33 +0000184inline 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 Chartiere4275c02015-08-06 15:34:15 -0700191} // namespace art
192
193#endif // ART_RUNTIME_CLASS_TABLE_INL_H_