blob: 975af61ca8af595cd9aed15b4a5ecfc87ffafa70 [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 Gampeaa910d52014-07-30 18:59:05 -070025#include "base/logging.h"
26#include "mirror/class.h"
Mathieu Chartierbc56fc32014-06-03 15:37:03 -070027#include "runtime.h"
28
Ian Rogers39ebcb82013-05-30 16:57:23 -070029namespace art {
30namespace mirror {
31
Mathieu Chartiere401d142015-04-22 13:56:20 -070032inline uint32_t DexCache::ClassSize(size_t pointer_size) {
Vladimir Markoc1363122015-04-09 14:13:13 +010033 uint32_t vtable_entries = Object::kVTableLength + 5;
Mathieu Chartiere401d142015-04-22 13:56:20 -070034 return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 0, 0, pointer_size);
Ian Rogers39ebcb82013-05-30 16:57:23 -070035}
36
Vladimir Marko05792b92015-08-03 11:56:49 +010037inline String* DexCache::GetResolvedString(uint32_t string_idx) {
38 DCHECK_LT(string_idx, NumStrings());
39 return GetStrings()[string_idx].Read();
Andreas Gampeaa910d52014-07-30 18:59:05 -070040}
41
Vladimir Marko05792b92015-08-03 11:56:49 +010042inline void DexCache::SetResolvedString(uint32_t string_idx, String* resolved) {
43 DCHECK_LT(string_idx, NumStrings());
44 // TODO default transaction support.
45 GetStrings()[string_idx] = GcRoot<String>(resolved);
46 // TODO: Fine-grained marking, so that we don't need to go through all arrays in full.
47 Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(this);
48}
49
50inline Class* DexCache::GetResolvedType(uint32_t type_idx) {
51 DCHECK_LT(type_idx, NumResolvedTypes());
52 return GetResolvedTypes()[type_idx].Read();
53}
54
55inline void DexCache::SetResolvedType(uint32_t type_idx, Class* resolved) {
56 DCHECK_LT(type_idx, NumResolvedTypes()); // NOTE: Unchecked, i.e. not throwing AIOOB.
57 // TODO default transaction support.
58 DCHECK(resolved == nullptr || !resolved->IsErroneous());
59 GetResolvedTypes()[type_idx] = GcRoot<Class>(resolved);
60 // TODO: Fine-grained marking, so that we don't need to go through all arrays in full.
61 Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(this);
62}
63
64inline ArtField* DexCache::GetResolvedField(uint32_t field_idx, size_t ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070065 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +010066 DCHECK_LT(field_idx, NumResolvedFields()); // NOTE: Unchecked, i.e. not throwing AIOOB.
67 ArtField* field = GetElementPtrSize(GetResolvedFields(), field_idx, ptr_size);
Mathieu Chartierc7853442015-03-27 14:35:38 -070068 if (field == nullptr || field->GetDeclaringClass()->IsErroneous()) {
69 return nullptr;
70 }
71 return field;
72}
73
Vladimir Marko05792b92015-08-03 11:56:49 +010074inline void DexCache::SetResolvedField(uint32_t field_idx, ArtField* field, size_t ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070075 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +010076 DCHECK_LT(field_idx, NumResolvedFields()); // NOTE: Unchecked, i.e. not throwing AIOOB.
77 SetElementPtrSize(GetResolvedFields(), field_idx, field, ptr_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -070078}
79
80inline ArtMethod* DexCache::GetResolvedMethod(uint32_t method_idx, size_t ptr_size) {
81 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +010082 DCHECK_LT(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB.
83 ArtMethod* method = GetElementPtrSize<ArtMethod*>(GetResolvedMethods(), method_idx, ptr_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -070084 // Hide resolution trampoline methods from the caller
85 if (method != nullptr && method->IsRuntimeMethod()) {
86 DCHECK_EQ(method, Runtime::Current()->GetResolutionMethod());
87 return nullptr;
Mathieu Chartierc7853442015-03-27 14:35:38 -070088 }
Mathieu Chartiere401d142015-04-22 13:56:20 -070089 return method;
90}
91
Vladimir Marko05792b92015-08-03 11:56:49 +010092inline void DexCache::SetResolvedMethod(uint32_t method_idx, ArtMethod* method, size_t ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070093 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +010094 DCHECK_LT(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB.
95 SetElementPtrSize(GetResolvedMethods(), method_idx, method, ptr_size);
96}
97
98template <typename PtrType>
99inline PtrType DexCache::GetElementPtrSize(PtrType* ptr_array, size_t idx, size_t ptr_size) {
100 if (ptr_size == 8u) {
101 uint64_t element = reinterpret_cast<const uint64_t*>(ptr_array)[idx];
102 return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element));
103 } else {
104 DCHECK_EQ(ptr_size, 4u);
105 uint32_t element = reinterpret_cast<const uint32_t*>(ptr_array)[idx];
106 return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element));
107 }
108}
109
110template <typename PtrType>
111inline void DexCache::SetElementPtrSize(PtrType* ptr_array,
112 size_t idx,
113 PtrType ptr,
114 size_t ptr_size) {
115 if (ptr_size == 8u) {
116 reinterpret_cast<uint64_t*>(ptr_array)[idx] =
117 dchecked_integral_cast<uint64_t>(reinterpret_cast<uintptr_t>(ptr));
118 } else {
119 DCHECK_EQ(ptr_size, 4u);
120 reinterpret_cast<uint32_t*>(ptr_array)[idx] =
121 dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(ptr));
122 }
123}
124
125template <VerifyObjectFlags kVerifyFlags, typename Visitor>
126inline void DexCache::VisitReferences(mirror::Class* klass, const Visitor& visitor) {
127 // Visit instance fields first.
128 VisitInstanceFieldsReferences(klass, visitor);
129 // Visit arrays after.
130 GcRoot<mirror::String>* strings = GetStrings();
131 for (size_t i = 0, num_strings = NumStrings(); i != num_strings; ++i) {
132 visitor.VisitRootIfNonNull(strings[i].AddressWithoutBarrier());
133 }
134 GcRoot<mirror::Class>* resolved_types = GetResolvedTypes();
135 for (size_t i = 0, num_types = NumResolvedTypes(); i != num_types; ++i) {
136 visitor.VisitRootIfNonNull(resolved_types[i].AddressWithoutBarrier());
137 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700138}
139
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800140template <typename Visitor>
141inline void DexCache::FixupStrings(GcRoot<mirror::String>* dest, const Visitor& visitor) {
142 GcRoot<mirror::String>* src = GetStrings();
143 for (size_t i = 0, count = NumStrings(); i < count; ++i) {
144 // TODO: Probably don't need read barrier for most callers.
145 mirror::String* source = src[i].Read();
146 mirror::String* new_source = visitor(source);
147 if (source != new_source) {
148 dest[i] = GcRoot<mirror::String>(new_source);
149 }
150 }
151}
152
153template <typename Visitor>
154inline void DexCache::FixupResolvedTypes(GcRoot<mirror::Class>* dest, const Visitor& visitor) {
155 GcRoot<mirror::Class>* src = GetResolvedTypes();
156 for (size_t i = 0, count = NumResolvedTypes(); i < count; ++i) {
157 // TODO: Probably don't need read barrier for most callers.
158 mirror::Class* source = src[i].Read();
159 mirror::Class* new_source = visitor(source);
160 if (source != new_source) {
161 dest[i] = GcRoot<mirror::Class>(new_source);
162 }
163 }
164}
165
Ian Rogers39ebcb82013-05-30 16:57:23 -0700166} // namespace mirror
167} // namespace art
168
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700169#endif // ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_