Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_ |
| 18 | #define ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_ |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 19 | |
| 20 | #include "dex_cache.h" |
| 21 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 22 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 23 | #include "art_method-inl.h" |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 24 | #include "base/casts.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 25 | #include "base/enums.h" |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 26 | #include "base/logging.h" |
Vladimir Marko | 1aea351 | 2016-12-08 11:39:42 +0000 | [diff] [blame] | 27 | #include "dex_file.h" |
Narayan Kamath | d08e39b | 2016-10-19 14:16:35 +0100 | [diff] [blame] | 28 | #include "gc_root.h" |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 29 | #include "mirror/class.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 30 | #include "mirror/call_site.h" |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 31 | #include "mirror/method_type.h" |
Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 32 | #include "runtime.h" |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 33 | #include "obj_ptr.h" |
Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 34 | |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 35 | #include <atomic> |
| 36 | |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 37 | namespace art { |
| 38 | namespace mirror { |
| 39 | |
Vladimir Marko | 1aea351 | 2016-12-08 11:39:42 +0000 | [diff] [blame] | 40 | template <typename T> |
| 41 | inline void NativeDexCachePair<T>::Initialize(std::atomic<NativeDexCachePair<T>>* dex_cache, |
| 42 | PointerSize pointer_size) { |
| 43 | NativeDexCachePair<T> first_elem; |
| 44 | first_elem.object = nullptr; |
| 45 | first_elem.index = InvalidIndexForSlot(0); |
| 46 | DexCache::SetNativePairPtrSize(dex_cache, 0, first_elem, pointer_size); |
| 47 | } |
| 48 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 49 | inline uint32_t DexCache::ClassSize(PointerSize pointer_size) { |
Vladimir Marko | c136312 | 2015-04-09 14:13:13 +0100 | [diff] [blame] | 50 | uint32_t vtable_entries = Object::kVTableLength + 5; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 51 | return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 0, 0, pointer_size); |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Vladimir Marko | bfb80d2 | 2017-02-14 14:08:12 +0000 | [diff] [blame] | 54 | inline uint32_t DexCache::StringSlotIndex(dex::StringIndex string_idx) { |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 55 | DCHECK_LT(string_idx.index_, GetDexFile()->NumStringIds()); |
Vladimir Marko | bfb80d2 | 2017-02-14 14:08:12 +0000 | [diff] [blame] | 56 | const uint32_t slot_idx = string_idx.index_ % kDexCacheStringCacheSize; |
| 57 | DCHECK_LT(slot_idx, NumStrings()); |
| 58 | return slot_idx; |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Vladimir Marko | bfb80d2 | 2017-02-14 14:08:12 +0000 | [diff] [blame] | 61 | inline String* DexCache::GetResolvedString(dex::StringIndex string_idx) { |
| 62 | return GetStrings()[StringSlotIndex(string_idx)].load( |
| 63 | std::memory_order_relaxed).GetObjectForIndex(string_idx.index_); |
| 64 | } |
| 65 | |
| 66 | inline void DexCache::SetResolvedString(dex::StringIndex string_idx, ObjPtr<String> resolved) { |
| 67 | DCHECK(resolved != nullptr); |
| 68 | GetStrings()[StringSlotIndex(string_idx)].store( |
| 69 | StringDexCachePair(resolved, string_idx.index_), std::memory_order_relaxed); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 70 | Runtime* const runtime = Runtime::Current(); |
| 71 | if (UNLIKELY(runtime->IsActiveTransaction())) { |
| 72 | DCHECK(runtime->IsAotCompiler()); |
| 73 | runtime->RecordResolveString(this, string_idx); |
| 74 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 75 | // TODO: Fine-grained marking, so that we don't need to go through all arrays in full. |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 76 | runtime->GetHeap()->WriteBarrierEveryFieldOf(this); |
| 77 | } |
| 78 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 79 | inline void DexCache::ClearString(dex::StringIndex string_idx) { |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 80 | DCHECK(Runtime::Current()->IsAotCompiler()); |
Vladimir Marko | bfb80d2 | 2017-02-14 14:08:12 +0000 | [diff] [blame] | 81 | uint32_t slot_idx = StringSlotIndex(string_idx); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 82 | StringDexCacheType* slot = &GetStrings()[slot_idx]; |
| 83 | // This is racy but should only be called from the transactional interpreter. |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 84 | if (slot->load(std::memory_order_relaxed).index == string_idx.index_) { |
Vladimir Marko | bfb80d2 | 2017-02-14 14:08:12 +0000 | [diff] [blame] | 85 | StringDexCachePair cleared(nullptr, StringDexCachePair::InvalidIndexForSlot(slot_idx)); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 86 | slot->store(cleared, std::memory_order_relaxed); |
| 87 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Vladimir Marko | bfb80d2 | 2017-02-14 14:08:12 +0000 | [diff] [blame] | 90 | inline uint32_t DexCache::TypeSlotIndex(dex::TypeIndex type_idx) { |
| 91 | DCHECK_LT(type_idx.index_, GetDexFile()->NumTypeIds()); |
| 92 | const uint32_t slot_idx = type_idx.index_ % kDexCacheTypeCacheSize; |
| 93 | DCHECK_LT(slot_idx, NumResolvedTypes()); |
| 94 | return slot_idx; |
| 95 | } |
| 96 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 97 | inline Class* DexCache::GetResolvedType(dex::TypeIndex type_idx) { |
Mathieu Chartier | 5df32d3 | 2016-12-06 16:02:27 -0800 | [diff] [blame] | 98 | // It is theorized that a load acquire is not required since obtaining the resolved class will |
Mathieu Chartier | db70ce5 | 2016-12-12 11:06:59 -0800 | [diff] [blame] | 99 | // always have an address dependency or a lock. |
Vladimir Marko | bfb80d2 | 2017-02-14 14:08:12 +0000 | [diff] [blame] | 100 | return GetResolvedTypes()[TypeSlotIndex(type_idx)].load( |
| 101 | std::memory_order_relaxed).GetObjectForIndex(type_idx.index_); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 104 | inline void DexCache::SetResolvedType(dex::TypeIndex type_idx, ObjPtr<Class> resolved) { |
Vladimir Marko | bfb80d2 | 2017-02-14 14:08:12 +0000 | [diff] [blame] | 105 | DCHECK(resolved != nullptr); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 106 | // TODO default transaction support. |
Mathieu Chartier | 5df32d3 | 2016-12-06 16:02:27 -0800 | [diff] [blame] | 107 | // Use a release store for SetResolvedType. This is done to prevent other threads from seeing a |
| 108 | // class but not necessarily seeing the loaded members like the static fields array. |
| 109 | // See b/32075261. |
Vladimir Marko | bfb80d2 | 2017-02-14 14:08:12 +0000 | [diff] [blame] | 110 | GetResolvedTypes()[TypeSlotIndex(type_idx)].store( |
| 111 | TypeDexCachePair(resolved, type_idx.index_), std::memory_order_release); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 112 | // TODO: Fine-grained marking, so that we don't need to go through all arrays in full. |
| 113 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(this); |
| 114 | } |
| 115 | |
Vladimir Marko | bfb80d2 | 2017-02-14 14:08:12 +0000 | [diff] [blame] | 116 | inline void DexCache::ClearResolvedType(dex::TypeIndex type_idx) { |
| 117 | DCHECK(Runtime::Current()->IsAotCompiler()); |
| 118 | uint32_t slot_idx = TypeSlotIndex(type_idx); |
| 119 | TypeDexCacheType* slot = &GetResolvedTypes()[slot_idx]; |
| 120 | // This is racy but should only be called from the single-threaded ImageWriter and tests. |
| 121 | if (slot->load(std::memory_order_relaxed).index == type_idx.index_) { |
| 122 | TypeDexCachePair cleared(nullptr, TypeDexCachePair::InvalidIndexForSlot(slot_idx)); |
| 123 | slot->store(cleared, std::memory_order_relaxed); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | inline uint32_t DexCache::MethodTypeSlotIndex(uint32_t proto_idx) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 128 | DCHECK(Runtime::Current()->IsMethodHandlesEnabled()); |
| 129 | DCHECK_LT(proto_idx, GetDexFile()->NumProtoIds()); |
Vladimir Marko | bfb80d2 | 2017-02-14 14:08:12 +0000 | [diff] [blame] | 130 | const uint32_t slot_idx = proto_idx % kDexCacheMethodTypeCacheSize; |
| 131 | DCHECK_LT(slot_idx, NumResolvedMethodTypes()); |
| 132 | return slot_idx; |
| 133 | } |
| 134 | |
| 135 | inline MethodType* DexCache::GetResolvedMethodType(uint32_t proto_idx) { |
| 136 | return GetResolvedMethodTypes()[MethodTypeSlotIndex(proto_idx)].load( |
| 137 | std::memory_order_relaxed).GetObjectForIndex(proto_idx); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | inline void DexCache::SetResolvedMethodType(uint32_t proto_idx, MethodType* resolved) { |
Vladimir Marko | bfb80d2 | 2017-02-14 14:08:12 +0000 | [diff] [blame] | 141 | DCHECK(resolved != nullptr); |
| 142 | GetResolvedMethodTypes()[MethodTypeSlotIndex(proto_idx)].store( |
| 143 | MethodTypeDexCachePair(resolved, proto_idx), std::memory_order_relaxed); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 144 | // TODO: Fine-grained marking, so that we don't need to go through all arrays in full. |
| 145 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(this); |
| 146 | } |
| 147 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 148 | inline CallSite* DexCache::GetResolvedCallSite(uint32_t call_site_idx) { |
| 149 | DCHECK(Runtime::Current()->IsMethodHandlesEnabled()); |
| 150 | DCHECK_LT(call_site_idx, GetDexFile()->NumCallSiteIds()); |
| 151 | GcRoot<mirror::CallSite>& target = GetResolvedCallSites()[call_site_idx]; |
| 152 | Atomic<GcRoot<mirror::CallSite>>& ref = |
| 153 | reinterpret_cast<Atomic<GcRoot<mirror::CallSite>>&>(target); |
| 154 | return ref.LoadSequentiallyConsistent().Read(); |
| 155 | } |
| 156 | |
| 157 | inline CallSite* DexCache::SetResolvedCallSite(uint32_t call_site_idx, CallSite* call_site) { |
| 158 | DCHECK(Runtime::Current()->IsMethodHandlesEnabled()); |
| 159 | DCHECK_LT(call_site_idx, GetDexFile()->NumCallSiteIds()); |
| 160 | |
| 161 | GcRoot<mirror::CallSite> null_call_site(nullptr); |
| 162 | GcRoot<mirror::CallSite> candidate(call_site); |
| 163 | GcRoot<mirror::CallSite>& target = GetResolvedCallSites()[call_site_idx]; |
| 164 | |
| 165 | // The first assignment for a given call site wins. |
| 166 | Atomic<GcRoot<mirror::CallSite>>& ref = |
| 167 | reinterpret_cast<Atomic<GcRoot<mirror::CallSite>>&>(target); |
| 168 | if (ref.CompareExchangeStrongSequentiallyConsistent(null_call_site, candidate)) { |
| 169 | // TODO: Fine-grained marking, so that we don't need to go through all arrays in full. |
| 170 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(this); |
| 171 | return call_site; |
| 172 | } else { |
| 173 | return target.Read(); |
| 174 | } |
| 175 | } |
| 176 | |
Vladimir Marko | 1aea351 | 2016-12-08 11:39:42 +0000 | [diff] [blame] | 177 | inline uint32_t DexCache::FieldSlotIndex(uint32_t field_idx) { |
| 178 | DCHECK_LT(field_idx, GetDexFile()->NumFieldIds()); |
| 179 | const uint32_t slot_idx = field_idx % kDexCacheFieldCacheSize; |
| 180 | DCHECK_LT(slot_idx, NumResolvedFields()); |
| 181 | return slot_idx; |
| 182 | } |
| 183 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 184 | inline ArtField* DexCache::GetResolvedField(uint32_t field_idx, PointerSize ptr_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 185 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size); |
Vladimir Marko | 1aea351 | 2016-12-08 11:39:42 +0000 | [diff] [blame] | 186 | auto pair = GetNativePairPtrSize(GetResolvedFields(), FieldSlotIndex(field_idx), ptr_size); |
| 187 | return pair.GetObjectForIndex(field_idx); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 190 | inline void DexCache::SetResolvedField(uint32_t field_idx, ArtField* field, PointerSize ptr_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 191 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size); |
Vladimir Marko | 1aea351 | 2016-12-08 11:39:42 +0000 | [diff] [blame] | 192 | DCHECK(field != nullptr); |
| 193 | FieldDexCachePair pair(field, field_idx); |
| 194 | SetNativePairPtrSize(GetResolvedFields(), FieldSlotIndex(field_idx), pair, ptr_size); |
| 195 | } |
| 196 | |
| 197 | inline void DexCache::ClearResolvedField(uint32_t field_idx, PointerSize ptr_size) { |
| 198 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size); |
| 199 | uint32_t slot_idx = FieldSlotIndex(field_idx); |
| 200 | auto* resolved_fields = GetResolvedFields(); |
| 201 | // This is racy but should only be called from the single-threaded ImageWriter. |
| 202 | DCHECK(Runtime::Current()->IsAotCompiler()); |
| 203 | if (GetNativePairPtrSize(resolved_fields, slot_idx, ptr_size).index == field_idx) { |
| 204 | FieldDexCachePair cleared(nullptr, FieldDexCachePair::InvalidIndexForSlot(slot_idx)); |
| 205 | SetNativePairPtrSize(resolved_fields, slot_idx, cleared, ptr_size); |
| 206 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 209 | inline ArtMethod* DexCache::GetResolvedMethod(uint32_t method_idx, PointerSize ptr_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 210 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 211 | DCHECK_LT(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB. |
| 212 | ArtMethod* method = GetElementPtrSize<ArtMethod*>(GetResolvedMethods(), method_idx, ptr_size); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 213 | // Hide resolution trampoline methods from the caller |
| 214 | if (method != nullptr && method->IsRuntimeMethod()) { |
| 215 | DCHECK_EQ(method, Runtime::Current()->GetResolutionMethod()); |
| 216 | return nullptr; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 217 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 218 | return method; |
| 219 | } |
| 220 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 221 | inline void DexCache::SetResolvedMethod(uint32_t method_idx, |
| 222 | ArtMethod* method, |
| 223 | PointerSize ptr_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 224 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 225 | DCHECK_LT(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB. |
| 226 | SetElementPtrSize(GetResolvedMethods(), method_idx, method, ptr_size); |
| 227 | } |
| 228 | |
| 229 | template <typename PtrType> |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 230 | inline PtrType DexCache::GetElementPtrSize(PtrType* ptr_array, size_t idx, PointerSize ptr_size) { |
| 231 | if (ptr_size == PointerSize::k64) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 232 | uint64_t element = reinterpret_cast<const uint64_t*>(ptr_array)[idx]; |
| 233 | return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element)); |
| 234 | } else { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 235 | uint32_t element = reinterpret_cast<const uint32_t*>(ptr_array)[idx]; |
| 236 | return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element)); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | template <typename PtrType> |
| 241 | inline void DexCache::SetElementPtrSize(PtrType* ptr_array, |
| 242 | size_t idx, |
| 243 | PtrType ptr, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 244 | PointerSize ptr_size) { |
| 245 | if (ptr_size == PointerSize::k64) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 246 | reinterpret_cast<uint64_t*>(ptr_array)[idx] = |
| 247 | dchecked_integral_cast<uint64_t>(reinterpret_cast<uintptr_t>(ptr)); |
| 248 | } else { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 249 | reinterpret_cast<uint32_t*>(ptr_array)[idx] = |
| 250 | dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(ptr)); |
| 251 | } |
| 252 | } |
| 253 | |
Vladimir Marko | 1aea351 | 2016-12-08 11:39:42 +0000 | [diff] [blame] | 254 | template <typename T> |
| 255 | NativeDexCachePair<T> DexCache::GetNativePairPtrSize(std::atomic<NativeDexCachePair<T>>* pair_array, |
| 256 | size_t idx, |
| 257 | PointerSize ptr_size) { |
| 258 | if (ptr_size == PointerSize::k64) { |
| 259 | auto* array = reinterpret_cast<std::atomic<ConversionPair64>*>(pair_array); |
| 260 | ConversionPair64 value = AtomicLoadRelaxed16B(&array[idx]); |
| 261 | return NativeDexCachePair<T>(reinterpret_cast64<T*>(value.first), |
| 262 | dchecked_integral_cast<size_t>(value.second)); |
| 263 | } else { |
| 264 | auto* array = reinterpret_cast<std::atomic<ConversionPair32>*>(pair_array); |
| 265 | ConversionPair32 value = array[idx].load(std::memory_order_relaxed); |
| 266 | return NativeDexCachePair<T>(reinterpret_cast<T*>(value.first), value.second); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | template <typename T> |
| 271 | void DexCache::SetNativePairPtrSize(std::atomic<NativeDexCachePair<T>>* pair_array, |
| 272 | size_t idx, |
| 273 | NativeDexCachePair<T> pair, |
| 274 | PointerSize ptr_size) { |
| 275 | if (ptr_size == PointerSize::k64) { |
| 276 | auto* array = reinterpret_cast<std::atomic<ConversionPair64>*>(pair_array); |
| 277 | ConversionPair64 v(reinterpret_cast64<uint64_t>(pair.object), pair.index); |
| 278 | AtomicStoreRelease16B(&array[idx], v); |
| 279 | } else { |
| 280 | auto* array = reinterpret_cast<std::atomic<ConversionPair32>*>(pair_array); |
| 281 | ConversionPair32 v( |
| 282 | dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(pair.object)), |
| 283 | dchecked_integral_cast<uint32_t>(pair.index)); |
| 284 | array[idx].store(v, std::memory_order_release); |
| 285 | } |
| 286 | } |
| 287 | |
Narayan Kamath | d08e39b | 2016-10-19 14:16:35 +0100 | [diff] [blame] | 288 | template <typename T, |
| 289 | ReadBarrierOption kReadBarrierOption, |
| 290 | typename Visitor> |
| 291 | inline void VisitDexCachePairs(std::atomic<DexCachePair<T>>* pairs, |
| 292 | size_t num_pairs, |
| 293 | const Visitor& visitor) |
| 294 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) { |
| 295 | for (size_t i = 0; i < num_pairs; ++i) { |
| 296 | DexCachePair<T> source = pairs[i].load(std::memory_order_relaxed); |
| 297 | // NOTE: We need the "template" keyword here to avoid a compilation |
| 298 | // failure. GcRoot<T> is a template argument-dependent type and we need to |
| 299 | // tell the compiler to treat "Read" as a template rather than a field or |
| 300 | // function. Otherwise, on encountering the "<" token, the compiler would |
| 301 | // treat "Read" as a field. |
Mathieu Chartier | 6b4c287 | 2016-11-01 14:45:26 -0700 | [diff] [blame] | 302 | T* const before = source.object.template Read<kReadBarrierOption>(); |
| 303 | visitor.VisitRootIfNonNull(source.object.AddressWithoutBarrier()); |
| 304 | if (source.object.template Read<kReadBarrierOption>() != before) { |
Narayan Kamath | d08e39b | 2016-10-19 14:16:35 +0100 | [diff] [blame] | 305 | pairs[i].store(source, std::memory_order_relaxed); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 310 | template <bool kVisitNativeRoots, |
| 311 | VerifyObjectFlags kVerifyFlags, |
| 312 | ReadBarrierOption kReadBarrierOption, |
| 313 | typename Visitor> |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 314 | inline void DexCache::VisitReferences(ObjPtr<Class> klass, const Visitor& visitor) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 315 | // Visit instance fields first. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 316 | VisitInstanceFieldsReferences<kVerifyFlags, kReadBarrierOption>(klass, visitor); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 317 | // Visit arrays after. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 318 | if (kVisitNativeRoots) { |
Vladimir Marko | bfb80d2 | 2017-02-14 14:08:12 +0000 | [diff] [blame] | 319 | VisitDexCachePairs<String, kReadBarrierOption, Visitor>( |
Narayan Kamath | d08e39b | 2016-10-19 14:16:35 +0100 | [diff] [blame] | 320 | GetStrings(), NumStrings(), visitor); |
| 321 | |
Vladimir Marko | bfb80d2 | 2017-02-14 14:08:12 +0000 | [diff] [blame] | 322 | VisitDexCachePairs<Class, kReadBarrierOption, Visitor>( |
| 323 | GetResolvedTypes(), NumResolvedTypes(), visitor); |
Narayan Kamath | d08e39b | 2016-10-19 14:16:35 +0100 | [diff] [blame] | 324 | |
Vladimir Marko | bfb80d2 | 2017-02-14 14:08:12 +0000 | [diff] [blame] | 325 | VisitDexCachePairs<MethodType, kReadBarrierOption, Visitor>( |
Narayan Kamath | d08e39b | 2016-10-19 14:16:35 +0100 | [diff] [blame] | 326 | GetResolvedMethodTypes(), NumResolvedMethodTypes(), visitor); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 327 | |
| 328 | GcRoot<mirror::CallSite>* resolved_call_sites = GetResolvedCallSites(); |
| 329 | for (size_t i = 0, num_call_sites = NumResolvedCallSites(); i != num_call_sites; ++i) { |
| 330 | visitor.VisitRootIfNonNull(resolved_call_sites[i].AddressWithoutBarrier()); |
| 331 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 332 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Mathieu Chartier | 60bc39c | 2016-01-27 18:37:48 -0800 | [diff] [blame] | 335 | template <ReadBarrierOption kReadBarrierOption, typename Visitor> |
Vladimir Marko | bfb80d2 | 2017-02-14 14:08:12 +0000 | [diff] [blame] | 336 | inline void DexCache::FixupStrings(StringDexCacheType* dest, const Visitor& visitor) { |
| 337 | StringDexCacheType* src = GetStrings(); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 338 | for (size_t i = 0, count = NumStrings(); i < count; ++i) { |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 339 | StringDexCachePair source = src[i].load(std::memory_order_relaxed); |
Vladimir Marko | bfb80d2 | 2017-02-14 14:08:12 +0000 | [diff] [blame] | 340 | String* ptr = source.object.Read<kReadBarrierOption>(); |
| 341 | String* new_source = visitor(ptr); |
Narayan Kamath | c38a6f8 | 2016-09-29 17:07:20 +0100 | [diff] [blame] | 342 | source.object = GcRoot<String>(new_source); |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 343 | dest[i].store(source, std::memory_order_relaxed); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Mathieu Chartier | 60bc39c | 2016-01-27 18:37:48 -0800 | [diff] [blame] | 347 | template <ReadBarrierOption kReadBarrierOption, typename Visitor> |
Vladimir Marko | bfb80d2 | 2017-02-14 14:08:12 +0000 | [diff] [blame] | 348 | inline void DexCache::FixupResolvedTypes(TypeDexCacheType* dest, const Visitor& visitor) { |
| 349 | TypeDexCacheType* src = GetResolvedTypes(); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 350 | for (size_t i = 0, count = NumResolvedTypes(); i < count; ++i) { |
Vladimir Marko | bfb80d2 | 2017-02-14 14:08:12 +0000 | [diff] [blame] | 351 | TypeDexCachePair source = src[i].load(std::memory_order_relaxed); |
| 352 | Class* ptr = source.object.Read<kReadBarrierOption>(); |
| 353 | Class* new_source = visitor(ptr); |
| 354 | source.object = GcRoot<Class>(new_source); |
| 355 | dest[i].store(source, std::memory_order_relaxed); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
Narayan Kamath | 7fe5658 | 2016-10-14 18:49:12 +0100 | [diff] [blame] | 359 | template <ReadBarrierOption kReadBarrierOption, typename Visitor> |
Vladimir Marko | bfb80d2 | 2017-02-14 14:08:12 +0000 | [diff] [blame] | 360 | inline void DexCache::FixupResolvedMethodTypes(MethodTypeDexCacheType* dest, |
Narayan Kamath | 7fe5658 | 2016-10-14 18:49:12 +0100 | [diff] [blame] | 361 | const Visitor& visitor) { |
Vladimir Marko | bfb80d2 | 2017-02-14 14:08:12 +0000 | [diff] [blame] | 362 | MethodTypeDexCacheType* src = GetResolvedMethodTypes(); |
Narayan Kamath | 7fe5658 | 2016-10-14 18:49:12 +0100 | [diff] [blame] | 363 | for (size_t i = 0, count = NumResolvedMethodTypes(); i < count; ++i) { |
| 364 | MethodTypeDexCachePair source = src[i].load(std::memory_order_relaxed); |
Vladimir Marko | bfb80d2 | 2017-02-14 14:08:12 +0000 | [diff] [blame] | 365 | MethodType* ptr = source.object.Read<kReadBarrierOption>(); |
| 366 | MethodType* new_source = visitor(ptr); |
Narayan Kamath | 7fe5658 | 2016-10-14 18:49:12 +0100 | [diff] [blame] | 367 | source.object = GcRoot<MethodType>(new_source); |
| 368 | dest[i].store(source, std::memory_order_relaxed); |
| 369 | } |
| 370 | } |
| 371 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 372 | template <ReadBarrierOption kReadBarrierOption, typename Visitor> |
| 373 | inline void DexCache::FixupResolvedCallSites(GcRoot<mirror::CallSite>* dest, |
| 374 | const Visitor& visitor) { |
| 375 | GcRoot<mirror::CallSite>* src = GetResolvedCallSites(); |
| 376 | for (size_t i = 0, count = NumResolvedCallSites(); i < count; ++i) { |
| 377 | mirror::CallSite* source = src[i].Read<kReadBarrierOption>(); |
| 378 | mirror::CallSite* new_source = visitor(source); |
| 379 | dest[i] = GcRoot<mirror::CallSite>(new_source); |
| 380 | } |
| 381 | } |
| 382 | |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 383 | } // namespace mirror |
| 384 | } // namespace art |
| 385 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 386 | #endif // ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_ |