1 /* 2 * Copyright (C) 2024 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_OAT_JNI_STUB_HASH_MAP_H_ 18 #define ART_RUNTIME_OAT_JNI_STUB_HASH_MAP_H_ 19 20 #include <memory> 21 #include <string_view> 22 23 #include "arch/instruction_set.h" 24 #include "base/hash_map.h" 25 #include "base/locks.h" 26 27 namespace art HIDDEN { 28 29 class ArtMethod; 30 31 class JniStubKey { 32 public: 33 JniStubKey() = default; 34 JniStubKey(const JniStubKey& other) = default; 35 JniStubKey& operator=(const JniStubKey& other) = default; 36 37 JniStubKey(uint32_t flags, std::string_view shorty); 38 39 explicit JniStubKey(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_); 40 Flags()41 uint32_t Flags() const { 42 return flags_; 43 } 44 Shorty()45 std::string_view Shorty() const { 46 return shorty_; 47 } 48 IsEmpty()49 bool IsEmpty() const { 50 return Shorty().empty(); 51 } 52 MakeEmpty()53 void MakeEmpty() { 54 shorty_ = {}; 55 } 56 57 private: 58 uint32_t flags_; 59 std::string_view shorty_; 60 }; 61 62 template <typename Value> 63 class JniStubKeyEmpty { 64 public: IsEmpty(const std::pair<JniStubKey,Value> & pair)65 bool IsEmpty(const std::pair<JniStubKey, Value>& pair) const { 66 return pair.first.IsEmpty(); 67 } 68 MakeEmpty(std::pair<JniStubKey,Value> & pair)69 void MakeEmpty(std::pair<JniStubKey, Value>& pair) { 70 pair.first.MakeEmpty(); 71 } 72 }; 73 74 using JniStubKeyHashFunction = size_t (*)(const JniStubKey& key); 75 76 class JniStubKeyHash { 77 public: 78 EXPORT explicit JniStubKeyHash(InstructionSet isa); 79 operator()80 size_t operator()(const JniStubKey& key) const { 81 return hash_func_(key); 82 } 83 84 private: 85 JniStubKeyHashFunction hash_func_; 86 }; 87 88 using JniStubKeyEqualsFunction = bool (*)(const JniStubKey& lhs, const JniStubKey& rhs); 89 90 class JniStubKeyEquals { 91 public: 92 EXPORT explicit JniStubKeyEquals(InstructionSet isa); 93 operator()94 bool operator()(const JniStubKey& lhs, const JniStubKey& rhs) const { 95 return equals_func_(lhs, rhs); 96 } 97 98 private: 99 JniStubKeyEqualsFunction equals_func_; 100 }; 101 102 template <typename Value, 103 typename Alloc = std::allocator<std::pair<JniStubKey, Value>>> 104 using JniStubHashMap = 105 HashMap<JniStubKey, Value, JniStubKeyEmpty<Value>, JniStubKeyHash, JniStubKeyEquals>; 106 107 } // namespace art 108 109 #endif // ART_RUNTIME_OAT_JNI_STUB_HASH_MAP_H_ 110