blob: a3071b7f63e313a52b001b5aeb82229a3b58cbf6 [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());
42 return StringDexCachePair::LookupString(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());
Vladimir Marko05792b92015-08-03 11:56:49 +010047 // TODO default transaction support.
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070048 StringDexCachePair idx_ptr;
49 idx_ptr.string_index = string_idx;
50 idx_ptr.string_pointer = GcRoot<String>(resolved);
51 GetStrings()[string_idx % NumStrings()].store(idx_ptr, std::memory_order_relaxed);
Vladimir Marko05792b92015-08-03 11:56:49 +010052 // TODO: Fine-grained marking, so that we don't need to go through all arrays in full.
53 Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(this);
54}
55
56inline Class* DexCache::GetResolvedType(uint32_t type_idx) {
57 DCHECK_LT(type_idx, NumResolvedTypes());
58 return GetResolvedTypes()[type_idx].Read();
59}
60
61inline void DexCache::SetResolvedType(uint32_t type_idx, Class* resolved) {
62 DCHECK_LT(type_idx, NumResolvedTypes()); // NOTE: Unchecked, i.e. not throwing AIOOB.
63 // TODO default transaction support.
Vladimir Marko05792b92015-08-03 11:56:49 +010064 GetResolvedTypes()[type_idx] = GcRoot<Class>(resolved);
65 // TODO: Fine-grained marking, so that we don't need to go through all arrays in full.
66 Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(this);
67}
68
Andreas Gampe542451c2016-07-26 09:02:02 -070069inline ArtField* DexCache::GetResolvedField(uint32_t field_idx, PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070070 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +010071 DCHECK_LT(field_idx, NumResolvedFields()); // NOTE: Unchecked, i.e. not throwing AIOOB.
72 ArtField* field = GetElementPtrSize(GetResolvedFields(), field_idx, ptr_size);
Mathieu Chartierc7853442015-03-27 14:35:38 -070073 if (field == nullptr || field->GetDeclaringClass()->IsErroneous()) {
74 return nullptr;
75 }
76 return field;
77}
78
Andreas Gampe542451c2016-07-26 09:02:02 -070079inline void DexCache::SetResolvedField(uint32_t field_idx, ArtField* field, PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070080 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +010081 DCHECK_LT(field_idx, NumResolvedFields()); // NOTE: Unchecked, i.e. not throwing AIOOB.
82 SetElementPtrSize(GetResolvedFields(), field_idx, field, ptr_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -070083}
84
Andreas Gampe542451c2016-07-26 09:02:02 -070085inline ArtMethod* DexCache::GetResolvedMethod(uint32_t method_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(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB.
88 ArtMethod* method = GetElementPtrSize<ArtMethod*>(GetResolvedMethods(), method_idx, ptr_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -070089 // Hide resolution trampoline methods from the caller
90 if (method != nullptr && method->IsRuntimeMethod()) {
91 DCHECK_EQ(method, Runtime::Current()->GetResolutionMethod());
92 return nullptr;
Mathieu Chartierc7853442015-03-27 14:35:38 -070093 }
Mathieu Chartiere401d142015-04-22 13:56:20 -070094 return method;
95}
96
Andreas Gampe542451c2016-07-26 09:02:02 -070097inline void DexCache::SetResolvedMethod(uint32_t method_idx,
98 ArtMethod* method,
99 PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700100 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100101 DCHECK_LT(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB.
102 SetElementPtrSize(GetResolvedMethods(), method_idx, method, ptr_size);
103}
104
105template <typename PtrType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700106inline PtrType DexCache::GetElementPtrSize(PtrType* ptr_array, size_t idx, PointerSize ptr_size) {
107 if (ptr_size == PointerSize::k64) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100108 uint64_t element = reinterpret_cast<const uint64_t*>(ptr_array)[idx];
109 return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element));
110 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +0100111 uint32_t element = reinterpret_cast<const uint32_t*>(ptr_array)[idx];
112 return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element));
113 }
114}
115
116template <typename PtrType>
117inline void DexCache::SetElementPtrSize(PtrType* ptr_array,
118 size_t idx,
119 PtrType ptr,
Andreas Gampe542451c2016-07-26 09:02:02 -0700120 PointerSize ptr_size) {
121 if (ptr_size == PointerSize::k64) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100122 reinterpret_cast<uint64_t*>(ptr_array)[idx] =
123 dchecked_integral_cast<uint64_t>(reinterpret_cast<uintptr_t>(ptr));
124 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +0100125 reinterpret_cast<uint32_t*>(ptr_array)[idx] =
126 dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(ptr));
127 }
128}
129
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800130template <bool kVisitNativeRoots,
131 VerifyObjectFlags kVerifyFlags,
132 ReadBarrierOption kReadBarrierOption,
133 typename Visitor>
Vladimir Marko05792b92015-08-03 11:56:49 +0100134inline void DexCache::VisitReferences(mirror::Class* klass, const Visitor& visitor) {
135 // Visit instance fields first.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800136 VisitInstanceFieldsReferences<kVerifyFlags, kReadBarrierOption>(klass, visitor);
Vladimir Marko05792b92015-08-03 11:56:49 +0100137 // Visit arrays after.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800138 if (kVisitNativeRoots) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700139 mirror::StringDexCacheType* strings = GetStrings();
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800140 for (size_t i = 0, num_strings = NumStrings(); i != num_strings; ++i) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700141 StringDexCachePair source = strings[i].load(std::memory_order_relaxed);
142 mirror::String* before = source.string_pointer.Read<kReadBarrierOption>();
143 GcRoot<mirror::String> root(before);
144 visitor.VisitRootIfNonNull(root.AddressWithoutBarrier());
145 if (root.Read() != before) {
146 source.string_pointer = GcRoot<String>(root.Read());
147 strings[i].store(source, std::memory_order_relaxed);
148 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800149 }
150 GcRoot<mirror::Class>* resolved_types = GetResolvedTypes();
151 for (size_t i = 0, num_types = NumResolvedTypes(); i != num_types; ++i) {
152 visitor.VisitRootIfNonNull(resolved_types[i].AddressWithoutBarrier());
153 }
Vladimir Marko05792b92015-08-03 11:56:49 +0100154 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700155}
156
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800157template <ReadBarrierOption kReadBarrierOption, typename Visitor>
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700158inline void DexCache::FixupStrings(mirror::StringDexCacheType* dest, const Visitor& visitor) {
159 mirror::StringDexCacheType* src = GetStrings();
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800160 for (size_t i = 0, count = NumStrings(); i < count; ++i) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700161 StringDexCachePair source = src[i].load(std::memory_order_relaxed);
162 mirror::String* ptr = source.string_pointer.Read<kReadBarrierOption>();
163 mirror::String* new_source = visitor(ptr);
164 source.string_pointer = GcRoot<String>(new_source);
165 dest[i].store(source, std::memory_order_relaxed);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800166 }
167}
168
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800169template <ReadBarrierOption kReadBarrierOption, typename Visitor>
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800170inline void DexCache::FixupResolvedTypes(GcRoot<mirror::Class>* dest, const Visitor& visitor) {
171 GcRoot<mirror::Class>* src = GetResolvedTypes();
172 for (size_t i = 0, count = NumResolvedTypes(); i < count; ++i) {
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800173 mirror::Class* source = src[i].Read<kReadBarrierOption>();
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800174 mirror::Class* new_source = visitor(source);
Nicolas Geoffrayd0668f22016-04-26 18:30:31 +0100175 dest[i] = GcRoot<mirror::Class>(new_source);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800176 }
177}
178
Ian Rogers39ebcb82013-05-30 16:57:23 -0700179} // namespace mirror
180} // namespace art
181
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700182#endif // ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_