• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "base/bit_utils.h"
21 #include "base/casts.h"
22 #include "base/enums.h"
23 #include "base/locks.h"
24 #include "base/macros.h"
25 
26 namespace art {
27 
28 class ArtMethod;
29 class DexFile;
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.
36   // When this value changes, old images become incompatible, so image file version must change too.
37   static constexpr size_t kSize = 43;
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);
42 
AddressOfElement(size_t index,PointerSize pointer_size)43   uint8_t* AddressOfElement(size_t index, PointerSize pointer_size) {
44     return reinterpret_cast<uint8_t*>(this) + OffsetOfElement(index, pointer_size);
45   }
46 
Get(size_t index,PointerSize pointer_size)47   ArtMethod* Get(size_t index, PointerSize pointer_size) {
48     DCHECK_LT(index, kSize);
49     uint8_t* ptr = AddressOfElement(index, pointer_size);
50     if (pointer_size == PointerSize::k32) {
51       uint32_t value = *reinterpret_cast<uint32_t*>(ptr);
52       return reinterpret_cast32<ArtMethod*>(value);
53     } else {
54       uint64_t value = *reinterpret_cast<uint64_t*>(ptr);
55       return reinterpret_cast64<ArtMethod*>(value);
56     }
57   }
58 
Set(size_t index,ArtMethod * method,PointerSize pointer_size)59   void Set(size_t index, ArtMethod* method, PointerSize pointer_size) {
60     DCHECK_LT(index, kSize);
61     uint8_t* ptr = AddressOfElement(index, pointer_size);
62     if (pointer_size == PointerSize::k32) {
63       *reinterpret_cast<uint32_t*>(ptr) = reinterpret_cast32<uint32_t>(method);
64     } else {
65       *reinterpret_cast<uint64_t*>(ptr) = reinterpret_cast64<uint64_t>(method);
66     }
67   }
68 
OffsetOfElement(size_t index,PointerSize pointer_size)69   static size_t OffsetOfElement(size_t index, PointerSize pointer_size) {
70     return index * static_cast<size_t>(pointer_size);
71   }
72 
Populate(ArtMethod ** data,PointerSize pointer_size)73   void Populate(ArtMethod** data, PointerSize pointer_size) {
74     for (size_t i = 0; i < kSize; ++i) {
75       Set(i, data[i], pointer_size);
76     }
77   }
78 
SizeInBytes(PointerSize pointer_size)79   constexpr static size_t SizeInBytes(PointerSize pointer_size) {
80     return kSize * static_cast<size_t>(pointer_size);
81   }
82 
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)
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_);
94 };
95 
96 }  // namespace art
97 
98 #endif  // ART_RUNTIME_IMTABLE_H_
99 
100