blob: df10cda816fc9a7df0d4470a419d0686dae5c7c2 [file] [log] [blame]
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001/*
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 Geoffrayd5a86952021-01-19 10:35:54 +000020#include "base/bit_utils.h"
Vladimir Marko78baed52018-10-11 10:44:58 +010021#include "base/casts.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070022#include "base/enums.h"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080023#include "base/locks.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070024#include "base/macros.h"
25
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000026namespace art {
27
28class ArtMethod;
Andreas Gampe75a7db62016-09-26 12:04:26 -070029class DexFile;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000030
31class 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 Markob50d4532020-06-25 14:13:47 +010036 // When this value changes, old images become incompatible, so image file version must change too.
37 static constexpr size_t kSize = 43;
Nicolas Geoffrayd5a86952021-01-19 10:35:54 +000038 // 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 Udovichenkoa62cb9b2016-06-30 09:18:25 +000042
Mathieu Chartier8c19d242017-03-06 12:35:10 -080043 uint8_t* AddressOfElement(size_t index, PointerSize pointer_size) {
44 return reinterpret_cast<uint8_t*>(this) + OffsetOfElement(index, pointer_size);
45 }
46
Andreas Gampe542451c2016-07-26 09:02:02 -070047 ArtMethod* Get(size_t index, PointerSize pointer_size) {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000048 DCHECK_LT(index, kSize);
Mathieu Chartier8c19d242017-03-06 12:35:10 -080049 uint8_t* ptr = AddressOfElement(index, pointer_size);
Andreas Gampe542451c2016-07-26 09:02:02 -070050 if (pointer_size == PointerSize::k32) {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000051 uint32_t value = *reinterpret_cast<uint32_t*>(ptr);
Vladimir Marko78baed52018-10-11 10:44:58 +010052 return reinterpret_cast32<ArtMethod*>(value);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000053 } else {
54 uint64_t value = *reinterpret_cast<uint64_t*>(ptr);
Vladimir Marko78baed52018-10-11 10:44:58 +010055 return reinterpret_cast64<ArtMethod*>(value);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000056 }
57 }
58
Andreas Gampe542451c2016-07-26 09:02:02 -070059 void Set(size_t index, ArtMethod* method, PointerSize pointer_size) {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000060 DCHECK_LT(index, kSize);
Mathieu Chartier8c19d242017-03-06 12:35:10 -080061 uint8_t* ptr = AddressOfElement(index, pointer_size);
Andreas Gampe542451c2016-07-26 09:02:02 -070062 if (pointer_size == PointerSize::k32) {
Vladimir Marko78baed52018-10-11 10:44:58 +010063 *reinterpret_cast<uint32_t*>(ptr) = reinterpret_cast32<uint32_t>(method);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000064 } else {
Vladimir Marko78baed52018-10-11 10:44:58 +010065 *reinterpret_cast<uint64_t*>(ptr) = reinterpret_cast64<uint64_t>(method);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000066 }
67 }
68
Andreas Gampe542451c2016-07-26 09:02:02 -070069 static size_t OffsetOfElement(size_t index, PointerSize pointer_size) {
70 return index * static_cast<size_t>(pointer_size);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000071 }
72
Andreas Gampe542451c2016-07-26 09:02:02 -070073 void Populate(ArtMethod** data, PointerSize pointer_size) {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000074 for (size_t i = 0; i < kSize; ++i) {
75 Set(i, data[i], pointer_size);
76 }
77 }
78
Andreas Gampe542451c2016-07-26 09:02:02 -070079 constexpr static size_t SizeInBytes(PointerSize pointer_size) {
80 return kSize * static_cast<size_t>(pointer_size);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000081 }
Andreas Gampe75a7db62016-09-26 12:04:26 -070082
Andreas Gampea1ff30f2016-09-27 12:19:45 -070083 // 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 Gampe75a7db62016-09-26 12:04:26 -070088 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 Udovichenkoa62cb9b2016-06-30 09:18:25 +000094};
95
96} // namespace art
97
98#endif // ART_RUNTIME_IMTABLE_H_
99