blob: 8d88dc6ca4fac2de341562013099cd0afaf49c14 [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
17#ifndef ART_SRC_DEX_CACHE_H_
18#define ART_SRC_DEX_CACHE_H_
19
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070020#include "dex_file.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070021#include "globals.h"
22#include "macros.h"
23#include "object.h"
24
25namespace art {
26
27class Class;
28class Field;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070029class ImageWriter;
Mathieu Chartier66f19252012-09-18 08:57:04 -070030class AbstractMethod;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070031class String;
32union JValue;
33
Mathieu Chartier66f19252012-09-18 08:57:04 -070034class MANAGED DexCacheClass : public Class {
35 private:
36 DISALLOW_IMPLICIT_CONSTRUCTORS(DexCacheClass);
37};
38
39class MANAGED DexCache : public Object {
Brian Carlstrom83db7722011-08-26 17:32:56 -070040 public:
Mathieu Chartier66f19252012-09-18 08:57:04 -070041 void Init(const DexFile* dex_file,
42 String* location,
Brian Carlstroma663ea52011-08-19 23:33:41 -070043 ObjectArray<String>* strings,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070044 ObjectArray<Class>* types,
Mathieu Chartier66f19252012-09-18 08:57:04 -070045 ObjectArray<AbstractMethod>* methods,
Brian Carlstrom83db7722011-08-26 17:32:56 -070046 ObjectArray<Field>* fields,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070047 ObjectArray<StaticStorageBase>* initialized_static_storage)
Ian Rogersb726dcb2012-09-05 08:57:23 -070048 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070049
Mathieu Chartier66f19252012-09-18 08:57:04 -070050 void Fixup(AbstractMethod* trampoline) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -080051
Ian Rogersb726dcb2012-09-05 08:57:23 -070052 String* GetLocation() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -070053 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(DexCache, location_), false);
Brian Carlstroma663ea52011-08-19 23:33:41 -070054 }
55
buzbee5cd21802011-08-26 10:40:14 -070056 static MemberOffset StringsOffset() {
Mathieu Chartier66f19252012-09-18 08:57:04 -070057 return OFFSET_OF_OBJECT_MEMBER(DexCache, strings_);
buzbeec5ef0462011-08-25 18:44:49 -070058 }
59
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070060 static MemberOffset ResolvedFieldsOffset() {
Mathieu Chartier66f19252012-09-18 08:57:04 -070061 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_fields_);
buzbeec5ef0462011-08-25 18:44:49 -070062 }
63
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070064 static MemberOffset ResolvedMethodsOffset() {
Mathieu Chartier66f19252012-09-18 08:57:04 -070065 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_methods_);
buzbeec5ef0462011-08-25 18:44:49 -070066 }
67
Ian Rogersb726dcb2012-09-05 08:57:23 -070068 size_t NumStrings() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070069 return GetStrings()->GetLength();
70 }
71
Ian Rogersb726dcb2012-09-05 08:57:23 -070072 size_t NumResolvedTypes() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070073 return GetResolvedTypes()->GetLength();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070074 }
75
Ian Rogersb726dcb2012-09-05 08:57:23 -070076 size_t NumResolvedMethods() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070077 return GetResolvedMethods()->GetLength();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070078 }
79
Ian Rogersb726dcb2012-09-05 08:57:23 -070080 size_t NumResolvedFields() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070081 return GetResolvedFields()->GetLength();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070082 }
83
Ian Rogers00f7d0e2012-07-19 15:28:27 -070084 size_t NumInitializedStaticStorage() const
Ian Rogersb726dcb2012-09-05 08:57:23 -070085 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070086 return GetInitializedStaticStorage()->GetLength();
Brian Carlstrom83db7722011-08-26 17:32:56 -070087 }
88
Ian Rogers00f7d0e2012-07-19 15:28:27 -070089 String* GetResolvedString(uint32_t string_idx) const
Ian Rogersb726dcb2012-09-05 08:57:23 -070090 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 Rogers00f7d0e2012-07-19 15:28:27 -070094 void SetResolvedString(uint32_t string_idx, String* resolved)
Ian Rogersb726dcb2012-09-05 08:57:23 -070095 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070096 GetStrings()->Set(string_idx, resolved);
97 }
98
Ian Rogers00f7d0e2012-07-19 15:28:27 -070099 Class* GetResolvedType(uint32_t type_idx) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700100 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700101 return GetResolvedTypes()->Get(type_idx);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700102 }
103
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700104 void SetResolvedType(uint32_t type_idx, Class* resolved)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700105 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700106 GetResolvedTypes()->Set(type_idx, resolved);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700107 }
108
Mathieu Chartier66f19252012-09-18 08:57:04 -0700109 AbstractMethod* GetResolvedMethod(uint32_t method_idx) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700110 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700111 AbstractMethod* method = GetResolvedMethods()->Get(method_idx);
Ian Rogers19846512012-02-24 11:42:47 -0800112 // Hide resolution trampoline methods from the caller
113 if (method != NULL && method->GetDexMethodIndex() == DexFile::kDexNoIndex16) {
114 DCHECK(method == Runtime::Current()->GetResolutionMethod());
115 return NULL;
116 } else {
117 return method;
118 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700119 }
120
Mathieu Chartier66f19252012-09-18 08:57:04 -0700121 void SetResolvedMethod(uint32_t method_idx, AbstractMethod* resolved)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700122 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700123 GetResolvedMethods()->Set(method_idx, resolved);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700124 }
125
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700126 Field* GetResolvedField(uint32_t field_idx) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700127 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700128 return GetResolvedFields()->Get(field_idx);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700129 }
130
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700131 void SetResolvedField(uint32_t field_idx, Field* resolved)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700132 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700133 GetResolvedFields()->Set(field_idx, resolved);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700134 }
135
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700136 ObjectArray<String>* GetStrings() const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700137 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700138 return GetFieldObject< ObjectArray<String>* >(StringsOffset(), false);
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700139 }
Brian Carlstrom83db7722011-08-26 17:32:56 -0700140
Mathieu Chartier66f19252012-09-18 08:57:04 -0700141 ObjectArray<Class>* GetResolvedTypes() const
142 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
143 return GetFieldObject< ObjectArray<Class>* >(
144 OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_types_), false);
145 }
146
147 ObjectArray<AbstractMethod>* GetResolvedMethods() const
148 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
149 return GetFieldObject< ObjectArray<AbstractMethod>* >(ResolvedMethodsOffset(), false);
150 }
151
152 ObjectArray<Field>* GetResolvedFields() const
153 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
154 return GetFieldObject< ObjectArray<Field>* >(ResolvedFieldsOffset(), false);
155 }
156
157 ObjectArray<StaticStorageBase>* GetInitializedStaticStorage() const
158 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
159 return GetFieldObject< ObjectArray<StaticStorageBase>* >(
160 OFFSET_OF_OBJECT_MEMBER(DexCache, initialized_static_storage_), false);
161 }
162
163 const DexFile* GetDexFile() const {
164 return GetFieldPtr<const DexFile*>(OFFSET_OF_OBJECT_MEMBER(DexCache, dex_file_), false);
165 }
166
167 void SetDexFile(const DexFile* dex_file) {
168 return SetFieldPtr(OFFSET_OF_OBJECT_MEMBER(DexCache, dex_file_), dex_file, false);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700169 }
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700170
171 private:
Mathieu Chartier66f19252012-09-18 08:57:04 -0700172 ObjectArray<StaticStorageBase>* initialized_static_storage_;
173 String* location_;
174 ObjectArray<Object>* resolved_fields_;
175 ObjectArray<AbstractMethod>* resolved_methods_;
176 ObjectArray<Class>* resolved_types_;
177 ObjectArray<String>* strings_;
178 uint32_t dex_file_;
Brian Carlstrom83db7722011-08-26 17:32:56 -0700179
Mathieu Chartier66f19252012-09-18 08:57:04 -0700180 friend struct DexCacheOffsets; // for verifying offset information
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700181 DISALLOW_IMPLICIT_CONSTRUCTORS(DexCache);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700182};
183
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700184} // namespace art
185
186#endif // ART_SRC_DEX_CACHE_H_