Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -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 | */ |
| 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_CLASS_LINKER_INL_H_ |
| 18 | #define ART_RUNTIME_CLASS_LINKER_INL_H_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | |
| 20 | #include "class_linker.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 21 | #include "mirror/art_field.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 22 | #include "mirror/class_loader.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 23 | #include "mirror/dex_cache.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 24 | #include "mirror/iftable.h" |
| 25 | #include "mirror/object_array.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 26 | #include "sirt_ref.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
| 30 | inline mirror::String* ClassLinker::ResolveString(uint32_t string_idx, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 31 | const mirror::ArtMethod* referrer) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 32 | mirror::String* resolved_string = referrer->GetDexCacheStrings()->Get(string_idx); |
| 33 | if (UNLIKELY(resolved_string == NULL)) { |
| 34 | mirror::Class* declaring_class = referrer->GetDeclaringClass(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 35 | SirtRef<mirror::DexCache> dex_cache(Thread::Current(), declaring_class->GetDexCache()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 36 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 37 | resolved_string = ResolveString(dex_file, string_idx, dex_cache); |
| 38 | } |
| 39 | return resolved_string; |
| 40 | } |
| 41 | |
| 42 | inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 43 | const mirror::ArtMethod* referrer) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 44 | mirror::Class* resolved_type = referrer->GetDexCacheResolvedTypes()->Get(type_idx); |
| 45 | if (UNLIKELY(resolved_type == NULL)) { |
| 46 | mirror::Class* declaring_class = referrer->GetDeclaringClass(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 47 | Thread* self = Thread::Current(); |
| 48 | SirtRef<mirror::DexCache> dex_cache(self, declaring_class->GetDexCache()); |
| 49 | SirtRef<mirror::ClassLoader> class_loader(self, declaring_class->GetClassLoader()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 50 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 51 | resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader); |
| 52 | } |
| 53 | return resolved_type; |
| 54 | } |
| 55 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 56 | inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx, const mirror::ArtField* referrer) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 57 | mirror::Class* declaring_class = referrer->GetDeclaringClass(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 58 | mirror::DexCache* dex_cache_ptr = declaring_class->GetDexCache(); |
| 59 | mirror::Class* resolved_type = dex_cache_ptr->GetResolvedType(type_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 60 | if (UNLIKELY(resolved_type == NULL)) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 61 | Thread* self = Thread::Current(); |
| 62 | SirtRef<mirror::DexCache> dex_cache(self, dex_cache_ptr); |
| 63 | SirtRef<mirror::ClassLoader> class_loader(self, declaring_class->GetClassLoader()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 64 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 65 | resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader); |
| 66 | } |
| 67 | return resolved_type; |
| 68 | } |
| 69 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 70 | inline mirror::ArtMethod* ClassLinker::ResolveMethod(uint32_t method_idx, |
| 71 | const mirror::ArtMethod* referrer, |
| 72 | InvokeType type) { |
| 73 | mirror::ArtMethod* resolved_method = |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 74 | referrer->GetDexCacheResolvedMethods()->Get(method_idx); |
| 75 | if (UNLIKELY(resolved_method == NULL || resolved_method->IsRuntimeMethod())) { |
| 76 | mirror::Class* declaring_class = referrer->GetDeclaringClass(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 77 | Thread* self = Thread::Current(); |
| 78 | SirtRef<mirror::DexCache> dex_cache(self, declaring_class->GetDexCache()); |
| 79 | SirtRef<mirror::ClassLoader> class_loader(self, declaring_class->GetClassLoader()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 80 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 81 | resolved_method = ResolveMethod(dex_file, method_idx, dex_cache, class_loader, referrer, type); |
| 82 | } |
| 83 | return resolved_method; |
| 84 | } |
| 85 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 86 | inline mirror::ArtField* ClassLinker::ResolveField(uint32_t field_idx, |
| 87 | const mirror::ArtMethod* referrer, |
| 88 | bool is_static) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 89 | mirror::Class* declaring_class = referrer->GetDeclaringClass(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 90 | mirror::ArtField* resolved_field = |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 91 | declaring_class->GetDexCache()->GetResolvedField(field_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 92 | if (UNLIKELY(resolved_field == NULL)) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 93 | Thread* self = Thread::Current(); |
| 94 | SirtRef<mirror::DexCache> dex_cache(self, declaring_class->GetDexCache()); |
| 95 | SirtRef<mirror::ClassLoader> class_loader(self, declaring_class->GetClassLoader()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 96 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 97 | resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static); |
| 98 | } |
| 99 | return resolved_field; |
| 100 | } |
| 101 | |
| 102 | template <class T> |
| 103 | inline mirror::ObjectArray<T>* ClassLinker::AllocObjectArray(Thread* self, size_t length) { |
| 104 | return mirror::ObjectArray<T>::Alloc(self, GetClassRoot(kObjectArrayClass), length); |
| 105 | } |
| 106 | |
| 107 | inline mirror::ObjectArray<mirror::Class>* ClassLinker::AllocClassArray(Thread* self, |
| 108 | size_t length) { |
| 109 | return mirror::ObjectArray<mirror::Class>::Alloc(self, GetClassRoot(kClassArrayClass), length); |
| 110 | } |
| 111 | |
| 112 | inline mirror::ObjectArray<mirror::String>* ClassLinker::AllocStringArray(Thread* self, |
| 113 | size_t length) { |
| 114 | return mirror::ObjectArray<mirror::String>::Alloc(self, GetClassRoot(kJavaLangStringArrayClass), |
| 115 | length); |
| 116 | } |
| 117 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 118 | inline mirror::ObjectArray<mirror::ArtMethod>* ClassLinker::AllocArtMethodArray(Thread* self, |
| 119 | size_t length) { |
| 120 | return mirror::ObjectArray<mirror::ArtMethod>::Alloc(self, |
| 121 | GetClassRoot(kJavaLangReflectArtMethodArrayClass), length); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | inline mirror::IfTable* ClassLinker::AllocIfTable(Thread* self, size_t ifcount) { |
| 125 | return down_cast<mirror::IfTable*>( |
| 126 | mirror::IfTable::Alloc(self, GetClassRoot(kObjectArrayClass), ifcount * mirror::IfTable::kMax)); |
| 127 | } |
| 128 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 129 | inline mirror::ObjectArray<mirror::ArtField>* ClassLinker::AllocArtFieldArray(Thread* self, |
| 130 | size_t length) { |
| 131 | return mirror::ObjectArray<mirror::ArtField>::Alloc(self, |
| 132 | GetClassRoot(kJavaLangReflectArtFieldArrayClass), |
| 133 | length); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | inline mirror::Class* ClassLinker::GetClassRoot(ClassRoot class_root) |
| 137 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 138 | DCHECK(class_roots_ != NULL); |
| 139 | mirror::Class* klass = class_roots_->Get(class_root); |
| 140 | DCHECK(klass != NULL); |
| 141 | return klass; |
| 142 | } |
| 143 | |
| 144 | } // namespace art |
| 145 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 146 | #endif // ART_RUNTIME_CLASS_LINKER_INL_H_ |