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 | |
| 20 | #ifndef IMT_SIZE |
| 21 | #error IMT_SIZE not defined |
| 22 | #endif |
| 23 | |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 24 | #include "base/enums.h" |
| 25 | #include "base/macros.h" |
Andreas Gampe | a1ff30f | 2016-09-27 12:19:45 -0700 | [diff] [blame] | 26 | #include "base/mutex.h" |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 27 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 28 | namespace art { |
| 29 | |
| 30 | class ArtMethod; |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 31 | class DexFile; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 32 | |
| 33 | class ImTable { |
| 34 | public: |
| 35 | // Interface method table size. Increasing this value reduces the chance of two interface methods |
| 36 | // colliding in the interface method table but increases the size of classes that implement |
| 37 | // (non-marker) interfaces. |
| 38 | static constexpr size_t kSize = IMT_SIZE; |
| 39 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 40 | ArtMethod* Get(size_t index, PointerSize pointer_size) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 41 | DCHECK_LT(index, kSize); |
| 42 | uint8_t* ptr = reinterpret_cast<uint8_t*>(this) + OffsetOfElement(index, pointer_size); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 43 | if (pointer_size == PointerSize::k32) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 44 | uint32_t value = *reinterpret_cast<uint32_t*>(ptr); |
| 45 | return reinterpret_cast<ArtMethod*>(value); |
| 46 | } else { |
| 47 | uint64_t value = *reinterpret_cast<uint64_t*>(ptr); |
| 48 | return reinterpret_cast<ArtMethod*>(value); |
| 49 | } |
| 50 | } |
| 51 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 52 | void Set(size_t index, ArtMethod* method, PointerSize pointer_size) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 53 | DCHECK_LT(index, kSize); |
| 54 | uint8_t* ptr = reinterpret_cast<uint8_t*>(this) + OffsetOfElement(index, pointer_size); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 55 | if (pointer_size == PointerSize::k32) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 56 | uintptr_t value = reinterpret_cast<uintptr_t>(method); |
| 57 | DCHECK_EQ(static_cast<uint32_t>(value), value); // Check that we dont lose any non 0 bits. |
| 58 | *reinterpret_cast<uint32_t*>(ptr) = static_cast<uint32_t>(value); |
| 59 | } else { |
| 60 | *reinterpret_cast<uint64_t*>(ptr) = reinterpret_cast<uint64_t>(method); |
| 61 | } |
| 62 | } |
| 63 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 64 | static size_t OffsetOfElement(size_t index, PointerSize pointer_size) { |
| 65 | return index * static_cast<size_t>(pointer_size); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 68 | void Populate(ArtMethod** data, PointerSize pointer_size) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 69 | for (size_t i = 0; i < kSize; ++i) { |
| 70 | Set(i, data[i], pointer_size); |
| 71 | } |
| 72 | } |
| 73 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 74 | constexpr static size_t SizeInBytes(PointerSize pointer_size) { |
| 75 | return kSize * static_cast<size_t>(pointer_size); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 76 | } |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 77 | |
Andreas Gampe | a1ff30f | 2016-09-27 12:19:45 -0700 | [diff] [blame] | 78 | // Converts a method to the base hash components used in GetImtIndex. |
| 79 | ALWAYS_INLINE static inline void GetImtHashComponents(ArtMethod* method, |
| 80 | uint32_t* class_hash, |
| 81 | uint32_t* name_hash, |
| 82 | uint32_t* signature_hash) |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 83 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 84 | |
| 85 | // The (complete) hashing scheme to map an ArtMethod to a slot in the Interface Method Table |
| 86 | // (IMT). |
| 87 | ALWAYS_INLINE static inline uint32_t GetImtIndex(ArtMethod* method) |
| 88 | REQUIRES_SHARED(Locks::mutator_lock_); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | } // namespace art |
| 92 | |
| 93 | #endif // ART_RUNTIME_IMTABLE_H_ |
| 94 | |