• 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_INL_H_
18 #define ART_RUNTIME_IMTABLE_INL_H_
19 
20 #include "imtable.h"
21 
22 #include "art_method-inl.h"
23 #include "dex/dex_file.h"
24 #include "dex/utf.h"
25 
26 namespace art {
27 
28 static constexpr bool kImTableHashUseName = true;
29 static constexpr bool kImTableHashUseCoefficients = true;
30 
31 // Magic configuration that minimizes some common runtime calls.
32 static constexpr uint32_t kImTableHashCoefficientClass = 427;
33 static constexpr uint32_t kImTableHashCoefficientName = 16;
34 static constexpr uint32_t kImTableHashCoefficientSignature = 14;
35 
GetImtHashComponents(ArtMethod * method,uint32_t * class_hash,uint32_t * name_hash,uint32_t * signature_hash)36 inline void ImTable::GetImtHashComponents(ArtMethod* method,
37                                           uint32_t* class_hash,
38                                           uint32_t* name_hash,
39                                           uint32_t* signature_hash) {
40   if (kImTableHashUseName) {
41     if (method->IsProxyMethod()) {
42       *class_hash = 0;
43       *name_hash = 0;
44       *signature_hash = 0;
45       return;
46     }
47 
48     const DexFile* dex_file = method->GetDexFile();
49     const dex::MethodId& method_id = dex_file->GetMethodId(method->GetDexMethodIndex());
50 
51     // Class descriptor for the class component.
52     *class_hash = ComputeModifiedUtf8Hash(dex_file->GetMethodDeclaringClassDescriptor(method_id));
53 
54     // Method name for the method component.
55     *name_hash = ComputeModifiedUtf8Hash(dex_file->GetMethodName(method_id));
56 
57     const dex::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
58 
59     // Read the proto for the signature component.
60     uint32_t tmp = ComputeModifiedUtf8Hash(
61         dex_file->GetTypeDescriptor(dex_file->GetTypeId(proto_id.return_type_idx_)));
62 
63     // Mix in the argument types.
64     // Note: we could consider just using the shorty. This would be faster, at the price of
65     //       potential collisions.
66     const dex::TypeList* param_types = dex_file->GetProtoParameters(proto_id);
67     if (param_types != nullptr) {
68       for (size_t i = 0; i != param_types->Size(); ++i) {
69         const dex::TypeItem& type = param_types->GetTypeItem(i);
70         tmp = 31 * tmp + ComputeModifiedUtf8Hash(
71             dex_file->GetTypeDescriptor(dex_file->GetTypeId(type.type_idx_)));
72       }
73     }
74 
75     *signature_hash = tmp;
76     return;
77   } else {
78     *class_hash = method->GetDexMethodIndex();
79     *name_hash = 0;
80     *signature_hash = 0;
81     return;
82   }
83 }
84 
GetImtIndex(ArtMethod * method)85 inline uint32_t ImTable::GetImtIndex(ArtMethod* method) {
86   DCHECK(!method->IsCopied());
87   if (!method->IsAbstract()) {
88     // For default methods, where we cannot store the imt_index, we use the
89     // method_index instead. We mask it with the closest power of two to
90     // simplify the interpreter.
91     return method->GetMethodIndex() & (ImTable::kSizeTruncToPowerOfTwo - 1);
92   }
93   uint32_t class_hash, name_hash, signature_hash;
94   GetImtHashComponents(method, &class_hash, &name_hash, &signature_hash);
95 
96   uint32_t mixed_hash;
97   if (!kImTableHashUseCoefficients) {
98     mixed_hash = class_hash + name_hash + signature_hash;
99   } else {
100     mixed_hash = kImTableHashCoefficientClass * class_hash +
101                  kImTableHashCoefficientName * name_hash +
102                  kImTableHashCoefficientSignature * signature_hash;
103   }
104 
105   return mixed_hash % ImTable::kSize;
106 }
107 
108 }  // namespace art
109 
110 #endif  // ART_RUNTIME_IMTABLE_INL_H_
111 
112