1 /** 2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef PANDA_INTERPRETER_CACHE_H_ 16 #define PANDA_INTERPRETER_CACHE_H_ 17 18 #include <array> 19 #include "libpandabase/utils/math_helpers.h" 20 #include "runtime/include/method.h" 21 22 namespace ark { 23 24 class InterpreterCache { 25 public: Has(const void * pc,Method * caller)26 bool Has(const void *pc, Method *caller) const 27 { 28 const auto &entry = data_[GetIndex(pc)]; 29 return entry.pc == pc && entry.caller == caller; 30 } 31 32 template <class T> Get(const void * pc,Method * caller)33 T *Get(const void *pc, Method *caller) const 34 { 35 if (UNLIKELY(!Has(pc, caller))) { 36 return nullptr; 37 } 38 return static_cast<T *>(data_[GetIndex(pc)].item); 39 } 40 41 template <class T> Set(const void * pc,T * item,Method * caller)42 void Set(const void *pc, T *item, Method *caller) 43 { 44 data_[GetIndex(pc)] = {pc, caller, item}; 45 } 46 Clear()47 void Clear() 48 { 49 data_.fill({}); 50 } 51 52 static constexpr size_t N = 256; 53 54 struct Entry { 55 const void *pc {nullptr}; 56 Method *caller {nullptr}; 57 void *item {nullptr}; 58 }; 59 60 private: GetIndex(const void * pc)61 static size_t GetIndex(const void *pc) 62 { 63 return ark::helpers::math::PowerOfTwoTableSlot(reinterpret_cast<size_t>(pc), N, 2U); 64 } 65 66 static_assert(ark::helpers::math::IsPowerOfTwo(N)); 67 std::array<Entry, N> data_ {}; 68 }; 69 70 } // namespace ark 71 72 #endif // PANDA_INTERPRETER_CACHE_H_ 73