blob: 256063882539d020e68040bb59b6670346ca46d9 [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;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070030class Method;
31class String;
32union JValue;
33
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -070034class MANAGED DexCache : public ObjectArray<Object> {
Brian Carlstrom83db7722011-08-26 17:32:56 -070035 public:
Brian Carlstroma663ea52011-08-19 23:33:41 -070036 void Init(String* location,
37 ObjectArray<String>* strings,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070038 ObjectArray<Class>* types,
Brian Carlstrom4a96b602011-07-26 16:40:23 -070039 ObjectArray<Method>* methods,
Brian Carlstrom83db7722011-08-26 17:32:56 -070040 ObjectArray<Field>* fields,
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070041 ObjectArray<StaticStorageBase>* initialized_static_storage);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070042
Ian Rogers19846512012-02-24 11:42:47 -080043 void Fixup(Method* trampoline);
44
Brian Carlstroma663ea52011-08-19 23:33:41 -070045 String* GetLocation() const {
46 return Get(kLocation)->AsString();
47 }
48
buzbee5cd21802011-08-26 10:40:14 -070049 static MemberOffset StringsOffset() {
Ian Rogersa15e67d2012-02-28 13:51:55 -080050 return MemberOffset(DataOffset(sizeof(Object*)).Int32Value() +
buzbee5cd21802011-08-26 10:40:14 -070051 kStrings * sizeof(Object*));
buzbeec5ef0462011-08-25 18:44:49 -070052 }
53
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070054 static MemberOffset ResolvedFieldsOffset() {
Ian Rogersa15e67d2012-02-28 13:51:55 -080055 return MemberOffset(DataOffset(sizeof(Object*)).Int32Value() +
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070056 kResolvedFields * sizeof(Object*));
buzbeec5ef0462011-08-25 18:44:49 -070057 }
58
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070059 static MemberOffset ResolvedMethodsOffset() {
Ian Rogersa15e67d2012-02-28 13:51:55 -080060 return MemberOffset(DataOffset(sizeof(Object*)).Int32Value() +
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070061 kResolvedMethods * sizeof(Object*));
buzbeec5ef0462011-08-25 18:44:49 -070062 }
63
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070064 size_t NumStrings() const {
65 return GetStrings()->GetLength();
66 }
67
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070068 size_t NumResolvedTypes() const {
69 return GetResolvedTypes()->GetLength();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070070 }
71
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070072 size_t NumResolvedMethods() const {
73 return GetResolvedMethods()->GetLength();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070074 }
75
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070076 size_t NumResolvedFields() const {
77 return GetResolvedFields()->GetLength();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070078 }
79
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070080 size_t NumInitializedStaticStorage() const {
81 return GetInitializedStaticStorage()->GetLength();
Brian Carlstrom83db7722011-08-26 17:32:56 -070082 }
83
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070084 String* GetResolvedString(uint32_t string_idx) const {
Brian Carlstrom4a96b602011-07-26 16:40:23 -070085 return GetStrings()->Get(string_idx);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070086 }
87
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070088 void SetResolvedString(uint32_t string_idx, String* resolved) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070089 GetStrings()->Set(string_idx, resolved);
90 }
91
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070092 Class* GetResolvedType(uint32_t type_idx) const {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070093 return GetResolvedTypes()->Get(type_idx);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070094 }
95
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070096 void SetResolvedType(uint32_t type_idx, Class* resolved) {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070097 GetResolvedTypes()->Set(type_idx, resolved);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070098 }
99
100 Method* GetResolvedMethod(uint32_t method_idx) const {
Ian Rogers19846512012-02-24 11:42:47 -0800101 Method* method = GetResolvedMethods()->Get(method_idx);
102 // Hide resolution trampoline methods from the caller
103 if (method != NULL && method->GetDexMethodIndex() == DexFile::kDexNoIndex16) {
104 DCHECK(method == Runtime::Current()->GetResolutionMethod());
105 return NULL;
106 } else {
107 return method;
108 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700109 }
110
111 void SetResolvedMethod(uint32_t method_idx, Method* resolved) {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700112 GetResolvedMethods()->Set(method_idx, resolved);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700113 }
114
115 Field* GetResolvedField(uint32_t field_idx) const {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700116 return GetResolvedFields()->Get(field_idx);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700117 }
118
Elliott Hughes4a2b4172011-09-20 17:08:25 -0700119 void SetResolvedField(uint32_t field_idx, Field* resolved) {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700120 GetResolvedFields()->Set(field_idx, resolved);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700121 }
122
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700123 ObjectArray<String>* GetStrings() const {
124 return static_cast<ObjectArray<String>*>(GetNonNull(kStrings));
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700125 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700126 ObjectArray<Class>* GetResolvedTypes() const {
127 return static_cast<ObjectArray<Class>*>(GetNonNull(kResolvedTypes));
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700128 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700129 ObjectArray<Method>* GetResolvedMethods() const {
130 return static_cast<ObjectArray<Method>*>(GetNonNull(kResolvedMethods));
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700131 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700132 ObjectArray<Field>* GetResolvedFields() const {
133 return static_cast<ObjectArray<Field>*>(GetNonNull(kResolvedFields));
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700134 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700135 ObjectArray<StaticStorageBase>* GetInitializedStaticStorage() const {
136 return static_cast<ObjectArray<StaticStorageBase>*>(GetNonNull(kInitializedStaticStorage));
137 }
Brian Carlstrom83db7722011-08-26 17:32:56 -0700138
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700139 static size_t LengthAsArray() {
140 return kMax;
141 }
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700142
143 private:
Brian Carlstrom83db7722011-08-26 17:32:56 -0700144
145 enum ArrayIndex {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700146 kLocation = 0,
147 kStrings = 1,
148 kResolvedTypes = 2,
149 kResolvedMethods = 3,
150 kResolvedFields = 4,
Ian Rogers19846512012-02-24 11:42:47 -0800151 kInitializedStaticStorage = 5,
152 kMax = 6,
Brian Carlstrom83db7722011-08-26 17:32:56 -0700153 };
154
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700155 Object* GetNonNull(ArrayIndex array_index) const {
156 Object* obj = Get(array_index);
157 DCHECK(obj != NULL);
158 return obj;
159 }
160 DISALLOW_IMPLICIT_CONSTRUCTORS(DexCache);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700161};
162
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700163} // namespace art
164
165#endif // ART_SRC_DEX_CACHE_H_