blob: cf45748bb8d5bd9c32301dcc5264e9b51d9179f1 [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) {
Narayan Kamath23136d12016-09-30 16:29:19 +010047 StringDexCachePair::Assign(GetStrings(), string_idx, resolved, NumStrings());
Mathieu Chartierbb816d62016-09-07 10:17:46 -070048 Runtime* const runtime = Runtime::Current();
49 if (UNLIKELY(runtime->IsActiveTransaction())) {
50 DCHECK(runtime->IsAotCompiler());
51 runtime->RecordResolveString(this, string_idx);
52 }
Vladimir Marko05792b92015-08-03 11:56:49 +010053 // TODO: Fine-grained marking, so that we don't need to go through all arrays in full.
Mathieu Chartierbb816d62016-09-07 10:17:46 -070054 runtime->GetHeap()->WriteBarrierEveryFieldOf(this);
55}
56
57inline void DexCache::ClearString(uint32_t string_idx) {
58 const uint32_t slot_idx = string_idx % NumStrings();
59 DCHECK(Runtime::Current()->IsAotCompiler());
60 StringDexCacheType* slot = &GetStrings()[slot_idx];
61 // This is racy but should only be called from the transactional interpreter.
Narayan Kamathc38a6f82016-09-29 17:07:20 +010062 if (slot->load(std::memory_order_relaxed).index == string_idx) {
Mathieu Chartierbb816d62016-09-07 10:17:46 -070063 StringDexCachePair cleared(
64 nullptr,
Narayan Kamathc38a6f82016-09-29 17:07:20 +010065 StringDexCachePair::InvalidIndexForSlot(slot_idx));
Mathieu Chartierbb816d62016-09-07 10:17:46 -070066 slot->store(cleared, std::memory_order_relaxed);
67 }
Vladimir Marko05792b92015-08-03 11:56:49 +010068}
69
70inline Class* DexCache::GetResolvedType(uint32_t type_idx) {
71 DCHECK_LT(type_idx, NumResolvedTypes());
72 return GetResolvedTypes()[type_idx].Read();
73}
74
75inline void DexCache::SetResolvedType(uint32_t type_idx, Class* resolved) {
76 DCHECK_LT(type_idx, NumResolvedTypes()); // NOTE: Unchecked, i.e. not throwing AIOOB.
77 // TODO default transaction support.
Vladimir Marko05792b92015-08-03 11:56:49 +010078 GetResolvedTypes()[type_idx] = GcRoot<Class>(resolved);
79 // TODO: Fine-grained marking, so that we don't need to go through all arrays in full.
80 Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(this);
81}
82
Narayan Kamath25352fc2016-08-03 12:46:58 +010083inline MethodType* DexCache::GetResolvedMethodType(uint32_t proto_idx) {
84 DCHECK(Runtime::Current()->IsMethodHandlesEnabled());
85 DCHECK_LT(proto_idx, GetDexFile()->NumProtoIds());
86 return MethodTypeDexCachePair::Lookup(
87 GetResolvedMethodTypes(), proto_idx, NumResolvedMethodTypes()).Read();
88}
89
90inline void DexCache::SetResolvedMethodType(uint32_t proto_idx, MethodType* resolved) {
91 DCHECK(Runtime::Current()->IsMethodHandlesEnabled());
Narayan Kamath42b3dd02016-10-03 11:33:01 +010092 DCHECK_LT(proto_idx, GetDexFile()->NumProtoIds());
Narayan Kamath25352fc2016-08-03 12:46:58 +010093
Narayan Kamath23136d12016-09-30 16:29:19 +010094 MethodTypeDexCachePair::Assign(GetResolvedMethodTypes(), proto_idx, resolved,
95 NumResolvedMethodTypes());
Narayan Kamath25352fc2016-08-03 12:46:58 +010096 // TODO: Fine-grained marking, so that we don't need to go through all arrays in full.
97 Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(this);
98}
99
Andreas Gampe542451c2016-07-26 09:02:02 -0700100inline ArtField* DexCache::GetResolvedField(uint32_t field_idx, PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700101 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100102 DCHECK_LT(field_idx, NumResolvedFields()); // NOTE: Unchecked, i.e. not throwing AIOOB.
103 ArtField* field = GetElementPtrSize(GetResolvedFields(), field_idx, ptr_size);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700104 if (field == nullptr || field->GetDeclaringClass()->IsErroneous()) {
105 return nullptr;
106 }
107 return field;
108}
109
Andreas Gampe542451c2016-07-26 09:02:02 -0700110inline void DexCache::SetResolvedField(uint32_t field_idx, ArtField* field, PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700111 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100112 DCHECK_LT(field_idx, NumResolvedFields()); // NOTE: Unchecked, i.e. not throwing AIOOB.
113 SetElementPtrSize(GetResolvedFields(), field_idx, field, ptr_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700114}
115
Andreas Gampe542451c2016-07-26 09:02:02 -0700116inline ArtMethod* DexCache::GetResolvedMethod(uint32_t method_idx, PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700117 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100118 DCHECK_LT(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB.
119 ArtMethod* method = GetElementPtrSize<ArtMethod*>(GetResolvedMethods(), method_idx, ptr_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700120 // Hide resolution trampoline methods from the caller
121 if (method != nullptr && method->IsRuntimeMethod()) {
122 DCHECK_EQ(method, Runtime::Current()->GetResolutionMethod());
123 return nullptr;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700124 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700125 return method;
126}
127
Andreas Gampe542451c2016-07-26 09:02:02 -0700128inline void DexCache::SetResolvedMethod(uint32_t method_idx,
129 ArtMethod* method,
130 PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700131 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100132 DCHECK_LT(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB.
133 SetElementPtrSize(GetResolvedMethods(), method_idx, method, ptr_size);
134}
135
136template <typename PtrType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700137inline PtrType DexCache::GetElementPtrSize(PtrType* ptr_array, size_t idx, PointerSize ptr_size) {
138 if (ptr_size == PointerSize::k64) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100139 uint64_t element = reinterpret_cast<const uint64_t*>(ptr_array)[idx];
140 return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element));
141 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +0100142 uint32_t element = reinterpret_cast<const uint32_t*>(ptr_array)[idx];
143 return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element));
144 }
145}
146
147template <typename PtrType>
148inline void DexCache::SetElementPtrSize(PtrType* ptr_array,
149 size_t idx,
150 PtrType ptr,
Andreas Gampe542451c2016-07-26 09:02:02 -0700151 PointerSize ptr_size) {
152 if (ptr_size == PointerSize::k64) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100153 reinterpret_cast<uint64_t*>(ptr_array)[idx] =
154 dchecked_integral_cast<uint64_t>(reinterpret_cast<uintptr_t>(ptr));
155 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +0100156 reinterpret_cast<uint32_t*>(ptr_array)[idx] =
157 dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(ptr));
158 }
159}
160
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800161template <bool kVisitNativeRoots,
162 VerifyObjectFlags kVerifyFlags,
163 ReadBarrierOption kReadBarrierOption,
164 typename Visitor>
Vladimir Marko05792b92015-08-03 11:56:49 +0100165inline void DexCache::VisitReferences(mirror::Class* klass, const Visitor& visitor) {
166 // Visit instance fields first.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800167 VisitInstanceFieldsReferences<kVerifyFlags, kReadBarrierOption>(klass, visitor);
Vladimir Marko05792b92015-08-03 11:56:49 +0100168 // Visit arrays after.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800169 if (kVisitNativeRoots) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700170 mirror::StringDexCacheType* strings = GetStrings();
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800171 for (size_t i = 0, num_strings = NumStrings(); i != num_strings; ++i) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700172 StringDexCachePair source = strings[i].load(std::memory_order_relaxed);
Narayan Kamathc38a6f82016-09-29 17:07:20 +0100173 mirror::String* before = source.object.Read<kReadBarrierOption>();
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700174 GcRoot<mirror::String> root(before);
175 visitor.VisitRootIfNonNull(root.AddressWithoutBarrier());
176 if (root.Read() != before) {
Narayan Kamathc38a6f82016-09-29 17:07:20 +0100177 source.object = GcRoot<String>(root.Read());
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700178 strings[i].store(source, std::memory_order_relaxed);
179 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800180 }
181 GcRoot<mirror::Class>* resolved_types = GetResolvedTypes();
182 for (size_t i = 0, num_types = NumResolvedTypes(); i != num_types; ++i) {
183 visitor.VisitRootIfNonNull(resolved_types[i].AddressWithoutBarrier());
184 }
Vladimir Marko05792b92015-08-03 11:56:49 +0100185 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700186}
187
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800188template <ReadBarrierOption kReadBarrierOption, typename Visitor>
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700189inline void DexCache::FixupStrings(mirror::StringDexCacheType* dest, const Visitor& visitor) {
190 mirror::StringDexCacheType* src = GetStrings();
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800191 for (size_t i = 0, count = NumStrings(); i < count; ++i) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700192 StringDexCachePair source = src[i].load(std::memory_order_relaxed);
Narayan Kamathc38a6f82016-09-29 17:07:20 +0100193 mirror::String* ptr = source.object.Read<kReadBarrierOption>();
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700194 mirror::String* new_source = visitor(ptr);
Narayan Kamathc38a6f82016-09-29 17:07:20 +0100195 source.object = GcRoot<String>(new_source);
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700196 dest[i].store(source, std::memory_order_relaxed);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800197 }
198}
199
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800200template <ReadBarrierOption kReadBarrierOption, typename Visitor>
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800201inline void DexCache::FixupResolvedTypes(GcRoot<mirror::Class>* dest, const Visitor& visitor) {
202 GcRoot<mirror::Class>* src = GetResolvedTypes();
203 for (size_t i = 0, count = NumResolvedTypes(); i < count; ++i) {
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800204 mirror::Class* source = src[i].Read<kReadBarrierOption>();
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800205 mirror::Class* new_source = visitor(source);
Nicolas Geoffrayd0668f22016-04-26 18:30:31 +0100206 dest[i] = GcRoot<mirror::Class>(new_source);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800207 }
208}
209
Narayan Kamath7fe56582016-10-14 18:49:12 +0100210template <ReadBarrierOption kReadBarrierOption, typename Visitor>
211inline void DexCache::FixupResolvedMethodTypes(mirror::MethodTypeDexCacheType* dest,
212 const Visitor& visitor) {
213 mirror::MethodTypeDexCacheType* src = GetResolvedMethodTypes();
214 for (size_t i = 0, count = NumResolvedMethodTypes(); i < count; ++i) {
215 MethodTypeDexCachePair source = src[i].load(std::memory_order_relaxed);
216 mirror::MethodType* ptr = source.object.Read<kReadBarrierOption>();
217 mirror::MethodType* new_source = visitor(ptr);
218 source.object = GcRoot<MethodType>(new_source);
219 dest[i].store(source, std::memory_order_relaxed);
220 }
221}
222
Ian Rogers39ebcb82013-05-30 16:57:23 -0700223} // namespace mirror
224} // namespace art
225
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700226#endif // ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_