blob: df3865b1fef76f2c7f6f85db1028d0084d94959f [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"
Narayan Kamathd08e39b2016-10-19 14:16:35 +010027#include "gc_root.h"
Andreas Gampeaa910d52014-07-30 18:59:05 -070028#include "mirror/class.h"
Narayan Kamath25352fc2016-08-03 12:46:58 +010029#include "mirror/method_type.h"
Mathieu Chartierbc56fc32014-06-03 15:37:03 -070030#include "runtime.h"
Mathieu Chartier31e88222016-10-14 18:43:19 -070031#include "obj_ptr.h"
Mathieu Chartierbc56fc32014-06-03 15:37:03 -070032
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070033#include <atomic>
34
Ian Rogers39ebcb82013-05-30 16:57:23 -070035namespace art {
36namespace mirror {
37
Andreas Gampe542451c2016-07-26 09:02:02 -070038inline uint32_t DexCache::ClassSize(PointerSize pointer_size) {
Vladimir Markoc1363122015-04-09 14:13:13 +010039 uint32_t vtable_entries = Object::kVTableLength + 5;
Mathieu Chartiere401d142015-04-22 13:56:20 -070040 return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 0, 0, pointer_size);
Ian Rogers39ebcb82013-05-30 16:57:23 -070041}
42
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070043inline mirror::String* DexCache::GetResolvedString(uint32_t string_idx) {
44 DCHECK_LT(string_idx, GetDexFile()->NumStringIds());
Narayan Kamathc38a6f82016-09-29 17:07:20 +010045 return StringDexCachePair::Lookup(GetStrings(), string_idx, NumStrings()).Read();
Andreas Gampeaa910d52014-07-30 18:59:05 -070046}
47
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070048inline void DexCache::SetResolvedString(uint32_t string_idx, ObjPtr<mirror::String> resolved) {
49 StringDexCachePair::Assign(GetStrings(), string_idx, resolved.Ptr(), NumStrings());
Mathieu Chartierbb816d62016-09-07 10:17:46 -070050 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
Mathieu Chartier31e88222016-10-14 18:43:19 -070077inline void DexCache::SetResolvedType(uint32_t type_idx, ObjPtr<Class> resolved) {
Vladimir Marko05792b92015-08-03 11:56:49 +010078 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
Narayan Kamath25352fc2016-08-03 12:46:58 +010085inline MethodType* DexCache::GetResolvedMethodType(uint32_t proto_idx) {
86 DCHECK(Runtime::Current()->IsMethodHandlesEnabled());
87 DCHECK_LT(proto_idx, GetDexFile()->NumProtoIds());
88 return MethodTypeDexCachePair::Lookup(
89 GetResolvedMethodTypes(), proto_idx, NumResolvedMethodTypes()).Read();
90}
91
92inline void DexCache::SetResolvedMethodType(uint32_t proto_idx, MethodType* resolved) {
93 DCHECK(Runtime::Current()->IsMethodHandlesEnabled());
Narayan Kamath42b3dd02016-10-03 11:33:01 +010094 DCHECK_LT(proto_idx, GetDexFile()->NumProtoIds());
Narayan Kamath25352fc2016-08-03 12:46:58 +010095
Narayan Kamath23136d12016-09-30 16:29:19 +010096 MethodTypeDexCachePair::Assign(GetResolvedMethodTypes(), proto_idx, resolved,
97 NumResolvedMethodTypes());
Narayan Kamath25352fc2016-08-03 12:46:58 +010098 // TODO: Fine-grained marking, so that we don't need to go through all arrays in full.
99 Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(this);
100}
101
Andreas Gampe542451c2016-07-26 09:02:02 -0700102inline ArtField* DexCache::GetResolvedField(uint32_t field_idx, PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700103 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100104 DCHECK_LT(field_idx, NumResolvedFields()); // NOTE: Unchecked, i.e. not throwing AIOOB.
105 ArtField* field = GetElementPtrSize(GetResolvedFields(), field_idx, ptr_size);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700106 if (field == nullptr || field->GetDeclaringClass()->IsErroneous()) {
107 return nullptr;
108 }
109 return field;
110}
111
Andreas Gampe542451c2016-07-26 09:02:02 -0700112inline void DexCache::SetResolvedField(uint32_t field_idx, ArtField* field, PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700113 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100114 DCHECK_LT(field_idx, NumResolvedFields()); // NOTE: Unchecked, i.e. not throwing AIOOB.
115 SetElementPtrSize(GetResolvedFields(), field_idx, field, ptr_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700116}
117
Andreas Gampe542451c2016-07-26 09:02:02 -0700118inline ArtMethod* DexCache::GetResolvedMethod(uint32_t method_idx, PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700119 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100120 DCHECK_LT(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB.
121 ArtMethod* method = GetElementPtrSize<ArtMethod*>(GetResolvedMethods(), method_idx, ptr_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700122 // Hide resolution trampoline methods from the caller
123 if (method != nullptr && method->IsRuntimeMethod()) {
124 DCHECK_EQ(method, Runtime::Current()->GetResolutionMethod());
125 return nullptr;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700126 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700127 return method;
128}
129
Andreas Gampe542451c2016-07-26 09:02:02 -0700130inline void DexCache::SetResolvedMethod(uint32_t method_idx,
131 ArtMethod* method,
132 PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700133 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100134 DCHECK_LT(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB.
135 SetElementPtrSize(GetResolvedMethods(), method_idx, method, ptr_size);
136}
137
138template <typename PtrType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700139inline PtrType DexCache::GetElementPtrSize(PtrType* ptr_array, size_t idx, PointerSize ptr_size) {
140 if (ptr_size == PointerSize::k64) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100141 uint64_t element = reinterpret_cast<const uint64_t*>(ptr_array)[idx];
142 return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element));
143 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +0100144 uint32_t element = reinterpret_cast<const uint32_t*>(ptr_array)[idx];
145 return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element));
146 }
147}
148
149template <typename PtrType>
150inline void DexCache::SetElementPtrSize(PtrType* ptr_array,
151 size_t idx,
152 PtrType ptr,
Andreas Gampe542451c2016-07-26 09:02:02 -0700153 PointerSize ptr_size) {
154 if (ptr_size == PointerSize::k64) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100155 reinterpret_cast<uint64_t*>(ptr_array)[idx] =
156 dchecked_integral_cast<uint64_t>(reinterpret_cast<uintptr_t>(ptr));
157 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +0100158 reinterpret_cast<uint32_t*>(ptr_array)[idx] =
159 dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(ptr));
160 }
161}
162
Narayan Kamathd08e39b2016-10-19 14:16:35 +0100163template <typename T,
164 ReadBarrierOption kReadBarrierOption,
165 typename Visitor>
166inline void VisitDexCachePairs(std::atomic<DexCachePair<T>>* pairs,
167 size_t num_pairs,
168 const Visitor& visitor)
169 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) {
170 for (size_t i = 0; i < num_pairs; ++i) {
171 DexCachePair<T> source = pairs[i].load(std::memory_order_relaxed);
172 // NOTE: We need the "template" keyword here to avoid a compilation
173 // failure. GcRoot<T> is a template argument-dependent type and we need to
174 // tell the compiler to treat "Read" as a template rather than a field or
175 // function. Otherwise, on encountering the "<" token, the compiler would
176 // treat "Read" as a field.
177 T* before = source.object.template Read<kReadBarrierOption>();
178 // TODO(narayan): This additional GC root construction and assignment
179 // is unnecessary. We're already operating on a copy of the DexCachePair
180 // that's in the cache.
181 GcRoot<T> root(before);
182 visitor.VisitRootIfNonNull(root.AddressWithoutBarrier());
183 if (root.Read() != before) {
184 source.object = GcRoot<T>(root.Read());
185 pairs[i].store(source, std::memory_order_relaxed);
186 }
187 }
188}
189
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800190template <bool kVisitNativeRoots,
191 VerifyObjectFlags kVerifyFlags,
192 ReadBarrierOption kReadBarrierOption,
193 typename Visitor>
Mathieu Chartier31e88222016-10-14 18:43:19 -0700194inline void DexCache::VisitReferences(ObjPtr<Class> klass, const Visitor& visitor) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100195 // Visit instance fields first.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800196 VisitInstanceFieldsReferences<kVerifyFlags, kReadBarrierOption>(klass, visitor);
Vladimir Marko05792b92015-08-03 11:56:49 +0100197 // Visit arrays after.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800198 if (kVisitNativeRoots) {
Narayan Kamathd08e39b2016-10-19 14:16:35 +0100199 VisitDexCachePairs<mirror::String, kReadBarrierOption, Visitor>(
200 GetStrings(), NumStrings(), visitor);
201
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800202 GcRoot<mirror::Class>* resolved_types = GetResolvedTypes();
203 for (size_t i = 0, num_types = NumResolvedTypes(); i != num_types; ++i) {
204 visitor.VisitRootIfNonNull(resolved_types[i].AddressWithoutBarrier());
205 }
Narayan Kamathd08e39b2016-10-19 14:16:35 +0100206
207 VisitDexCachePairs<mirror::MethodType, kReadBarrierOption, Visitor>(
208 GetResolvedMethodTypes(), NumResolvedMethodTypes(), visitor);
Vladimir Marko05792b92015-08-03 11:56:49 +0100209 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700210}
211
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800212template <ReadBarrierOption kReadBarrierOption, typename Visitor>
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700213inline void DexCache::FixupStrings(mirror::StringDexCacheType* dest, const Visitor& visitor) {
214 mirror::StringDexCacheType* src = GetStrings();
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800215 for (size_t i = 0, count = NumStrings(); i < count; ++i) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700216 StringDexCachePair source = src[i].load(std::memory_order_relaxed);
Narayan Kamathc38a6f82016-09-29 17:07:20 +0100217 mirror::String* ptr = source.object.Read<kReadBarrierOption>();
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700218 mirror::String* new_source = visitor(ptr);
Narayan Kamathc38a6f82016-09-29 17:07:20 +0100219 source.object = GcRoot<String>(new_source);
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700220 dest[i].store(source, std::memory_order_relaxed);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800221 }
222}
223
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800224template <ReadBarrierOption kReadBarrierOption, typename Visitor>
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800225inline void DexCache::FixupResolvedTypes(GcRoot<mirror::Class>* dest, const Visitor& visitor) {
226 GcRoot<mirror::Class>* src = GetResolvedTypes();
227 for (size_t i = 0, count = NumResolvedTypes(); i < count; ++i) {
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800228 mirror::Class* source = src[i].Read<kReadBarrierOption>();
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800229 mirror::Class* new_source = visitor(source);
Nicolas Geoffrayd0668f22016-04-26 18:30:31 +0100230 dest[i] = GcRoot<mirror::Class>(new_source);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800231 }
232}
233
Narayan Kamath7fe56582016-10-14 18:49:12 +0100234template <ReadBarrierOption kReadBarrierOption, typename Visitor>
235inline void DexCache::FixupResolvedMethodTypes(mirror::MethodTypeDexCacheType* dest,
236 const Visitor& visitor) {
237 mirror::MethodTypeDexCacheType* src = GetResolvedMethodTypes();
238 for (size_t i = 0, count = NumResolvedMethodTypes(); i < count; ++i) {
239 MethodTypeDexCachePair source = src[i].load(std::memory_order_relaxed);
240 mirror::MethodType* ptr = source.object.Read<kReadBarrierOption>();
241 mirror::MethodType* new_source = visitor(ptr);
242 source.object = GcRoot<MethodType>(new_source);
243 dest[i].store(source, std::memory_order_relaxed);
244 }
245}
246
Ian Rogers39ebcb82013-05-30 16:57:23 -0700247} // namespace mirror
248} // namespace art
249
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700250#endif // ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_