Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_CLASS_LINKER_H_ |
| 4 | #define ART_SRC_CLASS_LINKER_H_ |
| 5 | |
| 6 | #include <map> |
| 7 | #include <utility> |
| 8 | #include <vector> |
| 9 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 10 | #include "dex_file.h" |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 11 | #include "heap.h" |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 12 | #include "macros.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 13 | #include "object.h" |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 14 | #include "thread.h" |
| 15 | #include "unordered_map.h" |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 16 | #include "unordered_set.h" |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 17 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 18 | #include "gtest/gtest.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 19 | |
| 20 | namespace art { |
| 21 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 22 | class ClassLoader; |
| 23 | class InternTable; |
| 24 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 25 | class ClassLinker { |
| 26 | public: |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 27 | // Initializes the class linker using DexFile and an optional boot Space. |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 28 | static ClassLinker* Create(const std::vector<const DexFile*>& boot_class_path, |
| 29 | InternTable* intern_table, Space* boot_space); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 30 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 31 | ~ClassLinker(); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 32 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 33 | // Finds a class by its descriptor name. |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 34 | // If class_loader is null, searches boot_class_path_. |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 35 | Class* FindClass(const StringPiece& descriptor, |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 36 | const ClassLoader* class_loader); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 37 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 38 | Class* FindPrimitiveClass(char type); |
| 39 | |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 40 | Class* FindSystemClass(const StringPiece& descriptor) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 41 | return FindClass(descriptor, NULL); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 44 | size_t NumLoadedClasses() const; |
| 45 | |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 46 | // Resolve a String with the given index from the DexFile, storing the |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 47 | // result in the DexCache. |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 48 | const String* ResolveString(const DexFile& dex_file, uint32_t string_idx, DexCache* dex_cache); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 49 | |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 50 | // Resolve a Type with the given index from the DexFile, storing the |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 51 | // result in the DexCache. The referrer is used to identity the |
| 52 | // target DexCache and ClassLoader to use for resolution. |
| 53 | Class* ResolveType(const DexFile& dex_file, |
| 54 | uint32_t type_idx, |
| 55 | const Class* referrer) { |
| 56 | return ResolveType(dex_file, |
| 57 | type_idx, |
| 58 | referrer->GetDexCache(), |
| 59 | referrer->GetClassLoader()); |
| 60 | } |
| 61 | |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 62 | // Resolve a Type with the given index from the DexFile, storing the |
| 63 | // result in the DexCache. The referrer is used to identity the |
| 64 | // target DexCache and ClassLoader to use for resolution. |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 65 | Class* ResolveType(uint32_t type_idx, const Method* referrer) { |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 66 | Class* declaring_class = referrer->GetDeclaringClass(); |
| 67 | DexCache* dex_cache = declaring_class->GetDexCache(); |
| 68 | const ClassLoader* class_loader = declaring_class->GetClassLoader(); |
| 69 | const DexFile& dex_file = FindDexFile(dex_cache); |
| 70 | return ResolveType(dex_file, type_idx, dex_cache, class_loader); |
| 71 | } |
| 72 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 73 | // Resolve a type with the given ID from the DexFile, storing the |
| 74 | // result in DexCache. The ClassLoader is used to search for the |
| 75 | // type, since it may be referenced from but not contained within |
| 76 | // the given DexFile. |
| 77 | Class* ResolveType(const DexFile& dex_file, |
| 78 | uint32_t type_idx, |
| 79 | DexCache* dex_cache, |
| 80 | const ClassLoader* class_loader); |
| 81 | |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 82 | static StaticStorageBase* InitializeStaticStorageFromCode(uint32_t type_idx, |
| 83 | const Method* referrer); |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 84 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 85 | // Resolve a method with a given ID from the DexFile, storing the |
| 86 | // result in DexCache. The ClassLinker and ClassLoader are used as |
| 87 | // in ResolveType. What is unique is the method type argument which |
| 88 | // is used to determine if this method is a direct, static, or |
| 89 | // virtual method. |
| 90 | Method* ResolveMethod(const DexFile& dex_file, |
| 91 | uint32_t method_idx, |
| 92 | DexCache* dex_cache, |
| 93 | const ClassLoader* class_loader, |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 94 | bool is_direct); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 95 | |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 96 | Field* ResolveField(uint32_t field_idx, const Method* referrer) { |
| 97 | Class* declaring_class = referrer->GetDeclaringClass(); |
| 98 | DexCache* dex_cache = declaring_class->GetDexCache(); |
| 99 | const ClassLoader* class_loader = declaring_class->GetClassLoader(); |
| 100 | const DexFile& dex_file = FindDexFile(dex_cache); |
| 101 | return ResolveField(dex_file, field_idx, dex_cache, class_loader, true); |
| 102 | } |
| 103 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 104 | // Resolve a method with a given ID from the DexFile, storing the |
| 105 | // result in DexCache. The ClassLinker and ClassLoader are used as |
| 106 | // in ResolveType. What is unique is the is_static argument which is |
| 107 | // used to determine if we are resolving a static or non-static |
| 108 | // field. |
| 109 | Field* ResolveField(const DexFile& dex_file, |
| 110 | uint32_t field_idx, |
| 111 | DexCache* dex_cache, |
| 112 | const ClassLoader* class_loader, |
| 113 | bool is_static); |
| 114 | |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 115 | // Returns true on success, false if there's an exception pending. |
| 116 | bool EnsureInitialized(Class* c); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 117 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 118 | void RegisterDexFile(const DexFile& dex_file); |
| 119 | void RegisterDexFile(const DexFile& dex_file, DexCache* dex_cache); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 120 | |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 121 | const std::vector<const DexFile*>& GetBootClassPath() { |
| 122 | return boot_class_path_; |
| 123 | } |
| 124 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 125 | void VisitRoots(Heap::RootVisitor* visitor, void* arg) const; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 126 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 127 | const DexFile& FindDexFile(const DexCache* dex_cache) const; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 128 | DexCache* FindDexCache(const DexFile& dex_file) const; |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 129 | |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 130 | template <class T> |
| 131 | ObjectArray<T>* AllocObjectArray(size_t length) { |
| 132 | return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length); |
| 133 | } |
| 134 | |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 135 | ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length); |
| 136 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 137 | private: |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 138 | ClassLinker(InternTable*); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 139 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 140 | // Initialize class linker from DexFile instances. |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 141 | void Init(const std::vector<const DexFile*>& boot_class_path_); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 142 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 143 | // Initialize class linker from pre-initialized space. |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 144 | void Init(const std::vector<const DexFile*>& boot_class_path_, Space* space); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 145 | static void InitCallback(Object* obj, void *arg); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 146 | struct InitCallbackState; |
| 147 | |
| 148 | void FinishInit(); |
| 149 | |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 150 | bool InitializeClass(Class* klass); |
| 151 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 152 | // For early bootstrapping by Init |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 153 | Class* AllocClass(Class* java_lang_Class, size_t class_size); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 154 | |
| 155 | // Alloc* convenience functions to avoid needing to pass in Class* |
| 156 | // values that are known to the ClassLinker such as |
| 157 | // kObjectArrayClass and kJavaLangString etc. |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 158 | Class* AllocClass(size_t class_size); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 159 | DexCache* AllocDexCache(const DexFile& dex_file); |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 160 | Field* AllocField(); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 161 | Method* AllocMethod(); |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 162 | CodeAndDirectMethods* AllocCodeAndDirectMethods(size_t length); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 163 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 164 | Class* CreatePrimitiveClass(const char* descriptor); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 165 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 166 | Class* CreateArrayClass(const StringPiece& descriptor, |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 167 | const ClassLoader* class_loader); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 168 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 169 | void AppendToBootClassPath(const DexFile& dex_file); |
| 170 | void AppendToBootClassPath(const DexFile& dex_file, DexCache* dex_cache); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 171 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 172 | size_t SizeOfClass(const DexFile& dex_file, |
| 173 | const DexFile::ClassDef& dex_class_def); |
| 174 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 175 | void LoadClass(const DexFile& dex_file, |
| 176 | const DexFile::ClassDef& dex_class_def, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 177 | Class* klass, |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 178 | const ClassLoader* class_loader); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 179 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 180 | void LoadInterfaces(const DexFile& dex_file, |
| 181 | const DexFile::ClassDef& dex_class_def, |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 182 | Class *klass); |
| 183 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 184 | void LoadField(const DexFile& dex_file, |
| 185 | const DexFile::Field& dex_field, |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 186 | Class* klass, |
| 187 | Field* dst); |
| 188 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 189 | void LoadMethod(const DexFile& dex_file, |
| 190 | const DexFile::Method& dex_method, |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 191 | Class* klass, |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 192 | Method* dst); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 193 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 194 | Class* LookupClass(const StringPiece& descriptor, const ClassLoader* class_loader); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 195 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 196 | // Inserts a class into the class table. Returns true if the class |
| 197 | // was inserted. |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 198 | bool InsertClass(const StringPiece& descriptor, Class* klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 199 | |
| 200 | bool InitializeSuperClass(Class* klass); |
| 201 | |
| 202 | void InitializeStaticFields(Class* klass); |
| 203 | |
| 204 | bool ValidateSuperClassDescriptors(const Class* klass); |
| 205 | |
| 206 | bool HasSameDescriptorClasses(const char* descriptor, |
| 207 | const Class* klass1, |
| 208 | const Class* klass2); |
| 209 | |
| 210 | bool HasSameMethodDescriptorClasses(const Method* descriptor, |
| 211 | const Class* klass1, |
| 212 | const Class* klass2); |
| 213 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 214 | bool LinkClass(Class* klass, const DexFile& dex_file); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 215 | |
| 216 | bool LinkSuperClass(Class* klass); |
| 217 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 218 | bool LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 219 | |
| 220 | bool LinkMethods(Class* klass); |
| 221 | |
| 222 | bool LinkVirtualMethods(Class* klass); |
| 223 | |
| 224 | bool LinkInterfaceMethods(Class* klass); |
| 225 | |
| 226 | void LinkAbstractMethods(Class* klass); |
| 227 | |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 228 | bool LinkStaticFields(Class* klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 229 | bool LinkInstanceFields(Class* klass); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 230 | bool LinkFields(size_t field_offset, |
| 231 | size_t& num_reference_fields, |
| 232 | size_t num_fields, |
| 233 | ObjectArray<Field>* fields, |
| 234 | size_t& size); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 235 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 236 | void CreateReferenceInstanceOffsets(Class* klass); |
| 237 | void CreateReferenceStaticOffsets(Class* klass); |
| 238 | void CreateReferenceOffsets(uint32_t& reference_offsets, |
| 239 | size_t num_reference_fields, |
| 240 | const ObjectArray<Field>* fields); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 241 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 242 | std::vector<const DexFile*> boot_class_path_; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 243 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 244 | std::vector<const DexFile*> dex_files_; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 245 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 246 | std::vector<DexCache*> dex_caches_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 247 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 248 | // multimap from a StringPiece hash code of a class descriptor to |
| 249 | // Class* instances. Results should be compared for a matching |
| 250 | // Class::descriptor_ and Class::class_loader_. |
| 251 | typedef std::tr1::unordered_multimap<size_t, Class*> Table; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 252 | Table classes_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 253 | Mutex* classes_lock_; |
| 254 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 255 | // indexes into class_roots_. |
| 256 | // needs to be kept in sync with class_roots_descriptors_. |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 257 | enum ClassRoot { |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 258 | kJavaLangClass, |
| 259 | kJavaLangObject, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 260 | kObjectArrayClass, |
| 261 | kJavaLangString, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 262 | kJavaLangReflectField, |
| 263 | kJavaLangReflectMethod, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 264 | kJavaLangClassLoader, |
| 265 | kDalvikSystemBaseDexClassLoader, |
| 266 | kDalvikSystemPathClassLoader, |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 267 | kJavaLangStackTraceElement, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 268 | kPrimitiveBoolean, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 269 | kPrimitiveByte, |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 270 | kPrimitiveChar, |
| 271 | kPrimitiveDouble, |
| 272 | kPrimitiveFloat, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 273 | kPrimitiveInt, |
| 274 | kPrimitiveLong, |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 275 | kPrimitiveShort, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 276 | kPrimitiveVoid, |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 277 | kBooleanArrayClass, |
| 278 | kByteArrayClass, |
| 279 | kCharArrayClass, |
| 280 | kDoubleArrayClass, |
| 281 | kFloatArrayClass, |
| 282 | kIntArrayClass, |
| 283 | kLongArrayClass, |
| 284 | kShortArrayClass, |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 285 | kJavaLangStackTraceElementArrayClass, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 286 | kClassRootsMax, |
| 287 | }; |
| 288 | ObjectArray<Class>* class_roots_; |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 289 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 290 | Class* GetClassRoot(ClassRoot class_root) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 291 | DCHECK(class_roots_ != NULL); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 292 | Class* klass = class_roots_->Get(class_root); |
| 293 | DCHECK(klass != NULL); |
| 294 | return klass; |
| 295 | } |
| 296 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 297 | void SetClassRoot(ClassRoot class_root, Class* klass) { |
| 298 | DCHECK(!init_done_); |
| 299 | |
| 300 | DCHECK(klass != NULL); |
| 301 | DCHECK(klass->class_loader_ == NULL); |
| 302 | DCHECK(klass->descriptor_ != NULL); |
| 303 | DCHECK(klass->descriptor_->Equals(GetClassRootDescriptor(class_root))); |
| 304 | |
| 305 | DCHECK(class_roots_ != NULL); |
| 306 | DCHECK(class_roots_->Get(class_root) == NULL); |
| 307 | class_roots_->Set(class_root, klass); |
| 308 | } |
| 309 | |
| 310 | static const char* class_roots_descriptors_[kClassRootsMax]; |
| 311 | |
| 312 | const char* GetClassRootDescriptor(ClassRoot class_root) { |
| 313 | const char* descriptor = class_roots_descriptors_[class_root]; |
| 314 | CHECK(descriptor != NULL); |
| 315 | return descriptor; |
| 316 | } |
| 317 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 318 | ObjectArray<Class>* array_interfaces_; |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 319 | InterfaceEntry* array_iftable_; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 320 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 321 | bool init_done_; |
| 322 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 323 | InternTable* intern_table_; |
| 324 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 325 | friend class CommonTest; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 326 | FRIEND_TEST(DexCacheTest, Open); |
| 327 | friend class ObjectTest; |
| 328 | FRIEND_TEST(ObjectTest, AllocObjectArray); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 329 | FRIEND_TEST(ExceptionTest, FindExceptionHandler); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 330 | DISALLOW_COPY_AND_ASSIGN(ClassLinker); |
| 331 | }; |
| 332 | |
| 333 | } // namespace art |
| 334 | |
| 335 | #endif // ART_SRC_CLASS_LINKER_H_ |