blob: 6df890d14bb5ea57720a053b7d71d6ad85153794 [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
Andreas Gampe75a7db62016-09-26 12:04:26 -070024#include "base/enums.h"
25#include "base/macros.h"
26
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000027namespace art {
28
29class ArtMethod;
Andreas Gampe75a7db62016-09-26 12:04:26 -070030class DexFile;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000031
32class ImTable {
33 public:
34 // Interface method table size. Increasing this value reduces the chance of two interface methods
35 // colliding in the interface method table but increases the size of classes that implement
36 // (non-marker) interfaces.
37 static constexpr size_t kSize = IMT_SIZE;
38
Andreas Gampe542451c2016-07-26 09:02:02 -070039 ArtMethod* Get(size_t index, PointerSize pointer_size) {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000040 DCHECK_LT(index, kSize);
41 uint8_t* ptr = reinterpret_cast<uint8_t*>(this) + OffsetOfElement(index, pointer_size);
Andreas Gampe542451c2016-07-26 09:02:02 -070042 if (pointer_size == PointerSize::k32) {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000043 uint32_t value = *reinterpret_cast<uint32_t*>(ptr);
44 return reinterpret_cast<ArtMethod*>(value);
45 } else {
46 uint64_t value = *reinterpret_cast<uint64_t*>(ptr);
47 return reinterpret_cast<ArtMethod*>(value);
48 }
49 }
50
Andreas Gampe542451c2016-07-26 09:02:02 -070051 void Set(size_t index, ArtMethod* method, PointerSize pointer_size) {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000052 DCHECK_LT(index, kSize);
53 uint8_t* ptr = reinterpret_cast<uint8_t*>(this) + OffsetOfElement(index, pointer_size);
Andreas Gampe542451c2016-07-26 09:02:02 -070054 if (pointer_size == PointerSize::k32) {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000055 uintptr_t value = reinterpret_cast<uintptr_t>(method);
56 DCHECK_EQ(static_cast<uint32_t>(value), value); // Check that we dont lose any non 0 bits.
57 *reinterpret_cast<uint32_t*>(ptr) = static_cast<uint32_t>(value);
58 } else {
59 *reinterpret_cast<uint64_t*>(ptr) = reinterpret_cast<uint64_t>(method);
60 }
61 }
62
Andreas Gampe542451c2016-07-26 09:02:02 -070063 static size_t OffsetOfElement(size_t index, PointerSize pointer_size) {
64 return index * static_cast<size_t>(pointer_size);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000065 }
66
Andreas Gampe542451c2016-07-26 09:02:02 -070067 void Populate(ArtMethod** data, PointerSize pointer_size) {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000068 for (size_t i = 0; i < kSize; ++i) {
69 Set(i, data[i], pointer_size);
70 }
71 }
72
Andreas Gampe542451c2016-07-26 09:02:02 -070073 constexpr static size_t SizeInBytes(PointerSize pointer_size) {
74 return kSize * static_cast<size_t>(pointer_size);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000075 }
Andreas Gampe75a7db62016-09-26 12:04:26 -070076
77 // Converts a method to the base hash used in GetImtIndex.
78 ALWAYS_INLINE static inline uint32_t GetBaseImtHash(ArtMethod* method)
79 REQUIRES_SHARED(Locks::mutator_lock_);
80 ALWAYS_INLINE static inline uint32_t GetBaseImtHash(const DexFile* dex_file, uint32_t method_idx)
81 REQUIRES_SHARED(Locks::mutator_lock_);
82
83 // The (complete) hashing scheme to map an ArtMethod to a slot in the Interface Method Table
84 // (IMT).
85 ALWAYS_INLINE static inline uint32_t GetImtIndex(ArtMethod* method)
86 REQUIRES_SHARED(Locks::mutator_lock_);
87 ALWAYS_INLINE static inline uint32_t GetImtIndex(const DexFile* dex_file, uint32_t method_idx)
88 REQUIRES_SHARED(Locks::mutator_lock_);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000089};
90
91} // namespace art
92
93#endif // ART_RUNTIME_IMTABLE_H_
94