blob: 359462d21a862b3127f06214a3250acaac1def1e [file] [log] [blame]
Ian Rogers39ebcb82013-05-30 16:57:23 -07001/*
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 Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_
18#define ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_
Ian Rogers39ebcb82013-05-30 16:57:23 -070019
20#include "dex_cache.h"
21
Mathieu Chartierc7853442015-03-27 14:35:38 -070022#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070023#include "art_method-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010024#include "base/casts.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070025#include "base/enums.h"
Andreas Gampeaa910d52014-07-30 18:59:05 -070026#include "base/logging.h"
27#include "mirror/class.h"
Mathieu Chartierbc56fc32014-06-03 15:37:03 -070028#include "runtime.h"
29
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070030#include <atomic>
31
Ian Rogers39ebcb82013-05-30 16:57:23 -070032namespace art {
33namespace mirror {
34
Andreas Gampe542451c2016-07-26 09:02:02 -070035inline uint32_t DexCache::ClassSize(PointerSize pointer_size) {
Vladimir Markoc1363122015-04-09 14:13:13 +010036 uint32_t vtable_entries = Object::kVTableLength + 5;
Mathieu Chartiere401d142015-04-22 13:56:20 -070037 return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 0, 0, pointer_size);
Ian Rogers39ebcb82013-05-30 16:57:23 -070038}
39
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070040inline mirror::String* DexCache::GetResolvedString(uint32_t string_idx) {
41 DCHECK_LT(string_idx, GetDexFile()->NumStringIds());
Narayan Kamathc38a6f82016-09-29 17:07:20 +010042 return StringDexCachePair::Lookup(GetStrings(), string_idx, NumStrings()).Read();
Andreas Gampeaa910d52014-07-30 18:59:05 -070043}
44
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070045inline void DexCache::SetResolvedString(uint32_t string_idx, mirror::String* resolved) {
46 DCHECK_LT(string_idx % NumStrings(), NumStrings());
Mathieu Chartierbb816d62016-09-07 10:17:46 -070047 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 Marko05792b92015-08-03 11:56:49 +010055 // TODO: Fine-grained marking, so that we don't need to go through all arrays in full.
Mathieu Chartierbb816d62016-09-07 10:17:46 -070056 runtime->GetHeap()->WriteBarrierEveryFieldOf(this);
57}
58
59inline 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.
Narayan Kamathc38a6f82016-09-29 17:07:20 +010064 if (slot->load(std::memory_order_relaxed).index == string_idx) {
Mathieu Chartierbb816d62016-09-07 10:17:46 -070065 StringDexCachePair cleared(
66 nullptr,
Narayan Kamathc38a6f82016-09-29 17:07:20 +010067 StringDexCachePair::InvalidIndexForSlot(slot_idx));
Mathieu Chartierbb816d62016-09-07 10:17:46 -070068 slot->store(cleared, std::memory_order_relaxed);
69 }
Vladimir Marko05792b92015-08-03 11:56:49 +010070}
71
72inline Class* DexCache::GetResolvedType(uint32_t type_idx) {
73 DCHECK_LT(type_idx, NumResolvedTypes());
74 return GetResolvedTypes()[type_idx].Read();
75}
76
77inline 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 Marko05792b92015-08-03 11:56:49 +010080 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 Gampe542451c2016-07-26 09:02:02 -070085inline ArtField* DexCache::GetResolvedField(uint32_t field_idx, PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070086 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +010087 DCHECK_LT(field_idx, NumResolvedFields()); // NOTE: Unchecked, i.e. not throwing AIOOB.
88 ArtField* field = GetElementPtrSize(GetResolvedFields(), field_idx, ptr_size);
Mathieu Chartierc7853442015-03-27 14:35:38 -070089 if (field == nullptr || field->GetDeclaringClass()->IsErroneous()) {
90 return nullptr;
91 }
92 return field;
93}
94
Andreas Gampe542451c2016-07-26 09:02:02 -070095inline void DexCache::SetResolvedField(uint32_t field_idx, ArtField* field, PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070096 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +010097 DCHECK_LT(field_idx, NumResolvedFields()); // NOTE: Unchecked, i.e. not throwing AIOOB.
98 SetElementPtrSize(GetResolvedFields(), field_idx, field, ptr_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -070099}
100
Andreas Gampe542451c2016-07-26 09:02:02 -0700101inline ArtMethod* DexCache::GetResolvedMethod(uint32_t method_idx, PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700102 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100103 DCHECK_LT(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB.
104 ArtMethod* method = GetElementPtrSize<ArtMethod*>(GetResolvedMethods(), method_idx, ptr_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700105 // Hide resolution trampoline methods from the caller
106 if (method != nullptr && method->IsRuntimeMethod()) {
107 DCHECK_EQ(method, Runtime::Current()->GetResolutionMethod());
108 return nullptr;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700109 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700110 return method;
111}
112
Andreas Gampe542451c2016-07-26 09:02:02 -0700113inline void DexCache::SetResolvedMethod(uint32_t method_idx,
114 ArtMethod* method,
115 PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700116 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100117 DCHECK_LT(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB.
118 SetElementPtrSize(GetResolvedMethods(), method_idx, method, ptr_size);
119}
120
121template <typename PtrType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700122inline PtrType DexCache::GetElementPtrSize(PtrType* ptr_array, size_t idx, PointerSize ptr_size) {
123 if (ptr_size == PointerSize::k64) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100124 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 Marko05792b92015-08-03 11:56:49 +0100127 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
132template <typename PtrType>
133inline void DexCache::SetElementPtrSize(PtrType* ptr_array,
134 size_t idx,
135 PtrType ptr,
Andreas Gampe542451c2016-07-26 09:02:02 -0700136 PointerSize ptr_size) {
137 if (ptr_size == PointerSize::k64) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100138 reinterpret_cast<uint64_t*>(ptr_array)[idx] =
139 dchecked_integral_cast<uint64_t>(reinterpret_cast<uintptr_t>(ptr));
140 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +0100141 reinterpret_cast<uint32_t*>(ptr_array)[idx] =
142 dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(ptr));
143 }
144}
145
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800146template <bool kVisitNativeRoots,
147 VerifyObjectFlags kVerifyFlags,
148 ReadBarrierOption kReadBarrierOption,
149 typename Visitor>
Vladimir Marko05792b92015-08-03 11:56:49 +0100150inline void DexCache::VisitReferences(mirror::Class* klass, const Visitor& visitor) {
151 // Visit instance fields first.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800152 VisitInstanceFieldsReferences<kVerifyFlags, kReadBarrierOption>(klass, visitor);
Vladimir Marko05792b92015-08-03 11:56:49 +0100153 // Visit arrays after.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800154 if (kVisitNativeRoots) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700155 mirror::StringDexCacheType* strings = GetStrings();
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800156 for (size_t i = 0, num_strings = NumStrings(); i != num_strings; ++i) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700157 StringDexCachePair source = strings[i].load(std::memory_order_relaxed);
Narayan Kamathc38a6f82016-09-29 17:07:20 +0100158 mirror::String* before = source.object.Read<kReadBarrierOption>();
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700159 GcRoot<mirror::String> root(before);
160 visitor.VisitRootIfNonNull(root.AddressWithoutBarrier());
161 if (root.Read() != before) {
Narayan Kamathc38a6f82016-09-29 17:07:20 +0100162 source.object = GcRoot<String>(root.Read());
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700163 strings[i].store(source, std::memory_order_relaxed);
164 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800165 }
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 Marko05792b92015-08-03 11:56:49 +0100170 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700171}
172
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800173template <ReadBarrierOption kReadBarrierOption, typename Visitor>
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700174inline void DexCache::FixupStrings(mirror::StringDexCacheType* dest, const Visitor& visitor) {
175 mirror::StringDexCacheType* src = GetStrings();
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800176 for (size_t i = 0, count = NumStrings(); i < count; ++i) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700177 StringDexCachePair source = src[i].load(std::memory_order_relaxed);
Narayan Kamathc38a6f82016-09-29 17:07:20 +0100178 mirror::String* ptr = source.object.Read<kReadBarrierOption>();
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700179 mirror::String* new_source = visitor(ptr);
Narayan Kamathc38a6f82016-09-29 17:07:20 +0100180 source.object = GcRoot<String>(new_source);
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700181 dest[i].store(source, std::memory_order_relaxed);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800182 }
183}
184
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800185template <ReadBarrierOption kReadBarrierOption, typename Visitor>
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800186inline 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 Chartier60bc39c2016-01-27 18:37:48 -0800189 mirror::Class* source = src[i].Read<kReadBarrierOption>();
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800190 mirror::Class* new_source = visitor(source);
Nicolas Geoffrayd0668f22016-04-26 18:30:31 +0100191 dest[i] = GcRoot<mirror::Class>(new_source);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800192 }
193}
194
Ian Rogers39ebcb82013-05-30 16:57:23 -0700195} // namespace mirror
196} // namespace art
197
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700198#endif // ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_