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 
24 #include "base/casts.h"
25 #include "base/enums.h"
26 #include "base/locks.h"
27 #include "base/macros.h"
28 
29 namespace art {
30 
31 class ArtMethod;
32 class DexFile;
33 
34 class ImTable {
35  public:
36   // Interface method table size. Increasing this value reduces the chance of two interface methods
37   // colliding in the interface method table but increases the size of classes that implement
38   // (non-marker) interfaces.
39   static constexpr size_t kSize = IMT_SIZE;
40 
AddressOfElement(size_t index,PointerSize pointer_size)41   uint8_t* AddressOfElement(size_t index, PointerSize pointer_size) {
42     return reinterpret_cast<uint8_t*>(this) + OffsetOfElement(index, pointer_size);
43   }
44 
Get(size_t index,PointerSize pointer_size)45   ArtMethod* Get(size_t index, PointerSize pointer_size) {
46     DCHECK_LT(index, kSize);
47     uint8_t* ptr = AddressOfElement(index, pointer_size);
48     if (pointer_size == PointerSize::k32) {
49       uint32_t value = *reinterpret_cast<uint32_t*>(ptr);
50       return reinterpret_cast32<ArtMethod*>(value);
51     } else {
52       uint64_t value = *reinterpret_cast<uint64_t*>(ptr);
53       return reinterpret_cast64<ArtMethod*>(value);
54     }
55   }
56 
Set(size_t index,ArtMethod * method,PointerSize pointer_size)57   void Set(size_t index, ArtMethod* method, PointerSize pointer_size) {
58     DCHECK_LT(index, kSize);
59     uint8_t* ptr = AddressOfElement(index, pointer_size);
60     if (pointer_size == PointerSize::k32) {
61       *reinterpret_cast<uint32_t*>(ptr) = reinterpret_cast32<uint32_t>(method);
62     } else {
63       *reinterpret_cast<uint64_t*>(ptr) = reinterpret_cast64<uint64_t>(method);
64     }
65   }
66 
OffsetOfElement(size_t index,PointerSize pointer_size)67   static size_t OffsetOfElement(size_t index, PointerSize pointer_size) {
68     return index * static_cast<size_t>(pointer_size);
69   }
70 
Populate(ArtMethod ** data,PointerSize pointer_size)71   void Populate(ArtMethod** data, PointerSize pointer_size) {
72     for (size_t i = 0; i < kSize; ++i) {
73       Set(i, data[i], pointer_size);
74     }
75   }
76 
SizeInBytes(PointerSize pointer_size)77   constexpr static size_t SizeInBytes(PointerSize pointer_size) {
78     return kSize * static_cast<size_t>(pointer_size);
79   }
80 
81   // Converts a method to the base hash components used in GetImtIndex.
82   ALWAYS_INLINE static inline void GetImtHashComponents(ArtMethod* method,
83                                                         uint32_t* class_hash,
84                                                         uint32_t* name_hash,
85                                                         uint32_t* signature_hash)
86       REQUIRES_SHARED(Locks::mutator_lock_);
87 
88   // The (complete) hashing scheme to map an ArtMethod to a slot in the Interface Method Table
89   // (IMT).
90   ALWAYS_INLINE static inline uint32_t GetImtIndex(ArtMethod* method)
91       REQUIRES_SHARED(Locks::mutator_lock_);
92 };
93 
94 }  // namespace art
95 
96 #endif  // ART_RUNTIME_IMTABLE_H_
97 
98