Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| 17 | #ifndef ART_RUNTIME_IMTABLE_H_ |
| 18 | #define ART_RUNTIME_IMTABLE_H_ |
| 19 | |
Nicolas Geoffray | d5a8695 | 2021-01-19 10:35:54 +0000 | [diff] [blame] | 20 | #include "base/bit_utils.h" |
Vladimir Marko | 78baed5 | 2018-10-11 10:44:58 +0100 | [diff] [blame] | 21 | #include "base/casts.h" |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 22 | #include "base/enums.h" |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 23 | #include "base/locks.h" |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 24 | #include "base/macros.h" |
| 25 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 26 | namespace art { |
| 27 | |
| 28 | class ArtMethod; |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 29 | class DexFile; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 30 | |
| 31 | class ImTable { |
| 32 | public: |
| 33 | // Interface method table size. Increasing this value reduces the chance of two interface methods |
| 34 | // colliding in the interface method table but increases the size of classes that implement |
| 35 | // (non-marker) interfaces. |
Vladimir Marko | b50d453 | 2020-06-25 14:13:47 +0100 | [diff] [blame] | 36 | // When this value changes, old images become incompatible, so image file version must change too. |
| 37 | static constexpr size_t kSize = 43; |
Nicolas Geoffray | d5a8695 | 2021-01-19 10:35:54 +0000 | [diff] [blame] | 38 | // Default methods cannot store the imt_index, so instead we make its IMT index depend on the |
| 39 | // method_index and mask it with the closest power of 2 of kSize - 1. This |
| 40 | // is to simplify fetching it in the interpreter. |
| 41 | static constexpr size_t kSizeTruncToPowerOfTwo = TruncToPowerOfTwo(kSize); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 42 | |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 43 | uint8_t* AddressOfElement(size_t index, PointerSize pointer_size) { |
| 44 | return reinterpret_cast<uint8_t*>(this) + OffsetOfElement(index, pointer_size); |
| 45 | } |
| 46 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 47 | ArtMethod* Get(size_t index, PointerSize pointer_size) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 48 | DCHECK_LT(index, kSize); |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 49 | uint8_t* ptr = AddressOfElement(index, pointer_size); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 50 | if (pointer_size == PointerSize::k32) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 51 | uint32_t value = *reinterpret_cast<uint32_t*>(ptr); |
Vladimir Marko | 78baed5 | 2018-10-11 10:44:58 +0100 | [diff] [blame] | 52 | return reinterpret_cast32<ArtMethod*>(value); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 53 | } else { |
| 54 | uint64_t value = *reinterpret_cast<uint64_t*>(ptr); |
Vladimir Marko | 78baed5 | 2018-10-11 10:44:58 +0100 | [diff] [blame] | 55 | return reinterpret_cast64<ArtMethod*>(value); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 59 | void Set(size_t index, ArtMethod* method, PointerSize pointer_size) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 60 | DCHECK_LT(index, kSize); |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 61 | uint8_t* ptr = AddressOfElement(index, pointer_size); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 62 | if (pointer_size == PointerSize::k32) { |
Vladimir Marko | 78baed5 | 2018-10-11 10:44:58 +0100 | [diff] [blame] | 63 | *reinterpret_cast<uint32_t*>(ptr) = reinterpret_cast32<uint32_t>(method); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 64 | } else { |
Vladimir Marko | 78baed5 | 2018-10-11 10:44:58 +0100 | [diff] [blame] | 65 | *reinterpret_cast<uint64_t*>(ptr) = reinterpret_cast64<uint64_t>(method); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 69 | static size_t OffsetOfElement(size_t index, PointerSize pointer_size) { |
| 70 | return index * static_cast<size_t>(pointer_size); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 73 | void Populate(ArtMethod** data, PointerSize pointer_size) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 74 | for (size_t i = 0; i < kSize; ++i) { |
| 75 | Set(i, data[i], pointer_size); |
| 76 | } |
| 77 | } |
| 78 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 79 | constexpr static size_t SizeInBytes(PointerSize pointer_size) { |
| 80 | return kSize * static_cast<size_t>(pointer_size); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 81 | } |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 82 | |
Andreas Gampe | a1ff30f | 2016-09-27 12:19:45 -0700 | [diff] [blame] | 83 | // Converts a method to the base hash components used in GetImtIndex. |
| 84 | ALWAYS_INLINE static inline void GetImtHashComponents(ArtMethod* method, |
| 85 | uint32_t* class_hash, |
| 86 | uint32_t* name_hash, |
| 87 | uint32_t* signature_hash) |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 88 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 89 | |
| 90 | // The (complete) hashing scheme to map an ArtMethod to a slot in the Interface Method Table |
| 91 | // (IMT). |
| 92 | ALWAYS_INLINE static inline uint32_t GetImtIndex(ArtMethod* method) |
| 93 | REQUIRES_SHARED(Locks::mutator_lock_); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | } // namespace art |
| 97 | |
| 98 | #endif // ART_RUNTIME_IMTABLE_H_ |
| 99 | |