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" |
| 27 | #include "mirror/class.h" |
Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 28 | #include "runtime.h" |
| 29 | |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 30 | #include <atomic> |
| 31 | |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 32 | namespace art { |
| 33 | namespace mirror { |
| 34 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 35 | inline uint32_t DexCache::ClassSize(PointerSize pointer_size) { |
Vladimir Marko | c136312 | 2015-04-09 14:13:13 +0100 | [diff] [blame] | 36 | uint32_t vtable_entries = Object::kVTableLength + 5; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 37 | 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] | 38 | } |
| 39 | |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 40 | inline mirror::String* DexCache::GetResolvedString(uint32_t string_idx) { |
| 41 | DCHECK_LT(string_idx, GetDexFile()->NumStringIds()); |
| 42 | return StringDexCachePair::LookupString(GetStrings(), string_idx, NumStrings()).Read(); |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 45 | inline void DexCache::SetResolvedString(uint32_t string_idx, mirror::String* resolved) { |
| 46 | DCHECK_LT(string_idx % NumStrings(), NumStrings()); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 47 | GetStrings()[string_idx % NumStrings()].store( |
| 48 | StringDexCachePair(resolved, string_idx), |
| 49 | std::memory_order_relaxed); |
| 50 | Runtime* const runtime = Runtime::Current(); |
| 51 | if (UNLIKELY(runtime->IsActiveTransaction())) { |
| 52 | DCHECK(runtime->IsAotCompiler()); |
| 53 | runtime->RecordResolveString(this, string_idx); |
| 54 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 55 | // 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] | 56 | runtime->GetHeap()->WriteBarrierEveryFieldOf(this); |
| 57 | } |
| 58 | |
| 59 | inline void DexCache::ClearString(uint32_t string_idx) { |
| 60 | const uint32_t slot_idx = string_idx % NumStrings(); |
| 61 | DCHECK(Runtime::Current()->IsAotCompiler()); |
| 62 | StringDexCacheType* slot = &GetStrings()[slot_idx]; |
| 63 | // This is racy but should only be called from the transactional interpreter. |
| 64 | if (slot->load(std::memory_order_relaxed).string_index == string_idx) { |
| 65 | StringDexCachePair cleared( |
| 66 | nullptr, |
| 67 | StringDexCachePair::InvalidStringIndexForSlot(slot_idx)); |
| 68 | slot->store(cleared, std::memory_order_relaxed); |
| 69 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | inline Class* DexCache::GetResolvedType(uint32_t type_idx) { |
| 73 | DCHECK_LT(type_idx, NumResolvedTypes()); |
| 74 | return GetResolvedTypes()[type_idx].Read(); |
| 75 | } |
| 76 | |
| 77 | inline void DexCache::SetResolvedType(uint32_t type_idx, Class* resolved) { |
| 78 | DCHECK_LT(type_idx, NumResolvedTypes()); // NOTE: Unchecked, i.e. not throwing AIOOB. |
| 79 | // TODO default transaction support. |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 80 | GetResolvedTypes()[type_idx] = GcRoot<Class>(resolved); |
| 81 | // TODO: Fine-grained marking, so that we don't need to go through all arrays in full. |
| 82 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(this); |
| 83 | } |
| 84 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 85 | inline ArtField* DexCache::GetResolvedField(uint32_t field_idx, PointerSize ptr_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 86 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 87 | DCHECK_LT(field_idx, NumResolvedFields()); // NOTE: Unchecked, i.e. not throwing AIOOB. |
| 88 | ArtField* field = GetElementPtrSize(GetResolvedFields(), field_idx, ptr_size); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 89 | if (field == nullptr || field->GetDeclaringClass()->IsErroneous()) { |
| 90 | return nullptr; |
| 91 | } |
| 92 | return field; |
| 93 | } |
| 94 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 95 | 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] | 96 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 97 | DCHECK_LT(field_idx, NumResolvedFields()); // NOTE: Unchecked, i.e. not throwing AIOOB. |
| 98 | SetElementPtrSize(GetResolvedFields(), field_idx, field, ptr_size); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 101 | inline ArtMethod* DexCache::GetResolvedMethod(uint32_t method_idx, PointerSize ptr_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 102 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 103 | DCHECK_LT(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB. |
| 104 | ArtMethod* method = GetElementPtrSize<ArtMethod*>(GetResolvedMethods(), method_idx, ptr_size); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 105 | // Hide resolution trampoline methods from the caller |
| 106 | if (method != nullptr && method->IsRuntimeMethod()) { |
| 107 | DCHECK_EQ(method, Runtime::Current()->GetResolutionMethod()); |
| 108 | return nullptr; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 109 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 110 | return method; |
| 111 | } |
| 112 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 113 | inline void DexCache::SetResolvedMethod(uint32_t method_idx, |
| 114 | ArtMethod* method, |
| 115 | PointerSize ptr_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 116 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 117 | DCHECK_LT(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB. |
| 118 | SetElementPtrSize(GetResolvedMethods(), method_idx, method, ptr_size); |
| 119 | } |
| 120 | |
| 121 | template <typename PtrType> |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 122 | inline PtrType DexCache::GetElementPtrSize(PtrType* ptr_array, size_t idx, PointerSize ptr_size) { |
| 123 | if (ptr_size == PointerSize::k64) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 124 | uint64_t element = reinterpret_cast<const uint64_t*>(ptr_array)[idx]; |
| 125 | return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element)); |
| 126 | } else { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 127 | uint32_t element = reinterpret_cast<const uint32_t*>(ptr_array)[idx]; |
| 128 | return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element)); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | template <typename PtrType> |
| 133 | inline void DexCache::SetElementPtrSize(PtrType* ptr_array, |
| 134 | size_t idx, |
| 135 | PtrType ptr, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 136 | PointerSize ptr_size) { |
| 137 | if (ptr_size == PointerSize::k64) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 138 | reinterpret_cast<uint64_t*>(ptr_array)[idx] = |
| 139 | dchecked_integral_cast<uint64_t>(reinterpret_cast<uintptr_t>(ptr)); |
| 140 | } else { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 141 | reinterpret_cast<uint32_t*>(ptr_array)[idx] = |
| 142 | dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(ptr)); |
| 143 | } |
| 144 | } |
| 145 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 146 | template <bool kVisitNativeRoots, |
| 147 | VerifyObjectFlags kVerifyFlags, |
| 148 | ReadBarrierOption kReadBarrierOption, |
| 149 | typename Visitor> |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 150 | inline void DexCache::VisitReferences(mirror::Class* klass, const Visitor& visitor) { |
| 151 | // Visit instance fields first. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 152 | VisitInstanceFieldsReferences<kVerifyFlags, kReadBarrierOption>(klass, visitor); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 153 | // Visit arrays after. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 154 | if (kVisitNativeRoots) { |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 155 | mirror::StringDexCacheType* strings = GetStrings(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 156 | for (size_t i = 0, num_strings = NumStrings(); i != num_strings; ++i) { |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 157 | StringDexCachePair source = strings[i].load(std::memory_order_relaxed); |
| 158 | mirror::String* before = source.string_pointer.Read<kReadBarrierOption>(); |
| 159 | GcRoot<mirror::String> root(before); |
| 160 | visitor.VisitRootIfNonNull(root.AddressWithoutBarrier()); |
| 161 | if (root.Read() != before) { |
| 162 | source.string_pointer = GcRoot<String>(root.Read()); |
| 163 | strings[i].store(source, std::memory_order_relaxed); |
| 164 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 165 | } |
| 166 | GcRoot<mirror::Class>* resolved_types = GetResolvedTypes(); |
| 167 | for (size_t i = 0, num_types = NumResolvedTypes(); i != num_types; ++i) { |
| 168 | visitor.VisitRootIfNonNull(resolved_types[i].AddressWithoutBarrier()); |
| 169 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 170 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Mathieu Chartier | 60bc39c | 2016-01-27 18:37:48 -0800 | [diff] [blame] | 173 | template <ReadBarrierOption kReadBarrierOption, typename Visitor> |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 174 | inline void DexCache::FixupStrings(mirror::StringDexCacheType* dest, const Visitor& visitor) { |
| 175 | mirror::StringDexCacheType* src = GetStrings(); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 176 | for (size_t i = 0, count = NumStrings(); i < count; ++i) { |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 177 | StringDexCachePair source = src[i].load(std::memory_order_relaxed); |
| 178 | mirror::String* ptr = source.string_pointer.Read<kReadBarrierOption>(); |
| 179 | mirror::String* new_source = visitor(ptr); |
| 180 | source.string_pointer = GcRoot<String>(new_source); |
| 181 | dest[i].store(source, std::memory_order_relaxed); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Mathieu Chartier | 60bc39c | 2016-01-27 18:37:48 -0800 | [diff] [blame] | 185 | template <ReadBarrierOption kReadBarrierOption, typename Visitor> |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 186 | inline void DexCache::FixupResolvedTypes(GcRoot<mirror::Class>* dest, const Visitor& visitor) { |
| 187 | GcRoot<mirror::Class>* src = GetResolvedTypes(); |
| 188 | for (size_t i = 0, count = NumResolvedTypes(); i < count; ++i) { |
Mathieu Chartier | 60bc39c | 2016-01-27 18:37:48 -0800 | [diff] [blame] | 189 | mirror::Class* source = src[i].Read<kReadBarrierOption>(); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 190 | mirror::Class* new_source = visitor(source); |
Nicolas Geoffray | d0668f2 | 2016-04-26 18:30:31 +0100 | [diff] [blame] | 191 | dest[i] = GcRoot<mirror::Class>(new_source); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 195 | } // namespace mirror |
| 196 | } // namespace art |
| 197 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 198 | #endif // ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_ |