blob: e68b0c72195433b92c67a579f68bfd3f2e818cf2 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 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 */
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_MIRROR_DEX_CACHE_H_
18#define ART_RUNTIME_MIRROR_DEX_CACHE_H_
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070019
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "array.h"
Vladimir Markoec786222016-12-20 16:24:13 +000021#include "base/bit_utils.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080022#include "dex_file_types.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070023#include "object.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "object_array.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070025
26namespace art {
27
Vladimir Markoec786222016-12-20 16:24:13 +000028class ArtField;
Alex Lightdba61482016-12-21 08:20:29 -080029class ArtMethod;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030struct DexCacheOffsets;
31class DexFile;
32class ImageWriter;
33union JValue;
Andreas Gampecc1b5352016-12-01 16:58:38 -080034class LinearAlloc;
35class Thread;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036
37namespace mirror {
38
Vladimir Markoec786222016-12-20 16:24:13 +000039class Class;
Narayan Kamath25352fc2016-08-03 12:46:58 +010040class MethodType;
Mingyao Yang98d1cc82014-05-15 17:02:16 -070041class String;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070042
Narayan Kamathc38a6f82016-09-29 17:07:20 +010043template <typename T> struct PACKED(8) DexCachePair {
44 GcRoot<T> object;
45 uint32_t index;
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070046 // The array is initially [ {0,0}, {0,0}, {0,0} ... ]
47 // We maintain the invariant that once a dex cache entry is populated,
48 // the pointer is always non-0
49 // Any given entry would thus be:
50 // {non-0, non-0} OR {0,0}
51 //
52 // It's generally sufficiently enough then to check if the
Narayan Kamathc38a6f82016-09-29 17:07:20 +010053 // lookup index matches the stored index (for a >0 lookup index)
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070054 // because if it's true the pointer is also non-null.
55 //
56 // For the 0th entry which is a special case, the value is either
57 // {0,0} (initial state) or {non-0, 0} which indicates
Narayan Kamathc38a6f82016-09-29 17:07:20 +010058 // that a valid object is stored at that index for a dex section id of 0.
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070059 //
Narayan Kamathc38a6f82016-09-29 17:07:20 +010060 // As an optimization, we want to avoid branching on the object pointer since
61 // it's always non-null if the id branch succeeds (except for the 0th id).
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070062 // Set the initial state for the 0th entry to be {0,1} which is guaranteed to fail
Narayan Kamathc38a6f82016-09-29 17:07:20 +010063 // the lookup id == stored id branch.
Vladimir Markoec786222016-12-20 16:24:13 +000064 DexCachePair(ObjPtr<T> object, uint32_t index)
Narayan Kamathc38a6f82016-09-29 17:07:20 +010065 : object(object),
66 index(index) {}
67 DexCachePair() = default;
68 DexCachePair(const DexCachePair<T>&) = default;
69 DexCachePair& operator=(const DexCachePair<T>&) = default;
Mathieu Chartierbb816d62016-09-07 10:17:46 -070070
Narayan Kamathc38a6f82016-09-29 17:07:20 +010071 static void Initialize(std::atomic<DexCachePair<T>>* dex_cache) {
72 DexCachePair<T> first_elem;
73 first_elem.object = GcRoot<T>(nullptr);
74 first_elem.index = InvalidIndexForSlot(0);
75 dex_cache[0].store(first_elem, std::memory_order_relaxed);
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070076 }
Mathieu Chartierbb816d62016-09-07 10:17:46 -070077
Narayan Kamathc38a6f82016-09-29 17:07:20 +010078 static uint32_t InvalidIndexForSlot(uint32_t slot) {
Mathieu Chartierbb816d62016-09-07 10:17:46 -070079 // Since the cache size is a power of two, 0 will always map to slot 0.
80 // Use 1 for slot 0 and 0 for all other slots.
81 return (slot == 0) ? 1u : 0u;
82 }
Vladimir Markoec786222016-12-20 16:24:13 +000083
84 T* GetObjectForIndex(uint32_t idx) REQUIRES_SHARED(Locks::mutator_lock_) {
85 if (idx != index) {
86 return nullptr;
87 }
88 DCHECK(!object.IsNull());
89 return object.Read();
90 }
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070091};
Narayan Kamathc38a6f82016-09-29 17:07:20 +010092
Vladimir Markoec786222016-12-20 16:24:13 +000093using TypeDexCachePair = DexCachePair<Class>;
94using TypeDexCacheType = std::atomic<TypeDexCachePair>;
95
96using StringDexCachePair = DexCachePair<String>;
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070097using StringDexCacheType = std::atomic<StringDexCachePair>;
98
Vladimir Markoec786222016-12-20 16:24:13 +000099using MethodTypeDexCachePair = DexCachePair<MethodType>;
Narayan Kamath25352fc2016-08-03 12:46:58 +0100100using MethodTypeDexCacheType = std::atomic<MethodTypeDexCachePair>;
101
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700102// C++ mirror of java.lang.DexCache.
103class MANAGED DexCache FINAL : public Object {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700104 public:
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700105 // Size of java.lang.DexCache.class.
Andreas Gampe542451c2016-07-26 09:02:02 -0700106 static uint32_t ClassSize(PointerSize pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700107
Vladimir Markoec786222016-12-20 16:24:13 +0000108 // Size of type dex cache. Needs to be a power of 2 for entrypoint assumptions to hold.
109 static constexpr size_t kDexCacheTypeCacheSize = 1024;
110 static_assert(IsPowerOfTwo(kDexCacheTypeCacheSize),
111 "Type dex cache size is not a power of 2.");
112
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700113 // Size of string dex cache. Needs to be a power of 2 for entrypoint assumptions to hold.
114 static constexpr size_t kDexCacheStringCacheSize = 1024;
115 static_assert(IsPowerOfTwo(kDexCacheStringCacheSize),
116 "String dex cache size is not a power of 2.");
117
Narayan Kamath25352fc2016-08-03 12:46:58 +0100118 // Size of method type dex cache. Needs to be a power of 2 for entrypoint assumptions
119 // to hold.
120 static constexpr size_t kDexCacheMethodTypeCacheSize = 1024;
121 static_assert(IsPowerOfTwo(kDexCacheMethodTypeCacheSize),
122 "MethodType dex cache size is not a power of 2.");
123
Vladimir Markoec786222016-12-20 16:24:13 +0000124 static constexpr size_t StaticTypeSize() {
125 return kDexCacheTypeCacheSize;
126 }
127
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700128 static constexpr size_t StaticStringSize() {
129 return kDexCacheStringCacheSize;
130 }
131
Narayan Kamath25352fc2016-08-03 12:46:58 +0100132 static constexpr size_t StaticMethodTypeSize() {
133 return kDexCacheMethodTypeCacheSize;
134 }
135
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700136 // Size of an instance of java.lang.DexCache not including referenced values.
137 static constexpr uint32_t InstanceSize() {
138 return sizeof(DexCache);
139 }
140
Andreas Gampecc1b5352016-12-01 16:58:38 -0800141 static void InitializeDexCache(Thread* self,
142 ObjPtr<mirror::DexCache> dex_cache,
143 ObjPtr<mirror::String> location,
144 const DexFile* dex_file,
145 LinearAlloc* linear_alloc,
146 PointerSize image_pointer_size)
147 REQUIRES_SHARED(Locks::mutator_lock_)
148 REQUIRES(Locks::dex_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700149
Andreas Gampe542451c2016-07-26 09:02:02 -0700150 void Fixup(ArtMethod* trampoline, PointerSize pointer_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700151 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -0800152
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800153 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier, typename Visitor>
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700154 void FixupStrings(StringDexCacheType* dest, const Visitor& visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700155 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800156
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800157 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier, typename Visitor>
Vladimir Markoec786222016-12-20 16:24:13 +0000158 void FixupResolvedTypes(TypeDexCacheType* dest, const Visitor& visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700159 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800160
Narayan Kamath7fe56582016-10-14 18:49:12 +0100161 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier, typename Visitor>
162 void FixupResolvedMethodTypes(MethodTypeDexCacheType* dest, const Visitor& visitor)
163 REQUIRES_SHARED(Locks::mutator_lock_);
164
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700165 String* GetLocation() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700166 return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(DexCache, location_));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700167 }
168
Andreas Gampedd9d0552015-03-09 12:57:41 -0700169 static MemberOffset DexOffset() {
170 return OFFSET_OF_OBJECT_MEMBER(DexCache, dex_);
171 }
172
buzbee5cd21802011-08-26 10:40:14 -0700173 static MemberOffset StringsOffset() {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700174 return OFFSET_OF_OBJECT_MEMBER(DexCache, strings_);
buzbeec5ef0462011-08-25 18:44:49 -0700175 }
176
Vladimir Marko05792b92015-08-03 11:56:49 +0100177 static MemberOffset ResolvedTypesOffset() {
178 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_types_);
179 }
180
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700181 static MemberOffset ResolvedFieldsOffset() {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700182 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_fields_);
buzbeec5ef0462011-08-25 18:44:49 -0700183 }
184
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700185 static MemberOffset ResolvedMethodsOffset() {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700186 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_methods_);
buzbeec5ef0462011-08-25 18:44:49 -0700187 }
188
Narayan Kamath25352fc2016-08-03 12:46:58 +0100189 static MemberOffset ResolvedMethodTypesOffset() {
190 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_method_types_);
191 }
192
Vladimir Marko05792b92015-08-03 11:56:49 +0100193 static MemberOffset NumStringsOffset() {
194 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_strings_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700195 }
196
Vladimir Marko05792b92015-08-03 11:56:49 +0100197 static MemberOffset NumResolvedTypesOffset() {
198 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_resolved_types_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700199 }
200
Vladimir Marko05792b92015-08-03 11:56:49 +0100201 static MemberOffset NumResolvedFieldsOffset() {
202 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_resolved_fields_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700203 }
204
Vladimir Marko05792b92015-08-03 11:56:49 +0100205 static MemberOffset NumResolvedMethodsOffset() {
206 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_resolved_methods_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700207 }
208
Narayan Kamath25352fc2016-08-03 12:46:58 +0100209 static MemberOffset NumResolvedMethodTypesOffset() {
210 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_resolved_method_types_);
211 }
212
Vladimir Markoec786222016-12-20 16:24:13 +0000213 String* GetResolvedString(dex::StringIndex string_idx) ALWAYS_INLINE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700214 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700215
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800216 void SetResolvedString(dex::StringIndex string_idx, ObjPtr<mirror::String> resolved) ALWAYS_INLINE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700217 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700218
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700219 // Clear a string for a string_idx, used to undo string intern transactions to make sure
220 // the string isn't kept live.
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800221 void ClearString(dex::StringIndex string_idx) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700222
Andreas Gampea5b09a62016-11-17 15:21:22 -0800223 Class* GetResolvedType(dex::TypeIndex type_idx) REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko05792b92015-08-03 11:56:49 +0100224
Andreas Gampea5b09a62016-11-17 15:21:22 -0800225 void SetResolvedType(dex::TypeIndex type_idx, ObjPtr<Class> resolved)
Mathieu Chartier31e88222016-10-14 18:43:19 -0700226 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko05792b92015-08-03 11:56:49 +0100227
Vladimir Markoec786222016-12-20 16:24:13 +0000228 void ClearResolvedType(dex::TypeIndex type_idx) REQUIRES_SHARED(Locks::mutator_lock_);
229
Andreas Gampe542451c2016-07-26 09:02:02 -0700230 ALWAYS_INLINE ArtMethod* GetResolvedMethod(uint32_t method_idx, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700231 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700232
Andreas Gampe542451c2016-07-26 09:02:02 -0700233 ALWAYS_INLINE void SetResolvedMethod(uint32_t method_idx,
234 ArtMethod* resolved,
235 PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700236 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700237
Mathieu Chartierc7853442015-03-27 14:35:38 -0700238 // Pointer sized variant, used for patching.
Andreas Gampe542451c2016-07-26 09:02:02 -0700239 ALWAYS_INLINE ArtField* GetResolvedField(uint32_t idx, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700240 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700241
242 // Pointer sized variant, used for patching.
Andreas Gampe542451c2016-07-26 09:02:02 -0700243 ALWAYS_INLINE void SetResolvedField(uint32_t idx, ArtField* field, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700244 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700245
Narayan Kamath25352fc2016-08-03 12:46:58 +0100246 MethodType* GetResolvedMethodType(uint32_t proto_idx) REQUIRES_SHARED(Locks::mutator_lock_);
247
248 void SetResolvedMethodType(uint32_t proto_idx, MethodType* resolved) REQUIRES_SHARED(Locks::mutator_lock_);
249
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700250 StringDexCacheType* GetStrings() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700251 return GetFieldPtr64<StringDexCacheType*>(StringsOffset());
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700252 }
Brian Carlstrom83db7722011-08-26 17:32:56 -0700253
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700254 void SetStrings(StringDexCacheType* strings) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800255 SetFieldPtr<false>(StringsOffset(), strings);
256 }
257
Vladimir Markoec786222016-12-20 16:24:13 +0000258 TypeDexCacheType* GetResolvedTypes() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
259 return GetFieldPtr<TypeDexCacheType*>(ResolvedTypesOffset());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700260 }
261
Vladimir Markoec786222016-12-20 16:24:13 +0000262 void SetResolvedTypes(TypeDexCacheType* resolved_types)
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800263 ALWAYS_INLINE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700264 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800265 SetFieldPtr<false>(ResolvedTypesOffset(), resolved_types);
266 }
267
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700268 ArtMethod** GetResolvedMethods() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100269 return GetFieldPtr<ArtMethod**>(ResolvedMethodsOffset());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700270 }
271
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800272 void SetResolvedMethods(ArtMethod** resolved_methods)
273 ALWAYS_INLINE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700274 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800275 SetFieldPtr<false>(ResolvedMethodsOffset(), resolved_methods);
276 }
277
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700278 ArtField** GetResolvedFields() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100279 return GetFieldPtr<ArtField**>(ResolvedFieldsOffset());
280 }
281
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800282 void SetResolvedFields(ArtField** resolved_fields)
283 ALWAYS_INLINE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700284 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800285 SetFieldPtr<false>(ResolvedFieldsOffset(), resolved_fields);
286 }
287
Narayan Kamath25352fc2016-08-03 12:46:58 +0100288 MethodTypeDexCacheType* GetResolvedMethodTypes()
289 ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Narayan Kamath7fe56582016-10-14 18:49:12 +0100290 return GetFieldPtr64<MethodTypeDexCacheType*>(ResolvedMethodTypesOffset());
Narayan Kamath25352fc2016-08-03 12:46:58 +0100291 }
292
293 void SetResolvedMethodTypes(MethodTypeDexCacheType* resolved_method_types)
294 ALWAYS_INLINE
295 REQUIRES_SHARED(Locks::mutator_lock_) {
296 SetFieldPtr<false>(ResolvedMethodTypesOffset(), resolved_method_types);
297 }
298
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700299 size_t NumStrings() REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100300 return GetField32(NumStringsOffset());
301 }
302
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700303 size_t NumResolvedTypes() REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100304 return GetField32(NumResolvedTypesOffset());
305 }
306
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700307 size_t NumResolvedMethods() REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100308 return GetField32(NumResolvedMethodsOffset());
309 }
310
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700311 size_t NumResolvedFields() REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100312 return GetField32(NumResolvedFieldsOffset());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700313 }
314
Narayan Kamath25352fc2016-08-03 12:46:58 +0100315 size_t NumResolvedMethodTypes() REQUIRES_SHARED(Locks::mutator_lock_) {
316 return GetField32(NumResolvedMethodTypesOffset());
317 }
318
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700319 const DexFile* GetDexFile() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700320 return GetFieldPtr<const DexFile*>(OFFSET_OF_OBJECT_MEMBER(DexCache, dex_file_));
Mathieu Chartier66f19252012-09-18 08:57:04 -0700321 }
322
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700323 void SetDexFile(const DexFile* dex_file) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier76172162016-01-26 14:54:06 -0800324 SetFieldPtr<false>(OFFSET_OF_OBJECT_MEMBER(DexCache, dex_file_), dex_file);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700325 }
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700326
Vladimir Markoec786222016-12-20 16:24:13 +0000327 void SetLocation(ObjPtr<String> location) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier76172162016-01-26 14:54:06 -0800328
Vladimir Marko05792b92015-08-03 11:56:49 +0100329 // NOTE: Get/SetElementPtrSize() are intended for working with ArtMethod** and ArtField**
330 // provided by GetResolvedMethods/Fields() and ArtMethod::GetDexCacheResolvedMethods(),
331 // so they need to be public.
332
333 template <typename PtrType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700334 static PtrType GetElementPtrSize(PtrType* ptr_array, size_t idx, PointerSize ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100335
336 template <typename PtrType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700337 static void SetElementPtrSize(PtrType* ptr_array, size_t idx, PtrType ptr, PointerSize ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100338
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700339 private:
Andreas Gampecc1b5352016-12-01 16:58:38 -0800340 void Init(const DexFile* dex_file,
341 ObjPtr<String> location,
342 StringDexCacheType* strings,
343 uint32_t num_strings,
Vladimir Markoec786222016-12-20 16:24:13 +0000344 TypeDexCacheType* resolved_types,
Andreas Gampecc1b5352016-12-01 16:58:38 -0800345 uint32_t num_resolved_types,
346 ArtMethod** resolved_methods,
347 uint32_t num_resolved_methods,
348 ArtField** resolved_fields,
349 uint32_t num_resolved_fields,
350 MethodTypeDexCacheType* resolved_methodtypes,
351 uint32_t num_resolved_methodtypes,
352 PointerSize pointer_size)
353 REQUIRES_SHARED(Locks::mutator_lock_);
354
Vladimir Markoec786222016-12-20 16:24:13 +0000355 uint32_t StringSlotIndex(dex::StringIndex string_idx) REQUIRES_SHARED(Locks::mutator_lock_);
356 uint32_t TypeSlotIndex(dex::TypeIndex type_idx) REQUIRES_SHARED(Locks::mutator_lock_);
357 uint32_t MethodTypeSlotIndex(uint32_t proto_idx) REQUIRES_SHARED(Locks::mutator_lock_);
358
Vladimir Marko05792b92015-08-03 11:56:49 +0100359 // Visit instance fields of the dex cache as well as its associated arrays.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800360 template <bool kVisitNativeRoots,
361 VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
362 ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
363 typename Visitor>
Vladimir Markoec786222016-12-20 16:24:13 +0000364 void VisitReferences(ObjPtr<Class> klass, const Visitor& visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700365 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_);
Vladimir Marko05792b92015-08-03 11:56:49 +0100366
Ian Rogersef7d42f2014-01-06 12:55:46 -0800367 HeapReference<Object> dex_;
368 HeapReference<String> location_;
Narayan Kamath25352fc2016-08-03 12:46:58 +0100369 uint64_t dex_file_; // const DexFile*
370 uint64_t resolved_fields_; // ArtField*, array with num_resolved_fields_ elements.
371 uint64_t resolved_method_types_; // std::atomic<MethodTypeDexCachePair>* array with
372 // num_resolved_method_types_ elements.
373 uint64_t resolved_methods_; // ArtMethod*, array with num_resolved_methods_ elements.
Vladimir Markoec786222016-12-20 16:24:13 +0000374 uint64_t resolved_types_; // TypeDexCacheType*, array with num_resolved_types_ elements.
Narayan Kamath25352fc2016-08-03 12:46:58 +0100375 uint64_t strings_; // std::atomic<StringDexCachePair>*, array with num_strings_
376 // elements.
377
378 uint32_t num_resolved_fields_; // Number of elements in the resolved_fields_ array.
379 uint32_t num_resolved_method_types_; // Number of elements in the resolved_method_types_ array.
380 uint32_t num_resolved_methods_; // Number of elements in the resolved_methods_ array.
381 uint32_t num_resolved_types_; // Number of elements in the resolved_types_ array.
382 uint32_t num_strings_; // Number of elements in the strings_ array.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700383
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700384 friend struct art::DexCacheOffsets; // for verifying offset information
Vladimir Marko05792b92015-08-03 11:56:49 +0100385 friend class Object; // For VisitReferences
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700386 DISALLOW_IMPLICIT_CONSTRUCTORS(DexCache);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700387};
388
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800389} // namespace mirror
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700390} // namespace art
391
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700392#endif // ART_RUNTIME_MIRROR_DEX_CACHE_H_