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/casts.h"
21 #include "base/enums.h"
22 #include "base/locks.h"
23 #include "base/macros.h"
24 
25 namespace art {
26 
27 class ArtMethod;
28 class DexFile;
29 
30 class ImTable {
31  public:
32   // Interface method table size. Increasing this value reduces the chance of two interface methods
33   // colliding in the interface method table but increases the size of classes that implement
34   // (non-marker) interfaces.
35   // When this value changes, old images become incompatible, so image file version must change too.
36   static constexpr size_t kSize = 43;
37 
AddressOfElement(size_t index,PointerSize pointer_size)38   uint8_t* AddressOfElement(size_t index, PointerSize pointer_size) {
39     return reinterpret_cast<uint8_t*>(this) + OffsetOfElement(index, pointer_size);
40   }
41 
Get(size_t index,PointerSize pointer_size)42   ArtMethod* Get(size_t index, PointerSize pointer_size) {
43     DCHECK_LT(index, kSize);
44     uint8_t* ptr = AddressOfElement(index, pointer_size);
45     if (pointer_size == PointerSize::k32) {
46       uint32_t value = *reinterpret_cast<uint32_t*>(ptr);
47       return reinterpret_cast32<ArtMethod*>(value);
48     } else {
49       uint64_t value = *reinterpret_cast<uint64_t*>(ptr);
50       return reinterpret_cast64<ArtMethod*>(value);
51     }
52   }
53 
Set(size_t index,ArtMethod * method,PointerSize pointer_size)54   void Set(size_t index, ArtMethod* method, PointerSize pointer_size) {
55     DCHECK_LT(index, kSize);
56     uint8_t* ptr = AddressOfElement(index, pointer_size);
57     if (pointer_size == PointerSize::k32) {
58       *reinterpret_cast<uint32_t*>(ptr) = reinterpret_cast32<uint32_t>(method);
59     } else {
60       *reinterpret_cast<uint64_t*>(ptr) = reinterpret_cast64<uint64_t>(method);
61     }
62   }
63 
OffsetOfElement(size_t index,PointerSize pointer_size)64   static size_t OffsetOfElement(size_t index, PointerSize pointer_size) {
65     return index * static_cast<size_t>(pointer_size);
66   }
67 
Populate(ArtMethod ** data,PointerSize pointer_size)68   void Populate(ArtMethod** data, PointerSize pointer_size) {
69     for (size_t i = 0; i < kSize; ++i) {
70       Set(i, data[i], pointer_size);
71     }
72   }
73 
SizeInBytes(PointerSize pointer_size)74   constexpr static size_t SizeInBytes(PointerSize pointer_size) {
75     return kSize * static_cast<size_t>(pointer_size);
76   }
77 
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)
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_);
89 };
90 
91 }  // namespace art
92 
93 #endif  // ART_RUNTIME_IMTABLE_H_
94 
95