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_MIRROR_CLASS_INL_H_ |
| 18 | #define ART_RUNTIME_MIRROR_CLASS_INL_H_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | |
| 20 | #include "class.h" |
| 21 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 22 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 23 | #include "art_method.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 24 | #include "art_method-inl.h" |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame^] | 25 | #include "base/array_slice.h" |
| 26 | #include "base/length_prefixed_array.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 27 | #include "class_loader.h" |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 28 | #include "common_throws.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 29 | #include "dex_cache.h" |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 30 | #include "dex_file.h" |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 31 | #include "gc/heap-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 32 | #include "iftable.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 33 | #include "object_array-inl.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 34 | #include "read_barrier-inl.h" |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 35 | #include "reference-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 36 | #include "runtime.h" |
| 37 | #include "string.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 38 | #include "utils.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 39 | |
| 40 | namespace art { |
| 41 | namespace mirror { |
| 42 | |
Hiroshi Yamauchi | 25023c7 | 2014-05-09 11:45:53 -0700 | [diff] [blame] | 43 | template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 44 | inline uint32_t Class::GetObjectSize() { |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 45 | // Note: Extra parentheses to avoid the comma being interpreted as macro parameter separator. |
| 46 | DCHECK((!IsVariableSize<kVerifyFlags, kReadBarrierOption>())) << " class=" << PrettyTypeOf(this); |
Hiroshi Yamauchi | e01a520 | 2015-03-19 12:35:04 -0700 | [diff] [blame] | 47 | return GetField32(ObjectSizeOffset()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 50 | inline Class* Class::GetSuperClass() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 51 | // Can only get super class for loaded classes (hack for when runtime is |
| 52 | // initializing) |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 53 | DCHECK(IsLoaded() || IsErroneous() || !Runtime::Current()->IsStarted()) << IsLoaded(); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 54 | return GetFieldObject<Class>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 57 | inline ClassLoader* Class::GetClassLoader() { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 58 | return GetFieldObject<ClassLoader>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_)); |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Mathieu Chartier | c2f4d02 | 2014-03-03 16:11:42 -0800 | [diff] [blame] | 61 | template<VerifyObjectFlags kVerifyFlags> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 62 | inline DexCache* Class::GetDexCache() { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 63 | return GetFieldObject<DexCache, kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_)); |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame^] | 66 | inline uint32_t Class::GetCopiedMethodsStartOffset() { |
| 67 | return GetFieldShort(OFFSET_OF_OBJECT_MEMBER(Class, copied_methods_offset_)); |
| 68 | } |
| 69 | |
| 70 | inline uint32_t Class::GetDirectMethodsStartOffset() { |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | inline uint32_t Class::GetVirtualMethodsStartOffset() { |
| 75 | return GetFieldShort(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_offset_)); |
| 76 | } |
| 77 | |
| 78 | template<VerifyObjectFlags kVerifyFlags> |
| 79 | inline ArraySlice<ArtMethod> Class::GetDirectMethodsSlice(size_t pointer_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 80 | DCHECK(IsLoaded() || IsErroneous()); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame^] | 81 | DCHECK(ValidPointerSize(pointer_size)) << pointer_size; |
| 82 | return GetDirectMethodsSliceUnchecked(pointer_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame^] | 85 | inline ArraySlice<ArtMethod> Class::GetDirectMethodsSliceUnchecked(size_t pointer_size) { |
| 86 | return ArraySlice<ArtMethod>(GetMethodsPtr(), |
| 87 | GetDirectMethodsStartOffset(), |
| 88 | GetVirtualMethodsStartOffset(), |
| 89 | ArtMethod::Size(pointer_size), |
| 90 | ArtMethod::Alignment(pointer_size)); |
| 91 | } |
| 92 | |
| 93 | template<VerifyObjectFlags kVerifyFlags> |
| 94 | inline ArraySlice<ArtMethod> Class::GetDeclaredMethodsSlice(size_t pointer_size) { |
| 95 | DCHECK(IsLoaded() || IsErroneous()); |
| 96 | DCHECK(ValidPointerSize(pointer_size)) << pointer_size; |
| 97 | return GetDeclaredMethodsSliceUnchecked(pointer_size); |
| 98 | } |
| 99 | |
| 100 | inline ArraySlice<ArtMethod> Class::GetDeclaredMethodsSliceUnchecked(size_t pointer_size) { |
| 101 | return ArraySlice<ArtMethod>(GetMethodsPtr(), |
| 102 | GetDirectMethodsStartOffset(), |
| 103 | GetCopiedMethodsStartOffset(), |
| 104 | ArtMethod::Size(pointer_size), |
| 105 | ArtMethod::Alignment(pointer_size)); |
| 106 | } |
| 107 | template<VerifyObjectFlags kVerifyFlags> |
| 108 | inline ArraySlice<ArtMethod> Class::GetDeclaredVirtualMethodsSlice(size_t pointer_size) { |
| 109 | DCHECK(IsLoaded() || IsErroneous()); |
| 110 | DCHECK(ValidPointerSize(pointer_size)) << pointer_size; |
| 111 | return GetDeclaredVirtualMethodsSliceUnchecked(pointer_size); |
| 112 | } |
| 113 | |
| 114 | inline ArraySlice<ArtMethod> Class::GetDeclaredVirtualMethodsSliceUnchecked(size_t pointer_size) { |
| 115 | return ArraySlice<ArtMethod>(GetMethodsPtr(), |
| 116 | GetVirtualMethodsStartOffset(), |
| 117 | GetCopiedMethodsStartOffset(), |
| 118 | ArtMethod::Size(pointer_size), |
| 119 | ArtMethod::Alignment(pointer_size)); |
| 120 | } |
| 121 | |
| 122 | template<VerifyObjectFlags kVerifyFlags> |
| 123 | inline ArraySlice<ArtMethod> Class::GetVirtualMethodsSlice(size_t pointer_size) { |
| 124 | DCHECK(IsLoaded() || IsErroneous()); |
| 125 | DCHECK(ValidPointerSize(pointer_size)) << pointer_size; |
| 126 | return GetVirtualMethodsSliceUnchecked(pointer_size); |
| 127 | } |
| 128 | |
| 129 | inline ArraySlice<ArtMethod> Class::GetVirtualMethodsSliceUnchecked(size_t pointer_size) { |
| 130 | LengthPrefixedArray<ArtMethod>* methods = GetMethodsPtr(); |
| 131 | return ArraySlice<ArtMethod>(methods, |
| 132 | GetVirtualMethodsStartOffset(), |
| 133 | NumMethods(), |
| 134 | ArtMethod::Size(pointer_size), |
| 135 | ArtMethod::Alignment(pointer_size)); |
| 136 | } |
| 137 | |
| 138 | template<VerifyObjectFlags kVerifyFlags> |
| 139 | inline ArraySlice<ArtMethod> Class::GetCopiedMethodsSlice(size_t pointer_size) { |
| 140 | DCHECK(IsLoaded() || IsErroneous()); |
| 141 | DCHECK(ValidPointerSize(pointer_size)) << pointer_size; |
| 142 | return GetCopiedMethodsSliceUnchecked(pointer_size); |
| 143 | } |
| 144 | |
| 145 | inline ArraySlice<ArtMethod> Class::GetCopiedMethodsSliceUnchecked(size_t pointer_size) { |
| 146 | LengthPrefixedArray<ArtMethod>* methods = GetMethodsPtr(); |
| 147 | return ArraySlice<ArtMethod>(methods, |
| 148 | GetCopiedMethodsStartOffset(), |
| 149 | NumMethods(), |
| 150 | ArtMethod::Size(pointer_size), |
| 151 | ArtMethod::Alignment(pointer_size)); |
| 152 | } |
| 153 | |
| 154 | inline LengthPrefixedArray<ArtMethod>* Class::GetMethodsPtr() { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 155 | return reinterpret_cast<LengthPrefixedArray<ArtMethod>*>( |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame^] | 156 | GetField64(OFFSET_OF_OBJECT_MEMBER(Class, methods_))); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame^] | 159 | template<VerifyObjectFlags kVerifyFlags> |
| 160 | inline ArraySlice<ArtMethod> Class::GetMethodsSlice(size_t pointer_size) { |
| 161 | DCHECK(IsLoaded() || IsErroneous()); |
| 162 | LengthPrefixedArray<ArtMethod>* methods = GetMethodsPtr(); |
| 163 | return ArraySlice<ArtMethod>(methods, |
| 164 | 0, |
| 165 | NumMethods(), |
| 166 | ArtMethod::Size(pointer_size), |
| 167 | ArtMethod::Alignment(pointer_size)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 170 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame^] | 171 | inline uint32_t Class::NumMethods() { |
| 172 | LengthPrefixedArray<ArtMethod>* methods = GetMethodsPtr(); |
| 173 | return (methods == nullptr) ? 0 : methods->size(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | inline ArtMethod* Class::GetDirectMethodUnchecked(size_t i, size_t pointer_size) { |
| 177 | CheckPointerSize(pointer_size); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame^] | 178 | return &GetDirectMethodsSliceUnchecked(pointer_size).At(i); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | inline ArtMethod* Class::GetDirectMethod(size_t i, size_t pointer_size) { |
| 182 | CheckPointerSize(pointer_size); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame^] | 183 | return &GetDirectMethodsSlice(pointer_size).At(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame^] | 186 | inline void Class::SetMethodsPtr(LengthPrefixedArray<ArtMethod>* new_methods, |
| 187 | uint32_t num_direct, |
| 188 | uint32_t num_virtual) { |
| 189 | DCHECK(GetMethodsPtr() == nullptr); |
| 190 | SetMethodsPtrUnchecked(new_methods, num_direct, num_virtual); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame^] | 193 | |
| 194 | inline void Class::SetMethodsPtrUnchecked(LengthPrefixedArray<ArtMethod>* new_methods, |
| 195 | uint32_t num_direct, |
| 196 | uint32_t num_virtual) { |
| 197 | DCHECK_LE(num_direct + num_virtual, (new_methods == nullptr) ? 0 : new_methods->size()); |
| 198 | SetMethodsPtrInternal(new_methods); |
| 199 | SetFieldShort<false>(OFFSET_OF_OBJECT_MEMBER(Class, copied_methods_offset_), |
| 200 | dchecked_integral_cast<uint16_t>(num_direct + num_virtual)); |
| 201 | SetFieldShort<false>(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_offset_), |
| 202 | dchecked_integral_cast<uint16_t>(num_direct)); |
| 203 | } |
| 204 | |
| 205 | inline void Class::SetMethodsPtrInternal(LengthPrefixedArray<ArtMethod>* new_methods) { |
| 206 | SetField64<false>(OFFSET_OF_OBJECT_MEMBER(Class, methods_), |
| 207 | reinterpret_cast<uint64_t>(new_methods)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 210 | template<VerifyObjectFlags kVerifyFlags> |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 211 | inline ArtMethod* Class::GetVirtualMethod(size_t i, size_t pointer_size) { |
| 212 | CheckPointerSize(pointer_size); |
Hiroshi Yamauchi | f4c15a1 | 2014-10-20 16:56:58 -0700 | [diff] [blame] | 213 | DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>()) |
| 214 | << PrettyClass(this) << " status=" << GetStatus(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 215 | return GetVirtualMethodUnchecked(i, pointer_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 218 | inline ArtMethod* Class::GetVirtualMethodDuringLinking(size_t i, size_t pointer_size) { |
| 219 | CheckPointerSize(pointer_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 220 | DCHECK(IsLoaded() || IsErroneous()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 221 | return GetVirtualMethodUnchecked(i, pointer_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 224 | inline ArtMethod* Class::GetVirtualMethodUnchecked(size_t i, size_t pointer_size) { |
| 225 | CheckPointerSize(pointer_size); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame^] | 226 | return &GetVirtualMethodsSliceUnchecked(pointer_size).At(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 229 | inline PointerArray* Class::GetVTable() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 230 | DCHECK(IsResolved() || IsErroneous()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 231 | return GetFieldObject<PointerArray>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 234 | inline PointerArray* Class::GetVTableDuringLinking() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 235 | DCHECK(IsLoaded() || IsErroneous()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 236 | return GetFieldObject<PointerArray>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 239 | inline void Class::SetVTable(PointerArray* new_vtable) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 240 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 243 | inline MemberOffset Class::EmbeddedImTableEntryOffset(uint32_t i, size_t pointer_size) { |
| 244 | DCHECK_LT(i, kImtSize); |
| 245 | return MemberOffset( |
| 246 | EmbeddedImTableOffset(pointer_size).Uint32Value() + i * ImTableEntrySize(pointer_size)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 249 | inline ArtMethod* Class::GetEmbeddedImTableEntry(uint32_t i, size_t pointer_size) { |
| 250 | DCHECK(ShouldHaveEmbeddedImtAndVTable()); |
| 251 | return GetFieldPtrWithSize<ArtMethod*>( |
| 252 | EmbeddedImTableEntryOffset(i, pointer_size), pointer_size); |
| 253 | } |
| 254 | |
| 255 | inline void Class::SetEmbeddedImTableEntry(uint32_t i, ArtMethod* method, size_t pointer_size) { |
| 256 | DCHECK(ShouldHaveEmbeddedImtAndVTable()); |
| 257 | SetFieldPtrWithSize<false>(EmbeddedImTableEntryOffset(i, pointer_size), method, pointer_size); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 260 | inline bool Class::HasVTable() { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 261 | return GetVTable() != nullptr || ShouldHaveEmbeddedImtAndVTable(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | inline int32_t Class::GetVTableLength() { |
| 265 | if (ShouldHaveEmbeddedImtAndVTable()) { |
| 266 | return GetEmbeddedVTableLength(); |
| 267 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 268 | return GetVTable() != nullptr ? GetVTable()->GetLength() : 0; |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 271 | inline ArtMethod* Class::GetVTableEntry(uint32_t i, size_t pointer_size) { |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 272 | if (ShouldHaveEmbeddedImtAndVTable()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 273 | return GetEmbeddedVTableEntry(i, pointer_size); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 274 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 275 | auto* vtable = GetVTable(); |
| 276 | DCHECK(vtable != nullptr); |
| 277 | return vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | inline int32_t Class::GetEmbeddedVTableLength() { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 281 | return GetField32(MemberOffset(EmbeddedVTableLengthOffset())); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | inline void Class::SetEmbeddedVTableLength(int32_t len) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 285 | SetField32<false>(MemberOffset(EmbeddedVTableLengthOffset()), len); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 288 | inline MemberOffset Class::EmbeddedVTableEntryOffset(uint32_t i, size_t pointer_size) { |
| 289 | return MemberOffset( |
| 290 | EmbeddedVTableOffset(pointer_size).Uint32Value() + i * VTableEntrySize(pointer_size)); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 293 | inline ArtMethod* Class::GetEmbeddedVTableEntry(uint32_t i, size_t pointer_size) { |
| 294 | return GetFieldPtrWithSize<ArtMethod*>(EmbeddedVTableEntryOffset(i, pointer_size), pointer_size); |
| 295 | } |
| 296 | |
| 297 | inline void Class::SetEmbeddedVTableEntryUnchecked( |
| 298 | uint32_t i, ArtMethod* method, size_t pointer_size) { |
| 299 | SetFieldPtrWithSize<false>(EmbeddedVTableEntryOffset(i, pointer_size), method, pointer_size); |
| 300 | } |
| 301 | |
| 302 | inline void Class::SetEmbeddedVTableEntry(uint32_t i, ArtMethod* method, size_t pointer_size) { |
| 303 | auto* vtable = GetVTableDuringLinking(); |
| 304 | CHECK_EQ(method, vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size)); |
| 305 | SetEmbeddedVTableEntryUnchecked(i, method, pointer_size); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 308 | inline bool Class::Implements(Class* klass) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 309 | DCHECK(klass != nullptr); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 310 | DCHECK(klass->IsInterface()) << PrettyClass(this); |
| 311 | // All interfaces implemented directly and by our superclass, and |
| 312 | // recursively all super-interfaces of those interfaces, are listed |
| 313 | // in iftable_, so we can just do a linear scan through that. |
| 314 | int32_t iftable_count = GetIfTableCount(); |
| 315 | IfTable* iftable = GetIfTable(); |
| 316 | for (int32_t i = 0; i < iftable_count; i++) { |
| 317 | if (iftable->GetInterface(i) == klass) { |
| 318 | return true; |
| 319 | } |
| 320 | } |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | // Determine whether "this" is assignable from "src", where both of these |
| 325 | // are array classes. |
| 326 | // |
| 327 | // Consider an array class, e.g. Y[][], where Y is a subclass of X. |
| 328 | // Y[][] = Y[][] --> true (identity) |
| 329 | // X[][] = Y[][] --> true (element superclass) |
| 330 | // Y = Y[][] --> false |
| 331 | // Y[] = Y[][] --> false |
| 332 | // Object = Y[][] --> true (everything is an object) |
| 333 | // Object[] = Y[][] --> true |
| 334 | // Object[][] = Y[][] --> true |
| 335 | // Object[][][] = Y[][] --> false (too many []s) |
| 336 | // Serializable = Y[][] --> true (all arrays are Serializable) |
| 337 | // Serializable[] = Y[][] --> true |
| 338 | // Serializable[][] = Y[][] --> false (unless Y is Serializable) |
| 339 | // |
| 340 | // Don't forget about primitive types. |
| 341 | // Object[] = int[] --> false |
| 342 | // |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 343 | inline bool Class::IsArrayAssignableFromArray(Class* src) { |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 344 | DCHECK(IsArrayClass()) << PrettyClass(this); |
| 345 | DCHECK(src->IsArrayClass()) << PrettyClass(src); |
| 346 | return GetComponentType()->IsAssignableFrom(src->GetComponentType()); |
| 347 | } |
| 348 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 349 | inline bool Class::IsAssignableFromArray(Class* src) { |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 350 | DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom |
| 351 | DCHECK(src->IsArrayClass()) << PrettyClass(src); |
| 352 | if (!IsArrayClass()) { |
| 353 | // If "this" is not also an array, it must be Object. |
| 354 | // src's super should be java_lang_Object, since it is an array. |
| 355 | Class* java_lang_Object = src->GetSuperClass(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 356 | DCHECK(java_lang_Object != nullptr) << PrettyClass(src); |
| 357 | DCHECK(java_lang_Object->GetSuperClass() == nullptr) << PrettyClass(src); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 358 | return this == java_lang_Object; |
| 359 | } |
| 360 | return IsArrayAssignableFromArray(src); |
| 361 | } |
| 362 | |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame] | 363 | template <bool throw_on_failure, bool use_referrers_cache> |
| 364 | inline bool Class::ResolvedFieldAccessTest(Class* access_to, ArtField* field, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 365 | uint32_t field_idx, DexCache* dex_cache) { |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame] | 366 | DCHECK_EQ(use_referrers_cache, dex_cache == nullptr); |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 367 | if (UNLIKELY(!this->CanAccess(access_to))) { |
| 368 | // The referrer class can't access the field's declaring class but may still be able |
| 369 | // to access the field if the FieldId specifies an accessible subclass of the declaring |
| 370 | // class rather than the declaring class itself. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 371 | DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache; |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 372 | uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetFieldId(field_idx).class_idx_; |
| 373 | // The referenced class has already been resolved with the field, get it from the dex cache. |
| 374 | Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx); |
| 375 | DCHECK(dex_access_to != nullptr); |
| 376 | if (UNLIKELY(!this->CanAccess(dex_access_to))) { |
| 377 | if (throw_on_failure) { |
| 378 | ThrowIllegalAccessErrorClass(this, dex_access_to); |
| 379 | } |
| 380 | return false; |
| 381 | } |
| 382 | DCHECK_EQ(this->CanAccessMember(access_to, field->GetAccessFlags()), |
| 383 | this->CanAccessMember(dex_access_to, field->GetAccessFlags())); |
| 384 | } |
| 385 | if (LIKELY(this->CanAccessMember(access_to, field->GetAccessFlags()))) { |
| 386 | return true; |
| 387 | } |
| 388 | if (throw_on_failure) { |
| 389 | ThrowIllegalAccessErrorField(this, field); |
| 390 | } |
| 391 | return false; |
| 392 | } |
| 393 | |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame] | 394 | template <bool throw_on_failure, bool use_referrers_cache, InvokeType throw_invoke_type> |
| 395 | inline bool Class::ResolvedMethodAccessTest(Class* access_to, ArtMethod* method, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 396 | uint32_t method_idx, DexCache* dex_cache) { |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 397 | static_assert(throw_on_failure || throw_invoke_type == kStatic, "Non-default throw invoke type"); |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame] | 398 | DCHECK_EQ(use_referrers_cache, dex_cache == nullptr); |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 399 | if (UNLIKELY(!this->CanAccess(access_to))) { |
| 400 | // The referrer class can't access the method's declaring class but may still be able |
| 401 | // to access the method if the MethodId specifies an accessible subclass of the declaring |
| 402 | // class rather than the declaring class itself. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 403 | DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache; |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 404 | uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetMethodId(method_idx).class_idx_; |
| 405 | // The referenced class has already been resolved with the method, get it from the dex cache. |
| 406 | Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx); |
| 407 | DCHECK(dex_access_to != nullptr); |
| 408 | if (UNLIKELY(!this->CanAccess(dex_access_to))) { |
| 409 | if (throw_on_failure) { |
| 410 | ThrowIllegalAccessErrorClassForMethodDispatch(this, dex_access_to, |
| 411 | method, throw_invoke_type); |
| 412 | } |
| 413 | return false; |
| 414 | } |
| 415 | DCHECK_EQ(this->CanAccessMember(access_to, method->GetAccessFlags()), |
| 416 | this->CanAccessMember(dex_access_to, method->GetAccessFlags())); |
| 417 | } |
| 418 | if (LIKELY(this->CanAccessMember(access_to, method->GetAccessFlags()))) { |
| 419 | return true; |
| 420 | } |
| 421 | if (throw_on_failure) { |
| 422 | ThrowIllegalAccessErrorMethod(this, method); |
| 423 | } |
| 424 | return false; |
| 425 | } |
| 426 | |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame] | 427 | inline bool Class::CanAccessResolvedField(Class* access_to, ArtField* field, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 428 | DexCache* dex_cache, uint32_t field_idx) { |
| 429 | return ResolvedFieldAccessTest<false, false>(access_to, field, field_idx, dex_cache); |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | inline bool Class::CheckResolvedFieldAccess(Class* access_to, ArtField* field, |
| 433 | uint32_t field_idx) { |
| 434 | return ResolvedFieldAccessTest<true, true>(access_to, field, field_idx, nullptr); |
| 435 | } |
| 436 | |
| 437 | inline bool Class::CanAccessResolvedMethod(Class* access_to, ArtMethod* method, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 438 | DexCache* dex_cache, uint32_t method_idx) { |
| 439 | return ResolvedMethodAccessTest<false, false, kStatic>(access_to, method, method_idx, dex_cache); |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | template <InvokeType throw_invoke_type> |
| 443 | inline bool Class::CheckResolvedMethodAccess(Class* access_to, ArtMethod* method, |
| 444 | uint32_t method_idx) { |
| 445 | return ResolvedMethodAccessTest<true, true, throw_invoke_type>(access_to, method, method_idx, |
| 446 | nullptr); |
| 447 | } |
| 448 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 449 | inline bool Class::IsSubClass(Class* klass) { |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 450 | DCHECK(!IsInterface()) << PrettyClass(this); |
| 451 | DCHECK(!IsArrayClass()) << PrettyClass(this); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 452 | Class* current = this; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 453 | do { |
| 454 | if (current == klass) { |
| 455 | return true; |
| 456 | } |
| 457 | current = current->GetSuperClass(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 458 | } while (current != nullptr); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 459 | return false; |
| 460 | } |
| 461 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 462 | inline ArtMethod* Class::FindVirtualMethodForInterface(ArtMethod* method, size_t pointer_size) { |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 463 | Class* declaring_class = method->GetDeclaringClass(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 464 | DCHECK(declaring_class != nullptr) << PrettyClass(this); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 465 | DCHECK(declaring_class->IsInterface()) << PrettyMethod(method); |
| 466 | // TODO cache to improve lookup speed |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 467 | const int32_t iftable_count = GetIfTableCount(); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 468 | IfTable* iftable = GetIfTable(); |
| 469 | for (int32_t i = 0; i < iftable_count; i++) { |
| 470 | if (iftable->GetInterface(i) == declaring_class) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 471 | return iftable->GetMethodArray(i)->GetElementPtrSize<ArtMethod*>( |
| 472 | method->GetMethodIndex(), pointer_size); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 473 | } |
| 474 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 475 | return nullptr; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 476 | } |
| 477 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 478 | inline ArtMethod* Class::FindVirtualMethodForVirtual(ArtMethod* method, size_t pointer_size) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 479 | // Only miranda or default methods may come from interfaces and be used as a virtual. |
| 480 | DCHECK(!method->GetDeclaringClass()->IsInterface() || method->IsDefault() || method->IsMiranda()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 481 | // The argument method may from a super class. |
| 482 | // Use the index to a potentially overridden one for this instance's class. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 483 | return GetVTableEntry(method->GetMethodIndex(), pointer_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 484 | } |
| 485 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 486 | inline ArtMethod* Class::FindVirtualMethodForSuper(ArtMethod* method, size_t pointer_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 487 | DCHECK(!method->GetDeclaringClass()->IsInterface()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 488 | return GetSuperClass()->GetVTableEntry(method->GetMethodIndex(), pointer_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 489 | } |
| 490 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 491 | inline ArtMethod* Class::FindVirtualMethodForVirtualOrInterface(ArtMethod* method, |
| 492 | size_t pointer_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 493 | if (method->IsDirect()) { |
| 494 | return method; |
| 495 | } |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 496 | if (method->GetDeclaringClass()->IsInterface() && !method->IsMiranda()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 497 | return FindVirtualMethodForInterface(method, pointer_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 498 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 499 | return FindVirtualMethodForVirtual(method, pointer_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 500 | } |
| 501 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 502 | inline IfTable* Class::GetIfTable() { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 503 | return GetFieldObject<IfTable>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 504 | } |
| 505 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 506 | inline int32_t Class::GetIfTableCount() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 507 | IfTable* iftable = GetIfTable(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 508 | if (iftable == nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 509 | return 0; |
| 510 | } |
| 511 | return iftable->Count(); |
| 512 | } |
| 513 | |
| 514 | inline void Class::SetIfTable(IfTable* new_iftable) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 515 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 516 | } |
| 517 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 518 | inline LengthPrefixedArray<ArtField>* Class::GetIFieldsPtr() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 519 | DCHECK(IsLoaded() || IsErroneous()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 520 | return GetFieldPtr<LengthPrefixedArray<ArtField>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 521 | } |
| 522 | |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 523 | inline MemberOffset Class::GetFirstReferenceInstanceFieldOffset() { |
| 524 | Class* super_class = GetSuperClass(); |
| 525 | return (super_class != nullptr) |
| 526 | ? MemberOffset(RoundUp(super_class->GetObjectSize(), |
| 527 | sizeof(mirror::HeapReference<mirror::Object>))) |
| 528 | : ClassOffset(); |
| 529 | } |
| 530 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 531 | inline MemberOffset Class::GetFirstReferenceStaticFieldOffset(size_t pointer_size) { |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 532 | DCHECK(IsResolved()); |
| 533 | uint32_t base = sizeof(mirror::Class); // Static fields come after the class. |
| 534 | if (ShouldHaveEmbeddedImtAndVTable()) { |
| 535 | // Static fields come after the embedded tables. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 536 | base = mirror::Class::ComputeClassSize( |
| 537 | true, GetEmbeddedVTableLength(), 0, 0, 0, 0, 0, pointer_size); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 538 | } |
| 539 | return MemberOffset(base); |
| 540 | } |
| 541 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 542 | inline MemberOffset Class::GetFirstReferenceStaticFieldOffsetDuringLinking(size_t pointer_size) { |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 543 | DCHECK(IsLoaded()); |
| 544 | uint32_t base = sizeof(mirror::Class); // Static fields come after the class. |
| 545 | if (ShouldHaveEmbeddedImtAndVTable()) { |
| 546 | // Static fields come after the embedded tables. |
| 547 | base = mirror::Class::ComputeClassSize(true, GetVTableDuringLinking()->GetLength(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 548 | 0, 0, 0, 0, 0, pointer_size); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 549 | } |
| 550 | return MemberOffset(base); |
| 551 | } |
| 552 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 553 | inline void Class::SetIFieldsPtr(LengthPrefixedArray<ArtField>* new_ifields) { |
| 554 | DCHECK(GetIFieldsPtrUnchecked() == nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 555 | return SetFieldPtr<false>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 556 | } |
| 557 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 558 | inline void Class::SetIFieldsPtrUnchecked(LengthPrefixedArray<ArtField>* new_ifields) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 559 | SetFieldPtr<false, true, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields); |
| 560 | } |
| 561 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 562 | inline LengthPrefixedArray<ArtField>* Class::GetSFieldsPtrUnchecked() { |
| 563 | return GetFieldPtr<LengthPrefixedArray<ArtField>*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 566 | inline LengthPrefixedArray<ArtField>* Class::GetIFieldsPtrUnchecked() { |
| 567 | return GetFieldPtr<LengthPrefixedArray<ArtField>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 570 | inline LengthPrefixedArray<ArtField>* Class::GetSFieldsPtr() { |
Mathieu Chartier | 987ca8b | 2015-03-15 14:19:14 -0700 | [diff] [blame] | 571 | DCHECK(IsLoaded() || IsErroneous()) << GetStatus(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 572 | return GetSFieldsPtrUnchecked(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 573 | } |
| 574 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 575 | inline void Class::SetSFieldsPtr(LengthPrefixedArray<ArtField>* new_sfields) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 576 | DCHECK((IsRetired() && new_sfields == nullptr) || |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 577 | GetFieldPtr<ArtField*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_)) == nullptr); |
| 578 | SetFieldPtr<false>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 579 | } |
| 580 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 581 | inline void Class::SetSFieldsPtrUnchecked(LengthPrefixedArray<ArtField>* new_sfields) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 582 | SetFieldPtr<false, true, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 583 | } |
| 584 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 585 | inline ArtField* Class::GetStaticField(uint32_t i) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 586 | return &GetSFieldsPtr()->At(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 587 | } |
| 588 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 589 | inline ArtField* Class::GetInstanceField(uint32_t i) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 590 | return &GetIFieldsPtr()->At(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 591 | } |
| 592 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 593 | template<VerifyObjectFlags kVerifyFlags> |
| 594 | inline uint32_t Class::GetReferenceInstanceOffsets() { |
| 595 | DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>()); |
| 596 | return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_)); |
| 597 | } |
| 598 | |
| 599 | inline void Class::SetClinitThreadId(pid_t new_clinit_thread_id) { |
| 600 | if (Runtime::Current()->IsActiveTransaction()) { |
| 601 | SetField32<true>(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id); |
| 602 | } else { |
| 603 | SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id); |
| 604 | } |
| 605 | } |
| 606 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 607 | template<VerifyObjectFlags kVerifyFlags> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 608 | inline uint32_t Class::GetAccessFlags() { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 609 | // Check class is loaded/retired or this is java.lang.String that has a |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 610 | // circularity issue during loading the names of its members |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 611 | DCHECK(IsIdxLoaded<kVerifyFlags>() || IsRetired<kVerifyFlags>() || |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 612 | IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() || |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 613 | this == String::GetJavaLangString()) |
Andreas Gampe | a692866 | 2014-12-12 11:06:00 -0800 | [diff] [blame] | 614 | << "IsIdxLoaded=" << IsIdxLoaded<kVerifyFlags>() |
| 615 | << " IsRetired=" << IsRetired<kVerifyFlags>() |
| 616 | << " IsErroneous=" << |
| 617 | IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() |
| 618 | << " IsString=" << (this == String::GetJavaLangString()) |
Andreas Gampe | a692866 | 2014-12-12 11:06:00 -0800 | [diff] [blame] | 619 | << " descriptor=" << PrettyDescriptor(this); |
Hiroshi Yamauchi | e01a520 | 2015-03-19 12:35:04 -0700 | [diff] [blame] | 620 | return GetField32<kVerifyFlags>(AccessFlagsOffset()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 621 | } |
| 622 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 623 | inline String* Class::GetName() { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 624 | return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(Class, name_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 625 | } |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 626 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 627 | inline void Class::SetName(String* name) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 628 | if (Runtime::Current()->IsActiveTransaction()) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 629 | SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 630 | } else { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 631 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 632 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 633 | } |
| 634 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 635 | template<VerifyObjectFlags kVerifyFlags> |
| 636 | inline Primitive::Type Class::GetPrimitiveType() { |
Roland Levillain | 33d6903 | 2015-06-18 18:20:59 +0100 | [diff] [blame] | 637 | static_assert(sizeof(Primitive::Type) == sizeof(int32_t), |
| 638 | "art::Primitive::Type and int32_t have different sizes."); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 639 | int32_t v32 = GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_)); |
| 640 | Primitive::Type type = static_cast<Primitive::Type>(v32 & 0xFFFF); |
| 641 | DCHECK_EQ(static_cast<size_t>(v32 >> 16), Primitive::ComponentSizeShift(type)); |
| 642 | return type; |
| 643 | } |
| 644 | |
| 645 | template<VerifyObjectFlags kVerifyFlags> |
| 646 | inline size_t Class::GetPrimitiveTypeSizeShift() { |
Roland Levillain | 33d6903 | 2015-06-18 18:20:59 +0100 | [diff] [blame] | 647 | static_assert(sizeof(Primitive::Type) == sizeof(int32_t), |
| 648 | "art::Primitive::Type and int32_t have different sizes."); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 649 | int32_t v32 = GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_)); |
| 650 | size_t size_shift = static_cast<Primitive::Type>(v32 >> 16); |
| 651 | DCHECK_EQ(size_shift, Primitive::ComponentSizeShift(static_cast<Primitive::Type>(v32 & 0xFFFF))); |
| 652 | return size_shift; |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 655 | inline void Class::CheckObjectAlloc() { |
Hiroshi Yamauchi | 4cd662e | 2014-04-03 16:28:10 -0700 | [diff] [blame] | 656 | DCHECK(!IsArrayClass()) |
| 657 | << PrettyClass(this) |
| 658 | << "A array shouldn't be allocated through this " |
| 659 | << "as it requires a pre-fence visitor that sets the class size."; |
| 660 | DCHECK(!IsClassClass()) |
| 661 | << PrettyClass(this) |
| 662 | << "A class object shouldn't be allocated through this " |
| 663 | << "as it requires a pre-fence visitor that sets the class size."; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 664 | DCHECK(!IsStringClass()) |
| 665 | << PrettyClass(this) |
| 666 | << "A string shouldn't be allocated through this " |
| 667 | << "as it requires a pre-fence visitor that sets the class size."; |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 668 | DCHECK(IsInstantiable()) << PrettyClass(this); |
| 669 | // TODO: decide whether we want this check. It currently fails during bootstrap. |
| 670 | // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this); |
| 671 | DCHECK_GE(this->object_size_, sizeof(Object)); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Mathieu Chartier | 8668c3c | 2014-04-24 16:48:11 -0700 | [diff] [blame] | 674 | template<bool kIsInstrumented, bool kCheckAddFinalizer> |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 675 | inline Object* Class::Alloc(Thread* self, gc::AllocatorType allocator_type) { |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 676 | CheckObjectAlloc(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 677 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 8668c3c | 2014-04-24 16:48:11 -0700 | [diff] [blame] | 678 | const bool add_finalizer = kCheckAddFinalizer && IsFinalizable(); |
| 679 | if (!kCheckAddFinalizer) { |
| 680 | DCHECK(!IsFinalizable()); |
| 681 | } |
| 682 | mirror::Object* obj = |
| 683 | heap->AllocObjectWithAllocator<kIsInstrumented, false>(self, this, this->object_size_, |
| 684 | allocator_type, VoidFunctor()); |
| 685 | if (add_finalizer && LIKELY(obj != nullptr)) { |
| 686 | heap->AddFinalizerReference(self, &obj); |
Pavel Vyssotski | 3ac90da | 2014-12-02 19:54:50 +0600 | [diff] [blame] | 687 | if (UNLIKELY(self->IsExceptionPending())) { |
| 688 | // Failed to allocate finalizer reference, it means that the whole allocation failed. |
| 689 | obj = nullptr; |
| 690 | } |
Mathieu Chartier | 8668c3c | 2014-04-24 16:48:11 -0700 | [diff] [blame] | 691 | } |
| 692 | return obj; |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | inline Object* Class::AllocObject(Thread* self) { |
| 696 | return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator()); |
| 697 | } |
| 698 | |
| 699 | inline Object* Class::AllocNonMovableObject(Thread* self) { |
| 700 | return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentNonMovingAllocator()); |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 701 | } |
| 702 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 703 | inline uint32_t Class::ComputeClassSize(bool has_embedded_tables, |
| 704 | uint32_t num_vtable_entries, |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 705 | uint32_t num_8bit_static_fields, |
| 706 | uint32_t num_16bit_static_fields, |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 707 | uint32_t num_32bit_static_fields, |
| 708 | uint32_t num_64bit_static_fields, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 709 | uint32_t num_ref_static_fields, |
| 710 | size_t pointer_size) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 711 | // Space used by java.lang.Class and its instance fields. |
| 712 | uint32_t size = sizeof(Class); |
| 713 | // Space used by embedded tables. |
| 714 | if (has_embedded_tables) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 715 | const uint32_t embedded_imt_size = kImtSize * ImTableEntrySize(pointer_size); |
| 716 | const uint32_t embedded_vtable_size = num_vtable_entries * VTableEntrySize(pointer_size); |
| 717 | size = RoundUp(size + sizeof(uint32_t) /* embedded vtable len */, pointer_size) + |
| 718 | embedded_imt_size + embedded_vtable_size; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 719 | } |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 720 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 721 | // Space used by reference statics. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 722 | size += num_ref_static_fields * sizeof(HeapReference<Object>); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 723 | if (!IsAligned<8>(size) && num_64bit_static_fields > 0) { |
| 724 | uint32_t gap = 8 - (size & 0x7); |
| 725 | size += gap; // will be padded |
| 726 | // Shuffle 4-byte fields forward. |
| 727 | while (gap >= sizeof(uint32_t) && num_32bit_static_fields != 0) { |
| 728 | --num_32bit_static_fields; |
| 729 | gap -= sizeof(uint32_t); |
| 730 | } |
| 731 | // Shuffle 2-byte fields forward. |
| 732 | while (gap >= sizeof(uint16_t) && num_16bit_static_fields != 0) { |
| 733 | --num_16bit_static_fields; |
| 734 | gap -= sizeof(uint16_t); |
| 735 | } |
| 736 | // Shuffle byte fields forward. |
| 737 | while (gap >= sizeof(uint8_t) && num_8bit_static_fields != 0) { |
| 738 | --num_8bit_static_fields; |
| 739 | gap -= sizeof(uint8_t); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 740 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 741 | } |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 742 | // Guaranteed to be at least 4 byte aligned. No need for further alignments. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 743 | // Space used for primitive static fields. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 744 | size += num_8bit_static_fields * sizeof(uint8_t) + num_16bit_static_fields * sizeof(uint16_t) + |
| 745 | num_32bit_static_fields * sizeof(uint32_t) + num_64bit_static_fields * sizeof(uint64_t); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 746 | return size; |
| 747 | } |
| 748 | |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 749 | template <typename Visitor> |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 750 | inline void Class::VisitReferences(mirror::Class* klass, const Visitor& visitor) { |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 751 | VisitInstanceFieldsReferences(klass, visitor); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 752 | // Right after a class is allocated, but not yet loaded |
Igor Murashkin | c449e8b | 2015-06-10 15:56:42 -0700 | [diff] [blame] | 753 | // (kStatusNotReady, see ClassLinker::LoadClass()), GC may find it |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 754 | // and scan it. IsTemp() may call Class::GetAccessFlags() but may |
| 755 | // fail in the DCHECK in Class::GetAccessFlags() because the class |
| 756 | // status is kStatusNotReady. To avoid it, rely on IsResolved() |
| 757 | // only. This is fine because a temp class never goes into the |
| 758 | // kStatusResolved state. |
| 759 | if (IsResolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 760 | // Temp classes don't ever populate imt/vtable or static fields and they are not even |
Hiroshi Yamauchi | f4c15a1 | 2014-10-20 16:56:58 -0700 | [diff] [blame] | 761 | // allocated with the right size for those. Also, unresolved classes don't have fields |
| 762 | // linked yet. |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 763 | VisitStaticFieldsReferences(this, visitor); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 764 | } |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 765 | // Since this class is reachable, we must also visit the associated roots when we scan it. |
| 766 | VisitNativeRoots(visitor, Runtime::Current()->GetClassLinker()->GetImagePointerSize()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 767 | } |
| 768 | |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 769 | template<ReadBarrierOption kReadBarrierOption> |
| 770 | inline bool Class::IsReferenceClass() const { |
| 771 | return this == Reference::GetJavaLangRefReference<kReadBarrierOption>(); |
| 772 | } |
| 773 | |
Hiroshi Yamauchi | 25023c7 | 2014-05-09 11:45:53 -0700 | [diff] [blame] | 774 | template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> |
| 775 | inline bool Class::IsClassClass() { |
| 776 | Class* java_lang_Class = GetClass<kVerifyFlags, kReadBarrierOption>()-> |
| 777 | template GetClass<kVerifyFlags, kReadBarrierOption>(); |
| 778 | return this == java_lang_Class; |
| 779 | } |
| 780 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 781 | inline const DexFile& Class::GetDexFile() { |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 782 | return *GetDexCache()->GetDexFile(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | inline bool Class::DescriptorEquals(const char* match) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 786 | if (IsArrayClass()) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 787 | return match[0] == '[' && GetComponentType()->DescriptorEquals(match + 1); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 788 | } else if (IsPrimitive()) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 789 | return strcmp(Primitive::Descriptor(GetPrimitiveType()), match) == 0; |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 790 | } else if (IsProxyClass()) { |
| 791 | return ProxyDescriptorEquals(match); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 792 | } else { |
| 793 | const DexFile& dex_file = GetDexFile(); |
| 794 | const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_); |
| 795 | return strcmp(dex_file.GetTypeDescriptor(type_id), match) == 0; |
| 796 | } |
| 797 | } |
| 798 | |
Sebastien Hertz | 4e99b3d | 2014-06-24 14:35:40 +0200 | [diff] [blame] | 799 | inline void Class::AssertInitializedOrInitializingInThread(Thread* self) { |
| 800 | if (kIsDebugBuild && !IsInitialized()) { |
| 801 | CHECK(IsInitializing()) << PrettyClass(this) << " is not initializing: " << GetStatus(); |
| 802 | CHECK_EQ(GetClinitThreadId(), self->GetTid()) << PrettyClass(this) |
| 803 | << " is initializing in a different thread"; |
| 804 | } |
| 805 | } |
| 806 | |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 807 | inline ObjectArray<Class>* Class::GetInterfaces() { |
| 808 | CHECK(IsProxyClass()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 809 | // First static field. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 810 | auto* field = GetStaticField(0); |
| 811 | DCHECK_STREQ(field->GetName(), "interfaces"); |
| 812 | MemberOffset field_offset = field->GetOffset(); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 813 | return GetFieldObject<ObjectArray<Class>>(field_offset); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 816 | inline ObjectArray<ObjectArray<Class>>* Class::GetThrows() { |
| 817 | CHECK(IsProxyClass()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 818 | // Second static field. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 819 | auto* field = GetStaticField(1); |
| 820 | DCHECK_STREQ(field->GetName(), "throws"); |
| 821 | MemberOffset field_offset = field->GetOffset(); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 822 | return GetFieldObject<ObjectArray<ObjectArray<Class>>>(field_offset); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 825 | inline MemberOffset Class::GetDisableIntrinsicFlagOffset() { |
| 826 | CHECK(IsReferenceClass()); |
| 827 | // First static field |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 828 | auto* field = GetStaticField(0); |
| 829 | DCHECK_STREQ(field->GetName(), "disableIntrinsic"); |
| 830 | return field->GetOffset(); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | inline MemberOffset Class::GetSlowPathFlagOffset() { |
| 834 | CHECK(IsReferenceClass()); |
| 835 | // Second static field |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 836 | auto* field = GetStaticField(1); |
| 837 | DCHECK_STREQ(field->GetName(), "slowPathEnabled"); |
| 838 | return field->GetOffset(); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | inline bool Class::GetSlowPathEnabled() { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 842 | return GetFieldBoolean(GetSlowPathFlagOffset()); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | inline void Class::SetSlowPath(bool enabled) { |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 846 | SetFieldBoolean<false, false>(GetSlowPathFlagOffset(), enabled); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 849 | inline void Class::InitializeClassVisitor::operator()( |
| 850 | mirror::Object* obj, size_t usable_size) const { |
| 851 | DCHECK_LE(class_size_, usable_size); |
| 852 | // Avoid AsClass as object is not yet in live bitmap or allocation stack. |
| 853 | mirror::Class* klass = down_cast<mirror::Class*>(obj); |
| 854 | // DCHECK(klass->IsClass()); |
| 855 | klass->SetClassSize(class_size_); |
| 856 | klass->SetPrimitiveType(Primitive::kPrimNot); // Default to not being primitive. |
| 857 | klass->SetDexClassDefIndex(DexFile::kDexNoIndex16); // Default to no valid class def index. |
| 858 | klass->SetDexTypeIndex(DexFile::kDexNoIndex16); // Default to no valid type index. |
| 859 | } |
| 860 | |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 861 | inline void Class::SetAccessFlags(uint32_t new_access_flags) { |
| 862 | // Called inside a transaction when setting pre-verified flag during boot image compilation. |
| 863 | if (Runtime::Current()->IsActiveTransaction()) { |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 864 | SetField32<true>(AccessFlagsOffset(), new_access_flags); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 865 | } else { |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 866 | SetField32<false>(AccessFlagsOffset(), new_access_flags); |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | inline void Class::SetClassFlags(uint32_t new_flags) { |
| 871 | if (Runtime::Current()->IsActiveTransaction()) { |
| 872 | SetField32<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_flags_), new_flags); |
| 873 | } else { |
| 874 | SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_flags_), new_flags); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 875 | } |
| 876 | } |
| 877 | |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 878 | inline uint32_t Class::NumDirectInterfaces() { |
| 879 | if (IsPrimitive()) { |
| 880 | return 0; |
| 881 | } else if (IsArrayClass()) { |
| 882 | return 2; |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 883 | } else if (IsProxyClass()) { |
| 884 | mirror::ObjectArray<mirror::Class>* interfaces = GetInterfaces(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 885 | return interfaces != nullptr ? interfaces->GetLength() : 0; |
| 886 | } else { |
| 887 | const DexFile::TypeList* interfaces = GetInterfaceTypeList(); |
| 888 | if (interfaces == nullptr) { |
| 889 | return 0; |
| 890 | } else { |
| 891 | return interfaces->Size(); |
| 892 | } |
| 893 | } |
| 894 | } |
| 895 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 896 | inline void Class::SetDexCacheStrings(GcRoot<String>* new_dex_cache_strings) { |
| 897 | SetFieldPtr<false>(DexCacheStringsOffset(), new_dex_cache_strings); |
Mathieu Chartier | eace458 | 2014-11-24 18:29:54 -0800 | [diff] [blame] | 898 | } |
| 899 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 900 | inline GcRoot<String>* Class::GetDexCacheStrings() { |
| 901 | return GetFieldPtr<GcRoot<String>*>(DexCacheStringsOffset()); |
Mathieu Chartier | eace458 | 2014-11-24 18:29:54 -0800 | [diff] [blame] | 902 | } |
| 903 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 904 | template<class Visitor> |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 905 | void mirror::Class::VisitNativeRoots(Visitor& visitor, size_t pointer_size) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 906 | for (ArtField& field : GetSFieldsUnchecked()) { |
| 907 | // Visit roots first in case the declaring class gets moved. |
| 908 | field.VisitRoots(visitor); |
| 909 | if (kIsDebugBuild && IsResolved()) { |
| 910 | CHECK_EQ(field.GetDeclaringClass(), this) << GetStatus(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 911 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 912 | } |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 913 | for (ArtField& field : GetIFieldsUnchecked()) { |
| 914 | // Visit roots first in case the declaring class gets moved. |
| 915 | field.VisitRoots(visitor); |
| 916 | if (kIsDebugBuild && IsResolved()) { |
| 917 | CHECK_EQ(field.GetDeclaringClass(), this) << GetStatus(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 918 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 919 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame^] | 920 | for (ArtMethod& method : GetMethods(pointer_size)) { |
Mathieu Chartier | 1147b9b | 2015-09-14 18:50:08 -0700 | [diff] [blame] | 921 | method.VisitRoots(visitor, pointer_size); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 925 | inline IterationRange<StrideIterator<ArtMethod>> Class::GetDirectMethods(size_t pointer_size) { |
| 926 | CheckPointerSize(pointer_size); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame^] | 927 | return GetDirectMethodsSliceUnchecked(pointer_size).AsRange(); |
| 928 | } |
| 929 | |
| 930 | inline IterationRange<StrideIterator<ArtMethod>> Class::GetDeclaredMethods( |
| 931 | size_t pointer_size) { |
| 932 | CheckPointerSize(pointer_size); |
| 933 | return GetDeclaredMethodsSliceUnchecked(pointer_size).AsRange(); |
| 934 | } |
| 935 | |
| 936 | inline IterationRange<StrideIterator<ArtMethod>> Class::GetDeclaredVirtualMethods( |
| 937 | size_t pointer_size) { |
| 938 | CheckPointerSize(pointer_size); |
| 939 | return GetDeclaredVirtualMethodsSliceUnchecked(pointer_size).AsRange(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | inline IterationRange<StrideIterator<ArtMethod>> Class::GetVirtualMethods(size_t pointer_size) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 943 | CheckPointerSize(pointer_size); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame^] | 944 | return GetVirtualMethodsSliceUnchecked(pointer_size).AsRange(); |
| 945 | } |
| 946 | |
| 947 | inline IterationRange<StrideIterator<ArtMethod>> Class::GetCopiedMethods(size_t pointer_size) { |
| 948 | CheckPointerSize(pointer_size); |
| 949 | return GetCopiedMethodsSliceUnchecked(pointer_size).AsRange(); |
| 950 | } |
| 951 | |
| 952 | |
| 953 | inline IterationRange<StrideIterator<ArtMethod>> Class::GetMethods(size_t pointer_size) { |
| 954 | CheckPointerSize(pointer_size); |
| 955 | return MakeIterationRangeFromLengthPrefixedArray(GetMethodsPtr(), |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 956 | ArtMethod::Size(pointer_size), |
| 957 | ArtMethod::Alignment(pointer_size)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | inline IterationRange<StrideIterator<ArtField>> Class::GetIFields() { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 961 | return MakeIterationRangeFromLengthPrefixedArray(GetIFieldsPtr()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | inline IterationRange<StrideIterator<ArtField>> Class::GetSFields() { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 965 | return MakeIterationRangeFromLengthPrefixedArray(GetSFieldsPtr()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | inline IterationRange<StrideIterator<ArtField>> Class::GetIFieldsUnchecked() { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 969 | return MakeIterationRangeFromLengthPrefixedArray(GetIFieldsPtrUnchecked()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | inline IterationRange<StrideIterator<ArtField>> Class::GetSFieldsUnchecked() { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 973 | return MakeIterationRangeFromLengthPrefixedArray(GetSFieldsPtrUnchecked()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | inline MemberOffset Class::EmbeddedImTableOffset(size_t pointer_size) { |
| 977 | CheckPointerSize(pointer_size); |
| 978 | // Round up since we want the embedded imt and vtable to be pointer size aligned in case 64 bits. |
| 979 | // Add 32 bits for embedded vtable length. |
| 980 | return MemberOffset( |
| 981 | RoundUp(EmbeddedVTableLengthOffset().Uint32Value() + sizeof(uint32_t), pointer_size)); |
| 982 | } |
| 983 | |
| 984 | inline MemberOffset Class::EmbeddedVTableOffset(size_t pointer_size) { |
| 985 | CheckPointerSize(pointer_size); |
| 986 | return MemberOffset(EmbeddedImTableOffset(pointer_size).Uint32Value() + |
| 987 | kImtSize * ImTableEntrySize(pointer_size)); |
| 988 | } |
| 989 | |
| 990 | inline void Class::CheckPointerSize(size_t pointer_size) { |
| 991 | DCHECK(ValidPointerSize(pointer_size)) << pointer_size; |
| 992 | DCHECK_EQ(pointer_size, Runtime::Current()->GetClassLinker()->GetImagePointerSize()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 993 | } |
| 994 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 995 | template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> |
| 996 | inline Class* Class::GetComponentType() { |
| 997 | return GetFieldObject<Class, kVerifyFlags, kReadBarrierOption>(ComponentTypeOffset()); |
| 998 | } |
| 999 | |
| 1000 | template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> |
| 1001 | inline bool Class::IsArrayClass() { |
| 1002 | return GetComponentType<kVerifyFlags, kReadBarrierOption>() != nullptr; |
| 1003 | } |
| 1004 | |
| 1005 | inline bool Class::IsAssignableFrom(Class* src) { |
| 1006 | DCHECK(src != nullptr); |
| 1007 | if (this == src) { |
| 1008 | // Can always assign to things of the same type. |
| 1009 | return true; |
| 1010 | } else if (IsObjectClass()) { |
| 1011 | // Can assign any reference to java.lang.Object. |
| 1012 | return !src->IsPrimitive(); |
| 1013 | } else if (IsInterface()) { |
| 1014 | return src->Implements(this); |
| 1015 | } else if (src->IsArrayClass()) { |
| 1016 | return IsAssignableFromArray(src); |
| 1017 | } else { |
| 1018 | return !src->IsInterface() && src->IsSubClass(this); |
| 1019 | } |
| 1020 | } |
| 1021 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1022 | inline uint32_t Class::NumDirectMethods() { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame^] | 1023 | return GetVirtualMethodsStartOffset(); |
| 1024 | } |
| 1025 | |
| 1026 | inline uint32_t Class::NumDeclaredVirtualMethods() { |
| 1027 | return GetCopiedMethodsStartOffset() - GetVirtualMethodsStartOffset(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | inline uint32_t Class::NumVirtualMethods() { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame^] | 1031 | return NumMethods() - GetVirtualMethodsStartOffset(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | inline uint32_t Class::NumInstanceFields() { |
| 1035 | LengthPrefixedArray<ArtField>* arr = GetIFieldsPtrUnchecked(); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 1036 | return arr != nullptr ? arr->size() : 0u; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | inline uint32_t Class::NumStaticFields() { |
| 1040 | LengthPrefixedArray<ArtField>* arr = GetSFieldsPtrUnchecked(); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 1041 | return arr != nullptr ? arr->size() : 0u; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1044 | template <typename Visitor> |
| 1045 | inline void Class::FixupNativePointers(mirror::Class* dest, |
| 1046 | size_t pointer_size, |
| 1047 | const Visitor& visitor) { |
| 1048 | // Update the field arrays. |
| 1049 | LengthPrefixedArray<ArtField>* const sfields = GetSFieldsPtr(); |
| 1050 | LengthPrefixedArray<ArtField>* const new_sfields = visitor(sfields); |
| 1051 | if (sfields != new_sfields) { |
| 1052 | dest->SetSFieldsPtrUnchecked(new_sfields); |
| 1053 | } |
| 1054 | LengthPrefixedArray<ArtField>* const ifields = GetIFieldsPtr(); |
| 1055 | LengthPrefixedArray<ArtField>* const new_ifields = visitor(ifields); |
| 1056 | if (ifields != new_ifields) { |
| 1057 | dest->SetIFieldsPtrUnchecked(new_ifields); |
| 1058 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame^] | 1059 | // Update method array. |
| 1060 | LengthPrefixedArray<ArtMethod>* methods = GetMethodsPtr(); |
| 1061 | LengthPrefixedArray<ArtMethod>* new_methods = visitor(methods); |
| 1062 | if (methods != new_methods) { |
| 1063 | dest->SetMethodsPtrInternal(new_methods); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1064 | } |
| 1065 | // Update dex cache strings. |
| 1066 | GcRoot<mirror::String>* strings = GetDexCacheStrings(); |
| 1067 | GcRoot<mirror::String>* new_strings = visitor(strings); |
| 1068 | if (strings != new_strings) { |
| 1069 | dest->SetDexCacheStrings(new_strings); |
| 1070 | } |
| 1071 | // Fix up embedded tables. |
| 1072 | if (!IsTemp() && ShouldHaveEmbeddedImtAndVTable()) { |
| 1073 | for (int32_t i = 0, count = GetEmbeddedVTableLength(); i < count; ++i) { |
| 1074 | ArtMethod* method = GetEmbeddedVTableEntry(i, pointer_size); |
| 1075 | ArtMethod* new_method = visitor(method); |
| 1076 | if (method != new_method) { |
| 1077 | dest->SetEmbeddedVTableEntryUnchecked(i, new_method, pointer_size); |
| 1078 | } |
| 1079 | } |
| 1080 | for (size_t i = 0; i < mirror::Class::kImtSize; ++i) { |
| 1081 | ArtMethod* method = GetEmbeddedImTableEntry(i, pointer_size); |
| 1082 | ArtMethod* new_method = visitor(method); |
| 1083 | if (method != new_method) { |
| 1084 | dest->SetEmbeddedImTableEntry(i, new_method, pointer_size); |
| 1085 | } |
| 1086 | } |
| 1087 | } |
| 1088 | } |
| 1089 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1090 | } // namespace mirror |
| 1091 | } // namespace art |
| 1092 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 1093 | #endif // ART_RUNTIME_MIRROR_CLASS_INL_H_ |