Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 16 | |
| 17 | #ifndef ART_SRC_DEX_CACHE_H_ |
| 18 | #define ART_SRC_DEX_CACHE_H_ |
| 19 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 20 | #include "dex_file.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 21 | #include "globals.h" |
| 22 | #include "macros.h" |
| 23 | #include "object.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | class Class; |
| 28 | class Field; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 29 | class ImageWriter; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 30 | class Method; |
| 31 | class String; |
| 32 | union JValue; |
| 33 | |
Brian Carlstrom | e4f7e1d | 2011-09-11 13:21:12 -0700 | [diff] [blame] | 34 | class MANAGED DexCache : public ObjectArray<Object> { |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 35 | public: |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 36 | void Init(String* location, |
| 37 | ObjectArray<String>* strings, |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 38 | ObjectArray<Class>* types, |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 39 | ObjectArray<Method>* methods, |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 40 | ObjectArray<Field>* fields, |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 41 | ObjectArray<StaticStorageBase>* initialized_static_storage); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 42 | |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 43 | void Fixup(Method* trampoline); |
| 44 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 45 | String* GetLocation() const { |
| 46 | return Get(kLocation)->AsString(); |
| 47 | } |
| 48 | |
buzbee | 5cd2180 | 2011-08-26 10:40:14 -0700 | [diff] [blame] | 49 | static MemberOffset StringsOffset() { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 50 | return MemberOffset(DataOffset(sizeof(Object*)).Int32Value() + |
buzbee | 5cd2180 | 2011-08-26 10:40:14 -0700 | [diff] [blame] | 51 | kStrings * sizeof(Object*)); |
buzbee | c5ef046 | 2011-08-25 18:44:49 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 54 | static MemberOffset ResolvedFieldsOffset() { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 55 | return MemberOffset(DataOffset(sizeof(Object*)).Int32Value() + |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 56 | kResolvedFields * sizeof(Object*)); |
buzbee | c5ef046 | 2011-08-25 18:44:49 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 59 | static MemberOffset ResolvedMethodsOffset() { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 60 | return MemberOffset(DataOffset(sizeof(Object*)).Int32Value() + |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 61 | kResolvedMethods * sizeof(Object*)); |
buzbee | c5ef046 | 2011-08-25 18:44:49 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 64 | size_t NumStrings() const { |
| 65 | return GetStrings()->GetLength(); |
| 66 | } |
| 67 | |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 68 | size_t NumResolvedTypes() const { |
| 69 | return GetResolvedTypes()->GetLength(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 72 | size_t NumResolvedMethods() const { |
| 73 | return GetResolvedMethods()->GetLength(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 76 | size_t NumResolvedFields() const { |
| 77 | return GetResolvedFields()->GetLength(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 80 | size_t NumInitializedStaticStorage() const { |
| 81 | return GetInitializedStaticStorage()->GetLength(); |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 84 | String* GetResolvedString(uint32_t string_idx) const { |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 85 | return GetStrings()->Get(string_idx); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 88 | void SetResolvedString(uint32_t string_idx, String* resolved) { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 89 | GetStrings()->Set(string_idx, resolved); |
| 90 | } |
| 91 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 92 | Class* GetResolvedType(uint32_t type_idx) const { |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 93 | return GetResolvedTypes()->Get(type_idx); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 96 | void SetResolvedType(uint32_t type_idx, Class* resolved) { |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 97 | GetResolvedTypes()->Set(type_idx, resolved); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | Method* GetResolvedMethod(uint32_t method_idx) const { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 101 | 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 Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void SetResolvedMethod(uint32_t method_idx, Method* resolved) { |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 112 | GetResolvedMethods()->Set(method_idx, resolved); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | Field* GetResolvedField(uint32_t field_idx) const { |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 116 | return GetResolvedFields()->Get(field_idx); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 119 | void SetResolvedField(uint32_t field_idx, Field* resolved) { |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 120 | GetResolvedFields()->Set(field_idx, resolved); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 123 | ObjectArray<String>* GetStrings() const { |
| 124 | return static_cast<ObjectArray<String>*>(GetNonNull(kStrings)); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 125 | } |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 126 | ObjectArray<Class>* GetResolvedTypes() const { |
| 127 | return static_cast<ObjectArray<Class>*>(GetNonNull(kResolvedTypes)); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 128 | } |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 129 | ObjectArray<Method>* GetResolvedMethods() const { |
| 130 | return static_cast<ObjectArray<Method>*>(GetNonNull(kResolvedMethods)); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 131 | } |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 132 | ObjectArray<Field>* GetResolvedFields() const { |
| 133 | return static_cast<ObjectArray<Field>*>(GetNonNull(kResolvedFields)); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 134 | } |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 135 | ObjectArray<StaticStorageBase>* GetInitializedStaticStorage() const { |
| 136 | return static_cast<ObjectArray<StaticStorageBase>*>(GetNonNull(kInitializedStaticStorage)); |
| 137 | } |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 138 | |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 139 | static size_t LengthAsArray() { |
| 140 | return kMax; |
| 141 | } |
Brian Carlstrom | c4fa2c0 | 2011-08-21 03:00:12 -0700 | [diff] [blame] | 142 | |
| 143 | private: |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 144 | |
| 145 | enum ArrayIndex { |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 146 | kLocation = 0, |
| 147 | kStrings = 1, |
| 148 | kResolvedTypes = 2, |
| 149 | kResolvedMethods = 3, |
| 150 | kResolvedFields = 4, |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 151 | kInitializedStaticStorage = 5, |
| 152 | kMax = 6, |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
Brian Carlstrom | c4fa2c0 | 2011-08-21 03:00:12 -0700 | [diff] [blame] | 155 | 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 Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 161 | }; |
| 162 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 163 | } // namespace art |
| 164 | |
| 165 | #endif // ART_SRC_DEX_CACHE_H_ |