blob: 3c947ab37bc913ec79ec098184e1589511249f29 [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
Andreas Gampe58a5af82014-07-31 16:23:49 -070020#include "art_field.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070021#include "art_method.h"
Andreas Gampe58a5af82014-07-31 16:23:49 -070022#include "class.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
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028struct DexCacheOffsets;
29class DexFile;
30class ImageWriter;
31union JValue;
32
33namespace mirror {
34
Mingyao Yang98d1cc82014-05-15 17:02:16 -070035class String;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070036
Mingyao Yang98d1cc82014-05-15 17:02:16 -070037// C++ mirror of java.lang.DexCache.
38class MANAGED DexCache FINAL : public Object {
Brian Carlstrom83db7722011-08-26 17:32:56 -070039 public:
Mingyao Yang98d1cc82014-05-15 17:02:16 -070040 // Size of java.lang.DexCache.class.
41 static uint32_t ClassSize();
42
43 // Size of an instance of java.lang.DexCache not including referenced values.
44 static constexpr uint32_t InstanceSize() {
45 return sizeof(DexCache);
46 }
47
Mathieu Chartier66f19252012-09-18 08:57:04 -070048 void Init(const DexFile* dex_file,
49 String* location,
Brian Carlstroma663ea52011-08-19 23:33:41 -070050 ObjectArray<String>* strings,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070051 ObjectArray<Class>* types,
Brian Carlstromea46f952013-07-30 01:26:50 -070052 ObjectArray<ArtMethod>* methods,
Ian Rogers5ddb4102014-01-07 08:58:46 -080053 ObjectArray<ArtField>* fields)
Ian Rogersb726dcb2012-09-05 08:57:23 -070054 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070055
Brian Carlstromea46f952013-07-30 01:26:50 -070056 void Fixup(ArtMethod* trampoline) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -080057
Ian Rogersef7d42f2014-01-06 12:55:46 -080058 String* GetLocation() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070059 return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(DexCache, location_));
Brian Carlstroma663ea52011-08-19 23:33:41 -070060 }
61
buzbee5cd21802011-08-26 10:40:14 -070062 static MemberOffset StringsOffset() {
Mathieu Chartier66f19252012-09-18 08:57:04 -070063 return OFFSET_OF_OBJECT_MEMBER(DexCache, strings_);
buzbeec5ef0462011-08-25 18:44:49 -070064 }
65
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070066 static MemberOffset ResolvedFieldsOffset() {
Mathieu Chartier66f19252012-09-18 08:57:04 -070067 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_fields_);
buzbeec5ef0462011-08-25 18:44:49 -070068 }
69
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070070 static MemberOffset ResolvedMethodsOffset() {
Mathieu Chartier66f19252012-09-18 08:57:04 -070071 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_methods_);
buzbeec5ef0462011-08-25 18:44:49 -070072 }
73
Ian Rogersef7d42f2014-01-06 12:55:46 -080074 size_t NumStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070075 return GetStrings()->GetLength();
76 }
77
Ian Rogersef7d42f2014-01-06 12:55:46 -080078 size_t NumResolvedTypes() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070079 return GetResolvedTypes()->GetLength();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070080 }
81
Ian Rogersef7d42f2014-01-06 12:55:46 -080082 size_t NumResolvedMethods() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070083 return GetResolvedMethods()->GetLength();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070084 }
85
Ian Rogersef7d42f2014-01-06 12:55:46 -080086 size_t NumResolvedFields() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070087 return GetResolvedFields()->GetLength();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070088 }
89
Ian Rogersef7d42f2014-01-06 12:55:46 -080090 String* GetResolvedString(uint32_t string_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -070091 return GetStrings()->Get(string_idx);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070092 }
93
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070094 void SetResolvedString(uint32_t string_idx, String* resolved) ALWAYS_INLINE
Ian Rogersb726dcb2012-09-05 08:57:23 -070095 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010096 // TODO default transaction support.
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070097 GetStrings()->Set(string_idx, resolved);
98 }
99
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700100 Class* GetResolvedType(uint32_t type_idx) ALWAYS_INLINE
101 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700102 return GetResolvedTypes()->Get(type_idx);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700103 }
104
Andreas Gampeaa910d52014-07-30 18:59:05 -0700105 void SetResolvedType(uint32_t type_idx, Class* resolved)
106 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700107
Ian Rogersef7d42f2014-01-06 12:55:46 -0800108 ArtMethod* GetResolvedMethod(uint32_t method_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700109
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700110 void SetResolvedMethod(uint32_t method_idx, ArtMethod* resolved) ALWAYS_INLINE
Ian Rogersb726dcb2012-09-05 08:57:23 -0700111 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700112 GetResolvedMethods()->Set(method_idx, resolved);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700113 }
114
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700115 ArtField* GetResolvedField(uint32_t field_idx) ALWAYS_INLINE
116 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700117 ArtField* field = GetResolvedFields()->Get(field_idx);
118 if (UNLIKELY(field == nullptr || field->GetDeclaringClass()->IsErroneous())) {
119 return nullptr;
120 } else {
121 return field;
122 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700123 }
124
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700125 void SetResolvedField(uint32_t field_idx, ArtField* resolved) ALWAYS_INLINE
Ian Rogersb726dcb2012-09-05 08:57:23 -0700126 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700127 GetResolvedFields()->Set(field_idx, resolved);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700128 }
129
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700130 ObjectArray<String>* GetStrings() ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers700a4022014-05-19 16:49:03 -0700131 return GetFieldObject< ObjectArray<String>>(StringsOffset());
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700132 }
Brian Carlstrom83db7722011-08-26 17:32:56 -0700133
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700134 ObjectArray<Class>* GetResolvedTypes() ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers700a4022014-05-19 16:49:03 -0700135 return GetFieldObject<ObjectArray<Class>>(
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700136 OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_types_));
Mathieu Chartier66f19252012-09-18 08:57:04 -0700137 }
138
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700139 ObjectArray<ArtMethod>* GetResolvedMethods() ALWAYS_INLINE
140 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers700a4022014-05-19 16:49:03 -0700141 return GetFieldObject< ObjectArray<ArtMethod>>(ResolvedMethodsOffset());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700142 }
143
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700144 ObjectArray<ArtField>* GetResolvedFields() ALWAYS_INLINE
145 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers700a4022014-05-19 16:49:03 -0700146 return GetFieldObject<ObjectArray<ArtField>>(ResolvedFieldsOffset());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700147 }
148
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700149 const DexFile* GetDexFile() ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
150 return GetFieldPtr<const DexFile*>(OFFSET_OF_OBJECT_MEMBER(DexCache, dex_file_));
Mathieu Chartier66f19252012-09-18 08:57:04 -0700151 }
152
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700153 void SetDexFile(const DexFile* dex_file) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
154 ALWAYS_INLINE {
155 return SetFieldPtr<false>(OFFSET_OF_OBJECT_MEMBER(DexCache, dex_file_), dex_file);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700156 }
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700157
158 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800159 HeapReference<Object> dex_;
160 HeapReference<String> location_;
Ian Rogers700a4022014-05-19 16:49:03 -0700161 HeapReference<ObjectArray<ArtField>> resolved_fields_;
162 HeapReference<ObjectArray<ArtMethod>> resolved_methods_;
163 HeapReference<ObjectArray<Class>> resolved_types_;
164 HeapReference<ObjectArray<String>> strings_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800165 uint64_t dex_file_;
Brian Carlstrom83db7722011-08-26 17:32:56 -0700166
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700167 friend struct art::DexCacheOffsets; // for verifying offset information
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700168 DISALLOW_IMPLICIT_CONSTRUCTORS(DexCache);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700169};
170
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171} // namespace mirror
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700172} // namespace art
173
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700174#endif // ART_RUNTIME_MIRROR_DEX_CACHE_H_