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