blob: cb85fa6e5663a4264357387fd0691774112886c2 [file] [log] [blame]
Andreas Gampe75a7db62016-09-26 12:04:26 -07001/*
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"
Andreas Gampea1ff30f2016-09-27 12:19:45 -070023#include "dex_file.h"
24#include "utf.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070025
26namespace art {
27
Andreas Gampea1ff30f2016-09-27 12:19:45 -070028static constexpr bool kImTableHashUseName = true;
29static constexpr bool kImTableHashUseCoefficients = true;
30
31// Magic configuration that minimizes some common runtime calls.
32static constexpr uint32_t kImTableHashCoefficientClass = 427;
33static constexpr uint32_t kImTableHashCoefficientName = 16;
34static constexpr uint32_t kImTableHashCoefficientSignature = 14;
35
36inline 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 DexFile::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 DexFile::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 DexFile::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 DexFile::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 }
Andreas Gampe75a7db62016-09-26 12:04:26 -070083}
84
85inline uint32_t ImTable::GetImtIndex(ArtMethod* method) {
Andreas Gampea1ff30f2016-09-27 12:19:45 -070086 uint32_t class_hash, name_hash, signature_hash;
87 GetImtHashComponents(method, &class_hash, &name_hash, &signature_hash);
88
89 uint32_t mixed_hash;
90 if (!kImTableHashUseCoefficients) {
91 mixed_hash = class_hash + name_hash + signature_hash;
92 } else {
93 mixed_hash = kImTableHashCoefficientClass * class_hash +
94 kImTableHashCoefficientName * name_hash +
95 kImTableHashCoefficientSignature * signature_hash;
96 }
97
98 return mixed_hash % ImTable::kSize;
Andreas Gampe75a7db62016-09-26 12:04:26 -070099}
100
101} // namespace art
102
103#endif // ART_RUNTIME_IMTABLE_INL_H_
104