blob: 2fcabb507692b26934020ef33560f5e0c080a997 [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"
Andreas Gampe58a5af82014-07-31 16:23:49 -070021#include "art_field.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070022#include "art_method.h"
Andreas Gampe58a5af82014-07-31 16:23:49 -070023#include "class.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070024#include "object.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "object_array.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070026
27namespace art {
28
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029struct DexCacheOffsets;
30class DexFile;
31class ImageWriter;
32union JValue;
33
34namespace mirror {
35
Narayan Kamath25352fc2016-08-03 12:46:58 +010036class MethodType;
Mingyao Yang98d1cc82014-05-15 17:02:16 -070037class String;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070038
Narayan Kamathc38a6f82016-09-29 17:07:20 +010039template <typename T> struct PACKED(8) DexCachePair {
40 GcRoot<T> object;
41 uint32_t index;
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070042 // The array is initially [ {0,0}, {0,0}, {0,0} ... ]
43 // We maintain the invariant that once a dex cache entry is populated,
44 // the pointer is always non-0
45 // Any given entry would thus be:
46 // {non-0, non-0} OR {0,0}
47 //
48 // It's generally sufficiently enough then to check if the
Narayan Kamathc38a6f82016-09-29 17:07:20 +010049 // lookup index matches the stored index (for a >0 lookup index)
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070050 // because if it's true the pointer is also non-null.
51 //
52 // For the 0th entry which is a special case, the value is either
53 // {0,0} (initial state) or {non-0, 0} which indicates
Narayan Kamathc38a6f82016-09-29 17:07:20 +010054 // that a valid object is stored at that index for a dex section id of 0.
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070055 //
Narayan Kamathc38a6f82016-09-29 17:07:20 +010056 // As an optimization, we want to avoid branching on the object pointer since
57 // it's always non-null if the id branch succeeds (except for the 0th id).
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070058 // Set the initial state for the 0th entry to be {0,1} which is guaranteed to fail
Narayan Kamathc38a6f82016-09-29 17:07:20 +010059 // the lookup id == stored id branch.
60 DexCachePair(T* object, uint32_t index)
61 : object(object),
62 index(index) {}
63 DexCachePair() = default;
64 DexCachePair(const DexCachePair<T>&) = default;
65 DexCachePair& operator=(const DexCachePair<T>&) = default;
Mathieu Chartierbb816d62016-09-07 10:17:46 -070066
Narayan Kamathc38a6f82016-09-29 17:07:20 +010067 static void Initialize(std::atomic<DexCachePair<T>>* dex_cache) {
68 DexCachePair<T> first_elem;
69 first_elem.object = GcRoot<T>(nullptr);
70 first_elem.index = InvalidIndexForSlot(0);
71 dex_cache[0].store(first_elem, std::memory_order_relaxed);
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070072 }
Mathieu Chartierbb816d62016-09-07 10:17:46 -070073
Narayan Kamathc38a6f82016-09-29 17:07:20 +010074 static GcRoot<T> Lookup(std::atomic<DexCachePair<T>>* dex_cache,
75 uint32_t idx,
76 uint32_t cache_size) {
77 DexCachePair<T> element = dex_cache[idx % cache_size].load(std::memory_order_relaxed);
78 if (idx != element.index) {
79 return GcRoot<T>(nullptr);
80 }
81
82 DCHECK(!element.object.IsNull());
83 return element.object;
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070084 }
Mathieu Chartierbb816d62016-09-07 10:17:46 -070085
Narayan Kamath23136d12016-09-30 16:29:19 +010086 static void Assign(std::atomic<DexCachePair<T>>* dex_cache,
87 uint32_t idx,
88 T* object,
89 uint32_t cache_size) {
90 DCHECK_LT(idx % cache_size, cache_size);
91 dex_cache[idx % cache_size].store(
92 DexCachePair<T>(object, idx), std::memory_order_relaxed);
93 }
94
Narayan Kamathc38a6f82016-09-29 17:07:20 +010095 static uint32_t InvalidIndexForSlot(uint32_t slot) {
Mathieu Chartierbb816d62016-09-07 10:17:46 -070096 // Since the cache size is a power of two, 0 will always map to slot 0.
97 // Use 1 for slot 0 and 0 for all other slots.
98 return (slot == 0) ? 1u : 0u;
99 }
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700100};
Narayan Kamathc38a6f82016-09-29 17:07:20 +0100101
102using StringDexCachePair = DexCachePair<mirror::String>;
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700103using StringDexCacheType = std::atomic<StringDexCachePair>;
104
Narayan Kamath25352fc2016-08-03 12:46:58 +0100105using MethodTypeDexCachePair = DexCachePair<mirror::MethodType>;
106using MethodTypeDexCacheType = std::atomic<MethodTypeDexCachePair>;
107
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700108// C++ mirror of java.lang.DexCache.
109class MANAGED DexCache FINAL : public Object {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700110 public:
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700111 // Size of java.lang.DexCache.class.
Andreas Gampe542451c2016-07-26 09:02:02 -0700112 static uint32_t ClassSize(PointerSize pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700113
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700114 // Size of string dex cache. Needs to be a power of 2 for entrypoint assumptions to hold.
115 static constexpr size_t kDexCacheStringCacheSize = 1024;
116 static_assert(IsPowerOfTwo(kDexCacheStringCacheSize),
117 "String dex cache size is not a power of 2.");
118
Narayan Kamath25352fc2016-08-03 12:46:58 +0100119 // Size of method type dex cache. Needs to be a power of 2 for entrypoint assumptions
120 // to hold.
121 static constexpr size_t kDexCacheMethodTypeCacheSize = 1024;
122 static_assert(IsPowerOfTwo(kDexCacheMethodTypeCacheSize),
123 "MethodType dex cache size is not a power of 2.");
124
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700125 static constexpr size_t StaticStringSize() {
126 return kDexCacheStringCacheSize;
127 }
128
Narayan Kamath25352fc2016-08-03 12:46:58 +0100129 static constexpr size_t StaticMethodTypeSize() {
130 return kDexCacheMethodTypeCacheSize;
131 }
132
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700133 // Size of an instance of java.lang.DexCache not including referenced values.
134 static constexpr uint32_t InstanceSize() {
135 return sizeof(DexCache);
136 }
137
Vladimir Marko05792b92015-08-03 11:56:49 +0100138 void Init(const DexFile* dex_file,
139 String* location,
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700140 StringDexCacheType* strings,
Vladimir Marko05792b92015-08-03 11:56:49 +0100141 uint32_t num_strings,
142 GcRoot<Class>* resolved_types,
143 uint32_t num_resolved_types,
144 ArtMethod** resolved_methods,
145 uint32_t num_resolved_methods,
146 ArtField** resolved_fields,
147 uint32_t num_resolved_fields,
Narayan Kamath25352fc2016-08-03 12:46:58 +0100148 MethodTypeDexCacheType* resolved_methodtypes,
149 uint32_t num_resolved_methodtypes,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700150 PointerSize pointer_size) REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700151
Andreas Gampe542451c2016-07-26 09:02:02 -0700152 void Fixup(ArtMethod* trampoline, PointerSize pointer_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700153 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -0800154
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800155 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier, typename Visitor>
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700156 void FixupStrings(StringDexCacheType* dest, const Visitor& visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700157 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800158
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800159 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier, typename Visitor>
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800160 void FixupResolvedTypes(GcRoot<mirror::Class>* dest, const Visitor& visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700161 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800162
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700163 String* GetLocation() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700164 return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(DexCache, location_));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700165 }
166
Andreas Gampedd9d0552015-03-09 12:57:41 -0700167 static MemberOffset DexOffset() {
168 return OFFSET_OF_OBJECT_MEMBER(DexCache, dex_);
169 }
170
buzbee5cd21802011-08-26 10:40:14 -0700171 static MemberOffset StringsOffset() {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700172 return OFFSET_OF_OBJECT_MEMBER(DexCache, strings_);
buzbeec5ef0462011-08-25 18:44:49 -0700173 }
174
Vladimir Marko05792b92015-08-03 11:56:49 +0100175 static MemberOffset ResolvedTypesOffset() {
176 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_types_);
177 }
178
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700179 static MemberOffset ResolvedFieldsOffset() {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700180 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_fields_);
buzbeec5ef0462011-08-25 18:44:49 -0700181 }
182
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700183 static MemberOffset ResolvedMethodsOffset() {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700184 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_methods_);
buzbeec5ef0462011-08-25 18:44:49 -0700185 }
186
Narayan Kamath25352fc2016-08-03 12:46:58 +0100187 static MemberOffset ResolvedMethodTypesOffset() {
188 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_method_types_);
189 }
190
Vladimir Marko05792b92015-08-03 11:56:49 +0100191 static MemberOffset NumStringsOffset() {
192 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_strings_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700193 }
194
Vladimir Marko05792b92015-08-03 11:56:49 +0100195 static MemberOffset NumResolvedTypesOffset() {
196 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_resolved_types_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700197 }
198
Vladimir Marko05792b92015-08-03 11:56:49 +0100199 static MemberOffset NumResolvedFieldsOffset() {
200 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_resolved_fields_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700201 }
202
Vladimir Marko05792b92015-08-03 11:56:49 +0100203 static MemberOffset NumResolvedMethodsOffset() {
204 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_resolved_methods_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700205 }
206
Narayan Kamath25352fc2016-08-03 12:46:58 +0100207 static MemberOffset NumResolvedMethodTypesOffset() {
208 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_resolved_method_types_);
209 }
210
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700211 mirror::String* GetResolvedString(uint32_t string_idx) ALWAYS_INLINE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700212 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700213
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700214 void SetResolvedString(uint32_t string_idx, mirror::String* resolved) ALWAYS_INLINE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700215 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700216
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700217 // Clear a string for a string_idx, used to undo string intern transactions to make sure
218 // the string isn't kept live.
219 void ClearString(uint32_t string_idx) REQUIRES_SHARED(Locks::mutator_lock_);
220
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700221 Class* GetResolvedType(uint32_t type_idx) REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko05792b92015-08-03 11:56:49 +0100222
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700223 void SetResolvedType(uint32_t type_idx, Class* resolved) REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko05792b92015-08-03 11:56:49 +0100224
Andreas Gampe542451c2016-07-26 09:02:02 -0700225 ALWAYS_INLINE ArtMethod* GetResolvedMethod(uint32_t method_idx, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700226 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700227
Andreas Gampe542451c2016-07-26 09:02:02 -0700228 ALWAYS_INLINE void SetResolvedMethod(uint32_t method_idx,
229 ArtMethod* resolved,
230 PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700231 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700232
Mathieu Chartierc7853442015-03-27 14:35:38 -0700233 // Pointer sized variant, used for patching.
Andreas Gampe542451c2016-07-26 09:02:02 -0700234 ALWAYS_INLINE ArtField* GetResolvedField(uint32_t idx, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700235 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700236
237 // Pointer sized variant, used for patching.
Andreas Gampe542451c2016-07-26 09:02:02 -0700238 ALWAYS_INLINE void SetResolvedField(uint32_t idx, ArtField* field, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700239 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700240
Narayan Kamath25352fc2016-08-03 12:46:58 +0100241 MethodType* GetResolvedMethodType(uint32_t proto_idx) REQUIRES_SHARED(Locks::mutator_lock_);
242
243 void SetResolvedMethodType(uint32_t proto_idx, MethodType* resolved) REQUIRES_SHARED(Locks::mutator_lock_);
244
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700245 StringDexCacheType* GetStrings() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700246 return GetFieldPtr64<StringDexCacheType*>(StringsOffset());
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700247 }
Brian Carlstrom83db7722011-08-26 17:32:56 -0700248
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700249 void SetStrings(StringDexCacheType* strings) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800250 SetFieldPtr<false>(StringsOffset(), strings);
251 }
252
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700253 GcRoot<Class>* GetResolvedTypes() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100254 return GetFieldPtr<GcRoot<Class>*>(ResolvedTypesOffset());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700255 }
256
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800257 void SetResolvedTypes(GcRoot<Class>* resolved_types)
258 ALWAYS_INLINE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700259 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800260 SetFieldPtr<false>(ResolvedTypesOffset(), resolved_types);
261 }
262
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700263 ArtMethod** GetResolvedMethods() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100264 return GetFieldPtr<ArtMethod**>(ResolvedMethodsOffset());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700265 }
266
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800267 void SetResolvedMethods(ArtMethod** resolved_methods)
268 ALWAYS_INLINE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700269 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800270 SetFieldPtr<false>(ResolvedMethodsOffset(), resolved_methods);
271 }
272
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700273 ArtField** GetResolvedFields() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100274 return GetFieldPtr<ArtField**>(ResolvedFieldsOffset());
275 }
276
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800277 void SetResolvedFields(ArtField** resolved_fields)
278 ALWAYS_INLINE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700279 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800280 SetFieldPtr<false>(ResolvedFieldsOffset(), resolved_fields);
281 }
282
Narayan Kamath25352fc2016-08-03 12:46:58 +0100283 MethodTypeDexCacheType* GetResolvedMethodTypes()
284 ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
285 return GetFieldPtr<MethodTypeDexCacheType*>(ResolvedMethodTypesOffset());
286 }
287
288 void SetResolvedMethodTypes(MethodTypeDexCacheType* resolved_method_types)
289 ALWAYS_INLINE
290 REQUIRES_SHARED(Locks::mutator_lock_) {
291 SetFieldPtr<false>(ResolvedMethodTypesOffset(), resolved_method_types);
292 }
293
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700294 size_t NumStrings() REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100295 return GetField32(NumStringsOffset());
296 }
297
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700298 size_t NumResolvedTypes() REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100299 return GetField32(NumResolvedTypesOffset());
300 }
301
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700302 size_t NumResolvedMethods() REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100303 return GetField32(NumResolvedMethodsOffset());
304 }
305
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700306 size_t NumResolvedFields() REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100307 return GetField32(NumResolvedFieldsOffset());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700308 }
309
Narayan Kamath25352fc2016-08-03 12:46:58 +0100310 size_t NumResolvedMethodTypes() REQUIRES_SHARED(Locks::mutator_lock_) {
311 return GetField32(NumResolvedMethodTypesOffset());
312 }
313
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700314 const DexFile* GetDexFile() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700315 return GetFieldPtr<const DexFile*>(OFFSET_OF_OBJECT_MEMBER(DexCache, dex_file_));
Mathieu Chartier66f19252012-09-18 08:57:04 -0700316 }
317
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700318 void SetDexFile(const DexFile* dex_file) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier76172162016-01-26 14:54:06 -0800319 SetFieldPtr<false>(OFFSET_OF_OBJECT_MEMBER(DexCache, dex_file_), dex_file);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700320 }
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700321
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700322 void SetLocation(mirror::String* location) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier76172162016-01-26 14:54:06 -0800323
Vladimir Marko05792b92015-08-03 11:56:49 +0100324 // NOTE: Get/SetElementPtrSize() are intended for working with ArtMethod** and ArtField**
325 // provided by GetResolvedMethods/Fields() and ArtMethod::GetDexCacheResolvedMethods(),
326 // so they need to be public.
327
328 template <typename PtrType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700329 static PtrType GetElementPtrSize(PtrType* ptr_array, size_t idx, PointerSize ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100330
331 template <typename PtrType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700332 static void SetElementPtrSize(PtrType* ptr_array, size_t idx, PtrType ptr, PointerSize ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100333
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700334 private:
Vladimir Marko05792b92015-08-03 11:56:49 +0100335 // Visit instance fields of the dex cache as well as its associated arrays.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800336 template <bool kVisitNativeRoots,
337 VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
338 ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
339 typename Visitor>
Vladimir Marko05792b92015-08-03 11:56:49 +0100340 void VisitReferences(mirror::Class* klass, const Visitor& visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700341 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_);
Vladimir Marko05792b92015-08-03 11:56:49 +0100342
Ian Rogersef7d42f2014-01-06 12:55:46 -0800343 HeapReference<Object> dex_;
344 HeapReference<String> location_;
Narayan Kamath25352fc2016-08-03 12:46:58 +0100345 uint64_t dex_file_; // const DexFile*
346 uint64_t resolved_fields_; // ArtField*, array with num_resolved_fields_ elements.
347 uint64_t resolved_method_types_; // std::atomic<MethodTypeDexCachePair>* array with
348 // num_resolved_method_types_ elements.
349 uint64_t resolved_methods_; // ArtMethod*, array with num_resolved_methods_ elements.
350 uint64_t resolved_types_; // GcRoot<Class>*, array with num_resolved_types_ elements.
351 uint64_t strings_; // std::atomic<StringDexCachePair>*, array with num_strings_
352 // elements.
353
354 uint32_t num_resolved_fields_; // Number of elements in the resolved_fields_ array.
355 uint32_t num_resolved_method_types_; // Number of elements in the resolved_method_types_ array.
356 uint32_t num_resolved_methods_; // Number of elements in the resolved_methods_ array.
357 uint32_t num_resolved_types_; // Number of elements in the resolved_types_ array.
358 uint32_t num_strings_; // Number of elements in the strings_ array.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700359
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700360 friend struct art::DexCacheOffsets; // for verifying offset information
Vladimir Marko05792b92015-08-03 11:56:49 +0100361 friend class Object; // For VisitReferences
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700362 DISALLOW_IMPLICIT_CONSTRUCTORS(DexCache);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700363};
364
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800365} // namespace mirror
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700366} // namespace art
367
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700368#endif // ART_RUNTIME_MIRROR_DEX_CACHE_H_