blob: d07ad33d21977d255fd5c5b234f64cb8a4ca3c10 [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"
Andreas Gampe508fdf32017-06-05 16:42:13 -070023#include "gc_root-inl.h"
Vladimir Marko6834d342018-05-25 13:12:09 +010024#include "mirror/class.h"
Vladimir Markoaad75c62016-10-03 08:46:48 +000025#include "oat_file.h"
Mathieu Chartiere4275c02015-08-06 15:34:15 -070026
27namespace art {
28
29template<class Visitor>
30void ClassTable::VisitRoots(Visitor& visitor) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070031 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiere4275c02015-08-06 15:34:15 -070032 for (ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080033 for (TableSlot& table_slot : class_set) {
34 table_slot.VisitRoot(visitor);
Mathieu Chartiere4275c02015-08-06 15:34:15 -070035 }
36 }
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -070037 for (GcRoot<mirror::Object>& root : strong_roots_) {
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -080038 visitor.VisitRoot(root.AddressWithoutBarrier());
39 }
Vladimir Markoaad75c62016-10-03 08:46:48 +000040 for (const OatFile* oat_file : oat_files_) {
41 for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) {
42 visitor.VisitRootIfNonNull(root.AddressWithoutBarrier());
43 }
44 }
Mathieu Chartiere4275c02015-08-06 15:34:15 -070045}
46
47template<class Visitor>
48void ClassTable::VisitRoots(const Visitor& visitor) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070049 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiere4275c02015-08-06 15:34:15 -070050 for (ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080051 for (TableSlot& table_slot : class_set) {
52 table_slot.VisitRoot(visitor);
Mathieu Chartiere4275c02015-08-06 15:34:15 -070053 }
54 }
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -070055 for (GcRoot<mirror::Object>& root : strong_roots_) {
Mathieu Chartier00310e02015-10-17 12:46:42 -070056 visitor.VisitRoot(root.AddressWithoutBarrier());
57 }
Vladimir Markoaad75c62016-10-03 08:46:48 +000058 for (const OatFile* oat_file : oat_files_) {
59 for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) {
60 visitor.VisitRootIfNonNull(root.AddressWithoutBarrier());
61 }
62 }
Mathieu Chartiere4275c02015-08-06 15:34:15 -070063}
64
Alexey Grebenkinbe4c2bd2018-02-01 19:09:59 +030065template <typename Visitor, ReadBarrierOption kReadBarrierOption>
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -080066bool ClassTable::Visit(Visitor& visitor) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070067 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -080068 for (ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080069 for (TableSlot& table_slot : class_set) {
Alexey Grebenkinbe4c2bd2018-02-01 19:09:59 +030070 if (!visitor(table_slot.Read<kReadBarrierOption>())) {
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -080071 return false;
72 }
73 }
74 }
75 return true;
76}
77
Alexey Grebenkinbe4c2bd2018-02-01 19:09:59 +030078template <typename Visitor, ReadBarrierOption kReadBarrierOption>
Mathieu Chartierdb70ce52016-12-12 11:06:59 -080079bool ClassTable::Visit(const Visitor& visitor) {
80 ReaderMutexLock mu(Thread::Current(), lock_);
81 for (ClassSet& class_set : classes_) {
82 for (TableSlot& table_slot : class_set) {
Alexey Grebenkinbe4c2bd2018-02-01 19:09:59 +030083 if (!visitor(table_slot.Read<kReadBarrierOption>())) {
Mathieu Chartierdb70ce52016-12-12 11:06:59 -080084 return false;
85 }
86 }
87 }
88 return true;
89}
90
Vladimir Markoc6934e32019-04-10 11:40:01 +010091inline bool ClassTable::TableSlot::IsNull() const {
92 return Read<kWithoutReadBarrier>() == nullptr;
93}
94
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080095template<ReadBarrierOption kReadBarrierOption>
96inline mirror::Class* ClassTable::TableSlot::Read() const {
Orion Hodson88591fe2018-03-06 13:35:43 +000097 const uint32_t before = data_.load(std::memory_order_relaxed);
Vladimir Marko0984e482019-03-27 16:41:41 +000098 const ObjPtr<mirror::Class> before_ptr(ExtractPtr(before));
99 const ObjPtr<mirror::Class> after_ptr(
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800100 GcRoot<mirror::Class>(before_ptr).Read<kReadBarrierOption>());
101 if (kReadBarrierOption != kWithoutReadBarrier && before_ptr != after_ptr) {
102 // If another thread raced and updated the reference, do not store the read barrier updated
103 // one.
Orion Hodson4557b382018-01-03 11:47:54 +0000104 data_.CompareAndSetStrongRelease(before, Encode(after_ptr, MaskHash(before)));
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800105 }
106 return after_ptr.Ptr();
107}
108
109template<typename Visitor>
110inline void ClassTable::TableSlot::VisitRoot(const Visitor& visitor) const {
Orion Hodson88591fe2018-03-06 13:35:43 +0000111 const uint32_t before = data_.load(std::memory_order_relaxed);
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800112 ObjPtr<mirror::Class> before_ptr(ExtractPtr(before));
113 GcRoot<mirror::Class> root(before_ptr);
114 visitor.VisitRoot(root.AddressWithoutBarrier());
115 ObjPtr<mirror::Class> after_ptr(root.Read<kWithoutReadBarrier>());
116 if (before_ptr != after_ptr) {
117 // If another thread raced and updated the reference, do not store the read barrier updated
118 // one.
Orion Hodson4557b382018-01-03 11:47:54 +0000119 data_.CompareAndSetStrongRelease(before, Encode(after_ptr, MaskHash(before)));
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800120 }
121}
122
123inline ObjPtr<mirror::Class> ClassTable::TableSlot::ExtractPtr(uint32_t data) {
124 return reinterpret_cast<mirror::Class*>(data & ~kHashMask);
125}
126
127inline uint32_t ClassTable::TableSlot::Encode(ObjPtr<mirror::Class> klass, uint32_t hash_bits) {
128 DCHECK_LE(hash_bits, kHashMask);
129 return reinterpret_cast<uintptr_t>(klass.Ptr()) | hash_bits;
130}
131
132inline ClassTable::TableSlot::TableSlot(ObjPtr<mirror::Class> klass, uint32_t descriptor_hash)
133 : data_(Encode(klass, MaskHash(descriptor_hash))) {
Vladimir Markoc6934e32019-04-10 11:40:01 +0100134 DCHECK_EQ(descriptor_hash, HashDescriptor(klass));
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800135}
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -0800136
Mathieu Chartier72041a02017-07-14 18:23:25 -0700137template <typename Filter>
138inline 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
Mathieu Chartiere4275c02015-08-06 15:34:15 -0700144} // namespace art
145
146#endif // ART_RUNTIME_CLASS_TABLE_INL_H_