blob: 0fa6d6bb21b289b043a543a4eb33357dd6a9257c [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"
Narayan Kamath25352fc2016-08-03 12:46:58 +010028#include "mirror/method_type.h"
Mathieu Chartierbc56fc32014-06-03 15:37:03 -070029#include "runtime.h"
30
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070031#include <atomic>
32
Ian Rogers39ebcb82013-05-30 16:57:23 -070033namespace art {
34namespace mirror {
35
Andreas Gampe542451c2016-07-26 09:02:02 -070036inline uint32_t DexCache::ClassSize(PointerSize pointer_size) {
Vladimir Markoc1363122015-04-09 14:13:13 +010037 uint32_t vtable_entries = Object::kVTableLength + 5;
Mathieu Chartiere401d142015-04-22 13:56:20 -070038 return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 0, 0, pointer_size);
Ian Rogers39ebcb82013-05-30 16:57:23 -070039}
40
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070041inline mirror::String* DexCache::GetResolvedString(uint32_t string_idx) {
42 DCHECK_LT(string_idx, GetDexFile()->NumStringIds());
Narayan Kamathc38a6f82016-09-29 17:07:20 +010043 return StringDexCachePair::Lookup(GetStrings(), string_idx, NumStrings()).Read();
Andreas Gampeaa910d52014-07-30 18:59:05 -070044}
45
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070046inline void DexCache::SetResolvedString(uint32_t string_idx, mirror::String* resolved) {
47 DCHECK_LT(string_idx % NumStrings(), NumStrings());
Mathieu Chartierbb816d62016-09-07 10:17:46 -070048 GetStrings()[string_idx % NumStrings()].store(
49 StringDexCachePair(resolved, string_idx),
50 std::memory_order_relaxed);
51 Runtime* const runtime = Runtime::Current();
52 if (UNLIKELY(runtime->IsActiveTransaction())) {
53 DCHECK(runtime->IsAotCompiler());
54 runtime->RecordResolveString(this, string_idx);
55 }
Vladimir Marko05792b92015-08-03 11:56:49 +010056 // TODO: Fine-grained marking, so that we don't need to go through all arrays in full.
Mathieu Chartierbb816d62016-09-07 10:17:46 -070057 runtime->GetHeap()->WriteBarrierEveryFieldOf(this);
58}
59
60inline void DexCache::ClearString(uint32_t string_idx) {
61 const uint32_t slot_idx = string_idx % NumStrings();
62 DCHECK(Runtime::Current()->IsAotCompiler());
63 StringDexCacheType* slot = &GetStrings()[slot_idx];
64 // This is racy but should only be called from the transactional interpreter.
Narayan Kamathc38a6f82016-09-29 17:07:20 +010065 if (slot->load(std::memory_order_relaxed).index == string_idx) {
Mathieu Chartierbb816d62016-09-07 10:17:46 -070066 StringDexCachePair cleared(
67 nullptr,
Narayan Kamathc38a6f82016-09-29 17:07:20 +010068 StringDexCachePair::InvalidIndexForSlot(slot_idx));
Mathieu Chartierbb816d62016-09-07 10:17:46 -070069 slot->store(cleared, std::memory_order_relaxed);
70 }
Vladimir Marko05792b92015-08-03 11:56:49 +010071}
72
73inline Class* DexCache::GetResolvedType(uint32_t type_idx) {
74 DCHECK_LT(type_idx, NumResolvedTypes());
75 return GetResolvedTypes()[type_idx].Read();
76}
77
78inline void DexCache::SetResolvedType(uint32_t type_idx, Class* resolved) {
79 DCHECK_LT(type_idx, NumResolvedTypes()); // NOTE: Unchecked, i.e. not throwing AIOOB.
80 // TODO default transaction support.
Vladimir Marko05792b92015-08-03 11:56:49 +010081 GetResolvedTypes()[type_idx] = GcRoot<Class>(resolved);
82 // TODO: Fine-grained marking, so that we don't need to go through all arrays in full.
83 Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(this);
84}
85
Narayan Kamath25352fc2016-08-03 12:46:58 +010086inline MethodType* DexCache::GetResolvedMethodType(uint32_t proto_idx) {
87 DCHECK(Runtime::Current()->IsMethodHandlesEnabled());
88 DCHECK_LT(proto_idx, GetDexFile()->NumProtoIds());
89 return MethodTypeDexCachePair::Lookup(
90 GetResolvedMethodTypes(), proto_idx, NumResolvedMethodTypes()).Read();
91}
92
93inline void DexCache::SetResolvedMethodType(uint32_t proto_idx, MethodType* resolved) {
94 DCHECK(Runtime::Current()->IsMethodHandlesEnabled());
Narayan Kamath42b3dd02016-10-03 11:33:01 +010095 DCHECK_LT(proto_idx, GetDexFile()->NumProtoIds());
Narayan Kamath25352fc2016-08-03 12:46:58 +010096
97 GetResolvedMethodTypes()[proto_idx % NumResolvedMethodTypes()].store(
98 MethodTypeDexCachePair(resolved, proto_idx), std::memory_order_relaxed);
99
100 // TODO: Fine-grained marking, so that we don't need to go through all arrays in full.
101 Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(this);
102}
103
Andreas Gampe542451c2016-07-26 09:02:02 -0700104inline ArtField* DexCache::GetResolvedField(uint32_t field_idx, PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700105 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100106 DCHECK_LT(field_idx, NumResolvedFields()); // NOTE: Unchecked, i.e. not throwing AIOOB.
107 ArtField* field = GetElementPtrSize(GetResolvedFields(), field_idx, ptr_size);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700108 if (field == nullptr || field->GetDeclaringClass()->IsErroneous()) {
109 return nullptr;
110 }
111 return field;
112}
113
Andreas Gampe542451c2016-07-26 09:02:02 -0700114inline void DexCache::SetResolvedField(uint32_t field_idx, ArtField* field, PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700115 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100116 DCHECK_LT(field_idx, NumResolvedFields()); // NOTE: Unchecked, i.e. not throwing AIOOB.
117 SetElementPtrSize(GetResolvedFields(), field_idx, field, ptr_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700118}
119
Andreas Gampe542451c2016-07-26 09:02:02 -0700120inline ArtMethod* DexCache::GetResolvedMethod(uint32_t method_idx, PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700121 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100122 DCHECK_LT(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB.
123 ArtMethod* method = GetElementPtrSize<ArtMethod*>(GetResolvedMethods(), method_idx, ptr_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700124 // Hide resolution trampoline methods from the caller
125 if (method != nullptr && method->IsRuntimeMethod()) {
126 DCHECK_EQ(method, Runtime::Current()->GetResolutionMethod());
127 return nullptr;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700128 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700129 return method;
130}
131
Andreas Gampe542451c2016-07-26 09:02:02 -0700132inline void DexCache::SetResolvedMethod(uint32_t method_idx,
133 ArtMethod* method,
134 PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700135 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100136 DCHECK_LT(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB.
137 SetElementPtrSize(GetResolvedMethods(), method_idx, method, ptr_size);
138}
139
140template <typename PtrType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700141inline PtrType DexCache::GetElementPtrSize(PtrType* ptr_array, size_t idx, PointerSize ptr_size) {
142 if (ptr_size == PointerSize::k64) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100143 uint64_t element = reinterpret_cast<const uint64_t*>(ptr_array)[idx];
144 return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element));
145 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +0100146 uint32_t element = reinterpret_cast<const uint32_t*>(ptr_array)[idx];
147 return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element));
148 }
149}
150
151template <typename PtrType>
152inline void DexCache::SetElementPtrSize(PtrType* ptr_array,
153 size_t idx,
154 PtrType ptr,
Andreas Gampe542451c2016-07-26 09:02:02 -0700155 PointerSize ptr_size) {
156 if (ptr_size == PointerSize::k64) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100157 reinterpret_cast<uint64_t*>(ptr_array)[idx] =
158 dchecked_integral_cast<uint64_t>(reinterpret_cast<uintptr_t>(ptr));
159 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +0100160 reinterpret_cast<uint32_t*>(ptr_array)[idx] =
161 dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(ptr));
162 }
163}
164
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800165template <bool kVisitNativeRoots,
166 VerifyObjectFlags kVerifyFlags,
167 ReadBarrierOption kReadBarrierOption,
168 typename Visitor>
Vladimir Marko05792b92015-08-03 11:56:49 +0100169inline void DexCache::VisitReferences(mirror::Class* klass, const Visitor& visitor) {
170 // Visit instance fields first.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800171 VisitInstanceFieldsReferences<kVerifyFlags, kReadBarrierOption>(klass, visitor);
Vladimir Marko05792b92015-08-03 11:56:49 +0100172 // Visit arrays after.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800173 if (kVisitNativeRoots) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700174 mirror::StringDexCacheType* strings = GetStrings();
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800175 for (size_t i = 0, num_strings = NumStrings(); i != num_strings; ++i) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700176 StringDexCachePair source = strings[i].load(std::memory_order_relaxed);
Narayan Kamathc38a6f82016-09-29 17:07:20 +0100177 mirror::String* before = source.object.Read<kReadBarrierOption>();
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700178 GcRoot<mirror::String> root(before);
179 visitor.VisitRootIfNonNull(root.AddressWithoutBarrier());
180 if (root.Read() != before) {
Narayan Kamathc38a6f82016-09-29 17:07:20 +0100181 source.object = GcRoot<String>(root.Read());
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700182 strings[i].store(source, std::memory_order_relaxed);
183 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800184 }
185 GcRoot<mirror::Class>* resolved_types = GetResolvedTypes();
186 for (size_t i = 0, num_types = NumResolvedTypes(); i != num_types; ++i) {
187 visitor.VisitRootIfNonNull(resolved_types[i].AddressWithoutBarrier());
188 }
Vladimir Marko05792b92015-08-03 11:56:49 +0100189 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700190}
191
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800192template <ReadBarrierOption kReadBarrierOption, typename Visitor>
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700193inline void DexCache::FixupStrings(mirror::StringDexCacheType* dest, const Visitor& visitor) {
194 mirror::StringDexCacheType* src = GetStrings();
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800195 for (size_t i = 0, count = NumStrings(); i < count; ++i) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700196 StringDexCachePair source = src[i].load(std::memory_order_relaxed);
Narayan Kamathc38a6f82016-09-29 17:07:20 +0100197 mirror::String* ptr = source.object.Read<kReadBarrierOption>();
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700198 mirror::String* new_source = visitor(ptr);
Narayan Kamathc38a6f82016-09-29 17:07:20 +0100199 source.object = GcRoot<String>(new_source);
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700200 dest[i].store(source, std::memory_order_relaxed);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800201 }
202}
203
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800204template <ReadBarrierOption kReadBarrierOption, typename Visitor>
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800205inline void DexCache::FixupResolvedTypes(GcRoot<mirror::Class>* dest, const Visitor& visitor) {
206 GcRoot<mirror::Class>* src = GetResolvedTypes();
207 for (size_t i = 0, count = NumResolvedTypes(); i < count; ++i) {
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800208 mirror::Class* source = src[i].Read<kReadBarrierOption>();
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800209 mirror::Class* new_source = visitor(source);
Nicolas Geoffrayd0668f22016-04-26 18:30:31 +0100210 dest[i] = GcRoot<mirror::Class>(new_source);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800211 }
212}
213
Ian Rogers39ebcb82013-05-30 16:57:23 -0700214} // namespace mirror
215} // namespace art
216
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700217#endif // ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_