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 | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 25 | #include "base/logging.h" |
| 26 | #include "mirror/class.h" |
Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 27 | #include "runtime.h" |
| 28 | |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 29 | namespace art { |
| 30 | namespace mirror { |
| 31 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 32 | inline uint32_t DexCache::ClassSize(size_t pointer_size) { |
Vladimir Marko | c136312 | 2015-04-09 14:13:13 +0100 | [diff] [blame] | 33 | uint32_t vtable_entries = Object::kVTableLength + 5; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 34 | 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] | 35 | } |
| 36 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 37 | inline String* DexCache::GetResolvedString(uint32_t string_idx) { |
| 38 | DCHECK_LT(string_idx, NumStrings()); |
| 39 | return GetStrings()[string_idx].Read(); |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 42 | inline void DexCache::SetResolvedString(uint32_t string_idx, String* resolved) { |
| 43 | DCHECK_LT(string_idx, NumStrings()); |
| 44 | // TODO default transaction support. |
| 45 | GetStrings()[string_idx] = GcRoot<String>(resolved); |
| 46 | // TODO: Fine-grained marking, so that we don't need to go through all arrays in full. |
| 47 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(this); |
| 48 | } |
| 49 | |
| 50 | inline Class* DexCache::GetResolvedType(uint32_t type_idx) { |
| 51 | DCHECK_LT(type_idx, NumResolvedTypes()); |
| 52 | return GetResolvedTypes()[type_idx].Read(); |
| 53 | } |
| 54 | |
| 55 | inline void DexCache::SetResolvedType(uint32_t type_idx, Class* resolved) { |
| 56 | DCHECK_LT(type_idx, NumResolvedTypes()); // NOTE: Unchecked, i.e. not throwing AIOOB. |
| 57 | // TODO default transaction support. |
| 58 | DCHECK(resolved == nullptr || !resolved->IsErroneous()); |
| 59 | GetResolvedTypes()[type_idx] = GcRoot<Class>(resolved); |
| 60 | // TODO: Fine-grained marking, so that we don't need to go through all arrays in full. |
| 61 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(this); |
| 62 | } |
| 63 | |
| 64 | inline ArtField* DexCache::GetResolvedField(uint32_t field_idx, size_t ptr_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 65 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 66 | DCHECK_LT(field_idx, NumResolvedFields()); // NOTE: Unchecked, i.e. not throwing AIOOB. |
| 67 | ArtField* field = GetElementPtrSize(GetResolvedFields(), field_idx, ptr_size); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 68 | if (field == nullptr || field->GetDeclaringClass()->IsErroneous()) { |
| 69 | return nullptr; |
| 70 | } |
| 71 | return field; |
| 72 | } |
| 73 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 74 | inline void DexCache::SetResolvedField(uint32_t field_idx, ArtField* field, size_t ptr_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 75 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 76 | DCHECK_LT(field_idx, NumResolvedFields()); // NOTE: Unchecked, i.e. not throwing AIOOB. |
| 77 | SetElementPtrSize(GetResolvedFields(), field_idx, field, ptr_size); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | inline ArtMethod* DexCache::GetResolvedMethod(uint32_t method_idx, size_t ptr_size) { |
| 81 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 82 | DCHECK_LT(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB. |
| 83 | ArtMethod* method = GetElementPtrSize<ArtMethod*>(GetResolvedMethods(), method_idx, ptr_size); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 84 | // Hide resolution trampoline methods from the caller |
| 85 | if (method != nullptr && method->IsRuntimeMethod()) { |
| 86 | DCHECK_EQ(method, Runtime::Current()->GetResolutionMethod()); |
| 87 | return nullptr; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 88 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 89 | return method; |
| 90 | } |
| 91 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 92 | inline void DexCache::SetResolvedMethod(uint32_t method_idx, ArtMethod* method, size_t ptr_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 93 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 94 | DCHECK_LT(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB. |
| 95 | SetElementPtrSize(GetResolvedMethods(), method_idx, method, ptr_size); |
| 96 | } |
| 97 | |
| 98 | template <typename PtrType> |
| 99 | inline PtrType DexCache::GetElementPtrSize(PtrType* ptr_array, size_t idx, size_t ptr_size) { |
| 100 | if (ptr_size == 8u) { |
| 101 | uint64_t element = reinterpret_cast<const uint64_t*>(ptr_array)[idx]; |
| 102 | return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element)); |
| 103 | } else { |
| 104 | DCHECK_EQ(ptr_size, 4u); |
| 105 | uint32_t element = reinterpret_cast<const uint32_t*>(ptr_array)[idx]; |
| 106 | return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element)); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | template <typename PtrType> |
| 111 | inline void DexCache::SetElementPtrSize(PtrType* ptr_array, |
| 112 | size_t idx, |
| 113 | PtrType ptr, |
| 114 | size_t ptr_size) { |
| 115 | if (ptr_size == 8u) { |
| 116 | reinterpret_cast<uint64_t*>(ptr_array)[idx] = |
| 117 | dchecked_integral_cast<uint64_t>(reinterpret_cast<uintptr_t>(ptr)); |
| 118 | } else { |
| 119 | DCHECK_EQ(ptr_size, 4u); |
| 120 | reinterpret_cast<uint32_t*>(ptr_array)[idx] = |
| 121 | dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(ptr)); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | template <VerifyObjectFlags kVerifyFlags, typename Visitor> |
| 126 | inline void DexCache::VisitReferences(mirror::Class* klass, const Visitor& visitor) { |
| 127 | // Visit instance fields first. |
| 128 | VisitInstanceFieldsReferences(klass, visitor); |
| 129 | // Visit arrays after. |
| 130 | GcRoot<mirror::String>* strings = GetStrings(); |
| 131 | for (size_t i = 0, num_strings = NumStrings(); i != num_strings; ++i) { |
| 132 | visitor.VisitRootIfNonNull(strings[i].AddressWithoutBarrier()); |
| 133 | } |
| 134 | GcRoot<mirror::Class>* resolved_types = GetResolvedTypes(); |
| 135 | for (size_t i = 0, num_types = NumResolvedTypes(); i != num_types; ++i) { |
| 136 | visitor.VisitRootIfNonNull(resolved_types[i].AddressWithoutBarrier()); |
| 137 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame^] | 140 | template <typename Visitor> |
| 141 | inline void DexCache::FixupStrings(GcRoot<mirror::String>* dest, const Visitor& visitor) { |
| 142 | GcRoot<mirror::String>* src = GetStrings(); |
| 143 | for (size_t i = 0, count = NumStrings(); i < count; ++i) { |
| 144 | // TODO: Probably don't need read barrier for most callers. |
| 145 | mirror::String* source = src[i].Read(); |
| 146 | mirror::String* new_source = visitor(source); |
| 147 | if (source != new_source) { |
| 148 | dest[i] = GcRoot<mirror::String>(new_source); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | template <typename Visitor> |
| 154 | inline void DexCache::FixupResolvedTypes(GcRoot<mirror::Class>* dest, const Visitor& visitor) { |
| 155 | GcRoot<mirror::Class>* src = GetResolvedTypes(); |
| 156 | for (size_t i = 0, count = NumResolvedTypes(); i < count; ++i) { |
| 157 | // TODO: Probably don't need read barrier for most callers. |
| 158 | mirror::Class* source = src[i].Read(); |
| 159 | mirror::Class* new_source = visitor(source); |
| 160 | if (source != new_source) { |
| 161 | dest[i] = GcRoot<mirror::Class>(new_source); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 166 | } // namespace mirror |
| 167 | } // namespace art |
| 168 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 169 | #endif // ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_ |