blob: 48a86439337ce4dccdd65e1df90cf5042203bdf8 [file] [log] [blame]
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001/*
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
Vladimir Marko78baed52018-10-11 10:44:58 +010024#include "base/casts.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070025#include "base/enums.h"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080026#include "base/locks.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070027#include "base/macros.h"
28
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000029namespace art {
30
31class ArtMethod;
Andreas Gampe75a7db62016-09-26 12:04:26 -070032class DexFile;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000033
34class 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
Mathieu Chartier8c19d242017-03-06 12:35:10 -080041 uint8_t* AddressOfElement(size_t index, PointerSize pointer_size) {
42 return reinterpret_cast<uint8_t*>(this) + OffsetOfElement(index, pointer_size);
43 }
44
Andreas Gampe542451c2016-07-26 09:02:02 -070045 ArtMethod* Get(size_t index, PointerSize pointer_size) {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000046 DCHECK_LT(index, kSize);
Mathieu Chartier8c19d242017-03-06 12:35:10 -080047 uint8_t* ptr = AddressOfElement(index, pointer_size);
Andreas Gampe542451c2016-07-26 09:02:02 -070048 if (pointer_size == PointerSize::k32) {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000049 uint32_t value = *reinterpret_cast<uint32_t*>(ptr);
Vladimir Marko78baed52018-10-11 10:44:58 +010050 return reinterpret_cast32<ArtMethod*>(value);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000051 } else {
52 uint64_t value = *reinterpret_cast<uint64_t*>(ptr);
Vladimir Marko78baed52018-10-11 10:44:58 +010053 return reinterpret_cast64<ArtMethod*>(value);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000054 }
55 }
56
Andreas Gampe542451c2016-07-26 09:02:02 -070057 void Set(size_t index, ArtMethod* method, PointerSize pointer_size) {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000058 DCHECK_LT(index, kSize);
Mathieu Chartier8c19d242017-03-06 12:35:10 -080059 uint8_t* ptr = AddressOfElement(index, pointer_size);
Andreas Gampe542451c2016-07-26 09:02:02 -070060 if (pointer_size == PointerSize::k32) {
Vladimir Marko78baed52018-10-11 10:44:58 +010061 *reinterpret_cast<uint32_t*>(ptr) = reinterpret_cast32<uint32_t>(method);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000062 } else {
Vladimir Marko78baed52018-10-11 10:44:58 +010063 *reinterpret_cast<uint64_t*>(ptr) = reinterpret_cast64<uint64_t>(method);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000064 }
65 }
66
Andreas Gampe542451c2016-07-26 09:02:02 -070067 static size_t OffsetOfElement(size_t index, PointerSize pointer_size) {
68 return index * static_cast<size_t>(pointer_size);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000069 }
70
Andreas Gampe542451c2016-07-26 09:02:02 -070071 void Populate(ArtMethod** data, PointerSize pointer_size) {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000072 for (size_t i = 0; i < kSize; ++i) {
73 Set(i, data[i], pointer_size);
74 }
75 }
76
Andreas Gampe542451c2016-07-26 09:02:02 -070077 constexpr static size_t SizeInBytes(PointerSize pointer_size) {
78 return kSize * static_cast<size_t>(pointer_size);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000079 }
Andreas Gampe75a7db62016-09-26 12:04:26 -070080
Andreas Gampea1ff30f2016-09-27 12:19:45 -070081 // 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)
Andreas Gampe75a7db62016-09-26 12:04:26 -070086 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_);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000092};
93
94} // namespace art
95
96#endif // ART_RUNTIME_IMTABLE_H_
97